Spaces:
Build error
Build error
Update app.py
Browse files1234562 unmodified lines91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556from concurrent.futures import ThreadPoolExecutor, as_completed# New function for batch processing images in paralleldef batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):# Fetch Aadhar and CCTV filesaadhar_files = list_files_in_folder(aadhar_folder_id)2 unmodified lines# Extract Aadhar embeddings in parallelaadhar_embeddings = []with ThreadPoolExecutor() as executor:future_to_aadhar = {executor.submit(download_file, file['id'], file['name']): file['name'] for file in aadhar_files}for future in as_completed(future_to_aadhar):file_name = 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))except Exception as e:print(f"Error processing Aadhar file {file_name}: {e}")# Process CCTV files in parallelunmatched_files = []with ThreadPoolExecutor() as executor:for file in cphotos_files:file_path = download_file(file['id'], file['name'])if not file_path:continueembedding_cctv = extract_face_embedding(file_path)if embedding_cctv is None:continuematched = Falsefor aadhar_name, aadhar_embedding in aadhar_embeddings:similarity = calculate_similarity(embedding_cctv, aadhar_embedding)if similarity > 0.8: # Adjust the threshold as necessarymatched = Trueprint(f"Match found: {file['name']} with Aadhar file: {aadhar_name}, Similarity: {similarity:.2f}")breakif not matched:unmatched_files.append(file_path)print(f"Unmatched image queued for upload: {file['name']}")# Upload unmatched fileswith ThreadPoolExecutor() as executor:for file_path in unmatched_files:executor.submit(upload_file, file_path, suspects_folder_id)# Replace the main function to call the new batch processing functiondef main():start_time = time.time()print("Script started...")1234562 unmodified lines9101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475from concurrent.futures import ThreadPoolExecutor, as_completed# 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 filesaadhar_files = list_files_in_folder(aadhar_folder_id)2 unmodified lines# Extract Aadhar embeddings in parallelaadhar_embeddings = []with ThreadPoolExecutor() as executor: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 = 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))except Exception as e:print(f"Error processing Aadhar file {file['name']}: {e}")# Verify that embeddings were successfully extracted for at least one Aadhar fileif not aadhar_embeddings:print("No valid Aadhar face embeddings found. Exiting.")return# Process CCTV files and compare with Aadhar embeddingsunmatched_files = []with ThreadPoolExecutor() as executor: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:continueembedding_cctv = extract_face_embedding(file_path)if embedding_cctv is None:continuematched = 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 fileswith ThreadPoolExecutor() as executor: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 Truereturn Falsedef main():start_time = time.time()print("Script started...")
app.py+48-29