Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

Threshold (#73)

Rory Clear1af54ae62df9
diff --git a/clearcam.py b/clearcam.py
index 5e3bc91..dfa4b57 100644
--- a/clearcam.py
+++ b/clearcam.py
@@ -427,12 +427,12 @@ class VideoCapture:
self.last_preds = []
self.dir = None
- self.zone = None
-
+ self.settings = None
+
alerts_file = CAMERA_BASE_DIR / cam_name / "alerts.pkl"
alerts_file.parent.mkdir(parents=True, exist_ok=True)
- zone_file = CAMERA_BASE_DIR / cam_name / "zone.pkl"
- zone_file.parent.mkdir(parents=True, exist_ok=True)
+ settings_file = CAMERA_BASE_DIR / cam_name / "settings.pkl"
+ settings_file.parent.mkdir(parents=True, exist_ok=True)
try:
with open(alerts_file, "rb") as f:
self.alert_counters = pickle.load(f)
@@ -444,8 +444,8 @@ class VideoCapture:
self.alert_counters[str(uuid.uuid4())] = RollingClassCounter(window_seconds=60, max=1, classes={0,1,2,3,5,7},cam_name=cam_name)
pickle.dump(self.alert_counters, f)
try:
- with open(zone_file, "rb") as f:
- self.zone = pickle.load(f)
+ with open(settings_file, "rb") as f:
+ self.settings = pickle.load(f)
except Exception:
print("zone file not found")
@@ -561,13 +561,12 @@ class VideoCapture:
for c in a.classes: classes.add(str(c))
added_alerts_file.unlink()
- edited_zone_file = CAMERA_BASE_DIR / self.cam_name / "edited_zone.pkl"
- if edited_zone_file.exists():
- with open(edited_zone_file, 'rb') as f:
+ edited_settings_file = CAMERA_BASE_DIR / self.cam_name / "edited_settings.pkl"
+ if edited_settings_file.exists():
+ with open(edited_settings_file, 'rb') as f:
zone = pickle.load(f)
- self.zone = zone
- edited_zone_file.unlink()
-
+ self.settings = zone
+ edited_settings_file.unlink()
if userID and live_link[self.cam_name] and (time.time() - last_live_seg) >= 4:
last_live_seg = time.time()
@@ -601,16 +600,18 @@ class VideoCapture:
pre = preprocess(frame)
preds = do_inf(pre).numpy()
if track:
- online_targets = tracker.update(preds, [1280,1280], [1280,1280]) # todo, zone in js also hardcoded to 1280
+ thresh = (self.settings.get("threshold") if self.settings else 0.5) or 0.5 #todo clean!
+ online_targets = tracker.update(preds, [1280,1280], [1280,1280], thresh) #todo, zone in js also hardcoded to 1280
preds = []
for x in online_targets:
if x.tracklet_len < 1: continue # dont alert for 1 frame, too many false positives
- if hasattr(self, "zone") and self.zone is not None and self.zone["is_on"]:
- outside = ((x.tlwh[0]+x.tlwh[2])<self.zone["dims"][0] or\
- x.tlwh[0]>=(self.zone["dims"][0]+self.zone["dims"][2]) or\
- (x.tlwh[1]+x.tlwh[3])<self.zone["dims"][1] or\
- x.tlwh[1]>(self.zone["dims"][1]+self.zone["dims"][3]))
- if outside ^ self.zone["outside"]: continue
+ if hasattr(self, "settings") and self.settings is not None and self.settings["is_on"]:
+ # todo, renmae dims to zone dims or something
+ outside = ((x.tlwh[0]+x.tlwh[2])<self.settings["dims"][0] or\
+ x.tlwh[0]>=(self.settings["dims"][0]+self.settings["dims"][2]) or\
+ (x.tlwh[1]+x.tlwh[3])<self.settings["dims"][1] or\
+ x.tlwh[1]>(self.settings["dims"][1]+self.settings["dims"][3]))
+ if outside ^ self.settings["outside"]: continue
preds.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))
@@ -838,34 +839,34 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
self.end_headers()
return
- if parsed_path.path == "/edit_zone":
+ if parsed_path.path == "/edit_settings":
if not cam_name:
self.send_error(400, "Missing cam or id")
return
- zone_file = CAMERA_BASE_DIR / cam_name / "zone.pkl"
- edited_zone_file = CAMERA_BASE_DIR / cam_name / "edited_zone.pkl"
- if not zone_file.exists():
- with open(zone_file, "wb") as f:
+ settings_file = CAMERA_BASE_DIR / cam_name / "settings.pkl"
+ edited_settings_file = CAMERA_BASE_DIR / cam_name / "edited_settings.pkl"
+ if not settings_file.exists():
+ with open(settings_file, "wb") as f:
pickle.dump(None, f)
- zone_file.parent.mkdir(parents=True, exist_ok=True)
- with open(zone_file, "rb") as f:
+ settings_file.parent.mkdir(parents=True, exist_ok=True)
+ with open(settings_file, "rb") as f:
zone = pickle.load(f)
if zone is None: zone = {}
outside = query.get("outside", [None])[0]
is_on = query.get("is_on", [None])[0]
+ threshold = query.get("threshold", ["0.5"])[0] #default 0.5?
if is_on is not None: is_on = str(is_on).lower() == "true"
if outside is not None: outside = str(outside).lower() == "true"
tl_x = query.get("tl_x", [None])[0]
tl_y = query.get("tl_y", [None])[0]
w = query.get("w", [None])[0]
h = query.get("h", [None])[0]
-
-
if tl_x is not None: zone["dims"] = [float(tl_x),float(tl_y),float(w),float(h)]
if is_on is not None: zone["is_on"] = is_on
if outside is not None: zone["outside"] = outside
- with open(zone_file, 'wb') as f: pickle.dump(zone, f)
- with open(edited_zone_file, 'wb') as f: pickle.dump(zone, f)
+ if threshold is not None: zone["threshold"] = float(threshold)
+ with open(settings_file, 'wb') as f: pickle.dump(zone, f)
+ with open(edited_settings_file, 'wb') as f: pickle.dump(zone, f)
self.send_response(200)
self.send_header("Content-Type", "application/json")
@@ -926,21 +927,20 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
self.wfile.write(b'{"status":"ok"}')
return
- if parsed_path.path == "/get_zone":
+ if parsed_path.path == "/get_settings":
zone = {}
if not cam_name:
self.send_error(400, "Missing cam parameter")
return
- zone_file = CAMERA_BASE_DIR / cam_name / "zone.pkl"
- if zone_file.exists():
+ settings_file = CAMERA_BASE_DIR / cam_name / "settings.pkl"
+ if settings_file.exists():
try:
- with open(zone_file, "rb") as f:
+ with open(settings_file, "rb") as f:
zone = pickle.load(f)
except Exception as e:
self.send_error(500, f"Failed to load zone: {e}")
return
- for _ in range(100): print("zone =",zone)
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
@@ -1595,7 +1595,7 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
<div id="alertsContainer">
<p>Loading alerts...</p>
<div style="display: flex; gap: 10px; justify-content: flex-start; margin-top: 10px;">
- <button onclick="openZoneEditor()">Zone Settings</button>
+ <button onclick="openSettingsEditor()">Settings</button>
<button onclick="openAlertModal()">Add Alert</button>
</div>
</div>
@@ -1664,11 +1664,15 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
<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> Enabled
+ <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>
+ <label style="display: flex; align-items: center; gap: 6px;">
+ Detection Threshold:
+ <input type="number" id="zoneThreshold" value="50" min="0" max="100" step="1" style="width: 60px;"> %
+ </label>
</div>
<div style="display: flex; gap: 10px;">
<button type="button" onclick="closeZoneModal()">Cancel</button>
@@ -1678,6 +1682,7 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
</div>
</div>
+
<div class="counts-wrapper">
<table id="objectCounts" style="font-size: 1rem; border-collapse: collapse;">
<tbody></tbody>
@@ -1730,24 +1735,20 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
let startWidth, startHeight;
let startLeft, startTop;
- function openZoneEditor() {{
+ function openSettingsEditor() {{
zoneModal.style.display = "flex";
- fetch(`/get_zone?cam=${{encodeURIComponent(cameraName)}}`)
+ fetch(`/get_settings?cam=${{encodeURIComponent(cameraName)}}`)
.then(res => res.json())
.then(data => {{
if (data && data.dims) {{
const [tl_x, tl_y, w, h] = data.dims;
const previewEl = document.getElementById("zonePreview");
-
const videoWidth = 1280;
const videoHeight = 720;
-
- // Convert video coords -> preview coords
const left = (tl_x / videoWidth) * previewEl.clientWidth;
const top = (tl_y / videoHeight) * previewEl.clientHeight;
const width = (w / videoWidth) * previewEl.clientWidth;
const height = (h / videoHeight) * previewEl.clientHeight;
-
zoneRect.style.left = left + "px";
zoneRect.style.top = top + "px";
zoneRect.style.width = width + "px";
@@ -1762,8 +1763,6 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
}} else {{
zoneEnabledCheckbox.checked = false;
}}
-
- // Set Outside Zone checkbox - check both possible response formats
const outsideZoneCheckbox = document.getElementById("outsideZoneCheckbox");
if (data.outside !== undefined) {{
outsideZoneCheckbox.checked = data.outside;
@@ -1772,6 +1771,20 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
}} else {{
outsideZoneCheckbox.checked = false;
}}
+ const thresholdInput = document.getElementById("zoneThreshold");
+ let rawThreshold = undefined;
+ if (data.threshold !== undefined) {{
+ rawThreshold = data.threshold;
+ }} else if (data.length && data[0] && data[0].threshold !== undefined) {{
+ rawThreshold = data[0].threshold;
+ }}
+ if (rawThreshold !== undefined) {{
+ if (rawThreshold <= 1) {{
+ thresholdInput.value = (rawThreshold * 100).toFixed(0);
+ }} else {{
+ thresholdInput.value = rawThreshold.toFixed(0);
+ }}
+ }}
}})
.catch(err => {{
console.error("Failed to load zone:", err);
@@ -1800,6 +1813,8 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
const is_on = document.getElementById("zoneEnabledCheckbox").checked;
const outside = document.getElementById("outsideZoneCheckbox").checked;
+ const thresholdPercent = parseFloat(document.getElementById("zoneThreshold").value) || 50;
+ const threshold = thresholdPercent / 100;
const params = new URLSearchParams({{
cam: cameraName,
tl_x: tl_x.toFixed(2),
@@ -1807,10 +1822,11 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
w: w.toFixed(2),
h: h.toFixed(2),
is_on: is_on,
+ threshold: threshold.toFixed(2),
outside: outside
}});
- fetch(`/edit_zone?${{params.toString()}}`)
+ fetch(`/edit_settings?${{params.toString()}}`)
.then(res => {{
if (!res.ok) throw new Error("Failed to save zone");
console.log("Zone saved successfully");
@@ -1822,6 +1838,7 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
}});
}}
+
function initZoneEditor() {{
const handle = zoneRect.querySelector('.resize-handle');
@@ -2102,7 +2119,7 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
container.innerHTML = `
<p>No alerts configured.</p>
<div style="display: flex; gap: 10px; justify-content: center; margin-top: 10px;">
- <button onclick="openZoneEditor()">Zone Settings</button>
+ <button onclick="openSettingsEditor()">Settings</button>
<button onclick="openAlertModal()">Add Alert</button>
</div>`;
return;
@@ -2163,7 +2180,7 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
html += `</tbody></table>
<div style="margin-top:10px; display:flex; gap:10px; justify-content:center;">
- <button onclick="openZoneEditor()">Zone Settings</button>
+ <button onclick="openSettingsEditor()">Settings</button>
<button onclick="openAlertModal()">Add Alert</button>
</div>`;
container.innerHTML = html;
diff --git a/yolox/tracker/byte_tracker.py b/yolox/tracker/byte_tracker.py
index dd8c9a9..3ed4577 100644
--- a/yolox/tracker/byte_tracker.py
+++ b/yolox/tracker/byte_tracker.py
@@ -146,13 +146,11 @@ class BYTETracker(object):
self.frame_id = 0
self.args = args
- #self.det_thresh = args.track_thresh
- self.det_thresh = args.track_thresh + 0.1
self.buffer_size = int(frame_rate / 30.0 * args.track_buffer)
self.max_time_lost = self.buffer_size
self.kalman_filter = KalmanFilter()
- def update(self, output_results, img_info, img_size):
+ def update(self, output_results, img_info, img_size, threshold=0.5):
self.frame_id += 1
activated_starcks = []
refind_stracks = []
@@ -258,7 +256,7 @@ class BYTETracker(object):
""" Step 4: Init new stracks"""
for inew in u_detection:
track = detections[inew]
- if track.score < self.det_thresh:
+ if track.score < threshold:
continue
track.activate(self.kalman_filter, self.frame_id)
activated_starcks.append(track)