Spaces:
Build error
Build error
Update app.py
Browse files12334562 unmodified lines910111212131414151617181920202122222324242526272829303127282930313233343536373839404142434445463233343536373839404142434445464748495051525051535455565758596061626364656667686970717253737475from concurrent.futures import ThreadPoolExecutor, as_completed
# New function for batch processing images in parallel# New function to extract embeddings in parallel and batch process CCTV images against Aadhar embeddingsdef batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id): # Fetch Aadhar and CCTV files aadhar_files = list_files_in_folder(aadhar_folder_id)2 unmodified lines # Extract Aadhar embeddings in parallel aadhar_embeddings = [] with ThreadPoolExecutor() as executor: future_to_aadhar = {executor.submit(download_file, file['id'], file['name']): file['name'] for file in aadhar_files} future_to_aadhar = {executor.submit(download_file, file['id'], file['name']): file for file in aadhar_files} for future in as_completed(future_to_aadhar): file_name = future_to_aadhar[future] file = future_to_aadhar[future] try: file_path = future.result() if file_path: embedding = extract_face_embedding(file_path) if embedding is not None: aadhar_embeddings.append((file_name, embedding)) aadhar_embeddings.append((file['name'], embedding)) except Exception as e: print(f"Error processing Aadhar file {file_name}: {e}") print(f"Error processing Aadhar file {file['name']}: {e}")
# Process CCTV files in parallel # Verify that embeddings were successfully extracted for at least one Aadhar file if not aadhar_embeddings: print("No valid Aadhar face embeddings found. Exiting.") return
# Process CCTV files and compare with Aadhar embeddings unmatched_files = [] with ThreadPoolExecutor() as executor: for file in cphotos_files: file_path = download_file(file['id'], file['name']) if not file_path: continue
embedding_cctv = extract_face_embedding(file_path) if embedding_cctv is None: continue
matched = False for aadhar_name, aadhar_embedding in aadhar_embeddings: similarity = calculate_similarity(embedding_cctv, aadhar_embedding) if similarity > 0.8: # Adjust the threshold as necessary matched = True print(f"Match found: {file['name']} with Aadhar file: {aadhar_name}, Similarity: {similarity:.2f}") break
if not matched: unmatched_files.append(file_path) print(f"Unmatched image queued for upload: {file['name']}") future_to_cphoto = {executor.submit(download_file, file['id'], file['name']): file for file in cphotos_files} for future in as_completed(future_to_cphoto): file = future_to_cphoto[future] try: file_path = future.result() if not file_path: continue
embedding_cctv = extract_face_embedding(file_path) if embedding_cctv is None: continue
matched = compare_faces_with_embeddings(embedding_cctv, aadhar_embeddings) if not matched: unmatched_files.append(file_path) print(f"Unmatched image queued for upload: {file['name']}") except Exception as e: print(f"Error processing CCTV file {file['name']}: {e}")
# Upload unmatched files with ThreadPoolExecutor() as executor: for file_path in unmatched_files: executor.submit(upload_file, file_path, suspects_folder_id) upload_futures = [executor.submit(upload_file, file_path, suspects_folder_id) for file_path in unmatched_files] for future in as_completed(upload_futures): try: future.result() print(f"Successfully uploaded unmatched image to suspects folder.") except Exception as e: print(f"Error uploading unmatched file: {e}")
def compare_faces_with_embeddings(embedding_cctv, aadhar_embeddings, threshold=0.7): """ Compare the given CCTV embedding with a list of Aadhar embeddings. If the similarity score exceeds the threshold, it's a match. """ for aadhar_name, aadhar_embedding in aadhar_embeddings: similarity = calculate_similarity(embedding_cctv, aadhar_embedding) if similarity > threshold: print(f"Match found: Similarity = {similarity:.2f} with Aadhar file: {aadhar_name}") return True return False
# Replace the main function to call the new batch processing functiondef main(): start_time = time.time() print("Script started...")app.py+48-29