Skip to content

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

63.4 KB
Raw Blame
from tinygrad.tensor import Tensor
from tinygrad.helpers import fetch
from tinygrad.dtype import dtypes
from detection.yolov9 import YOLOv9
from llm.qwen3vl import Qwen3VL
import numpy as np
from pathlib import Path
import cv2
from collections import defaultdict, deque
import time, sys
import json
import http
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
import threading
import shutil
from datetime import datetime, time as time_obj
import uuid
import urllib
from urllib.parse import urlparse, parse_qs
import struct
from urllib.parse import unquote, quote
import zlib
from utils.db import db
import multiprocessing
import re
import base64
from utils.helpers import send_notif, find_ffmpeg, export_clip, upload_file, encrypt_file, export_and_upload, jit_infer
import pickle

# RTSP URL
# Video capture thread
import subprocess
import threading
import time
import numpy as np
import cv2
from datetime import datetime
import os
import threading
from utils.helpers import BASE_DIR
from ocsort_tracker import ocsort

(BASE_DIR / "cameras").mkdir(parents=True, exist_ok=True)
models = {1: "t", 2: "s", 3: "m", 4: "c", 5: "e", 6: "nano", 7: "small", 8:"medium", 9:"large"}

class RollingClassCounter:
  def __init__(self, window_seconds=None, max=None, classes=None, sched=[[0,86399],True,True,True,True,True,True,True],cam_name=None, desc=None, threshold=0.28):
    self.window = window_seconds
    self.data = defaultdict(deque)
    self.max = max
    self.classes = classes
    self.last_det = 0
    self.sched = sched
    self.cam_name = cam_name
    self.is_on = True
    self.is_notif = True
    self.zone = True
    self.reset = False
    self.new = True
    self.desc = desc
    self.desc_emb = None
    self.threshold = threshold

  def add(self, class_id):
    if self.classes is not None and class_id not in self.classes: return
    now = time.time()
    self.data[class_id].append(now)
    self.cleanup(class_id, now)

  def cleanup(self, class_id, now):
    q = self.data[class_id]
    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?
    self.reset = True

  def get_counts(self):
    window = self.window if self.window else (60 if self.is_notif else 1)
    max_reached = False
    now = time.time()
    counts = {}
    for class_id, q in self.data.items():
      while window and q and now - q[0] > window:
        q.popleft()
      if q:
        counts[class_id] = len(q)
        if self.max and len(q) >= self.max: max_reached = True
    return counts, max_reached
  
  def is_active(self, offset=0):
    if not alerts_on[self.cam_name]: return False
    if not getattr(self, "is_on", False): return False
    if not self.sched: return True
    now = time.localtime()
    time_of_day = now.tm_hour * 3600 + now.tm_min * 60 + now.tm_sec
    if not self.sched[time.localtime().tm_wday + 1]: return False
    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
    height, width, _ = array.shape
    png_signature = b"\x89PNG\r\n\x1a\n"
    def chunk(chunk_type, data):
        return (struct.pack("!I", len(data)) +
                chunk_type +
                data +
                struct.pack("!I", zlib.crc32(chunk_type + data) & 0xffffffff))
    ihdr = struct.pack("!IIBBBBB", width, height, 8, 2, 0, 0, 0)
    raw_data = b"".join(b"\x00" + array[y].tobytes() for y in range(height))
    compressed = zlib.compress(raw_data, 9)
    png_bytes = (
        png_signature +
        chunk(b"IHDR", ihdr) +
        chunk(b"IDAT", compressed) +
        chunk(b"IEND", b"")
    )
    with open(filename, "wb") as f:
        f.write(png_bytes)

import numpy as np

def draw_rectangle_numpy(img, pt1, pt2, color, thickness=1):
    x1, y1 = pt1
    x2, y2 = pt2
    x1, y1 = max(0, x1), max(0, y1)
    x2, y2 = min(img.shape[1]-1, x2), min(img.shape[0]-1, y2)
    if thickness == -1:  # fill
        img[y1:y2+1, x1:x2+1] = color
    else:
        img[y1:y1+thickness, x1:x2+1] = color
        img[y2-thickness+1:y2+1, x1:x2+1] = color
        img[y1:y2+1, x1:x1+thickness] = color
        img[y1:y2+1, x2-thickness+1:x2+1] = color
    return img


def is_vod(cam_name): return Path("data/cameras", cam_name, "streams", "video").is_dir()

def _get_stream_resolution(src):
  ffmpeg_path = find_ffmpeg()
  command = [ffmpeg_path, "-i", src]
  try:
    result = subprocess.run(
        command,
        stderr=subprocess.PIPE,
        stdout=subprocess.DEVNULL,
        text=True,
        timeout=10
    )
    output = result.stderr
    match = re.search(r"Video:.*?(\d{2,5})x(\d{2,5})", output)
    if match:
      width, height = map(int, match.groups())
      return width, height
    return 1920, 1080
  except Exception as e:
    return 1920, 1080

