Add object detection, tracking, mobile notifications, and search to any security camera.
Change url (#141)
Rory Clear7db528d0b184
diff --git a/clearcam.py b/clearcam.pyindex fac553f..a221071 100644--- a/clearcam.py+++ b/clearcam.py@@ -316,6 +316,7 @@ class VideoCapture:last_preview_time = Nonelast_counter_update = time.time()self.pred_occs = {}+ self.last_link_check = time.time()count = 0self.det_path = BASE_DIR / "cameras" / self.cam_name / "dets" / datetime.now().strftime("%Y-%m-%d")Path(self.det_path).mkdir(parents=True, exist_ok=True)@@ -326,6 +327,13 @@ class VideoCapture:self.src_fps = self.cap.get(cv2.CAP_PROP_FPS) or 30self.frame_step = max(1, int(round(self.src_fps / self.max_frame_rate)))while self.running:+ if time.time() - last_live_check > 5:+ last_live_check = time.time()+ link = database.run_get("links", self.cam_name)+ if type(link) == list: link = link[0] # todo, flakey?+ if link != self.src:+ self.src = link+ self._open_ffmpeg()try:if not (BASE_DIR / "cameras" / self.cam_name).is_dir(): os._exit(1) # deleted camif self.vod:@@ -755,6 +763,8 @@ class HLSRequestHandler(BaseHTTPRequestHandler):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])}) #need the valdatabase.run_put("settings", cam_name, zone) # todo, key for each+ if (url := query.get("url")) is not None: database.run_put("links", cam_name, url)+self.send_response(200)self.send_header("Content-Type", "application/json")self.end_headers()@@ -1146,7 +1156,6 @@ def event_img_info(image): return {"ts": int(image.split('_')[0]), "object_id":idef start_cam(rtsp, cam_name, model_variant='t', yolo_res=960):if not rtsp or not cam_name: return-def upsert_arg(args, key, value):prefix = f"--{key}="for i, arg in enumerate(args):diff --git a/mainview.html b/mainview.htmlindex b5c4fd4..e1b05cb 100644--- a/mainview.html+++ b/mainview.html@@ -1419,6 +1419,10 @@Detection Threshold:<input type="number" id="zoneThreshold" value="50" min="0" max="100" step="1" style="width: 60px;"> %</label>+ <label style="display: flex; align-items: center; gap: 6px;">+ Change source:+ <input type="text" id="zoneSource" placeholder="Enter new URL (optional)" style="width: 200px;">+ </label></div><div style="display: flex; gap: 10px;"><button type="button" class="clear-zone-button" onclick="clearPolygon()">Clear Zone</button>@@ -2386,58 +2390,60 @@previewEl.addEventListener('click', handlePreviewClick);}- function saveZone() {- if (!currentEditingCamera) {- console.error("No camera is currently being edited");- alert("Error: No camera selected for editing.");- return;- }-- const previewEl = document.getElementById("zonePreview");- const points = Array.from(document.querySelectorAll('.polygon-point'));-- let coords = null;- if (points.length >= 3) {- const previewRect = previewEl.getBoundingClientRect();-- coords = points.map(point => {- const pointRect = point.getBoundingClientRect();- const centerX = pointRect.left + pointRect.width / 2;- const centerY = pointRect.top + pointRect.height / 2;-- const x = ((centerX - previewRect.left) / previewRect.width);- const y = ((centerY - previewRect.top) / previewRect.height);-- return [x, y];- });- } else if (points.length === 0) {- coords = [0];- }- const outside = document.getElementById("outsideZoneCheckbox").checked;- const thresholdPercent = parseFloat(document.getElementById("zoneThreshold").value) || 50;- const threshold = thresholdPercent / 100;-- const params = new URLSearchParams({- cam: currentEditingCamera,- threshold: threshold.toFixed(2),- outside: outside- });-- if (coords) {- params.set('coords', JSON.stringify(coords));- }+ function saveZone() {+ if (!currentEditingCamera) {+ console.error("No camera is currently being edited");+ alert("Error: No camera selected for editing.");+ return;+ }- fetch(`/edit_settings?${params.toString()}`)- .then(res => {- if (!res.ok) throw new Error("Failed to save settings");- console.log("Settings saved successfully for camera:", currentEditingCamera);- closeZoneModal();- })- .catch(err => {- console.error("Save settings failed:", err);- alert("Failed to save settings.");- });- }+ const previewEl = document.getElementById("zonePreview");+ const points = Array.from(document.querySelectorAll('.polygon-point'));++ let coords = null;+ if (points.length >= 3) {+ const previewRect = previewEl.getBoundingClientRect();++ coords = points.map(point => {+ const pointRect = point.getBoundingClientRect();+ const centerX = pointRect.left + pointRect.width / 2;+ const centerY = pointRect.top + pointRect.height / 2;++ const x = ((centerX - previewRect.left) / previewRect.width);+ const y = ((centerY - previewRect.top) / previewRect.height);++ return [x, y];+ });+ } else if (points.length === 0) {+ coords = [0];+ }+ const outside = document.getElementById("outsideZoneCheckbox").checked;+ const thresholdPercent = parseFloat(document.getElementById("zoneThreshold").value) || 50;+ const threshold = thresholdPercent / 100;++ const params = new URLSearchParams({+ cam: currentEditingCamera,+ threshold: threshold.toFixed(2),+ outside: outside+ });++ if (coords) {+ params.set('coords', JSON.stringify(coords));+ }+ const src = document.getElementById("zoneSource").value.trim();+ if (src) params.set('url', src);++ fetch(`/edit_settings?${params.toString()}`)+ .then(res => {+ if (!res.ok) throw new Error("Failed to save settings");+ console.log("Settings saved successfully for camera:", currentEditingCamera);+ closeZoneModal();+ })+ .catch(err => {+ console.error("Save settings failed:", err);+ alert("Failed to save settings.");+ });+ }function loadPolygon(coords, previewEl) {const videoWidth = 1280;