Add object detection, tracking, mobile notifications, and search to any security camera.
Non alert event (#98)
Rory Clear2bae5b8fc03d
diff --git a/clearcam.py b/clearcam.pyindex 8935acc..38a7b94 100644--- a/clearcam.py+++ b/clearcam.py@@ -358,6 +358,7 @@ class RollingClassCounter:self.cam_name = cam_nameself.is_on = Trueself.is_notif = True+ self.zone = Truedef add(self, class_id):if self.classes is not None and class_id not in self.classes: return@@ -367,19 +368,21 @@ class RollingClassCounter:def cleanup(self, class_id, now):q = self.data[class_id]- while self.window and q and now - q[0] > self.window:- q.popleft()+ window = self.window if self.window else (60 if self.is_notif else 1)+ while window and q and now - q[0] > window:+ q.popleft()def reset_counts(self):for class_id, _ in self.data.items():self.data[class_id] = deque() # todo, use in reset endpoint?def get_counts(self):+ window = self.window if self.window else (60 if self.is_notif else 1)max_reached = Falsenow = time.time()counts = {}for class_id, q in self.data.items():- while self.window and q and now - q[0] > self.window:+ while window and q and now - q[0] > window:q.popleft()if q:counts[class_id] = len(q)@@ -393,7 +396,8 @@ class RollingClassCounter:now = time.localtime()time_of_day = now.tm_hour * 3600 + now.tm_min * 60 + now.tm_secif not self.sched[time.localtime().tm_wday + 1]: return False- return time_of_day < self.sched[0][1] and time_of_day > ((self.sched[0][0] - self.window) + offset)+ window = self.window if self.window else (60 if self.is_notif else 1)+ return time_of_day < self.sched[0][1] and time_of_day > ((self.sched[0][0] - window) + offset)def write_png(filename, array):array = array[..., ::-1] # BGR to RGB@@ -456,6 +460,7 @@ class VideoCapture:self.counter = RollingClassCounter(cam_name=cam_name)self.cam_name = cam_nameself.object_set = set()+ self.object_set_zone = set()self.src = srcself.width = 1920 # todo 1080?@@ -483,7 +488,7 @@ class VideoCapture:except Exception:with open(alerts_file, 'wb') as f:self.alert_counters = dict()- self.alert_counters[str(uuid.uuid4())] = RollingClassCounter(window_seconds=60, max=1, classes={0,1,2,3,5,7},cam_name=cam_name)+ self.alert_counters[str(uuid.uuid4())] = RollingClassCounter(window_seconds=None, max=1, classes={0,1,2,3,5,7},cam_name=cam_name)pickle.dump(self.alert_counters, f)try:with open(settings_file, "rb") as f:@@ -545,6 +550,7 @@ class VideoCapture:time.sleep(8)command = [ffmpeg_path,+ "-live_start_index", "-1","-i", str(path / "stream.m3u8"),"-loglevel", "quiet","-reconnect", "1",@@ -602,16 +608,22 @@ class VideoCapture:if not alert.is_active():alert.reset_counts()continue+ window = alert.window if alert.window else (60 if alert.is_notif else 1)if not alert.is_active(offset=4): alert.last_det = time.time() # don't send alert when just activeif alert.get_counts()[1]:- if time.time() - alert.last_det >= alert.window:+ if time.time() - alert.last_det >= window:if alert.is_notif: send_det = Truetimestamp = datetime.now().strftime("%Y-%m-%d")filepath = CAMERA_BASE_DIR / f"{self.cam_name}/event_images/{timestamp}"filepath.mkdir(parents=True, exist_ok=True)- filename = filepath / f"{int(time.time() - self.streamer.start_time - 10)}.jpg"self.annotated_frame = self.draw_predictions(frame.copy(), filtered_preds)+ # todo alerts can be sent with the wrong thumbnail if two happen quickly, use map+ ts = int(time.time() - self.streamer.start_time - 10)+ filename = filepath / f"{ts}_notif.jpg" if alert.is_notif else filepath / f"{ts}.jpg"cv2.imwrite(str(filename), self.annotated_frame, [cv2.IMWRITE_JPEG_QUALITY, 85]) # we've 10MB limit for video file, raw png is 3MB!+ if (plain := filepath / f"{ts}.jpg").exists() and (filepath / f"{ts}_notif.jpg").exists():+ plain.unlink() # only one image per event+ filename = filepath / f"{ts}_notif.jpg"text = f"Event Detected ({getattr(alert, 'cam_name')})" if getattr(alert, 'cam_name', None) else Noneif userID is not None and alert.is_notif: threading.Thread(target=send_notif, args=(userID,text,), daemon=True).start()last_det = time.time()@@ -709,16 +721,28 @@ class VideoCapture:preds = []for x in online_targets:if x.tracklet_len < 1: continue # dont alert for 1 frame, too many false positives- if hasattr(self, "settings") and self.settings is not None and self.settings.get("is_on"):+ outside = False+ if hasattr(self, "settings") and self.settings is not None and self.settings.get("coords"):outside = point_not_in_polygon([[x.tlwh[0], x.tlwh[1]],[(x.tlwh[0]+x.tlwh[2]), x.tlwh[1]],[(x.tlwh[0]), (x.tlwh[1]+x.tlwh[3])],[(x.tlwh[0]+x.tlwh[2]), (x.tlwh[1]+x.tlwh[3])]], self.settings["coords"])- if outside ^ self.settings["outside"]: continue+ outside = outside ^ self.settings["outside"]+ non_zone_alert = False+ if outside: # check if any alerts don't use zone+ for _, alert in self.alert_counters.items():+ if not alert.zone:+ non_zone_alert = True+ break+ if not non_zone_alert and outside: continuepreds.append(np.array([x.tlwh[0],x.tlwh[1],(x.tlwh[0]+x.tlwh[2]),(x.tlwh[1]+x.tlwh[3]),x.score,x.class_id]))- if int(x.track_id) not in self.object_set and (classes is None or str(int(x.class_id)) in classes):- self.object_set.add(int(x.track_id))- self.counter.add(int(x.class_id))+ if (classes is None or str(int(x.class_id)) in classes):+ new = int(x.track_id) not in self.object_set+ new_in_zone = int(x.track_id) not in self.object_set_zone and not outside+ if new:+ self.object_set.add(int(x.track_id))+ self.counter.add(int(x.class_id))+ if new_in_zone: self.object_set_zone.add(int(x.track_id))for _, alert in self.alert_counters.items():- if not alert.get_counts()[1]:- alert.add(int(x.class_id)) #only add if empty, don't spam notifs+ if not alert.get_counts()[1] and ((new and not alert.zone) or (new_in_zone and alert.zone)): alert.add(int(x.class_id))+preds = np.array(preds)preds = scale_boxes(pre.shape[:2], preds, frame.shape)with self.lock:@@ -1057,7 +1081,6 @@ class HLSRequestHandler(BaseHTTPRequestHandler):coords = json.loads(coords_json)if isinstance(coords, list) and len(coords) >= 3:zone["coords"] = [[float(x), float(y)] for x, y in coords]- zone["is_on"] = (str(is_on).lower() == "true") if (is_on := query.get("is_on", [None])[0]) is not None else zone.get("is_on")zone["is_notif"] = (str(is_notif).lower() == "true") if (is_notif := query.get("is_notif", [None])[0]) is not None else zone.get("is_notif")zone["outside"] = (str(outside).lower() == "true") if (outside := query.get("outside", [None])[0]) is not None else zone.get("outside")query.get("threshold", [None])[0] is not None and zone.update({"threshold": float(query.get("threshold", [None])[0])})@@ -1080,13 +1103,14 @@ class HLSRequestHandler(BaseHTTPRequestHandler):alert = Nonealert_id = query.get("id", [None])[0]is_on = query.get("is_on", [None])[0]+ zone = query.get("zone", [None])[0]is_notif = query.get("is_notif", [None])[0]if alert_id is None: # no id, add alertwindow = query.get("window", [None])[0]max_count = query.get("max", [None])[0]class_ids = query.get("class_ids", [None])[0]sched = json.loads(query.get("sched", ["[[0,86400],[0,86400],[0,86400],[0,86400],[0,86400],[0,86400],[0,86400]]"])[0]) # todo, weekly- window = int(window)+ if window: window = int(window)max_count = int(max_count)classes = [int(c.strip()) for c in class_ids.split(",")]alert_id = str(uuid.uuid4())@@ -1099,9 +1123,10 @@ class HLSRequestHandler(BaseHTTPRequestHandler):)raw_alerts[alert_id] = alertelse:- if is_on is not None or is_notif is not None:+ if is_on is not None or is_notif is not None or zone is not None:if is_on is not None: raw_alerts[alert_id].is_on = str(is_on).lower() == "true"if is_notif is not None: raw_alerts[alert_id].is_notif = str(is_notif).lower() == "true"+ if zone is not None: raw_alerts[alert_id].zone = str(zone).lower() == "true"alert = raw_alerts[alert_id]else:del raw_alerts[alert_id]@@ -1153,6 +1178,7 @@ class HLSRequestHandler(BaseHTTPRequestHandler):"sched": sched,"is_on": alert.is_on,"is_notif": alert.is_notif,+ "zone": alert.zone,})except Exception as e:self.send_error(500, f"Failed to load alerts: {e}")@@ -1264,6 +1290,7 @@ class HLSRequestHandler(BaseHTTPRequestHandler):if parsed_path.path == '/event_thumbs' or parsed_path.path.endswith('/event_thumbs'): # todo cleanselected_dir = parse_qs(parsed_path.query).get("folder", [datetime.now().strftime("%Y-%m-%d")])[0]+ name_contains = parse_qs(parsed_path.query).get("name_contains", [None])[0]image_data = []if cam_name is not None:@@ -1271,12 +1298,13 @@ class HLSRequestHandler(BaseHTTPRequestHandler):event_image_path = event_image_dir / selected_direvent_images = sorted(event_image_path.glob("*.jpg"),- key=lambda p: int(p.stem),+ key=lambda p: int(p.stem.split('_')[0]), # n.jpg or n_{s}.jpg?reverse=True) if event_image_path.exists() else []for img in event_images:- ts = int(img.stem)+ if name_contains and name_contains not in img.name: continue+ ts = int(img.stem.split('_')[0]) # n.jpg or n_{s}.jpg?image_url = f"/{img.relative_to(self.base_dir.parent)}"image_data.append({"url": image_url,@@ -1293,11 +1321,12 @@ class HLSRequestHandler(BaseHTTPRequestHandler):if event_image_path.exists():event_images = sorted(event_image_path.glob("*.jpg"),- key=lambda p: int(p.stem),+ key=lambda p: int(p.stem.split('_')[0]), # n.jpg or n_{s}.jpg?reverse=True)for img in event_images:- ts = int(img.stem)+ if name_contains and name_contains not in img.name: continue+ ts = int(img.stem.split('_')[0])image_url = f"/{img.relative_to(self.base_dir.parent)}"image_data.append({"url": image_url,diff --git a/mainview.html b/mainview.htmlindex f3ab63f..dad34ea 100644--- a/mainview.html+++ b/mainview.html@@ -143,14 +143,14 @@transform: scale(1.1);}- #alertsModal .modal-content {- max-width: 800px;- max-height: 90vh;- overflow-y: auto;- font-family: sans-serif;- background-color: var(--bg-primary);- color: var(--text-primary);- }+ #alertsModal .modal-content {+ max-width: 800px;+ max-height: 90vh;+ overflow-y: auto;+ font-family: sans-serif;+ background-color: var(--bg-primary);+ color: var(--text-primary);+ }#alertsModal .modal-header {display: flex;@@ -911,6 +911,10 @@<select id="cameraFilter"><option value="">All Cameras</option></select>+ <label style="display: flex; align-items: center; gap: 6px; margin-left: 10px;">+ <input type="checkbox" id="alertsOnlyCheckbox">+ Alerts Only+ </label></div><div id="eventImagesContainer"><p style="text-align:center; color:#666; width:100%;">No events detected yet.</p>@@ -941,9 +945,6 @@</div><div class="form-actions" style="margin-top: 20px; display: flex; justify-content: space-between; align-items: center; width: 100%;"><div style="display: flex; flex-direction: column; gap: 10px;">- <label style="display: flex; align-items: center; gap: 6px;">- <input type="checkbox" id="zoneEnabledCheckbox" checked> Enable Zone- </label><label style="display: flex; align-items: center; gap: 6px;"><input type="checkbox" id="outsideZoneCheckbox"> Detect outside of zone</label>@@ -992,25 +993,28 @@<span class="close" onclick="closeAlertModal()">×</span></div><form id="alertForm" onsubmit="addAlert(event)" style="display: flex; flex-direction: column; align-items: center;">- <div class="form-group" style="width: 90%; text-align: center;">- <label for="maxCount">Trigger if there are</label>- <input type="number" id="maxCount" name="max" min="1" value="1" required- style="width: 80px; margin: 0 6px; text-align: center; display: inline-block;">- <span>objects detected</span>- </div><div class="form-group checkbox-group" style="width: 90%; text-align: center;">- <label>of class(es)</label><div id="checkboxContainer" class="checkbox-container" style="display: grid; grid-template-columns: auto auto; gap: 8px 16px; max-height: 200px; overflow-y: auto; margin: 0 auto; padding: 8px; justify-content: center;"></div></div>- <div class="form-group" style="width: 90%; text-align: center;">- <label for="windowMinutes">within this time window</label>- <input type="number" id="windowMinutes" name="window" min="1" value="1" required- style="width: 80px; margin: 0 6px; text-align: center; display: inline-block;">- <span>minutes</span>+ <div class="form-group" style="width: 90%; text-align: center; margin-top: 15px;">+ <label style="font-weight: 600; margin-bottom: 10px; display: block;">Time Window (Optional)</label>+ <div style="margin-bottom: 10px;">+ <label for="maxCount">Trigger if there are</label>+ <input type="number" id="maxCount" name="max" min="1" placeholder="1"+ style="width: 80px; margin: 0 6px; text-align: center; display: inline-block;">+ <span>objects detected</span>+ </div>++ <div>+ <label for="windowMinutes">within this time window</label>+ <input type="number" id="windowMinutes" name="window" min="1" placeholder="1"+ style="width: 80px; margin: 0 6px; text-align: center; display: inline-block;">+ <span>minutes</span>+ </div></div><div class="form-group" style="width: 90%; text-align: center;">- <label>Schedule (optional)</label>+ <label style="font-weight: 600; margin-bottom: 10px; display: block;">Schedule (Optional)</label><div style="display: flex; justify-content: center; gap: 10px; flex-wrap: wrap;"><div><label for="scheduleFrom">From</label><br>@@ -1022,6 +1026,7 @@</div></div></div>+<div class="form-group" style="width: 90%; text-align: center;"><div style="display: flex; flex-direction: column; align-items: center; gap: 5px; margin-top: 5px;"><label><input type="checkbox" name="days" value="0" checked> Mon</label>@@ -1190,6 +1195,13 @@<span class="tooltiptext">Enable to send notifications and clips to your clearcam app for this alert (requires premium).</span></span></th>+ <th style="text-align:center; padding:6px; border-bottom:1px solid #ccc;">+ <span class="tooltip">+ Zone+ <span class="tooltip-icon">?</span>+ <span class="tooltiptext">Enable to only trigger this alert when objects are detected within the defined zone.</span>+ </span>+ </th><th style="text-align:center; padding:6px; border-bottom:1px solid #ccc;">Actions</th></tr></thead><tbody>`;@@ -1224,12 +1236,14 @@const isOn = alert.hasOwnProperty("is_on") ? alert.is_on : true;const isNotif = alert.hasOwnProperty("is_notif") ? alert.is_notif : true;+ const isZone = alert.hasOwnProperty("zone") ? alert.zone : true;const checkedAttr = isOn ? "checked" : "";const notifCheckedAttr = isNotif ? "checked" : "";+ const zoneCheckedAttr = isZone ? "checked" : "";html += `<tr><td style="padding:6px; border-bottom:1px solid #eee; text-align:center;">${alert.max}</td>- <td style="padding:6px; border-bottom:1px solid #eee;">${classNames}</td>+ <td style="padding:6px; border-bottom:1px solid #eee; max-width:150px; white-space:normal; word-wrap:break-word; line-height:1.3;">${classNames}</td><td style="padding:6px; border-bottom:1px solid #eee;">${windowStr}</td><td style="padding:6px; border-bottom:1px solid #eee;">${scheduleStr}</td><td style="padding:6px; border-bottom:1px solid #eee; text-align:center;">@@ -1242,11 +1256,16 @@onchange="toggleAlert('${alert.id}', this.checked, 'is_notif')"style="transform:scale(1.2); cursor:pointer;"></td>+ <td style="padding:6px; border-bottom:1px solid #eee; text-align:center;">+ <input type="checkbox" data-alert-id="${alert.id}" ${zoneCheckedAttr}+ onchange="toggleAlert('${alert.id}', this.checked, 'zone')"+ style="transform:scale(1.2); cursor:pointer;">+ </td><td style="padding:6px; border-bottom:1px solid #eee; text-align:center;"><button onclick="deleteAlert('${alert.id}')" style="padding:4px 8px;">Delete</button></td></tr>`;- }+ }html += `</tbody></table><div style="margin-top:10px; display:flex; gap:10px; justify-content:center;">@@ -1292,25 +1311,21 @@function addAlert(event) {event.preventDefault();if (!currentAlertsCamera) return;-const form = event.target;const formData = new FormData(form);-const windowMinutes = parseFloat(formData.get('window'));- const windowSeconds = Math.round(windowMinutes * 60);-+ const windowSeconds = windowMinutes ? Math.round(windowMinutes * 60) : null;+ // Default max to 1 if empty+ const maxCount = formData.get('max') || '1';const checked = form.querySelectorAll('input[name="class_ids"]:checked');const classIds = Array.from(checked).map(cb => cb.value).join(',');-const scheduleFrom = formData.get("schedule_from") || "00:00";const scheduleTo = formData.get("schedule_to") || "23:59";-const toSeconds = (hhmm) => {const parts = hhmm.split(":").map(Number);const [h = 0, m = 0] = parts;return h * 3600 + m * 60;};-const fromSec = toSeconds(scheduleFrom);const toSec = toSeconds(scheduleTo);const dayChecks = form.querySelectorAll('input[name="days"]');@@ -1319,13 +1334,15 @@const params = new URLSearchParams({cam: currentAlertsCamera,- window: windowSeconds,- max: formData.get('max'),+ max: maxCount,class_ids: classIds,sched: JSON.stringify(schedArray),- is_notif: 'true' // Default to true for new alerts+ is_notif: 'true',+ zone: 'true'});-+ if (windowSeconds !== null) {+ params.set('window', windowSeconds);+ }fetch(`/edit_alert?${params.toString()}`).then(res => {if (!res.ok) throw new Error("Failed to add alert");@@ -1536,6 +1553,10 @@currentEventCameraFilter = this.value;loadEventImages();});+ alertsOnlyCheckbox.addEventListener('change', function() {+ alertsOnlyFilter = this.checked;+ loadEventImages();+ });});function createCameraCard(camName) {@@ -1612,15 +1633,6 @@loadPolygon(data.coords, previewEl);}}-- const zoneEnabledCheckbox = document.getElementById("zoneEnabledCheckbox");- if (data.is_on !== undefined) {- zoneEnabledCheckbox.checked = data.is_on;- } else if (data.length && data[0] && data[0].is_on !== undefined) {- zoneEnabledCheckbox.checked = data[0].is_on;- } else {- zoneEnabledCheckbox.checked = false;- }const outsideZoneCheckbox = document.getElementById("outsideZoneCheckbox");if (data.outside !== undefined) {@@ -1706,7 +1718,6 @@});}- const is_on = document.getElementById("zoneEnabledCheckbox").checked;const outside = document.getElementById("outsideZoneCheckbox").checked;const thresholdPercent = parseFloat(document.getElementById("zoneThreshold").value) || 50;@@ -1714,7 +1725,6 @@const params = new URLSearchParams({cam: currentEditingCamera,- is_on: is_on,threshold: threshold.toFixed(2),outside: outside});@@ -2303,6 +2313,7 @@});let currentEventCameraFilter = '';+ let alertsOnlyFilter = false;function populateCameraFilter(cameras) {const filter = document.getElementById('cameraFilter');const currentValue = filter.value;@@ -2327,7 +2338,9 @@if (currentEventCameraFilter) {url += `&cam=${encodeURIComponent(currentEventCameraFilter)}`;}-+ if (alertsOnlyFilter) {+ url += `&name_contains=_notif`;+ }const res = await fetch(url);const data = await res.json();const container = document.getElementById("eventImagesContainer");