class VideoCapture:
  def __init__(self):
    self.output_dir_raw = {}
    self.frame_num = {}
    self.last_frame_num = {}
    self.vod = {}
    # objects in scene count
    self.counter = {}
    self.object_set = {}
    self.object_set_zone = {}

    self.src = {}
    self.width, self.height = {}, {}
    self.proc = {}
    self.hls_proc = {}
    self.running = {}

    self.raw_frame = {}
    self.last_preds = {}
    self.last_frames = {}

    self.settings = {}
    self.count = {}
    self.prev_time = {}
    self.current_stream_dir_raw = {}
    self.pred_occs = {}
    self.tracker = {}
    self.cap = {}
    self.src_fps = {}
    self.last_det = {}
    self.last_live_check = {}
    self.last_counter_update = {}
    self.last_preview_time = {}
    self.last_live_seg = {}
    self.start_time = {}
    self.filename = {}
    self.alert_counters = {}
    self.live_link = {}
    self.live_link_lock = {}

    #self.last_shapes_time = time.time()
    #self.det_shapes = []

  def init_cam(self, cam_name, src):
    self.counter[cam_name] = RollingClassCounter(cam_name=cam_name, window_seconds=float('inf'))
    self.src[cam_name] = src # todo
    self.last_frames[cam_name] = deque(maxlen=2)
    self.vod[cam_name] = src.endswith(('.mp4', '.avi', '.mov', '.mkv', '.webm'))
    self.frame_num[cam_name] = -1
    self.last_frame_num[cam_name] = -1
    self.object_set[cam_name] = set()
    self.object_set_zone[cam_name] = set()
    self.running[cam_name] = True
    self.output_dir_raw[cam_name] = BASE_DIR / "cameras" / f'{cam_name}' / "streams"
    self.last_preds[cam_name] = []
    self.raw_frame[cam_name] = None
    self.width[cam_name], self.height[cam_name] = _get_stream_resolution(src)
    self.settings[cam_name] = None
    self.start_time[cam_name] = None
    
    self.alert_counters[cam_name] = database.run_get("alerts",cam_name)
    if not self.alert_counters[cam_name]:
      self.alert_counters[cam_name] = dict()
      id, alert_counter = str(uuid.uuid4()), RollingClassCounter(window_seconds=None, max=1, classes={0,1,2,3,5,7},cam_name=cam_name)
      self.alert_counters[cam_name][id] = alert_counter
      database.run_put("alerts", cam_name, alert_counter, id=id)

    self.last_det[cam_name] = -1
    self.last_live_check[cam_name] = time.time()
    self.last_live_seg[cam_name] = time.time()
    self.last_preview_time[cam_name] = None
    self.last_counter_update[cam_name] = time.time()
    self.pred_occs[cam_name] = {}
    self.hls_proc[cam_name], self.proc[cam_name] = self._open_ffmpeg(cam_name)
    self.tracker[cam_name] = ocsort.OCSort(max_age=100)
    self.count[cam_name] = 0
    self.prev_time[cam_name] = time.time()
    self.current_stream_dir_raw[cam_name] = self._get_new_stream_dir(cam_name)
    self.filename[cam_name] = None
    self.live_link_lock[cam_name] = threading.Lock()
    alerts_on[cam_name] = True

  def start(self):
    cam_check = time.time()
    cams = database.run_get("links", None)
    for cam_name in cams.keys():
      print("starting",cam_name,"src:",cams[cam_name])
      self.init_cam(cam_name=cam_name, src=cams[cam_name])
      threading.Thread(target=self.frame_loop, args=(cam_name,), daemon=True).start() # todo non vod only!
    while True:
      if time.time() - cam_check >= 5:
        cam_check = time.time()
        new_cams = database.run_get("links", None)
        for cam_name in new_cams.keys():
          if type(new_cams[cam_name]) != str: continue # todo find cause
          if cam_name not in cams:
            self.init_cam(cam_name=cam_name, src=new_cams[cam_name])
            if not self.vod[cam_name]: threading.Thread(target=self.frame_loop, args=(cam_name,), daemon=True).start() # todo non vod only
          else:
            if new_cams[cam_name] != cams[cam_name]:
              cams[cam_name] = new_cams[cam_name]
              self.src[cam_name] = new_cams[cam_name]
              self.hls_proc[cam_name], self.proc[cam_name] = self._open_ffmpeg(cam_name)
        cams = new_cams
      for cam_name in cams.keys():
        if self.vod[cam_name]: self.process_frame_vod(cam_name=cam_name) 
        else: self.process_frame(cam_name=cam_name)
      process_queue()
      if len(object_queue) > 0:
        try:
          img = cv2.imread(object_queue[0])
          img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
          clip_latest_img(img)
          process_latest_face(img)
        except Exception as e: print("error in object processing", object_queue[0], e)
        del object_queue[0] 
           


  def _get_new_stream_dir(self, cam_name):
      timestamp = "video" if self.vod[cam_name] else datetime.now().strftime("%Y-%m-%d")
      stream_dir_raw = self.output_dir_raw[cam_name] / timestamp
      stream_dir_raw.mkdir(parents=True, exist_ok=True)
      return stream_dir_raw

  def _open_ffmpeg(self, cam_name):
    path = self._get_new_stream_dir(cam_name)
    if cam_name in self.proc and self.proc[cam_name]: self.proc[cam_name].kill()
    if cam_name in self.hls_proc and self.hls_proc[cam_name]: self.hls_proc[cam_name].kill()
    src = self.src[cam_name]
    if type(src) != str: return # todo, fixes a crash, fix cause

    ffmpeg_path = find_ffmpeg()
    
    is_rtsp = src.startswith("rtsp")
    if self.vod[cam_name]:
      command = [
        ffmpeg_path,
        "-i", src,
        "-c:v", "copy",
        "-an",
        "-f", "hls",
        "-hls_time", "2",
        "-hls_list_size", "0",
        "-hls_flags", "independent_segments",
        "-hls_segment_type", "fmp4",
        "-hls_fmp4_init_filename", "init.mp4",
        "-hls_segment_filename", str(path / "seg_%06d.m4s"),
        str(path / "stream.m3u8"),
      ]
      return subprocess.Popen(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL), None
        
    else:  # Live streams
      # Original live stream pipeline
      command = [
          ffmpeg_path,
          *(["-rtsp_transport", "tcp"] if is_rtsp else []),
          "-fflags", "+genpts",
          "-avoid_negative_ts", "make_zero",
          "-i", src,
          "-c", "copy",
          "-an",
          "-f", "hls",
          "-hls_time", "2",
          "-hls_list_size", "0",
          "-hls_playlist_type", "event",
          "-hls_segment_type", "fmp4",
          "-hls_fmp4_init_filename", "init.mp4",
          "-hls_flags", "append_list+independent_segments+temp_file",
          "-hls_segment_filename", str(path / "stream_%06d.m4s"),
          "-start_number", "0",
          str(path / "stream.m3u8")
      ]
      hls_proc = subprocess.Popen(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
      if self.start_time[cam_name] is None: self.start_time[cam_name] = time.time()
      
      command = [
          ffmpeg_path,
          "-live_start_index", "-1",
          "-i", str(path / "stream.m3u8"),
          "-loglevel", "quiet",
          "-reconnect", "1",
          "-reconnect_streamed", "1",
          "-reconnect_delay_max", "2",
          "-an",
          "-f", "rawvideo",
          "-pix_fmt", "bgr24",
          "-vf", f"scale={self.width[cam_name]}:{self.height[cam_name]}",
          "-timeout", "5000000",
          "-rw_timeout", "15000000",
          "-vsync", "2",
          "-fflags", "+discardcorrupt+fastseek+flush_packets+nobuffer",
          "-avioflags", "direct",
          "-flags", "low_delay",
          "-max_delay", "100000",
          "-threads", "1",
          "-"
      ]
      return hls_proc, subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)

  def save_object(self, p, ts=0, cam_name=None):
    p = np.array([p.tlwh[0],p.tlwh[1],(p.tlwh[0]+p.tlwh[2]),(p.tlwh[1]+p.tlwh[3]),p.score,p.class_id,p.track_id])
    timestamp = "video" if self.vod[cam_name] else datetime.now().strftime("%Y-%m-%d")
    filepath = BASE_DIR / "cameras" / f"{cam_name}/objects/{timestamp}"
    filepath.mkdir(parents=True, exist_ok=True)
    (BASE_DIR / "cameras" / f"{cam_name}/faces/{timestamp}").mkdir(parents=True, exist_ok=True)
    object_filename = filepath / f"{ts}_{int(p[6])}_{int(p[5])}.jpg"
    x1, y1, x2, y2 = map(int, (p[0], p[1], p[2], p[3]))
    cx = (x1 + x2) // 2
    cy = (y1 + y2) // 2
    hw = (x2 - x1) // 2
    hh = (y2 - y1) // 2
    hw *= 2
    hh *= 2
    x1_new = cx - hw
    x2_new = cx + hw
    y1_new = cy - hh
    y2_new = cy + hh
    H, W = self.last_frames[cam_name][-1].shape[:2]
    x1_new = max(0, min(x1_new, W))
    x2_new = max(0, min(x2_new, W))
    y1_new = max(0, min(y1_new, H))
    y2_new = max(0, min(y2_new, H))
    if (y2_new - y1_new) < 100 or (x2_new - x1_new) < 100: return # too small
    crop = self.last_frames[cam_name][-1][y1_new:y2_new, x1_new:x2_new]
    cv2.imwrite(str(object_filename), crop)
    if global_settings.use_clip or global_settings.use_face: object_queue.append(object_filename)

  def frame_loop(self, cam_name):
    fail_count = 0
    frame_size = self.width[cam_name] * self.height[cam_name] * 3
    while (BASE_DIR / "cameras" / cam_name).exists():
      try:
        raw_bytes = self.proc[cam_name].stdout.read(frame_size)
        if len(raw_bytes) != frame_size:
          time.sleep(0.5)
          if time.time() - self.start_time[cam_name] > 15: # 15 sec grace before restart
            fail_count += 1
            if fail_count > 5:
              print(f"{cam_name} FFmpeg frame read failed (count={fail_count}), restarting stream...{self.src[cam_name]}")
              self.hls_proc[cam_name], self.proc[cam_name] = self._open_ffmpeg(cam_name)
              fail_count = 0
        else:
          fail_count = 0
        self.raw_frame[cam_name] = np.frombuffer(raw_bytes, np.uint8).reshape((self.height[cam_name], self.width[cam_name], 3))
        self.frame_num[cam_name] += 1
        time.sleep(1 / 100)
      except Exception as e:
        print(f"Waiting for frames from {cam_name}...")
        time.sleep(1)

  def process_frame_vod(self, cam_name):
    if cam_name not in self.cap:
      self.cap[cam_name] = cv2.VideoCapture(self.src[cam_name])
      self.src_fps[cam_name] = self.cap[cam_name].get(cv2.CAP_PROP_FPS) or 30

    self.cap[cam_name].grab()  # skip for max fps
    ret, frame = self.cap[cam_name].read()
    self.last_frames[cam_name].append(frame)
    if not ret or cam_name not in database.run_get("links", None):
      self.running[cam_name] = False
      if "Processing" not in database.run_get("analysis_prog", cam_name): database.run_put("analysis_prog", cam_name, {"Tracking":100}) # todo stop when done?
    else:
      self.last_preds[cam_name], _ = self.run_inference(frame, cam_name=cam_name)
      database.run_put("analysis_prog", cam_name, {"Tracking":self.cap[cam_name].get(cv2.CAP_PROP_POS_FRAMES)/self.cap[cam_name].get(cv2.CAP_PROP_FRAME_COUNT)*100})


  def process_frame(self, cam_name):
    frame_num = self.frame_num[cam_name]
    last_frame_num = self.last_frame_num[cam_name]
    if self.raw_frame[cam_name] is None: return
    frame = self.raw_frame[cam_name].copy()
    if frame_num == last_frame_num: return

    # don't run inference when no active scheds
    if not any(counter.is_active() for _, counter in self.alert_counters[cam_name].items()): self.last_preds[cam_name] = [] # to remove annotation when no alerts active
    else:
      if not global_settings.use_notifs() or alerts_on[cam_name]:
        preds, frame = self.run_inference(frame, cam_name=cam_name)
        self.last_frames[cam_name].append(frame.numpy().copy())
        self.last_preds[cam_name] = preds.copy()
        self.last_frame_num[cam_name] = self.frame_num[cam_name]

        curr_time = time.time()
        fps = 1 / (curr_time - self.prev_time[cam_name])
        self.prev_time[cam_name] = curr_time
        print(f"\rFPS: {fps:.2f}", end="", flush=True)
      else:
        self.last_frame_num[cam_name] = self.frame_num[cam_name]
        self.last_preds = []

    filtered_preds = self.last_preds[cam_name]

    if self.count[cam_name] > 10:
      if self.last_preview_time[cam_name] is None or time.time() - self.last_preview_time[cam_name] >= 3600: # preview every hour
        self.last_preview_time[cam_name] = time.time()
        self.filename[cam_name] = BASE_DIR / "cameras" / f"{cam_name}/preview.png"
        write_png(self.filename[cam_name], self.raw_frame[cam_name])
      for _,alert in self.alert_counters[cam_name].items():
          if alert.desc is not None: continue
          if not alert.is_active():
            alert.reset_counts()
            continue
          window = alert.window if alert.window else (60 if alert.is_notif else 1)
          if alert.get_counts()[1]:
            if time.time() - alert.last_det >= window and (time.time() - alert.last_det >= window):
              timestamp = "video" if self.vod[cam_name] else datetime.now().strftime("%Y-%m-%d")
              filepath = BASE_DIR / "cameras" / f"{cam_name}/event_images/{timestamp}"
              filepath.mkdir(parents=True, exist_ok=True)
              annotated_frame = draw_predictions(self.last_frames[cam_name][-1].copy(), filtered_preds, color_dict)
              # todo alerts can be sent with the wrong thumbnail if two happen quickly, use map
              ts = int(self.cap[cam_name].get(cv2.CAP_PROP_POS_FRAMES) / self.src_fps[cam_name]) - 5 if self.vod[cam_name] else int(time.time() - self.start_time[cam_name] - 15)
              self.filename[cam_name] = filepath / f"{ts}_notif.jpg" if alert.is_notif else filepath / f"{ts}.jpg"
              if not self.vod[cam_name]: cv2.imwrite(str(self.filename[cam_name]), 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
                self.filename[cam_name] = filepath / f"{ts}_notif.jpg"
              if global_settings.use_notifs() is not None and not self.vod[cam_name] and alert.is_notif:
                title = f"Event Detected ({cam_name})"
                threading.Thread(target=send_notif, kwargs={"session_token": global_settings.userID,"text":title,"body_text": None,"host": global_settings.server_url, "img": self.filename[cam_name]}, daemon=True).start()
                if global_settings.use_qwen: # extra notif if qwen
                  # use frames before last, only one reset needed, must convert to RGB
                  for i in range(len(self.last_frames[cam_name])-1): qwen.generate(image=cv2.cvtColor(self.last_frames[cam_name][i], cv2.COLOR_BGR2RGB), reset=True if i==0 else False)
                  text = qwen.generate(prompt=global_settings.qwen_prompt, image=cv2.cvtColor(cv2.imread(self.filename[cam_name]), cv2.COLOR_BGR2RGB), reset=False) # must reset or run out of context
                  threading.Thread(target=send_notif, kwargs={"session_token": global_settings.userID, "text": f"AI Summary ({cam_name}):", "body_text": text, "host": global_settings.server_url}, daemon=True).start()
                if global_settings.clearcam_user(): threading.Thread(target=export_and_upload, kwargs={"cam_name": cam_name, "thumbnail": self.filename[cam_name], "userID": global_settings.userID, "key": global_settings.key, "start": ts}, daemon=True).start()
              self.last_det[cam_name] = time.time()
              alert.last_det = time.time()
      
      if (time.time() - self.last_live_check[cam_name]) >= 5:
        self.last_live_check[cam_name] = time.time()
        link = database.run_get("links", cam_name)
        if type(link) == list: link = link[0] # todo, flakey?
        if link != self.src[cam_name]:
          self.src[cam_name] = link
          self.hls_proc[cam_name], self.proc[cam_name] = self._open_ffmpeg(cam_name)
        if global_settings.clearcam_user() and not self.vod[cam_name]: threading.Thread(target=self.check_upload_link, args=(cam_name,), daemon=True).start()
      if (time.time() - self.last_counter_update[cam_name]) >= 5: #update counter every 5 secs
        self.last_counter_update[cam_name] = time.time()

        counters = database.run_get("counters", cam_name)
        if counters not in [None, {}]:
          if counters.reset:
            self.counter[cam_name].reset_counts()
            self.counter[cam_name].reset = False
        database.run_put("counters", cam_name, self.counter[cam_name])
        
        alerts = database.run_get("alerts", cam_name)
        for id,a in alerts.items():
          if not a.new: continue
          a.new = False
          database.run_put("alerts", cam_name, a, id=id)
          if a is None:
            del self.alert_counters[cam_name][id]
            continue
          self.alert_counters[cam_name][id] = a
          for c in a.classes: classes.add(str(c))

        self.alert_counters[cam_name] = {i:a for i,a in self.alert_counters[cam_name].items() if i in alerts}
        
        new_settings = database.run_get("settings", cam_name)
        if self.settings[cam_name] is not None and new_settings != self.settings[cam_name] and is_vod(cam_name):
          self.reset_vod(cam_name)
          if "reset" in new_settings: del new_settings["reset"]
        self.settings[cam_name] = new_settings
          
      if global_settings.clearcam_user() and not self.vod[cam_name] and cam_name in self.live_link and (link:=self.live_link[cam_name]) and (time.time() - self.last_live_seg[cam_name]) >= 4:
        self.last_live_seg[cam_name] = time.time()
        threading.Thread(target=self.upload_live_segment, args=(link, cam_name,), daemon=True).start()
    else: self.count[cam_name]+=1

  def upload_live_segment(self, link, cam_name):
    self.last_live_seg[cam_name] = time.time()
    mp4_filename = f"segment.mp4"
    export_clip(self.current_stream_dir_raw[cam_name], Path(mp4_filename), live=True)
    encrypt_file(Path(mp4_filename), Path(f"""{mp4_filename}.aes"""), global_settings.key)
    Path(mp4_filename).unlink()
    upload_to_r2(file_path=Path(f"""{mp4_filename}.aes"""), signed_url=link)

  def check_upload_link(self, cam_name="camera"):
      query_params = urllib.parse.urlencode({
          "name": quote(cam_name),
          "session_token": global_settings.userID
      })
      url = f"https://clearcam.org/get_stream_upload_link?{query_params}"
      
      req = urllib.request.Request(url)
      with urllib.request.urlopen(req) as response:
          if response.status == 200:
              response_data = json.loads(response.read().decode('utf-8'))
              upload_link = response_data.get("upload_link")
              alerts_on_res = response_data.get("alerts_on")
              with self.live_link_lock[cam_name]: self.live_link[cam_name] = upload_link
              alerts_on[cam_name] = (alerts_on_res == 1)
          else:
              if cam_name in self.live_link: self.live_link[cam_name] = None

  def reset_vod(self, cam_name):
    self.cap[cam_name] = cv2.VideoCapture(self.src[cam_name]) # reset video on settings change
    shutil.rmtree(BASE_DIR / "cameras" / cam_name / "objects", ignore_errors=True)
    shutil.rmtree(BASE_DIR / "cameras" / cam_name / "faces", ignore_errors=True)
    shutil.rmtree(BASE_DIR / "cameras" / cam_name / "event_images", ignore_errors=True)

  def run_inference(self, frame, cam_name):
    global model
    frame = Tensor(frame)
    preds = jit_infer(model, frame, yolo_jit_cache).numpy()
    thresh = (self.settings[cam_name].get("threshold") if self.settings[cam_name] else 0.5) or 0.5 #todo clean!
    online_targets = self.tracker[cam_name].update(preds, thresh)
    online_targets = [p for p in online_targets if (classes is None or str(int(p.class_id)) in classes)]
    preds = []
    for x in online_targets:
      if x.tracklet_len < 1: continue # dont alert for 1 frame, too many false positives.  
      # add to objects, regarless of speed
      if x.track_id not in self.pred_occs[cam_name]: self.pred_occs[cam_name][x.track_id] = [time.time()]
      if (len(self.pred_occs[cam_name][x.track_id]) < 20 and (time.time() - self.pred_occs[cam_name][x.track_id][-1]) > 1) or (time.time() - self.pred_occs[cam_name][x.track_id][-1]) > 10:
        self. pred_occs[cam_name][x.track_id].append(time.time())
        ts = round((self.cap[cam_name].get(cv2.CAP_PROP_POS_FRAMES) / self.src_fps[cam_name]) - 5,1) if self.vod[cam_name] else round((time.time() - self.start_time[cam_name] - 5),1)
        self.save_object(x, ts, cam_name=cam_name)

      if x.speed < 2.5: continue #min speed, don't detect still objects, they jitter too. # TODO what's the best min value?
      outside = False
      if hasattr(self, "settings") and self.settings[cam_name] is not None and self.settings[cam_name].get("coords"):
        scaled_coors = np.array(self.settings[cam_name]["coords"])
        scaled_coors[:,] *= [frame.shape[1], frame.shape[0]] # decimal to full
        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])]], scaled_coors)
        outside = outside ^ self.settings[cam_name]["outside"]
      non_zone_alert = False
      if outside: # check if any alerts don't use zone
        for _, alert in self.alert_counters[cam_name].items():
          if not alert.zone:
            non_zone_alert = True
            break
        if not non_zone_alert and 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,x.track_id]))
      if (classes is None or str(int(x.class_id)) in classes):
        new = int(x.track_id) not in self.object_set[cam_name]
        new_in_zone = int(x.track_id) not in self.object_set_zone[cam_name] and not outside
        if new:
          self.object_set[cam_name].add(int(x.track_id))
          self.counter[cam_name].add(int(x.class_id))
        if new_in_zone: self.object_set_zone[cam_name].add(int(x.track_id))
        for _, alert in self.alert_counters[cam_name].items():
          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)
    return preds, frame

