Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 2 |
|
| 3 |
-
# New function
|
| 4 |
def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
|
| 5 |
# Fetch Aadhar and CCTV files
|
| 6 |
aadhar_files = list_files_in_folder(aadhar_folder_id)
|
|
@@ -9,48 +9,67 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
|
|
| 9 |
# Extract Aadhar embeddings in parallel
|
| 10 |
aadhar_embeddings = []
|
| 11 |
with ThreadPoolExecutor() as executor:
|
| 12 |
-
future_to_aadhar = {executor.submit(download_file, file['id'], file['name']): file
|
| 13 |
for future in as_completed(future_to_aadhar):
|
| 14 |
-
|
| 15 |
try:
|
| 16 |
file_path = future.result()
|
| 17 |
if file_path:
|
| 18 |
embedding = extract_face_embedding(file_path)
|
| 19 |
if embedding is not None:
|
| 20 |
-
aadhar_embeddings.append((
|
| 21 |
except Exception as e:
|
| 22 |
-
print(f"Error processing Aadhar file {
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
unmatched_files = []
|
| 26 |
with ThreadPoolExecutor() as executor:
|
| 27 |
-
for file in cphotos_files
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
unmatched_files.append(file_path)
|
| 46 |
-
print(f"Unmatched image queued for upload: {file['name']}")
|
| 47 |
|
| 48 |
# Upload unmatched files
|
| 49 |
with ThreadPoolExecutor() as executor:
|
| 50 |
-
for file_path in unmatched_files
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# Replace the main function to call the new batch processing function
|
| 54 |
def main():
|
| 55 |
start_time = time.time()
|
| 56 |
print("Script started...")
|
|
|
|
| 1 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 2 |
|
| 3 |
+
# New function to extract embeddings in parallel and batch process CCTV images against Aadhar embeddings
|
| 4 |
def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
|
| 5 |
# Fetch Aadhar and CCTV files
|
| 6 |
aadhar_files = list_files_in_folder(aadhar_folder_id)
|
|
|
|
| 9 |
# Extract Aadhar embeddings in parallel
|
| 10 |
aadhar_embeddings = []
|
| 11 |
with ThreadPoolExecutor() as executor:
|
| 12 |
+
future_to_aadhar = {executor.submit(download_file, file['id'], file['name']): file for file in aadhar_files}
|
| 13 |
for future in as_completed(future_to_aadhar):
|
| 14 |
+
file = future_to_aadhar[future]
|
| 15 |
try:
|
| 16 |
file_path = future.result()
|
| 17 |
if file_path:
|
| 18 |
embedding = extract_face_embedding(file_path)
|
| 19 |
if embedding is not None:
|
| 20 |
+
aadhar_embeddings.append((file['name'], embedding))
|
| 21 |
except Exception as e:
|
| 22 |
+
print(f"Error processing Aadhar file {file['name']}: {e}")
|
| 23 |
|
| 24 |
+
# Verify that embeddings were successfully extracted for at least one Aadhar file
|
| 25 |
+
if not aadhar_embeddings:
|
| 26 |
+
print("No valid Aadhar face embeddings found. Exiting.")
|
| 27 |
+
return
|
| 28 |
+
|
| 29 |
+
# Process CCTV files and compare with Aadhar embeddings
|
| 30 |
unmatched_files = []
|
| 31 |
with ThreadPoolExecutor() as executor:
|
| 32 |
+
future_to_cphoto = {executor.submit(download_file, file['id'], file['name']): file for file in cphotos_files}
|
| 33 |
+
for future in as_completed(future_to_cphoto):
|
| 34 |
+
file = future_to_cphoto[future]
|
| 35 |
+
try:
|
| 36 |
+
file_path = future.result()
|
| 37 |
+
if not file_path:
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
+
embedding_cctv = extract_face_embedding(file_path)
|
| 41 |
+
if embedding_cctv is None:
|
| 42 |
+
continue
|
| 43 |
+
|
| 44 |
+
matched = compare_faces_with_embeddings(embedding_cctv, aadhar_embeddings)
|
| 45 |
+
if not matched:
|
| 46 |
+
unmatched_files.append(file_path)
|
| 47 |
+
print(f"Unmatched image queued for upload: {file['name']}")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error processing CCTV file {file['name']}: {e}")
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Upload unmatched files
|
| 52 |
with ThreadPoolExecutor() as executor:
|
| 53 |
+
upload_futures = [executor.submit(upload_file, file_path, suspects_folder_id) for file_path in unmatched_files]
|
| 54 |
+
for future in as_completed(upload_futures):
|
| 55 |
+
try:
|
| 56 |
+
future.result()
|
| 57 |
+
print(f"Successfully uploaded unmatched image to suspects folder.")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"Error uploading unmatched file: {e}")
|
| 60 |
+
|
| 61 |
+
def compare_faces_with_embeddings(embedding_cctv, aadhar_embeddings, threshold=0.7):
|
| 62 |
+
"""
|
| 63 |
+
Compare the given CCTV embedding with a list of Aadhar embeddings.
|
| 64 |
+
If the similarity score exceeds the threshold, it's a match.
|
| 65 |
+
"""
|
| 66 |
+
for aadhar_name, aadhar_embedding in aadhar_embeddings:
|
| 67 |
+
similarity = calculate_similarity(embedding_cctv, aadhar_embedding)
|
| 68 |
+
if similarity > threshold:
|
| 69 |
+
print(f"Match found: Similarity = {similarity:.2f} with Aadhar file: {aadhar_name}")
|
| 70 |
+
return True
|
| 71 |
+
return False
|
| 72 |
|
|
|
|
| 73 |
def main():
|
| 74 |
start_time = time.time()
|
| 75 |
print("Script started...")
|