Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,9 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
import json
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load Google Drive API credentials from the uploaded JSON file
|
| 7 |
SERVICE_ACCOUNT_FILE = './.env' # Assuming the uploaded JSON file is named '.env'
|
|
@@ -19,25 +22,13 @@ aadhar_folder_id = '1Qtb5DYzSFE67Mbb5ZgDIqWUtdJaDD2F4'
|
|
| 19 |
cphotos_folder_id = '1DGeRqRbCPcfLDdEgP0h5fyX-MF8EQ8AH'
|
| 20 |
suspects_folder_id = '1N3RMhVD0OygeufLPYod6IYLtqzvlm3Jv'
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
# Improved function to batch process images with better face comparison logic
|
| 25 |
-
def list_files_in_folder(folder_id):
|
| 26 |
-
print(f"Listing files in folder ID: {folder_id}")
|
| 27 |
-
query = f"'{folder_id}' in parents and trashed=false"
|
| 28 |
-
try:
|
| 29 |
-
results = drive_service.files().list(q=query, pageSize=1000, fields="files(id, name, mimeType, owners, parents)").execute()
|
| 30 |
-
files = results.get('files', [])
|
| 31 |
-
if not files:
|
| 32 |
-
print(f"No files found in folder ID: {folder_id}. Ensure that the folder has files and the service account has access.")
|
| 33 |
-
else:
|
| 34 |
-
print(f"Found {len(files)} files in folder ID: {folder_id}.")
|
| 35 |
-
return files
|
| 36 |
-
except Exception as e:
|
| 37 |
-
print(f"Error listing files in folder ID: {folder_id} - {e}")
|
| 38 |
-
return []
|
| 39 |
-
|
| 40 |
def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
|
|
|
|
|
|
|
| 41 |
# Fetch Aadhar and CCTV files
|
| 42 |
aadhar_files = list_files_in_folder(aadhar_folder_id)
|
| 43 |
cphotos_files = list_files_in_folder(cphotos_folder_id)
|
|
@@ -45,17 +36,17 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
|
|
| 45 |
# Extract Aadhar embeddings in parallel
|
| 46 |
aadhar_embeddings = []
|
| 47 |
print("Extracting embeddings from Aadhar images...")
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
for future in as_completed(future_to_aadhar):
|
| 51 |
-
file = future_to_aadhar[future]
|
| 52 |
try:
|
| 53 |
-
|
| 54 |
-
if
|
| 55 |
-
embedding =
|
| 56 |
-
|
| 57 |
-
aadhar_embeddings.append((file['name'], embedding))
|
| 58 |
except Exception as e:
|
|
|
|
| 59 |
print(f"Error processing Aadhar file {file['name']}: {e}")
|
| 60 |
|
| 61 |
# Verify that embeddings were successfully extracted for at least one Aadhar file
|
|
@@ -66,30 +57,29 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
|
|
| 66 |
# Process CCTV files and compare with Aadhar embeddings
|
| 67 |
unmatched_files = []
|
| 68 |
print("Processing CCTV images and comparing embeddings...")
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
for future in as_completed(future_to_cphoto):
|
| 72 |
-
file = future_to_cphoto[future]
|
| 73 |
try:
|
| 74 |
-
|
| 75 |
-
if not
|
| 76 |
continue
|
| 77 |
|
| 78 |
-
embedding_cctv =
|
| 79 |
-
if embedding_cctv is None:
|
| 80 |
-
continue
|
| 81 |
|
| 82 |
# Compare CCTV embedding with all Aadhaar embeddings
|
| 83 |
matched = compare_faces_with_embeddings(embedding_cctv, aadhar_embeddings)
|
| 84 |
if not matched:
|
| 85 |
-
unmatched_files.append(
|
| 86 |
-
print(f"Unmatched image queued for upload: {
|
| 87 |
except Exception as e:
|
|
|
|
| 88 |
print(f"Error processing CCTV file {file['name']}: {e}")
|
| 89 |
|
| 90 |
# Upload unmatched files to suspect folder
|
| 91 |
print("Uploading unmatched images to the suspect folder...")
|
| 92 |
-
with ThreadPoolExecutor() as executor:
|
| 93 |
upload_futures = [executor.submit(upload_file, file_path, suspects_folder_id) for file_path in unmatched_files]
|
| 94 |
for future in as_completed(upload_futures):
|
| 95 |
try:
|
|
@@ -98,6 +88,23 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
|
|
| 98 |
except Exception as e:
|
| 99 |
print(f"Error uploading unmatched file: {e}")
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
def compare_faces_with_embeddings(embedding_cctv, aadhar_embeddings, threshold=0.75):
|
| 102 |
"""
|
| 103 |
Compare the given CCTV embedding with a list of Aadhar embeddings.
|
|
|
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
import json
|
| 5 |
+
from google.oauth2 import service_account
|
| 6 |
+
from googleapiclient.discovery import build
|
| 7 |
+
from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
|
| 8 |
|
| 9 |
# Load Google Drive API credentials from the uploaded JSON file
|
| 10 |
SERVICE_ACCOUNT_FILE = './.env' # Assuming the uploaded JSON file is named '.env'
|
|
|
|
| 22 |
cphotos_folder_id = '1DGeRqRbCPcfLDdEgP0h5fyX-MF8EQ8AH'
|
| 23 |
suspects_folder_id = '1N3RMhVD0OygeufLPYod6IYLtqzvlm3Jv'
|
| 24 |
|
| 25 |
+
# Increase the efficiency of ThreadPoolExecutor by balancing the number of threads
|
| 26 |
+
MAX_WORKERS = 10
|
| 27 |
|
| 28 |
# Improved function to batch process images with better face comparison logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
|
| 30 |
+
start_time = time.time()
|
| 31 |
+
|
| 32 |
# Fetch Aadhar and CCTV files
|
| 33 |
aadhar_files = list_files_in_folder(aadhar_folder_id)
|
| 34 |
cphotos_files = list_files_in_folder(cphotos_folder_id)
|
|
|
|
| 36 |
# Extract Aadhar embeddings in parallel
|
| 37 |
aadhar_embeddings = []
|
| 38 |
print("Extracting embeddings from Aadhar images...")
|
| 39 |
+
|
| 40 |
+
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
|
| 41 |
+
future_to_aadhar = {executor.submit(download_and_extract_embedding, file): file for file in aadhar_files}
|
| 42 |
for future in as_completed(future_to_aadhar):
|
|
|
|
| 43 |
try:
|
| 44 |
+
result = future.result()
|
| 45 |
+
if result:
|
| 46 |
+
file_name, embedding = result
|
| 47 |
+
aadhar_embeddings.append((file_name, embedding))
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
+
file = future_to_aadhar[future]
|
| 50 |
print(f"Error processing Aadhar file {file['name']}: {e}")
|
| 51 |
|
| 52 |
# Verify that embeddings were successfully extracted for at least one Aadhar file
|
|
|
|
| 57 |
# Process CCTV files and compare with Aadhar embeddings
|
| 58 |
unmatched_files = []
|
| 59 |
print("Processing CCTV images and comparing embeddings...")
|
| 60 |
+
|
| 61 |
+
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
|
| 62 |
+
future_to_cphoto = {executor.submit(download_and_extract_embedding, file): file for file in cphotos_files}
|
| 63 |
for future in as_completed(future_to_cphoto):
|
|
|
|
| 64 |
try:
|
| 65 |
+
result = future.result()
|
| 66 |
+
if not result:
|
| 67 |
continue
|
| 68 |
|
| 69 |
+
file_name, embedding_cctv = result
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# Compare CCTV embedding with all Aadhaar embeddings
|
| 72 |
matched = compare_faces_with_embeddings(embedding_cctv, aadhar_embeddings)
|
| 73 |
if not matched:
|
| 74 |
+
unmatched_files.append(file_name)
|
| 75 |
+
print(f"Unmatched image queued for upload: {file_name}")
|
| 76 |
except Exception as e:
|
| 77 |
+
file = future_to_cphoto[future]
|
| 78 |
print(f"Error processing CCTV file {file['name']}: {e}")
|
| 79 |
|
| 80 |
# Upload unmatched files to suspect folder
|
| 81 |
print("Uploading unmatched images to the suspect folder...")
|
| 82 |
+
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
|
| 83 |
upload_futures = [executor.submit(upload_file, file_path, suspects_folder_id) for file_path in unmatched_files]
|
| 84 |
for future in as_completed(upload_futures):
|
| 85 |
try:
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
print(f"Error uploading unmatched file: {e}")
|
| 90 |
|
| 91 |
+
end_time = time.time()
|
| 92 |
+
print(f"Total processing time: {end_time - start_time:.2f} seconds.")
|
| 93 |
+
|
| 94 |
+
def download_and_extract_embedding(file):
|
| 95 |
+
"""
|
| 96 |
+
Download the file and extract its face embedding.
|
| 97 |
+
This function combines the download and extraction step to reduce overhead.
|
| 98 |
+
"""
|
| 99 |
+
try:
|
| 100 |
+
file_path = download_file(file['id'], file['name'])
|
| 101 |
+
if file_path:
|
| 102 |
+
embedding = extract_face_embedding(file_path)
|
| 103 |
+
return file['name'], embedding
|
| 104 |
+
except Exception as e:
|
| 105 |
+
print(f"Error processing file {file['name']}: {e}")
|
| 106 |
+
return None
|
| 107 |
+
|
| 108 |
def compare_faces_with_embeddings(embedding_cctv, aadhar_embeddings, threshold=0.75):
|
| 109 |
"""
|
| 110 |
Compare the given CCTV embedding with a list of Aadhar embeddings.
|