def is_bright_color(color):
  r, g, b = color
  brightness = (r * 299 + g * 587 + b * 114) / 1000
  return brightness > 127

def draw_predictions(frame, preds, color_dict):
  for x1, y1, x2, y2, conf, cls, _ in preds:
    x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
    label = f"{class_labels[int(cls)]}:{conf:.2f}"
    color = color_dict[class_labels[int(cls)]]
    frame = draw_rectangle_numpy(frame, (x1, y1), (x2, y2), color, 3)
    (text_width, text_height), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
    font_color = (0, 0, 0) if is_bright_color(color) else (255, 255, 255)
    frame = draw_rectangle_numpy(frame, (x1, y1 - text_height - 10), (x1 + text_width + 2, y1), color, -1)
    cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, font_color, 1, cv2.LINE_AA)
  return frame

def point_not_in_polygon(coords, poly):
    n = len(poly)
    for j in range(len(coords)):
      inside = False
      p1x, p1y = poly[0]
      for i in range(1, n + 1):
        p2x, p2y = poly[i % n]
        if coords[j][1] > min(p1y, p2y):
          if coords[j][1] <= max(p1y, p2y):
            if coords[j][0] <= max(p1x, p2x):
              if p1y != p2y:
                x_intersect = (coords[j][1] - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
              else:
                x_intersect = p1x
              if p1x == p2x or coords[j][0] <= x_intersect: inside = not inside
        p1x, p1y = p2x, p2y
      if inside:
        return False
    return True

def run_encode_text(clip, text): return clip.model._encode_text(text, realize=True)

def run_search(clip, image_text, top_k, cam_name, selected_dir): return clip.search(image_text, top_k, cam_name, selected_dir)

def run_clip(clip, im, top_k, cam_name, selected_dir, is_face):
  im = clip.preprocess_face(im) if is_face else clip.preprocess_clip(im)
  if im is not None:
    embedding = clip.adaface(Tensor(im, dtype=dtypes.float32)).numpy() if is_face else jit_infer(clip.model.precompute_embedding, Tensor(im, dtype=dtypes.float32), jit_cache=jit_cache).numpy()
    res = clip.search(None, top_k, cam_name, selected_dir, embedding, is_face)
  else:
    res = []
  return res

class HLSRequestHandler(BaseHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def log_message(self, format, *args): pass # don't print stuff

    def send_results(self, results, start=0, count=100):
      image_data = []
      for path_str, score in results:
        if score < 0.21: break
        img_path = Path(path_str).resolve()
        ts = event_img_info(img_path.stem)["ts"]
        parts = img_path.parts
        cam_index = parts.index("cameras") + 1
        cam = parts[cam_index]
        rel = img_path.relative_to(BASE_DIR / "cameras")
        image_url = f"/{rel}"
        image_data.append({
          "url": image_url,
          "timestamp": ts,
          "filename": img_path.name,
          "cam_name": cam,
          "folder": img_path.parts[-2],
          "score": score,
        })
      image_data = image_data[start:start+count]
      response_data = {
        "images": image_data,
        "count": len(image_data),
      }
      self.send_200(response_data)

    def send_200(self, body=None):
      self.send_response(200)
      self.send_header("Content-Type", "application/json")
      self.end_headers()
      self.wfile.write(json.dumps(body).encode("utf-8"))

    def get_camera_path(self, cam_name=None):
        if cam_name:
            return BASE_DIR / "cameras" / cam_name / "streams"
        return BASE_DIR / "cameras"
    
    def do_GET(self):
        parsed_path = urlparse(unquote(self.path))
        query = parse_qs(parsed_path.query)
        cam_name = query.get("cam", [None])[0]

        if parsed_path.path == "/export":
          date = query.get("date", [None])[0]
          stream_dir = (Path("data") / "cameras" / cam_name / "streams" / date)
          output_path = stream_dir / "_export.mp4"
          m4s_files = sorted(stream_dir.glob("stream_*.m4s"))
          ffmpeg_path = find_ffmpeg()
          command = [
            ffmpeg_path,
            "-y",
            "-loglevel", "error",
            "-i", "pipe:0",
            "-c", "copy",
            "-movflags", "+faststart",
            str(output_path),
          ]
          proc = subprocess.Popen(command, stdin=subprocess.PIPE)
          try:
            with (stream_dir / "init.mp4").open("rb") as f: shutil.copyfileobj(f, proc.stdin)
            for m4s in m4s_files:
              with m4s.open("rb") as f: shutil.copyfileobj(f, proc.stdin)
          finally:
            proc.stdin.close()
          if proc.wait() != 0: output_path.unlink(missing_ok=True)
          self.send_response(200)
          self.send_header("Content-Type", "video/mp4")
          self.send_header("Content-Disposition", f'attachment; filename="{cam_name}_{date}.mp4"',)
          self.send_header("Content-Length", str(output_path.stat().st_size))
          self.end_headers()
          with output_path.open("rb") as f: shutil.copyfileobj(f, self.wfile)
          output_path.unlink()

        if parsed_path.path == "/set_max_storage":
          max_gb = float(query.get("max", [None])[0])
          self.server.max_gb = max_gb
          database.run_put("max_storage", "all", max_gb)
          self.send_200()
          return
        
        if parsed_path.path == "/get_global_settings":
          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})
          return

        if parsed_path.path == "/list_cameras":
          cams = database.run_get("links", None)
          progs = database.run_get("analysis_prog", None)
          cam_progress = {cam_name: progs.get(cam_name, None) for cam_name in cams}
          self.send_200(cam_progress)
          return

        if parsed_path.path == "/list_days":          
          base_path = "data/cameras"
          days = set()
          date_pattern = re.compile(r'^\d{4}-\d{2}-\d{2}$')
          if os.path.exists(base_path):
            for cam_name in os.listdir(base_path):
              cam_path = os.path.join(base_path, cam_name, "streams")    
              if os.path.exists(cam_path):
                for date_folder in os.listdir(cam_path):
                  date_folder_path = os.path.join(cam_path, date_folder)
                  if os.path.isdir(date_folder_path) and date_pattern.match(date_folder): days.add(date_folder)
          days_list = sorted(list(days), reverse=True, key=lambda x: datetime.strptime(x, "%Y-%m-%d"))
          self.send_200(days_list)
          return

        if parsed_path.path == '/add_camera':
            cam_name = query.get("cam_name", [None])[0]
            src = query.get("src", [None])[0]
            
            if not cam_name or not src:
                self.send_error(400, "Missing cam_name or src")
                return
            
            database.run_put("links", cam_name, src)
            self.send_response(302)
            self.send_header('Location', '/')
            self.end_headers()
            return
        
        if parsed_path.path == "/edit_settings":
            if not cam_name:
                self.send_error(400, "Missing cam or id")
                return
            zone = database.run_get("settings", cam_name)
            if zone is None: zone = {}
            coords_json = query.get("coords", [None])[0]
            if coords_json is not None:
              coords = json.loads(coords_json)
              if isinstance(coords, list):
                if len(coords) >= 3:
                  zone["coords"] = [[float(x), float(y)] for x, y in coords]
                else:
                  if "coords" in zone: del zone["coords"]
            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])}) #need the val  
            database.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[0])

            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(b'{"status":"ok"}')
            return

        if parsed_path.path == "/edit_alert":
            if not cam_name:
                self.send_error(400, "Missing cam or id")
                return

            raw_alerts = database.run_get("alerts", cam_name)
            alert = None
            alert_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]
            desc = query.get("desc", [None])[0]
            threshold = query.get("threshold", [None])[0]
            if threshold is not None: threshold = float(threshold) / 100
            if alert_id is None: # no id, add alert
                window = 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
                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())
                alert = RollingClassCounter(
                        window_seconds=window,
                        max=max_count,
                        classes=classes,
                        sched=sched,
                        cam_name=cam_name,
                        desc=desc,
                        threshold=threshold
                    )
                raw_alerts[alert_id] = alert
            else:
              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"
                if desc is not None: raw_alerts[alert_id].desc = desc
                if threshold is not None: raw_alerts[alert_id].threshold = threshold
                alert = raw_alerts[alert_id]
                alert.new = True
              else:
                del raw_alerts[alert_id]
            if alert is not None:
              database.run_put("alerts", cam_name, alert, alert_id)
            else:
              database.run_delete("alerts", cam_name, alert_id)
            
            # make vod reset
            settings = database.run_get("settings", cam_name)
            if settings is None: settings = {}
            settings["reset"] = True
            database.run_put("settings", cam_name, settings)

 
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(b'{"status":"ok"}')
            return

        if parsed_path.path == "/get_settings":
            zone = database.run_get("settings",cam_name)
            if zone is not None:
              if cam_name in zone and "settings" in zone[cam_name]: zone = zone[cam_name]["settings"]
            else:
              zone = {}
            
            self.send_200(zone)
            return

        if parsed_path.path == "/get_alerts":
            if not cam_name:
                self.send_error(400, "Missing cam parameter")
                return

            raw_alerts = database.run_get("alerts",cam_name)
            alert_info = []
            for key,alert in raw_alerts.items():
                sched = alert.sched if alert.sched else [[0,86399],True,True,True,True,True,True,True]
                alert_info.append({
                    "window": alert.window,
                    "max": alert.max,
                    "classes": list(alert.classes),
                    "id": str(key),
                    "sched": sched,
                    "is_on": alert.is_on,
                    "is_notif": alert.is_notif,
                    "zone": alert.zone,
                    "desc": alert.desc,
                    "threshold": alert.threshold,
                })
            self.send_200(alert_info)
            return

        if parsed_path.path == '/delete_camera':
            cam_name = query.get("cam_name", [None])[0]
            if not cam_name:
                self.send_error(400, "Missing cam_name parameter")
                return
            
            try:
              shutil.rmtree(BASE_DIR / "cameras" / cam_name, ignore_errors=True)
              if os.path.isfile(database.run_get("links", None)[cam_name]): os.remove(database.run_get("links", None)[cam_name])
              # todo clean
              alerts = database.run_get("alerts", cam_name)
              for id, _ in alerts.items():
                database.run_delete("alerts", cam_name, id=id)
              database.run_delete("links", cam_name)
              database.run_delete("analysis_prog", cam_name)
              database.run_delete("settings", cam_name)
              database.run_delete("counters", cam_name)
            except Exception as e:
              self.send_error(500, f"Error deleting camera: {e}")
              return

            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(b'{"status":"deleted"}')
            return

        if parsed_path.path == "/get_counts": # todo error fetching counts on first
            if not cam_name:
                self.send_error(400, "Missing cam parameter")
                return
            
            counter = database.run_get("counters", cam_name)
            if counter:
              labeled_counts = {
                class_labels[int(k)]: len(v)
                for k, v in counter.data.items()
                if int(k) < len(class_labels)
              }
              self.send_200(labeled_counts)
              return
            else:
              database.run_put("counters", cam_name, RollingClassCounter(cam_name=cam_name))
              self.send_200([])
      

        if parsed_path.path == "/reset_counts":
          if not cam_name:
            self.send_error(400, "Missing cam parameter")
            return
          counter = database.run_get("counters",cam_name)
          if counter: counter.reset_counts()
          database.run_put("counters", cam_name, counter)
          self.send_response(200)
          self.send_header("Content-Type", "application/json")
          self.end_headers()
          self.wfile.write(b"{}")
          return


        if parsed_path.path == '/' and "cam" not in query:
          with open("mainview.html", "r", encoding="utf-8") as f: html = f.read()
          self.send_response(200)
          self.send_header('Content-type', 'text/html')
          self.end_headers()
          self.wfile.write(html.encode('utf-8'))
          return
                            
        if parsed_path.path == '/' or parsed_path.path == f'/{cam_name}':
            selected_dir = parse_qs(parsed_path.query).get("folder", [datetime.now().strftime("%Y-%m-%d")])[0]
            start_param = parse_qs(parsed_path.query).get("start", [None])[0]
            try:
                start_time = max(float(start_param),0) if start_param is not None else None
            except ValueError:
                start_time = None

            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            with open('cameraview.html', 'r', encoding='utf-8') as f: html = f.read()
            replacements = {
                '{selected_dir}': selected_dir,
                '{class_labels}': json.dumps(class_labels),
                '{start_time}': str(start_time) if start_time is not None else 'null',
                '{cam_name}': cam_name
            }
            for placeholder, value in replacements.items(): html = html.replace(placeholder, value)
            self.wfile.write(html.encode("utf-8"))
            return
        
        requested_path = parsed_path.path.lstrip('/')
        if requested_path.startswith("cameras/"):
            requested_path = requested_path[len("cameras/"):]

        try: # todo
          cam_name = requested_path[:requested_path.index("/")]
          vod = is_vod(cam_name)
          if vod and "preview.png" not in requested_path: requested_path = requested_path.rsplit("/", 2)[0] + "/video/" + requested_path.rsplit("/", 1)[1]
        except Exception:
          pass

        file_path = BASE_DIR / "cameras" / requested_path
        try: file_path = ((BASE_DIR / "cameras").resolve() / requested_path).resolve()
        except (OSError, RuntimeError):
          self.send_error(404)
          return
        try: _ = file_path.relative_to((BASE_DIR / "cameras").resolve())
        except:
          self.send_error(404)
          return

        if not file_path.exists():
            self.send_error(404)
            return

        self.send_response(200)
        if file_path.suffix == '.m3u8':
            self.send_header('Content-Type', 'application/vnd.apple.mpegurl')
            self.send_header('Cache-Control', 'no-cache')
        elif file_path.suffix == '.ts':
            self.send_header('Content-Type', 'video/MP2T')
        elif file_path.suffix == '.m4s':
            self.send_header('Content-Type', 'video/iso.segment')
        elif file_path.suffix == '.png':
            self.send_header('Content-Type', 'image/jpeg')
        self.end_headers()

        with open(file_path, 'rb') as f:
            shutil.copyfileobj(f, self.wfile)

    def do_POST(self):
        parsed_path = urlparse(self.path)

        if self.path.startswith("/edit_settings"):
          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([])
          return

        if self.path.startswith("/analyse-footage"):
          params = parse_qs(parsed_path.query)
          filename = params.get("filename", [None])[0]
          chunk = int(params.get("chunk", [0])[0])
          total = int(params.get("total", [1])[0])
          if not filename:
            self.send_error(400, "Missing filename")
            return
          filename = os.path.basename(filename)
          upload_dir = BASE_DIR / "cameras"
          upload_dir.mkdir(exist_ok=True)
          length = int(self.headers.get("Content-Length", 0))
          if length <= 0:
            self.send_error(411, "Content-Length required")
            return
          final_path = upload_dir / filename
          temp_path = upload_dir / f"{filename}.part"
          with open(temp_path, "ab") as f:
            remaining = length
            while remaining > 0:
              data = self.rfile.read(min(1024 * 1024, remaining))
              if not data: break
              f.write(data)
              remaining -= len(data)
          if chunk == total - 1: temp_path.rename(final_path)
          self.send_200([])

        if parsed_path.path == "/event_thumbs":
            content_length = int(self.headers.get("Content-Length", 0))
            raw_body = self.rfile.read(content_length)

            try:
                data = json.loads(raw_body)
            except json.JSONDecodeError:
                self.send_error(400, "Invalid JSON")
                return

            cam_name     = data.get("cam")
            selected_dir = data.get("folder")
            name_contains = data.get("name_contains")
            image_text   = data.get("image_text")
            similar_img  = data.get("similar_img")
            start = data.get("start")
            count = data.get("count")
            is_face = data.get("is_face") or False
            if is_face and not global_settings.use_face: return
            if start is None: start, count = 0, 100
            uploaded_image = data.get("uploaded_image")
            if uploaded_image:
              if ',' in uploaded_image: uploaded_image = uploaded_image.split(',')[1]
              uploaded_image = base64.b64decode(uploaded_image)

            if cam_name:
              camera_dirs = [BASE_DIR / "cameras" / cam_name]
            else:
              camera_dirs = [d for d in (BASE_DIR / "cameras").iterdir() if d.is_dir()]

            if selected_dir:
              selected_dirs = [selected_dir]
            else:
              selected_dirs = list({
                subdir.name 
                for camera_dir in camera_dirs
                if (camera_dir / "streams").is_dir()
                for subdir in (camera_dir / "streams").iterdir() 
                if subdir.is_dir()
              })
            selected_dirs.append("video")

            if image_text and global_settings.use_clip: add_to_queue(object_finder._load_all_embeddings)
            if (uploaded_image or similar_img) and (global_settings.use_clip or global_settings.use_face): add_to_queue(object_finder._load_all_embeddings, is_face)
            
            if uploaded_image and (global_settings.use_clip or is_face):
              results = add_to_queue(run_clip, object_finder, uploaded_image, start+count, cam_name, selected_dir, is_face)
              self.send_results(results, start, count)
              return
            
            if similar_img and (global_settings.use_clip or is_face):
              results = add_to_queue(run_clip, object_finder, similar_img, start+count, cam_name, selected_dir, is_face) # todo one with above
              self.send_results(results, start, count)
              return

            if image_text and global_settings.use_clip:
              results = add_to_queue(run_search, object_finder, image_text, start+count, cam_name, selected_dir)
              self.send_results(results, start, count)
              return

            image_data = []
            for camera_dir in camera_dirs:
              for selected_dir in selected_dirs:
                event_image_path = camera_dir / "event_images" / selected_dir
                if not event_image_path.exists(): continue
                event_images = sorted(
                  event_image_path.glob("*.jpg"),
                  key=lambda p: int(p.stem.split('_')[0]),
                  reverse=True
                )
                for img in event_images:
                  if name_contains and name_contains not in img.name: continue
                  ts = int(img.stem.split('_')[0])
                  image_url = f"/{img.relative_to(BASE_DIR)}"
                  image_data.append({
                    "url": image_url,
                    "timestamp": ts,
                    "filename": img.name,
                    "cam_name": camera_dir.name,
                    "folder": selected_dir,
                  })

            image_data.sort(key=image_sort_key, reverse=True)
            if start is not None and count is not None: image_data = image_data[start:start+count]

            response_data = {
              "images": image_data,
              "count": len(image_data),
            }

            self.send_response(200)
            self.send_header('Content-type', 'application/json')
            self.end_headers()
            self.wfile.write(json.dumps(response_data).encode('utf-8'))
            return

