Add object detection, tracking, mobile notifications, and search to any security camera.
Imsearch2 (#102)
Rory Clear8e8b9f5008b2
diff --git a/clearcam.py b/clearcam.pyindex d64d4ee..189d510 100644--- a/clearcam.py+++ b/clearcam.py@@ -573,6 +573,32 @@ class VideoCapture:]self.proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)+ def save_object(self, p):+ timestamp = datetime.now().strftime("%Y-%m-%d")+ filepath = CAMERA_BASE_DIR / f"{self.cam_name}/objects/{timestamp}"+ filepath.mkdir(parents=True, exist_ok=True)+ ts = int(time.time() - self.streamer.start_time - 10)+ object_filename = filepath / f"{ts}_{int(p[6])}.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_frame.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))+ crop = self.last_frame[y1_new:y2_new, x1_new:x2_new]+ cv2.imwrite(str(object_filename), crop)++def capture_loop(self):frame_size = self.width * self.height * 3fail_count = 0@@ -582,6 +608,7 @@ class VideoCapture:last_live_seg = time.time()last_preview_time = Nonelast_counter_update = time.time()+ pred_occs = {}count = 0while self.running:if not (CAMERA_BASE_DIR / self.cam_name).is_dir(): os._exit(1) # deleted cam@@ -599,6 +626,14 @@ class VideoCapture:fail_count = 0frame = np.frombuffer(raw_bytes, np.uint8).reshape((self.height, self.width, 3))filtered_preds = [p for p in self.last_preds if (classes is None or str(int(p[5])) in classes)]+ for p in filtered_preds:+ if (p[2]-p[0]) < 100 or (p[3]-p[1]) < 100: continue # todo, best min size+ if p[6] not in pred_occs: pred_occs[p[6]] = [time.time()]+ if (len(pred_occs[p[6]]) < 20 and (time.time() - pred_occs[p[6]][-1]) > 1) or (time.time() - pred_occs[p[6]][-1]) > 600:+ pred_occs[p[6]].append(time.time())+ else:+ continue+ self.save_object(p)if count > 10:if last_preview_time is None or time.time() - last_preview_time >= 3600: # preview every hour@@ -1011,6 +1046,7 @@ class HLSRequestHandler(BaseHTTPRequestHandler):def __init__(self, *args, **kwargs):self.base_dir = CAMERA_BASE_DIRself.show_dets = None+ self.searcher = Nonesuper().__init__(*args, **kwargs)def send_200(self, body=None):@@ -1290,67 +1326,84 @@ class HLSRequestHandler(BaseHTTPRequestHandler):self.wfile.write(html.encode('utf-8'))return- if parsed_path.path == '/event_thumbs' or parsed_path.path.endswith('/event_thumbs'): # todo clean- selected_dir = parse_qs(parsed_path.query).get("folder", [datetime.now().strftime("%Y-%m-%d")])[0]- name_contains = parse_qs(parsed_path.query).get("name_contains", [None])[0]+ if parsed_path.path == '/event_thumbs' or parsed_path.path.endswith('/event_thumbs'):+ selected_dir = parse_qs(parsed_path.query).get("folder", [datetime.now().strftime("%Y-%m-%d")])[0]+ name_contains = parse_qs(parsed_path.query).get("name_contains", [None])[0]+ image_text = parse_qs(parsed_path.query).get("image_text", [None])[0]+ if cam_name:+ camera_dirs = [self.base_dir / cam_name]+ else:+ camera_dirs = [d for d in self.base_dir.iterdir() if d.is_dir()]+ if image_text and use_clip:+ if not self.searcher:+ self.searcher = CLIPSearch()+ self.searcher._load_all_embeddings()+ results = self.searcher.search(image_text, top_k=100, cam_name=cam_name, timestamp=selected_dir)image_data = []-- if cam_name is not None:- event_image_dir = self.base_dir / cam_name / "event_images"- event_image_path = event_image_dir / selected_dir- event_images = sorted(- event_image_path.glob("*.jpg"),- key=lambda p: int(p.stem.split('_')[0]), # n.jpg or n_{s}.jpg?- reverse=True- ) if event_image_path.exists() else []-- for img in event_images:- if name_contains and name_contains not in img.name: continue- ts = int(img.stem.split('_')[0]) # n.jpg or n_{s}.jpg?- image_url = f"/{img.relative_to(self.base_dir.parent)}"- image_data.append({- "url": image_url,- "timestamp": ts,- "filename": img.name,- "cam_name": cam_name,- "folder": selected_dir- })- else:- for camera_dir in self.base_dir.iterdir():- if camera_dir.is_dir():- event_image_dir = camera_dir / "event_images"- event_image_path = event_image_dir / selected_dir- if event_image_path.exists():- event_images = sorted(- event_image_path.glob("*.jpg"),- key=lambda p: int(p.stem.split('_')[0]), # n.jpg or n_{s}.jpg?- 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(self.base_dir.parent)}"- image_data.append({- "url": image_url,- "timestamp": ts,- "filename": img.name,- "cam_name": camera_dir.name,- "folder": selected_dir- })- image_data.sort(key=lambda x: x["timestamp"], reverse=True)+ for path_str, score in results:+ if score < 0.21: break+ img_path = (self.base_dir.parent.parent / Path(path_str)).resolve()+ ts = int(img_path.stem.split('_')[0])+ parts = img_path.parts+ cam_index = parts.index("cameras") + 1+ cam = parts[cam_index]+ rel = img_path.relative_to(self.base_dir)+ image_url = f"/{rel}"+ image_data.append({+ "url": image_url,+ "timestamp": ts,+ "filename": img_path.name,+ "cam_name": cam,+ "folder": selected_dir+ })response_data = {- "images": image_data,- "count": len(image_data),- "folder": selected_dir,- "cam_name": cam_name if cam_name is not None else "all_cameras"+ "images": image_data,+ "count": len(image_data),+ "folder": selected_dir,+ "cam_name": cam_name if cam_name else "all_cameras"}-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+ image_data = []+ for camera_dir in camera_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(self.base_dir.parent)}"+ image_data.append({+ "url": image_url,+ "timestamp": ts,+ "filename": img.name,+ "cam_name": camera_dir.name,+ "folder": selected_dir+ })++ image_data.sort(key=lambda x: x["timestamp"], reverse=True)++ response_data = {+ "images": image_data,+ "count": len(image_data),+ "folder": selected_dir,+ "cam_name": cam_name if cam_name else "all_cameras"+ }++ 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+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]@@ -1598,6 +1651,7 @@ def start_cam(rtsp, cam_name, yolo_variant='n'):new_args = upsert_arg(base_args, "cam_name", cam_name)new_args = upsert_arg(new_args, "rtsp", rtsp)new_args = upsert_arg(new_args, "yolo_size", yolo_variant)+ new_args = upsert_arg(new_args, "use_clip", use_clip)proc = subprocess.Popen(executable + new_args, close_fds=True)active_subprocesses.append(proc)@@ -1657,9 +1711,13 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):self.cleanup_stop_event = threading.Event()self.cleanup_thread = Noneself.max_gb = 40- self._setup_cleanup_thread()+ self.searcher = None+ self.clip_stop_event = threading.Event()+ self.clip_thread = None+ self._setup_cleanup_and_clip_thread()+- def _setup_cleanup_thread(self):+ 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(@@ -1669,13 +1727,33 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):)self.cleanup_thread.start()+ if use_clip and (self.clip_thread is None or not self.clip_thread.is_alive()):+ self.clip_stop_event.clear()+ self.clip_thread = threading.Thread(+ target=self._clip_task,+ daemon=True,+ name="CLIPMaintenance"+ )+ self.clip_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) # Check every 10 min+ self.cleanup_stop_event.wait(timeout=600)++ def _clip_task(self):+ if not self.searcher: self.searcher = CachedCLIPSearch()+ while not self.clip_stop_event.is_set():+ try:+ object_folders = self.searcher.find_object_folders("data/cameras")+ for folder in object_folders:+ self.searcher.precompute_embeddings(folder)+ except Exception as e:+ print(f"CLIP error: {e}")+ self.clip_stop_event.wait(timeout=60)def _check_and_cleanup_storage(self):total_size = sum(f.stat().st_size for f in CAMERA_BASE_DIR.glob('**/*') if f.is_file())@@ -1709,7 +1787,9 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):oldest_recording = recordings[0][0]shutil.rmtree(oldest_recording)event_images_dir = largest_cam.with_name(largest_cam.name.replace("_raw", "")) / Path("event_images") / Path(oldest_recording.name) # todo, remove _raw+ object_images_dir = largest_cam.with_name(largest_cam.name.replace("_raw", "")) / Path("objects") / Path(oldest_recording.name) # todo, remove _rawif event_images_dir.exists(): shutil.rmtree(event_images_dir)+ if object_images_dir.exists(): shutil.rmtree(object_images_dir)print(f"Deleted oldest recording: {oldest_recording}")def server_close(self):@@ -1717,6 +1797,11 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):self.cleanup_stop_event.set()if hasattr(self, 'cleanup_thread') and self.cleanup_thread:self.cleanup_thread.join(timeout=5)+ if hasattr(self, 'clip_stop_event'):+ self.clip_stop_event.set()+ if hasattr(self, 'clip_thread') and self.clip_thread:+ self.clip_thread.join(timeout=5)+super().server_close()if __name__ == "__main__":@@ -1736,15 +1821,24 @@ if __name__ == "__main__":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_clip = next((arg.split("=", 1)[1] for arg in sys.argv[1:] if arg.startswith("--use_clip=")), None)+ if use_clip: use_clip = use_clip != "False" # str to boolyolo_variant = next((arg.split("=", 1)[1] for arg in sys.argv[1:] if arg.startswith("--yolo_size=")), None)- if not yolo_variant: yolo_variant = input("Select YOLOV8 size from [n,s,m,l,x], or press enter to skip (defaults to n):") or "n"+ if not yolo_variant:+ yolo_variant = input("Select YOLOV8 size from [n,s,m,l,x], or press enter to skip (defaults to n):") or "n"+ use_clip = input("Would you like to use experimental clip search on events? (see README) (y/n), or press enter to skip:") or False+ use_clip = use_clip in ["y", "Y"]++ if use_clip:+ from clip_search import CLIPSearch+ from clip import CachedCLIPSearchif rtsp_url is None and userID is None:userID = input("enter your Clearcam user id or press Enter to skip: ")if len(userID) > 0:key = ""while len(key) < 1: key = input("enter a password for encryption: ")- sys.argv.extend([f"--userid={userID}", f"--key={key}", f"--yolo_size={yolo_variant}"])+ sys.argv.extend([f"--use_clip={use_clip}" ,f"--userid={userID}", f"--key={key}", f"--yolo_size={yolo_variant}"])else: userID = Noneif userID is not None and key is None:@@ -1782,6 +1876,7 @@ if __name__ == "__main__":hls_streamer = HLSStreamer(cam,cam_name=cam_name)cam.streamer = hls_streamer+ searcher = Nonetry:try:server = ThreadedHTTPServer(('0.0.0.0', 8080), HLSRequestHandler)diff --git a/clip.py b/clip.pynew file mode 100644index 0000000..5441936--- /dev/null+++ b/clip.py@@ -0,0 +1,134 @@+import torch+from PIL import Image+import open_clip+import os+import pickle+import time+from datetime import datetime++class CachedCLIPSearch:+ def __init__(self, model_name="ViT-L-14", pretrained_name="laion2b_s32b_b82k"):+ print(f"Loading OpenCLIP model: {model_name} ({pretrained_name})")+ self.model, _, self.preprocess = open_clip.create_model_and_transforms(+ model_name,+ pretrained=pretrained_name+ )++ self.model = self.model.to("cpu").eval()+ self.tokenizer = open_clip.get_tokenizer(model_name)++ self.image_embeddings = {}+ self.image_paths = {}++ def find_object_folders(self, base_path="data/cameras"):+ object_folders = []++ if not os.path.exists(base_path):+ print(f"Base path {base_path} does not exist!")+ return object_folders++ for camera_folder in os.listdir(base_path):+ camera_path = os.path.join(base_path, camera_folder)+ if os.path.isdir(camera_path):+ objects_path = os.path.join(camera_path, "objects")+ if os.path.isdir(objects_path):+ for date_folder in os.listdir(objects_path):+ date_path = os.path.join(objects_path, date_folder)+ if os.path.isdir(date_path):+ object_folders.append(date_path)+ print(f"Found object folder: {date_path}")++ return object_folders++ def precompute_embeddings(self, folder_path, batch_size=16):+ cache_file = os.path.join(folder_path, "embeddings.pkl")+ folder_embeddings = {}+ folder_paths = {}++ if os.path.exists(cache_file):+ with open(cache_file, "rb") as f:+ cache = pickle.load(f)+ folder_embeddings = cache.get("embeddings", {})+ folder_paths = cache.get("paths", {})++ current_images = {+ os.path.join(folder_path, f)+ for f in os.listdir(folder_path)+ if f.lower().endswith((".png", ".jpg", ".jpeg"))+ }++ cached_images = set(folder_embeddings.keys())+ new_images = current_images - cached_images+ deleted_images = cached_images - current_images++ # Remove deleted files+ for img in deleted_images:+ folder_embeddings.pop(img, None)+ folder_paths.pop(img, None)++ if not new_images:+ print(f"{datetime.now()}: No new images in {folder_path}. Saving cache...")+ os.makedirs(os.path.dirname(cache_file), exist_ok=True)+ with open(cache_file, "wb") as f:+ pickle.dump({"embeddings": folder_embeddings, "paths": folder_paths}, f)+ return++ print(f"{datetime.now()}: Found {len(new_images)} new images in {folder_path}, processing...")++ new_image_list = list(new_images)++ for i in range(0, len(new_image_list), batch_size):+ batch_paths = new_image_list[i:i + batch_size]+ batch_images = []++ for img_path in batch_paths:+ try:+ with Image.open(img_path) as img:+ if img.mode != "RGB":+ img = img.convert("RGB")+ batch_images.append(img.copy())+ except Exception as e:+ print(f"Error loading {img_path}: {e}")+ continue++ if batch_images:+ with torch.no_grad():+ batch_tensors = torch.stack(+ [self.preprocess(img) for img in batch_images]+ )+ embeddings = self.model.encode_image(batch_tensors)+ embeddings = embeddings / embeddings.norm(dim=-1, keepdim=True)++ for path, embedding in zip(batch_paths, embeddings):+ folder_embeddings[path] = embedding+ folder_paths[path] = path++ print(f"Processed {min(i + batch_size, len(new_image_list))}/{len(new_image_list)} new images...")++ for img in batch_images:+ img.close()++ # Save updated cache+ os.makedirs(os.path.dirname(cache_file), exist_ok=True)+ with open(cache_file, "wb") as f:+ pickle.dump({"embeddings": folder_embeddings, "paths": folder_paths}, f)++ print(f"{datetime.now()}: Updated cache for {folder_path}. Total images stored: {len(folder_embeddings)}")++'''+if __name__ == "__main__":+ searcher = CachedCLIPSearch(+ model_name="ViT-L-14",+ pretrained_name="laion2b_s32b_b82k"+ )++ while True:+ object_folders = searcher.find_object_folders("data/cameras")+ print(f"{datetime.now()}: Found {len(object_folders)} object folders")++ for folder in object_folders:+ searcher.precompute_embeddings(folder)++ print(f"{datetime.now()}: Sleeping for 15 mins...")+ time.sleep(900)+'''\ No newline at end of filediff --git a/clip_requirements.txt b/clip_requirements.txtnew file mode 100644index 0000000..8d3ffc2--- /dev/null+++ b/clip_requirements.txt@@ -0,0 +1 @@+open_clip_torch==3.2.0\ No newline at end of filediff --git a/clip_search.py b/clip_search.pynew file mode 100644index 0000000..84b1392--- /dev/null+++ b/clip_search.py@@ -0,0 +1,115 @@+import os+import pickle+import torch+import open_clip++class CLIPSearch:+ def __init__(self, base_path="data/cameras"):+ self.base_path = base_path+ self.image_embeddings = {}+ self.image_paths = {}+ self.device = torch.device("cpu")+ self.model, _, self.preprocess = open_clip.create_model_and_transforms(+ 'ViT-L-14',+ pretrained='laion2b_s32b_b82k'+ )+ self.model = self.model.to(self.device).eval()++ def _load_single_embeddings_file(self, cache_file):+ """Load embeddings from a single cache file."""+ try:+ with open(cache_file, "rb") as f:+ cache = pickle.load(f)++ folder_embeddings = cache.get("embeddings", {})+ folder_paths = cache.get("paths", {})++ self.image_embeddings.update(folder_embeddings)+ self.image_paths.update(folder_paths)++ loaded_count = len(folder_embeddings)+ print(f"Loaded {loaded_count} embeddings from {cache_file}")+ return loaded_count++ except Exception as e:+ print(f"Error loading {cache_file}: {e}")+ return 0++ def _load_all_embeddings(self):+ total_loaded = 0++ if not os.path.exists(self.base_path):+ print(f"Base path {self.base_path} does not exist!")+ return++ for camera_folder in os.listdir(self.base_path):+ camera_path = os.path.join(self.base_path, camera_folder)+ if not os.path.isdir(camera_path):+ continue++ objects_path = os.path.join(camera_path, "objects")+ if not os.path.isdir(objects_path):+ continue++ for date_folder in os.listdir(objects_path):+ date_path = os.path.join(objects_path, date_folder)+ if not os.path.isdir(date_path):+ continue++ cache_file = os.path.join(date_path, "embeddings.pkl")++ if os.path.exists(cache_file):+ total_loaded += self._load_single_embeddings_file(cache_file)++ print(f"\nTotal images loaded: {total_loaded}")++ def _encode_text(self, query):+ tokens = open_clip.tokenize([query]).to(self.device)+ with torch.no_grad():+ text_emb = self.model.encode_text(tokens)+ text_emb = text_emb / text_emb.norm(dim=-1, keepdim=True)+ return text_emb++ def search(self, query, top_k=10, cam_name=None, timestamp=None):+ if not self.image_embeddings:+ print("No embeddings available.")+ return []+ text_embedding = self._encode_text(query)+ all_similarities = []+ for path, img_embedding in self.image_embeddings.items():+ normalized_path = path.replace("\\", "/")+ if cam_name and f"/cameras/{cam_name}/" not in normalized_path:+ continue+ if timestamp and f"/objects/{timestamp}/" not in normalized_path:+ continue+ similarity = (img_embedding @ text_embedding.T).item()+ filename = os.path.basename(path)+ if filename.lower().endswith((".jpg", ".jpeg", ".png")):+ object_id = filename.split("_")[1].split(".")[0] if "_" in filename else None+ all_similarities.append((path, similarity, object_id))++ if any(item[2] for item in all_similarities):+ best_per_id = {}+ for path, score, object_id in all_similarities:+ if object_id is not None:+ if object_id not in best_per_id or score > best_per_id[object_id][1]:+ best_per_id[object_id] = (path, score)++ items_without_id = [(path, score) for path, score, object_id in all_similarities if object_id is None]+ results = list(best_per_id.values()) + items_without_id+ else:+ results = [(path, score) for path, score, _ in all_similarities]+ results.sort(key=lambda x: x[1], reverse=True)+ return results[:top_k]+++'''+if __name__ == "__main__":+ searcher = CLIPSearch()+ query = input("\nEnter search query (or 'quit' to exit): ").strip()+ results = searcher.search(query, top_k=10)++ print(f"\nTop results for '{query}' (best per object):")+ for i, (path, score) in enumerate(results, 1):+ print(f"{i}. Score: {score:.3f} - \"{path}\"")+'''\ No newline at end of filediff --git a/mainview.html b/mainview.htmlindex 9e2f9d1..827451c 100644--- a/mainview.html+++ b/mainview.html@@ -915,6 +915,11 @@<input type="checkbox" id="alertsOnlyCheckbox">Alerts Only</label>+ <div style="display: flex; align-items: center; gap: 6px; margin-left: 10px;">+ <input type="text" id="imageSearchInput" placeholder="Search text..." style="padding: 6px; border: 1px solid var(--border-color); border-radius: 4px; background: var(--bg-primary); color: var(--text-primary);">+ <button onclick="searchEventImages()" style="padding: 6px 12px; background: var(--accent-primary); color: white; border: none; border-radius: 4px; cursor: pointer;">Search</button>+ <button onclick="clearSearch()" style="padding: 6px 12px; background: var(--text-secondary); color: white; border: none; border-radius: 4px; cursor: pointer;">Clear</button>+ </div></div><div id="eventImagesContainer"><p style="text-align:center; color:#666; width:100%;">No events detected yet.</p>@@ -1452,7 +1457,13 @@});loadEventImages();- setInterval(loadEventImages, 5000);+ setInterval(() => {+ // Only refresh if there's no active search+ if (!currentImageSearchText) {+ loadEventImages();+ }+ }, 5000);+});function isToday(dateString) {@@ -2332,6 +2343,19 @@currentEventCameraFilter = filter.value;}+ let currentImageSearchText = '';+ function searchEventImages() {+ const searchInput = document.getElementById('imageSearchInput');+ currentImageSearchText = searchInput.value.trim();+ loadEventImages();+ }++ function clearSearch() {+ document.getElementById('imageSearchInput').value = '';+ currentImageSearchText = '';+ loadEventImages();+ }+async function loadEventImages() {try {let url = `/event_thumbs?folder=${currentStreamDate}`;@@ -2341,6 +2365,10 @@if (alertsOnlyFilter) {url += `&name_contains=_notif`;}+ if (currentImageSearchText) {+ url += `&image_text=${encodeURIComponent(currentImageSearchText)}`;+ }+const res = await fetch(url);const data = await res.json();const container = document.getElementById("eventImagesContainer");diff --git a/test/clip_images/.DS_Store b/test/clip_images/.DS_Storenew file mode 100644index 0000000..5008ddfBinary files /dev/null and b/test/clip_images/.DS_Store differdiff --git a/test/clip_images/embeddings.pkl b/test/clip_images/embeddings.pklnew file mode 100644index 0000000..b5fd91cBinary files /dev/null and b/test/clip_images/embeddings.pkl differdiff --git a/test/clip_images/f40.jpg b/test/clip_images/f40.jpgnew file mode 100644index 0000000..2c7ed60Binary files /dev/null and b/test/clip_images/f40.jpg differdiff --git a/test/clip_images/micra.jpg b/test/clip_images/micra.jpgnew file mode 100644index 0000000..b884e77Binary files /dev/null and b/test/clip_images/micra.jpg differdiff --git a/test/test_clip.py b/test/test_clip.pynew file mode 100644index 0000000..c49e828--- /dev/null+++ b/test/test_clip.py@@ -0,0 +1,24 @@+from clip import CachedCLIPSearch+from clip_search import CLIPSearch+import numpy as np++def setup_clip_test():+ scanner = CachedCLIPSearch()+ scanner.precompute_embeddings("test/clip_images")++def test_clip_search():+ searcher = CLIPSearch()+ searcher._load_single_embeddings_file("test/clip_images/embeddings.pkl")+ res = searcher.search("ferrari f40")+ np.testing.assert_allclose(res[0][1], 0.33788394927978516)+ np.testing.assert_allclose(res[1][1], 0.07776700705289841)+ assert res[0][0] == "test/clip_images/f40.jpg"+ assert res[1][0] == "test/clip_images/micra.jpg"+ res = searcher.search("nissan micra")+ np.testing.assert_allclose(res[0][1], 0.3227463960647583)+ np.testing.assert_allclose(res[1][1], 0.04420311748981476)+ assert res[1][0] == "test/clip_images/f40.jpg"+ assert res[0][0] == "test/clip_images/micra.jpg"++#setup_clip_test()+test_clip_search()\ No newline at end of file