Compare commits
10 Commits
05ac4be541
...
1b5fee77c2
Author | SHA1 | Date | |
---|---|---|---|
1b5fee77c2 | |||
aa8f87a6d3 | |||
1bef792b22 | |||
e9b4fbc757 | |||
2fd432f516 | |||
ed62e260a7 | |||
e41ca46d1d | |||
f734703dc3 | |||
c5b2c17ce2 | |||
028f9006c2 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -150,3 +150,4 @@ out.*
|
|||||||
*.csv
|
*.csv
|
||||||
*.txt
|
*.txt
|
||||||
*.json
|
*.json
|
||||||
|
.aider*
|
||||||
|
0
data/hourly_photos/.gitkeep
Normal file
0
data/hourly_photos/.gitkeep
Normal file
0
data/labelled/closed/.gitkeep
Normal file
0
data/labelled/closed/.gitkeep
Normal file
0
data/labelled/open/.gitkeep
Normal file
0
data/labelled/open/.gitkeep
Normal file
0
data/sorted/open/.gitkeep
Normal file
0
data/sorted/open/.gitkeep
Normal file
0
data/unsure/closed/.gitkeep
Normal file
0
data/unsure/closed/.gitkeep
Normal file
0
data/unsure/open/.gitkeep
Normal file
0
data/unsure/open/.gitkeep
Normal file
2
model.py
2
model.py
@@ -5,7 +5,7 @@ from PIL import Image, ImageDraw
|
|||||||
# For the custom crop transform. User can adjust these.
|
# For the custom crop transform. User can adjust these.
|
||||||
TRIANGLE_CROP_WIDTH = 556
|
TRIANGLE_CROP_WIDTH = 556
|
||||||
TRIANGLE_CROP_HEIGHT = 1184
|
TRIANGLE_CROP_HEIGHT = 1184
|
||||||
RESIZE_DIM = 64 # Resize cropped image to this dimension (square)
|
RESIZE_DIM = 256 # Resize cropped image to this dimension (square)
|
||||||
|
|
||||||
|
|
||||||
# Custom transform to crop a triangle from the lower right corner
|
# Custom transform to crop a triangle from the lower right corner
|
||||||
|
4
move.py
4
move.py
@@ -10,8 +10,8 @@ def main():
|
|||||||
them into data/open/ or data/closed/ based on the 'choice' field.
|
them into data/open/ or data/closed/ based on the 'choice' field.
|
||||||
"""
|
"""
|
||||||
data_dir = 'data'
|
data_dir = 'data'
|
||||||
open_dir = os.path.join(data_dir, 'open')
|
open_dir = os.path.join(data_dir, 'labelled/open')
|
||||||
closed_dir = os.path.join(data_dir, 'closed')
|
closed_dir = os.path.join(data_dir, 'labelled/closed')
|
||||||
|
|
||||||
# This is an assumption based on the path found in the 'd' parameter of the image URL.
|
# This is an assumption based on the path found in the 'd' parameter of the image URL.
|
||||||
# e.g., ...?d=.../data/hourly_photos/filename.jpg
|
# e.g., ...?d=.../data/hourly_photos/filename.jpg
|
||||||
|
137
server.py
Normal file
137
server.py
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
import asyncio
|
||||||
|
import aiohttp
|
||||||
|
from aiohttp import web
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import io
|
||||||
|
|
||||||
|
import torch
|
||||||
|
import torch.nn.functional as F
|
||||||
|
from torchvision import transforms
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
from model import (CropLowerRightTriangle, GarageDoorCNN, TRIANGLE_CROP_WIDTH,
|
||||||
|
TRIANGLE_CROP_HEIGHT, RESIZE_DIM)
|
||||||
|
|
||||||
|
# --- Configuration ---
|
||||||
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
|
|
||||||
|
BLUEIRIS_KEY = os.getenv('BLUEIRIS_KEY')
|
||||||
|
if not BLUEIRIS_KEY:
|
||||||
|
raise ValueError("BLUEIRIS_KEY environment variable not set.")
|
||||||
|
|
||||||
|
CAMERA_URL = "http://cameras.dns.t0.vc/image/SE-S?&w=9999&decode=1"
|
||||||
|
MODEL_PATH = 'garage_door_cnn.pth'
|
||||||
|
CLASS_NAMES = ['closed', 'open'] # From training, sorted alphabetically
|
||||||
|
POLL_INTERVAL_SECONDS = 10
|
||||||
|
REQUEST_TIMEOUT_SECONDS = 5
|
||||||
|
|
||||||
|
# --- Model Inference ---
|
||||||
|
def get_prediction(model, image_bytes, device):
|
||||||
|
"""Run model inference on the provided image bytes."""
|
||||||
|
try:
|
||||||
|
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Failed to open image from bytes: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Define the same transforms as used in validation
|
||||||
|
transform = transforms.Compose([
|
||||||
|
CropLowerRightTriangle(triangle_width=TRIANGLE_CROP_WIDTH, triangle_height=TRIANGLE_CROP_HEIGHT),
|
||||||
|
transforms.Resize((RESIZE_DIM, RESIZE_DIM)),
|
||||||
|
transforms.ToTensor(),
|
||||||
|
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
||||||
|
])
|
||||||
|
|
||||||
|
input_tensor = transform(image).unsqueeze(0).to(device)
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
output = model(input_tensor)
|
||||||
|
probabilities = F.softmax(output, dim=1)
|
||||||
|
confidence, pred_idx = torch.max(probabilities, 1)
|
||||||
|
return CLASS_NAMES[pred_idx.item()], confidence.item()
|
||||||
|
|
||||||
|
# --- Background Task ---
|
||||||
|
async def monitor_garage_door(app):
|
||||||
|
"""Periodically fetches an image and logs the garage door status."""
|
||||||
|
logging.info("Starting garage door monitoring task.")
|
||||||
|
session = app['client_session']
|
||||||
|
model = app['model']
|
||||||
|
device = app['device']
|
||||||
|
headers = {'Authorization': 'Basic ' + BLUEIRIS_KEY}
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
await asyncio.sleep(POLL_INTERVAL_SECONDS)
|
||||||
|
logging.debug("Fetching new image from camera...")
|
||||||
|
async with session.get(CAMERA_URL, headers=headers, timeout=REQUEST_TIMEOUT_SECONDS) as response:
|
||||||
|
if response.status == 200:
|
||||||
|
image_bytes = await response.read()
|
||||||
|
result = get_prediction(model, image_bytes, device)
|
||||||
|
if result:
|
||||||
|
prediction, confidence = result
|
||||||
|
logging.debug(f"Garage door status: {prediction} (confidence: {confidence:.4f})")
|
||||||
|
else:
|
||||||
|
logging.error(f"Failed to fetch image. Status: {response.status}, Reason: {response.reason}")
|
||||||
|
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
logging.warning("Request to camera timed out.")
|
||||||
|
except aiohttp.ClientError as e:
|
||||||
|
logging.error(f"Client error during image fetch: {e}")
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
logging.info("Monitoring task cancelled.")
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"An unexpected error occurred in the monitoring task: {e}", exc_info=True)
|
||||||
|
# Add a small delay before retrying on unexpected errors
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
|
# --- Web Server ---
|
||||||
|
async def handle_root(request):
|
||||||
|
"""Handler for the root GET request."""
|
||||||
|
return web.Response(text="hello world")
|
||||||
|
|
||||||
|
async def on_startup(app):
|
||||||
|
"""Actions to perform on application startup."""
|
||||||
|
# Set up device
|
||||||
|
app['device'] = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||||
|
logging.info(f"Using device: {app['device']}")
|
||||||
|
|
||||||
|
# Load model
|
||||||
|
logging.info(f"Loading model from {MODEL_PATH}...")
|
||||||
|
model = GarageDoorCNN(resize_dim=RESIZE_DIM).to(app['device'])
|
||||||
|
model.load_state_dict(torch.load(MODEL_PATH, map_location=app['device']))
|
||||||
|
model.eval()
|
||||||
|
app['model'] = model
|
||||||
|
logging.info("Model loaded successfully.")
|
||||||
|
|
||||||
|
# Create client session
|
||||||
|
app['client_session'] = aiohttp.ClientSession()
|
||||||
|
|
||||||
|
# Start background task
|
||||||
|
app['monitor_task'] = asyncio.create_task(monitor_garage_door(app))
|
||||||
|
|
||||||
|
async def on_cleanup(app):
|
||||||
|
"""Actions to perform on application cleanup."""
|
||||||
|
logging.info("Cleaning up...")
|
||||||
|
app['monitor_task'].cancel()
|
||||||
|
try:
|
||||||
|
await app['monitor_task']
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
pass
|
||||||
|
await app['client_session'].close()
|
||||||
|
logging.info("Cleanup complete.")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
app = web.Application()
|
||||||
|
app.router.add_get('/', handle_root)
|
||||||
|
app.on_startup.append(on_startup)
|
||||||
|
app.on_cleanup.append(on_cleanup)
|
||||||
|
web.run_app(app, port=8081)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if not os.path.exists(MODEL_PATH):
|
||||||
|
logging.error(f"Model file '{MODEL_PATH}' not found. Please run train.py first.")
|
||||||
|
else:
|
||||||
|
main()
|
2
sort.py
2
sort.py
@@ -14,7 +14,7 @@ def sort_images():
|
|||||||
MODEL_PATH = 'garage_door_cnn.pth'
|
MODEL_PATH = 'garage_door_cnn.pth'
|
||||||
SOURCE_DIR = 'data/hourly_photos/'
|
SOURCE_DIR = 'data/hourly_photos/'
|
||||||
DEST_DIR = 'data/sorted/open/'
|
DEST_DIR = 'data/sorted/open/'
|
||||||
CONFIDENCE_THRESHOLD = 0.90 # Only copy if confidence is over this value
|
CONFIDENCE_THRESHOLD = 0.80 # Only copy if confidence is over this value
|
||||||
|
|
||||||
|
|
||||||
# The classes are sorted alphabetically by ImageFolder: ['closed', 'open']
|
# The classes are sorted alphabetically by ImageFolder: ['closed', 'open']
|
||||||
|
Reference in New Issue
Block a user