def image_sort_key(item):
  try: return datetime.strptime(item["folder"], "%Y-%m-%d").timestamp() + item["timestamp"]
  except ValueError: return -1

def schedule_daily_restart(cam, restart_time):
    while True:
      now = datetime.now().time()
      target = time_obj(restart_time[0], restart_time[1])
      if now >= target:
        delta = (24 * 3600) - ((now.hour * 3600 + now.minute * 60 + now.second) - (target.hour * 3600 + target.minute * 60))
      else:
        delta = ((target.hour * 3600 + target.minute * 60) - 
          (now.hour * 3600 + now.minute * 60 + now.second))
      time.sleep(delta)
      cams = database.run_get("links", None)
      for cam_name in cams.keys():
        cam.start_time[cam_name] = None
        cam.hls_proc[cam_name], cam.proc[cam_name] = cam._open_ffmpeg(cam_name)
        cam.current_stream_dir_raw[cam_name] = cam._get_new_stream_dir(cam_name)



def get_lan_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
    except Exception:
        ip = '127.0.0.1'
    finally:
        s.close()
    return ip

def get_executable_args(): return ([sys.argv[0]], sys.argv[1:]) if getattr(sys, "frozen", False) else ([sys.executable, sys.argv[0]], sys.argv[1:])

def event_img_info(image): return {"ts": int(float(image.split('_')[0])), "object_id":int(image.split('_')[1]), "class_id":int(image.split('_')[2])}

