Skip to content

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

Settings4 (#173)

Rory Cleard7393b209cff
diff --git a/clearcam.py b/clearcam.py
index b3d7286..6ee5bfa 100644
--- a/clearcam.py
+++ b/clearcam.py
@@ -276,7 +276,7 @@ class VideoCapture:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
clip_latest_img(img)
process_latest_face(img)
- except Exception: print("error in object processing", object_queue[0])
+ except Exception as e: print("error in object processing", object_queue[0], e)
del object_queue[0]
@@ -736,7 +736,7 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
return
if parsed_path.path == "/get_global_settings":
- self.send_200(global_settings.__dict__)
+ self.send_200(secret_settings(global_settings).__dict__)
return
if parsed_path.path == "/get_max_storage":
self.send_200(body={"max_gb":self.server.max_gb})
@@ -1027,6 +1027,9 @@ class HLSRequestHandler(BaseHTTPRequestHandler):
content_length = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(content_length)
data = json.loads(body.decode('utf-8'))
+ # keep userid and key if "True"
+ if data["userID"] == True: data["userID"] = global_settings.userID
+ if data["key"] == True: data["key"] = global_settings.key
add_to_queue(db.run_put, database, "global_settings", "all", GlobalSettings(**data))
add_to_queue(set_settings, GlobalSettings(**data))
self.send_200([])
@@ -1269,15 +1272,18 @@ def set_settings(x): # todo, save to db, do logic in GlobalSettings class, sanit
if x.use_qwen:
qwen = Qwen3VL(size=f"{x.qwen_size}B", res=(544, 960)) # h, w. they need to be multiples of 32
qwen_prompt = "What has been detected on my CCTV camera? Write in one short sentence"
+ print("prewarming Qwen")
qwen.prewarm()
+ print("DONE")
else: qwen = None
-
global_settings = x
def clip_latest_img(img):
if global_settings.use_clip:
img = object_finder.preprocess(img)
- data = pickle.load(open(object_queue[0].parent / 'embeddings.pkl', 'rb')) if os.path.exists(object_queue[0].parent / 'embeddings.pkl') else {}
+ try:
+ data = pickle.load(open(object_queue[0].parent / 'embeddings.pkl', 'rb')) if os.path.exists(object_queue[0].parent / 'embeddings.pkl') else {}
+ except Exception: data = {}
if "embeddings" not in data: data["embeddings"] = {}
emb = jit_infer(object_finder.model.precompute_embedding, Tensor(img).unsqueeze(0), jit_cache).numpy()
data["embeddings"][str(object_queue[0])] = emb
@@ -1390,7 +1396,7 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
super().server_close()
class GlobalSettings:
- def __init__(self, use_clip=True, use_face=False ,model_size="t", model_res=960, userID=None, key=None, use_qwen=False, qwen_size=2):
+ def __init__(self, use_clip=False, use_face=False ,model_size="t", model_res=960, userID=None, key=None, use_qwen=False, qwen_size=2):
self.use_clip = use_clip
self.use_face = use_face
self.model_size = model_size
@@ -1400,6 +1406,18 @@ class GlobalSettings:
self.use_qwen = use_qwen
self.qwen_size = qwen_size
+def secret_settings(settings):
+ return GlobalSettings(
+ use_clip=settings.use_clip,
+ use_face=settings.use_face,
+ model_size=settings.model_size,
+ model_res=settings.model_res,
+ userID=settings.userID is not None,
+ key=settings.key is not None,
+ use_qwen=settings.use_qwen,
+ qwen_size=settings.qwen_size
+ )
+
if __name__ == "__main__":
jit_cache = {}
yolo_jit_cache = {}
@@ -1409,45 +1427,8 @@ if __name__ == "__main__":
cams = database.run_get("links", None)
classes = {"0","1","2","7"} # person, bike, car, truck, bird (14)
- userID = next((arg.split("=", 1)[1] for arg in sys.argv[1:] if arg.startswith("--userid=")), None)
- key = next((arg.split("=", 1)[1] for arg in sys.argv[1:] if arg.startswith("--key=")), None)
- use_face = next((arg.split("=", 1)[1] for arg in sys.argv[1:] if arg.startswith("--use_face=")), None)
- if use_face: use_face = use_face != "False" # str to bool
- model_variant = next((arg.split("=", 1)[1] for arg in sys.argv[1:] if arg.startswith("--yolo_size=")), None)
- yolo_res = next((arg.split("=", 1)[1] for arg in sys.argv[1:] if arg.startswith("--yolo_res=")), 640)
- if not model_variant:
- model_variant = int(input("Select a YOLOv9 model from: \n1: tiny\n2: small\n3: medium\n4: large\n5: xl\nor an RFDETER Model from \n6: nano\n7: small\n8: medium\n9: large\nor press enter to skip (defaults to tiny):") or "1")
- if model_variant < 6:
- yolo_ress = {"1":320, "2":640, "3":960,"4":1280,"5":1536}
- yolo_res = yolo_ress[input("\nSelect a YOLOV9 resoltuion from \n1: 320\n2: 640\n3: 960\n4: 1280\n5: 1536\nor press enter to skip (defaults to 960):") or "3"]
- use_clip = input("Would you like to enable clip search on events? (y/n) (1.7GB model), or press enter to skip:") or False
- use_clip = use_clip in ["y", "Y"]
- use_face = input("Would you like to enable (experimental) face recognition search? (y/n), or press enter to skip:") or False
- use_face = use_face in ["y", "Y"]
-
- userID = input("enter your Clearcam user id or press Enter to skip: ")
- use_qwen = False
- qwen_size = 2
- if len(userID) > 0:
- key = ""
- while len(key) < 1: key = input("enter a password for encryption: ")
- qwen_size = input("Select a Qwen3VL model for AI Summaries from \n2: 2B\n4: 4B\nor press enter to skip:") or False
- if qwen_size in ["2", "4"]:
- print("prewarming Qwen3VL....")
- qwen = Qwen3VL(size=f"{qwen_size}B", res=(544, 960)) # h, w. they need to be multiples of 32
- qwen_prompt = "What has been detected on my CCTV camera? Write in one short sentence"
- qwen.prewarm()
- print("DONE")
- use_qwen = True
- else: userID = None
-
- global_settings = GlobalSettings(use_clip=use_clip, model_size=models[model_variant], model_res=yolo_res,
- userID=userID, key=key, use_qwen=use_qwen, qwen_size=qwen_size)
+ global_settings = GlobalSettings()
database.run_put("global_settings", "all", global_settings)
-
- if userID is not None and key is None:
- print("Error: key is required when userID is provided")
- sys.exit(1)
from models.objects import ObjectFinder
@@ -1458,8 +1439,8 @@ if __name__ == "__main__":
color_dict = {label: tuple((((i+1) * 50) % 256, ((i+1) * 100) % 256, ((i+1) * 150) % 256)) for i, label in enumerate(class_labels)}
cam = None
- model = YOLOv9(models[int(model_variant)], res=int(yolo_res)) if int(model_variant) < 6 else RFDETR(models[int(model_variant)])
- object_finder = ObjectFinder(clip=global_settings.use_clip, face=use_face)
+ model = YOLOv9(global_settings.model_size, res=int(global_settings.model_res))
+ object_finder = ObjectFinder()
cam = VideoCapture()
try:
diff --git a/mainview.html b/mainview.html
index 7a09b65..89c9e78 100644
--- a/mainview.html
+++ b/mainview.html
@@ -1286,6 +1286,7 @@
<body>
<div id="cameraList" class="camera-grid"></div>
<div class="multi-view-wrapper">
+ <button onclick="openSettingsModal()">Settings</button>
<button onclick="toggleMultiView()">Multi View</button>
<div class="date-picker-container">
<select id="streamDateSelect" style="padding: 6px; border: 1px solid var(--border-color); border-radius: 4px; font-size: 14px; background: var(--bg-primary); color: var(--text-primary);">
@@ -1457,6 +1458,24 @@
</div>
</div>
</div>
+
+ <div id="settingsModal" class="modal">
+ <div class="modal-content" style="max-width: 500px;">
+ <div class="modal-header">
+ <h3>Global Settings</h3>
+ <span class="close" onclick="closeSettingsModal()">&times;</span>
+ </div>
+
+ <div id="settingsContainer" style="display: flex; flex-direction: column; gap: 12px;">
+ <p>Loading settings...</p>
+ </div>
+
+ <div class="form-actions" style="margin-top: 20px; display: flex; justify-content: center;">
+ <button type="button" onclick="saveSettings()">Save</button>
+ </div>
+ </div>
+ </div>
+
<div id="alertModal" class="modal">
<div class="modal-content" style="max-width: 400px;">
<div class="modal-header">
@@ -3419,6 +3438,189 @@
}
});
+ async function openSettingsModal() {
+ const modal = document.getElementById("settingsModal");
+ modal.style.display = "flex";
+
+ const container = document.getElementById("settingsContainer");
+ container.innerHTML = "<p>Loading settings...</p>";
+
+ try {
+ const res = await fetch("/get_global_settings");
+ const data = await res.json();
+
+ renderSettings(data);
+ } catch (e) {
+ container.innerHTML = "<p style='color:red;'>Failed to load settings</p>";
+ }
+ }
+
+ function closeSettingsModal() {
+ document.getElementById("settingsModal").style.display = "none";
+ }
+
+ function renderSettings(settings) {
+ const container = document.getElementById("settingsContainer");
+ container.innerHTML = "";
+ Object.entries(settings).forEach(([key, value]) => {
+
+ const labelMap = {
+ "use_clip": "OpenCLIP Search",
+ "use_face": "Face Detection (Experimental)",
+ "model_size": "YOLOv9 Model",
+ "model_res": "YOLOv9 Resolution",
+ "use_qwen": "AI Event Summaries (Qwen3VL)",
+ "qwen_size": "Qwen3VL Size",
+ "userID": "User ID",
+ "key": "Key"
+ };
+
+ const tooltipMap = {
+ "use_clip": "Enables searching events using natural language or image similarity with OpenCLIP.",
+ "use_face": "Enables searching detected people using a face image.",
+ "model_size": "YOLOv9 Detection model size (tiny, small, medium, compact, extended)",
+ "model_res": "Sets the input resolution for YOLOv9 detection.",
+ "use_qwen": "Enables AI-powered event summaries for notifications using Qwen3VL vision-language model (Clearcam User ID required).",
+ "qwen_size": "Select the Qwen3VL model size (2B or 4B parameters).",
+ "userID": "Your account user ID for notifications and remote config.",
+ "key": "Secret key for encrypting event clips."
+ };
+
+ const wrapper = document.createElement("div");
+ wrapper.style.display = "flex";
+ wrapper.style.justifyContent = "space-between";
+ wrapper.style.alignItems = "center";
+ wrapper.style.gap = "10px";
+
+ const label = document.createElement("label");
+ const labelContent = (labelMap[key] || key);
+ label.innerHTML = `
+ ${labelContent}
+ <span class="tooltip" style="margin-left: 4px;">
+ <span class="tooltip-icon">?</span>
+ <span class="tooltiptext">${tooltipMap[key] || ''}</span>
+ </span>
+ `;
+ label.style.flex = "1";
+ let input;
+ if (key === "model_size") {
+ input = document.createElement("select");
+ ["t", "s", "m", "c", "e"].forEach(opt => {
+ const option = document.createElement("option");
+ option.value = opt;
+ option.textContent = opt;
+ if (value === opt) option.selected = true;
+ input.appendChild(option);
+ });
+ }
+ else if (key === "model_res") {
+ input = document.createElement("select");
+ [320, 640, 960, 1280, 1536].forEach(opt => {
+ const option = document.createElement("option");
+ option.value = opt;
+ option.textContent = opt;
+ if (value == opt) option.selected = true;
+ input.appendChild(option);
+ });
+ }
+ else if (key === "qwen_size") {
+ input = document.createElement("select");
+ const qwenOptions = [
+ { value: 2, label: "2B" },
+ { value: 4, label: "4B" }
+ ];
+ qwenOptions.forEach(opt => {
+ const option = document.createElement("option");
+ option.value = opt.value;
+ option.textContent = opt.label;
+ if (value == opt.value) option.selected = true;
+ input.appendChild(option);
+ });
+ }
+ else if (key === "userID" || key === "key") {
+ input = document.createElement("input");
+ input.type = "password";
+ input.value = value ? "*****" : "";
+ input.placeholder = key;
+ input.style.width = "120px";
+ }
+ else if (typeof value === "boolean") {
+ input = document.createElement("input");
+ input.type = "checkbox";
+ input.checked = value;
+ }
+ else {
+ input = document.createElement("input");
+ input.type = "text";
+ input.value = value;
+ input.style.width = "120px";
+ }
+ input.dataset.key = key;
+ wrapper.appendChild(label);
+ wrapper.appendChild(input);
+ container.appendChild(wrapper);
+ });
+ }
+
+ async function saveSettings() {
+ const container = document.getElementById("settingsContainer");
+ const inputs = container.querySelectorAll("input, select");
+ const payload = {};
+
+ inputs.forEach(input => {
+ const key = input.dataset.key;
+ if (!key) return;
+
+ if (input.type === "checkbox") {
+ payload[key] = input.checked;
+ }
+ else if (input.tagName === "SELECT") {
+ const val = input.value;
+ if (["model_res", "qwen_size"].includes(key)) {
+ payload[key] = Number(val);
+ } else {
+ payload[key] = val;
+ }
+ }
+ else {
+ const val = input.value;
+ if (key === "userID" || key === "key") {
+ if (val === "*****") {
+ payload[key] = true;
+ } else if (val === "") {
+ payload[key] = null;
+ } else {
+ payload[key] = val;
+ }
+ } else {
+ const num = Number(val);
+ payload[key] = isNaN(num) ? val : num;
+ }
+ }
+ });
+
+ if ((payload.userID && payload.userID !== true) || (payload.key && payload.key !== true)) {
+ if (!confirm("Warning: Your User ID and Key will be sent over the network.\n\n" +
+ "For your security, avoid entering these on public or unsecured WiFi networks. " +
+ "It is recommended to configure this over localhost or a trusted private network.\n\n" +
+ "Do you want to continue?")) {
+ return;
+ }
+ }
+
+ const body = JSON.stringify(payload);
+ closeSettingsModal();
+ fetch("/edit_settings", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json"
+ },
+ body: body
+ }).catch(e => {
+ console.error("Failed to save settings:", e);
+ });
+ }
+
fetchCameras();
setInterval(fetchCameras, 5000);
</script>
diff --git a/models/objects.py b/models/objects.py
index aa7ecee..f0bc083 100644
--- a/models/objects.py
+++ b/models/objects.py
@@ -186,21 +186,15 @@ def encode_text(model, text):
return x / (x * x).sum(axis=-1, keepdim=True).sqrt()
class ObjectFinder:
- def __init__(self, base_path="data/cameras", clip=False, face=False):
+ def __init__(self, base_path="data/cameras"):
self.base_path = base_path
self.image_embeddings = {}
self.face_embeddings = {}
self.image_paths = {}
self.face_paths = {}
- self.clip = False # todo
- self.face = face
+ self.clip = False # todo, should be able to init at start
+ self.face = False # todo
self.jit_cache = {}
-
- if clip: self.init_clip()
-
- if self.face:
- self.blazeface = BlazeFace()
- self.adaface = ADAFACE()
def init_clip(self):
if self.clip: return