Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
|
|
| 9 |
from PIL import Image
|
| 10 |
import torch
|
| 11 |
import io
|
| 12 |
-
import
|
| 13 |
|
| 14 |
# Set up logging
|
| 15 |
logging.basicConfig(level=logging.DEBUG)
|
|
@@ -42,13 +42,13 @@ cphotos_folder_id = '1DGeRqRbCPcfLDdEgP0h5fyX-MF8EQ8AH'
|
|
| 42 |
suspects_folder_id = '1N3RMhVD0OygeufLPYod6IYLtqzvlm3Jv'
|
| 43 |
|
| 44 |
# Limit the number of threads for scalability
|
| 45 |
-
MAX_THREADS =
|
| 46 |
|
| 47 |
def list_files_in_folder(folder_id):
|
| 48 |
logging.debug(f"Listing files in folder ID: {folder_id}")
|
| 49 |
query = f"'{folder_id}' in parents and trashed=false"
|
| 50 |
try:
|
| 51 |
-
results = drive_service.files().list(q=query, pageSize=
|
| 52 |
files = results.get('files', [])
|
| 53 |
if not files:
|
| 54 |
logging.warning(f"No files found in folder ID: {folder_id}. Ensure that the folder has files and the service account has access.")
|
|
@@ -79,23 +79,19 @@ def download_file(file_id, file_name):
|
|
| 79 |
logging.error(f"Error downloading file ID: {file_id} - {e}")
|
| 80 |
return None
|
| 81 |
|
| 82 |
-
def
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
return face_encodings[0]
|
| 88 |
-
else:
|
| 89 |
-
logging.warning(f"No face detected in image: {image_path}")
|
| 90 |
-
return None
|
| 91 |
except Exception as e:
|
| 92 |
logging.error(f"Error extracting face encoding for {image_path}. Error: {e}")
|
| 93 |
return None
|
| 94 |
|
| 95 |
def calculate_similarity(encoding1, encoding2):
|
| 96 |
-
|
| 97 |
-
similarity =
|
| 98 |
-
return similarity
|
| 99 |
|
| 100 |
def compare_faces_with_encodings(encoding_cctv, aadhar_encodings, threshold=0.7):
|
| 101 |
for aadhar_name, aadhar_encoding in aadhar_encodings:
|
|
@@ -110,6 +106,7 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
|
|
| 110 |
aadhar_files = list_files_in_folder(aadhar_folder_id)
|
| 111 |
cphotos_files = list_files_in_folder(cphotos_folder_id)
|
| 112 |
|
|
|
|
| 113 |
aadhar_encodings = []
|
| 114 |
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
|
| 115 |
future_to_aadhar = {executor.submit(download_file, file['id'], file['name']): file for file in aadhar_files}
|
|
@@ -118,7 +115,7 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
|
|
| 118 |
try:
|
| 119 |
file_path = future.result()
|
| 120 |
if file_path:
|
| 121 |
-
encoding =
|
| 122 |
if encoding is not None:
|
| 123 |
aadhar_encodings.append((file['name'], encoding))
|
| 124 |
except Exception as e:
|
|
@@ -128,6 +125,7 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
|
|
| 128 |
logging.warning("No valid Aadhar face encodings found. Exiting.")
|
| 129 |
return
|
| 130 |
|
|
|
|
| 131 |
unmatched_files = []
|
| 132 |
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
|
| 133 |
future_to_cphoto = {executor.submit(download_file, file['id'], file['name']): file for file in cphotos_files}
|
|
@@ -138,7 +136,7 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
|
|
| 138 |
if not file_path:
|
| 139 |
continue
|
| 140 |
|
| 141 |
-
encoding_cctv =
|
| 142 |
if encoding_cctv is None:
|
| 143 |
continue
|
| 144 |
|
|
@@ -149,6 +147,7 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
|
|
| 149 |
except Exception as e:
|
| 150 |
logging.error(f"Error processing CCTV file {file['name']}: {e}")
|
| 151 |
|
|
|
|
| 152 |
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
|
| 153 |
upload_futures = [executor.submit(upload_file, file_path, suspects_folder_id) for file_path in unmatched_files]
|
| 154 |
for future in as_completed(upload_futures):
|
|
|
|
| 9 |
from PIL import Image
|
| 10 |
import torch
|
| 11 |
import io
|
| 12 |
+
from deepface import DeepFace # Use DeepFace for advanced face recognition
|
| 13 |
|
| 14 |
# Set up logging
|
| 15 |
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
| 42 |
suspects_folder_id = '1N3RMhVD0OygeufLPYod6IYLtqzvlm3Jv'
|
| 43 |
|
| 44 |
# Limit the number of threads for scalability
|
| 45 |
+
MAX_THREADS = 5
|
| 46 |
|
| 47 |
def list_files_in_folder(folder_id):
|
| 48 |
logging.debug(f"Listing files in folder ID: {folder_id}")
|
| 49 |
query = f"'{folder_id}' in parents and trashed=false"
|
| 50 |
try:
|
| 51 |
+
results = drive_service.files().list(q=query, pageSize=50, fields="files(id, name, mimeType)").execute()
|
| 52 |
files = results.get('files', [])
|
| 53 |
if not files:
|
| 54 |
logging.warning(f"No files found in folder ID: {folder_id}. Ensure that the folder has files and the service account has access.")
|
|
|
|
| 79 |
logging.error(f"Error downloading file ID: {file_id} - {e}")
|
| 80 |
return None
|
| 81 |
|
| 82 |
+
def extract_face_embedding(image_path):
|
| 83 |
try:
|
| 84 |
+
# Use DeepFace to extract embeddings
|
| 85 |
+
embedding = DeepFace.represent(img_path=image_path, model_name='VGG-Face')[0]["embedding"]
|
| 86 |
+
return embedding
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
logging.error(f"Error extracting face encoding for {image_path}. Error: {e}")
|
| 89 |
return None
|
| 90 |
|
| 91 |
def calculate_similarity(encoding1, encoding2):
|
| 92 |
+
# Use cosine similarity for comparison
|
| 93 |
+
similarity = DeepFace.find(img_path1=encoding1, img_path2=encoding2, distance_metric='cosine')[0]
|
| 94 |
+
return 1 - similarity # Convert distance to similarity
|
| 95 |
|
| 96 |
def compare_faces_with_encodings(encoding_cctv, aadhar_encodings, threshold=0.7):
|
| 97 |
for aadhar_name, aadhar_encoding in aadhar_encodings:
|
|
|
|
| 106 |
aadhar_files = list_files_in_folder(aadhar_folder_id)
|
| 107 |
cphotos_files = list_files_in_folder(cphotos_folder_id)
|
| 108 |
|
| 109 |
+
# Extract Aadhar encodings concurrently
|
| 110 |
aadhar_encodings = []
|
| 111 |
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
|
| 112 |
future_to_aadhar = {executor.submit(download_file, file['id'], file['name']): file for file in aadhar_files}
|
|
|
|
| 115 |
try:
|
| 116 |
file_path = future.result()
|
| 117 |
if file_path:
|
| 118 |
+
encoding = extract_face_embedding(file_path)
|
| 119 |
if encoding is not None:
|
| 120 |
aadhar_encodings.append((file['name'], encoding))
|
| 121 |
except Exception as e:
|
|
|
|
| 125 |
logging.warning("No valid Aadhar face encodings found. Exiting.")
|
| 126 |
return
|
| 127 |
|
| 128 |
+
# Extract CCTV encodings and compare concurrently
|
| 129 |
unmatched_files = []
|
| 130 |
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
|
| 131 |
future_to_cphoto = {executor.submit(download_file, file['id'], file['name']): file for file in cphotos_files}
|
|
|
|
| 136 |
if not file_path:
|
| 137 |
continue
|
| 138 |
|
| 139 |
+
encoding_cctv = extract_face_embedding(file_path)
|
| 140 |
if encoding_cctv is None:
|
| 141 |
continue
|
| 142 |
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
logging.error(f"Error processing CCTV file {file['name']}: {e}")
|
| 149 |
|
| 150 |
+
# Upload unmatched files concurrently
|
| 151 |
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
|
| 152 |
upload_futures = [executor.submit(upload_file, file_path, suspects_folder_id) for file_path in unmatched_files]
|
| 153 |
for future in as_completed(upload_futures):
|