def upload_to_r2(file_path: Path, signed_url: str, max_retries: int = 0) -> bool:
    try:
      url_parts = urllib.parse.urlparse(signed_url)
      if url_parts.scheme == 'https':
        conn = http.client.HTTPSConnection(url_parts.netloc)
      else:
        conn = http.client.HTTPConnection(url_parts.netloc)
      
      with file_path.open('rb') as f:
        file_size = file_path.stat().st_size
        headers = {'Content-Type': 'application/octet-stream', "Content-Length": str(file_size)}
        conn.request("PUT", url_parts.path + "?" + url_parts.query, body=f, headers=headers)
        response = conn.getresponse()
        if 200 <= response.status < 300: return True
        return False
    except Exception as e:
      print(f"Error uploading to R2: {e}")
      return False

import queue
task_queue = queue.Queue()
def add_to_queue(fn, *args):
  result_queue = queue.Queue(maxsize=1)
  task_queue.put((fn, args, result_queue))
  return result_queue.get()

def process_queue():
  try:
    fn, args, result_queue = task_queue.get_nowait()
  except queue.Empty: return
  result = fn(*args)
  result_queue.put(result)

def process_latest_face(img):
  if global_settings.use_face and str(object_queue[0]).endswith("_0.jpg"):
    face_img = object_finder.img_to_face(img)
    
    if face_img is not None:
      cv2.imwrite(str(object_queue[0]).replace("/objects/", "/faces/"), face_img)
      date = object_queue[0].parent.name
      pkl_path = object_queue[0].parent.parent.parent / "faces" / date /  "embeddings.pkl"
      face_emb = object_finder.adaface(Tensor(face_img).contiguous()).numpy()
      data = pickle.load(open(pkl_path, "rb")) if pkl_path.exists() else {}
      if "embeddings" not in data: data["embeddings"] = {}
      data["embeddings"][str(object_queue[0])] = face_emb
      pkl_path.parent.mkdir(parents=True, exist_ok=True)
      pickle.dump(data, open(pkl_path, "wb"))  

def set_settings(x): # todo, save to db, do logic in GlobalSettings class, sanitize inputs so yolo doesn't crash
  global global_settings
  global model
  global yolo_res
  global yolo_jit_cache
  global qwen
  global qwen_prompt
  if x.use_clip:
    object_finder.init_clip()
  else:
    object_finder.turn_off_clip()

  if x.use_face:
    object_finder.init_face()
  else:
    object_finder.turn_off_face()

  if x.model_size != global_settings.model_size or x.model_res != global_settings.model_res:
    yolo_jit_cache = {}
    model = YOLOv9(x.model_size, x.model_res)

  if x.key == None: x.userID = None

  # todo change size mid run doesn't work
  if (x.use_qwen and qwen is None) or (x.use_qwen and global_settings.qwen_size != x.qwen_size):
    qwen = None
    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")
  global_settings = x

def clip_latest_img(img):
  if global_settings.use_clip:
    img = object_finder.preprocess(img)
    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
    with open(object_queue[0].parent / 'embeddings.pkl', "wb") as f: pickle.dump(data, f)
  
    if global_settings.use_notifs():
      cam_name = object_queue[0].parts[object_queue[0].parts.index("cameras")+1:object_queue[0].parts.index("objects")][0]
      alerts = database.run_get("alerts", cam_name) # todo, get cam_name from file path!
      for k, v in alerts.items():
        if time.time() - v.last_det < 60 or not v.is_active(): continue
        if v.desc is None: continue
        if not hasattr(v, "desc_emb") or v.desc_emb is None:
          v.desc_emb = run_encode_text(object_finder, v.desc)
          database.run_put("alerts", cam_name, v, id=k)

        similarity = (v.desc_emb @ emb.T).item()
        print("sim =",similarity,v.desc,object_queue[0])
        if similarity > v.threshold:
          send_notif(session_token=global_settings.userID, text=f"Event Detected ({cam_name}: {v.desc})", body_text=None, host=global_settings.server_url)
          alerts[k].last_det = time.time()
          database.run_put("alerts", cam_name, alerts[k], k)
          if global_settings.clearcam_user():
            seen_time = event_img_info(str(object_queue[0]).split("/")[-1].split(".jpg")[0])["ts"]
            threading.Thread(target=export_and_upload, kwargs={"cam_name": cam_name, "thumbnail": object_queue[0], "userID": global_settings.userID, "key": global_settings.key, "start": seen_time, "length": 20}, daemon=True).start()
          break

cams = dict()
active_subprocesses = []
import socket
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    def __init__(self, server_address, RequestHandlerClass):
      ThreadingMixIn.__init__(self)
      HTTPServer.__init__(self, server_address, RequestHandlerClass)
      self.cleanup_stop_event = threading.Event()
      self.cleanup_thread = None
      max_gb = database.run_get("max_storage", None)
      if max_gb == {}:
        database.run_put("max_storage", "all", 256)
        max_gb = database.run_get("max_storage", None)
      self.max_gb = max_gb["all"]
      self.object_finder_stop_event = threading.Event()
      self.object_finder_thread = None
      self._setup_cleanup_and_clip_thread()

    def _setup_cleanup_and_clip_thread(self):
      if self.cleanup_thread is None or not self.cleanup_thread.is_alive():
        self.cleanup_stop_event.clear()
        self.cleanup_thread = threading.Thread(target=self._cleanup_task, daemon=True, name="StorageCleanup")
        self.cleanup_thread.start()

    def _cleanup_task(self):
      while not self.cleanup_stop_event.is_set():
          try:
              self._check_and_cleanup_storage()
          except Exception as e:
              print(f"Cleanup error: {e}")
          self.cleanup_stop_event.wait(timeout=600)

    def _check_and_cleanup_storage(self):
      total_size = sum(f.stat().st_size for f in (BASE_DIR / "cameras").glob('**/*') if f.is_file())
      size_gb = total_size / (1000 ** 3)
      free_gb = shutil.disk_usage(BASE_DIR / "cameras").free / (1000 ** 3)
      if size_gb > self.max_gb or free_gb < 5: self._cleanup_oldest_files() # todo unhardcode

    def _cleanup_oldest_files(self):
      camera_dirs = []
      for cam_dir in (BASE_DIR / "cameras").iterdir():
        if cam_dir.is_dir():
          dir_size = sum(f.stat().st_size for f in cam_dir.glob('**/*') if f.is_file())
          camera_dirs.append((cam_dir, dir_size))
      
      if not camera_dirs: return
          
      largest_cam_raw = max(camera_dirs, key=lambda x: x[1])[0]
      for largest_cam in [largest_cam_raw]:
        streams_dir = largest_cam / "streams"
        if not streams_dir.exists():
          shutil.rmtree(largest_cam)
          continue
            
        recordings = []
        for rec_dir in streams_dir.iterdir():
          if rec_dir.is_dir(): recordings.append((rec_dir, rec_dir.stat().st_ctime))
        if not recordings:
          shutil.rmtree(largest_cam)
          print(f"Deleted camera folder with empty streams: {largest_cam}")
          continue
        recordings.sort(key=lambda x: x[1])
        oldest_recording = recordings[0][0]
        shutil.rmtree(oldest_recording)
        event_images_dir = largest_cam.with_name(largest_cam.name) / Path("event_images") / Path(oldest_recording.name)
        object_images_dir = largest_cam.with_name(largest_cam.name) / Path("objects") / Path(oldest_recording.name)
        face_images_dir = largest_cam.with_name(largest_cam.name) / Path("faces") / Path(oldest_recording.name)
        #dets_dir = largest_cam.with_name(largest_cam.name) / Path("dets") / Path(oldest_recording.name)
        if event_images_dir.exists(): shutil.rmtree(event_images_dir)
        if object_images_dir.exists(): shutil.rmtree(object_images_dir)
        if face_images_dir.exists(): shutil.rmtree(face_images_dir)
        #if dets_dir.exists(): shutil.rmtree(dets_dir)
        print(f"Deleted oldest recording: {oldest_recording}")

class GlobalSettings:
  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,
               qwen_prompt="What has been detected on my CCTV camera? Write in one short sentence", server_url="https://clearcam.org"):
    self.use_clip = use_clip
    self.use_face = use_face
    self.model_size = model_size
    self.model_res = model_res
    self.userID = userID
    self.key= key
    self.use_qwen = use_qwen
    self.qwen_size = qwen_size
    self.qwen_prompt = qwen_prompt
    self.server_url = server_url

  # add any new properties here
  def __setstate__(self, state):
    self.__dict__.update(state)
    if not hasattr(self, "qwen_prompt"): self.qwen_prompt = "What has been detected on my CCTV camera? Write in one short sentence"
    if not hasattr(self, "server_url"): self.server_url = "https://clearcam.org"

  def use_notifs(self): return self.userID or self.server_url != "https://clearcam.org"
  def clearcam_user(self): return self.userID is not None

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,
        qwen_prompt=settings.qwen_prompt,
        server_url=settings.server_url
    )

if __name__ == "__main__":
  jit_cache = {}
  yolo_jit_cache = {}
  alerts_on = {}
  qwen = None
  multiprocessing.set_start_method("spawn", force=True)
  database = db()
  cams = database.run_get("links", None)
  classes = {"0","1","2","7"} # person, bike, car, truck, bird (14)

  
  from models.objects import ObjectFinder

  object_queue = []
  cam_name = next((arg.split("=", 1)[1] for arg in sys.argv[1:] if arg.startswith("--cam_name=")), "my_camera")
  
  class_labels = fetch('https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names').read_text().split("\n")
  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

  global_settings = database.run_get("global_settings", "all")
  if global_settings == {}: # todo, use None?
    global_settings = GlobalSettings()
    database.run_put("global_settings", "all", global_settings)

  model = YOLOv9(global_settings.model_size, res=int(global_settings.model_res))
  object_finder = ObjectFinder()
  cam = VideoCapture()

  if global_settings.use_clip: object_finder.init_clip()
  if global_settings.use_face: object_finder.init_face()

  if global_settings.use_qwen:
    qwen = Qwen3VL(size=f"{global_settings.qwen_size}B", res=(544, 960)) # h, w. they need to be multiples of 32
    print("prewarming Qwen")
    qwen.prewarm()
    print("DONE")


  try:
    server = ThreadedHTTPServer(('0.0.0.0', 8080), RequestHandlerClass=HLSRequestHandler)
    threading.Thread(target=server.serve_forever, daemon=True).start()
    print(f"Serving at http://{get_lan_ip()}:8080")
  except OSError as e:
    if e.errno == socket.errno.EADDRINUSE:
      print("Port in use, server not started.")
      server = None
    else:
        raise
    
  restart_time = (0, 0)
  threading.Thread(
    target=schedule_daily_restart,
    args=(cam, restart_time),
    daemon=True
  ).start()
  cam.start()