Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,42 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import time
|
| 4 |
-
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 5 |
from google.oauth2 import service_account
|
| 6 |
from googleapiclient.discovery import build
|
| 7 |
from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
|
| 8 |
-
from PIL import Image
|
| 9 |
import io
|
| 10 |
-
import
|
| 11 |
-
from
|
| 12 |
-
from
|
| 13 |
-
|
| 14 |
-
# Disable GPU to avoid cuDNN and cuBLAS errors
|
| 15 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 16 |
|
| 17 |
# Load Google Drive API credentials
|
| 18 |
-
SERVICE_ACCOUNT_FILE = './.env' # Assuming the uploaded JSON file is named '.env'
|
| 19 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
raise FileNotFoundError(f"Service account file '{SERVICE_ACCOUNT_FILE}' not found.")
|
| 24 |
-
|
| 25 |
-
with open(SERVICE_ACCOUNT_FILE, 'r') as json_file:
|
| 26 |
-
credentials_info = json.load(json_file)
|
| 27 |
-
credentials = service_account.Credentials.from_service_account_info(credentials_info, scopes=SCOPES)
|
| 28 |
-
drive_service = build('drive', 'v3', credentials=credentials)
|
| 29 |
-
except json.JSONDecodeError as e:
|
| 30 |
-
raise Exception(f"Error parsing JSON service account file: {e}")
|
| 31 |
-
except FileNotFoundError as e:
|
| 32 |
-
raise Exception(f"Service account file missing: {e}")
|
| 33 |
-
except Exception as e:
|
| 34 |
-
raise Exception(f"Unexpected error while loading credentials: {e}")
|
| 35 |
|
| 36 |
# Folder IDs
|
| 37 |
aadhar_folder_id = '1Qtb5DYzSFE67Mbb5ZgDIqWUtdJaDD2F4'
|
| 38 |
cphotos_folder_id = '1DGeRqRbCPcfLDdEgP0h5fyX-MF8EQ8AH'
|
| 39 |
suspects_folder_id = '1N3RMhVD0OygeufLPYod6IYLtqzvlm3Jv'
|
| 40 |
|
| 41 |
-
# Limit the number of threads for scalability
|
| 42 |
-
MAX_THREADS = 5
|
| 43 |
-
|
| 44 |
def list_files_in_folder(folder_id):
|
|
|
|
| 45 |
query = f"'{folder_id}' in parents and trashed=false"
|
| 46 |
try:
|
| 47 |
-
results = drive_service.files().list(q=query, pageSize=
|
| 48 |
files = results.get('files', [])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return files
|
| 50 |
except Exception as e:
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
def download_file(file_id, file_name):
|
|
|
|
| 54 |
if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 55 |
file_name += ".jpg"
|
| 56 |
try:
|
|
@@ -63,98 +49,95 @@ def download_file(file_id, file_name):
|
|
| 63 |
file_io.seek(0)
|
| 64 |
with open(file_name, 'wb') as f:
|
| 65 |
f.write(file_io.read())
|
|
|
|
| 66 |
return file_name
|
| 67 |
except Exception as e:
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
def
|
| 71 |
try:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
def
|
|
|
|
| 92 |
aadhar_files = list_files_in_folder(aadhar_folder_id)
|
| 93 |
cphotos_files = list_files_in_folder(cphotos_folder_id)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
file = future_to_aadhar[future]
|
| 101 |
try:
|
| 102 |
file_path = future.result()
|
| 103 |
if file_path:
|
| 104 |
-
|
| 105 |
-
if
|
| 106 |
-
|
| 107 |
except Exception as e:
|
| 108 |
-
print(f"Error processing Aadhar file {
|
| 109 |
|
| 110 |
-
if not
|
| 111 |
-
print("No valid Aadhar face
|
| 112 |
return
|
| 113 |
|
| 114 |
-
# Extract CCTV encodings and compare concurrently
|
| 115 |
unmatched_files = []
|
| 116 |
-
with ThreadPoolExecutor(
|
| 117 |
future_to_cphoto = {executor.submit(download_file, file['id'], file['name']): file for file in cphotos_files}
|
| 118 |
-
for future in
|
| 119 |
file = future_to_cphoto[future]
|
| 120 |
try:
|
| 121 |
file_path = future.result()
|
| 122 |
if not file_path:
|
| 123 |
continue
|
| 124 |
|
| 125 |
-
|
| 126 |
-
if
|
| 127 |
continue
|
| 128 |
|
| 129 |
-
matched =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
if not matched:
|
| 131 |
unmatched_files.append(file_path)
|
| 132 |
print(f"Unmatched image queued for upload: {file['name']}")
|
| 133 |
except Exception as e:
|
| 134 |
print(f"Error processing CCTV file {file['name']}: {e}")
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
for future in as_completed(upload_futures):
|
| 140 |
-
try:
|
| 141 |
-
future.result()
|
| 142 |
-
print(f"Successfully uploaded unmatched image to suspects folder.")
|
| 143 |
-
except Exception as e:
|
| 144 |
-
print(f"Error uploading unmatched file: {e}")
|
| 145 |
-
|
| 146 |
-
def upload_file(file_path, folder_id):
|
| 147 |
-
file_metadata = {'name': os.path.basename(file_path), 'parents': [folder_id]}
|
| 148 |
-
media = MediaFileUpload(file_path, resumable=True)
|
| 149 |
-
try:
|
| 150 |
-
file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 151 |
-
return file.get('id')
|
| 152 |
-
except Exception as e:
|
| 153 |
-
raise Exception(f"Error uploading file {file_path} to folder ID: {folder_id} - {e}")
|
| 154 |
|
| 155 |
def main():
|
| 156 |
start_time = time.time()
|
| 157 |
-
|
|
|
|
| 158 |
end_time = time.time()
|
| 159 |
print(f"Script completed in {end_time - start_time:.2f} seconds.")
|
| 160 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import cv2
|
|
|
|
|
|
|
| 3 |
from google.oauth2 import service_account
|
| 4 |
from googleapiclient.discovery import build
|
| 5 |
from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
|
|
|
|
| 6 |
import io
|
| 7 |
+
import time
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 10 |
+
from deepface import DeepFace # Advanced face recognition for better accuracy
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Load Google Drive API credentials
|
|
|
|
| 13 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
| 14 |
+
SERVICE_ACCOUNT_FILE = './salesforce-api-439514-d6b432a2e20e.json' # Assuming file in current directory
|
| 15 |
+
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
| 16 |
+
drive_service = build('drive', 'v3', credentials=credentials)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Folder IDs
|
| 19 |
aadhar_folder_id = '1Qtb5DYzSFE67Mbb5ZgDIqWUtdJaDD2F4'
|
| 20 |
cphotos_folder_id = '1DGeRqRbCPcfLDdEgP0h5fyX-MF8EQ8AH'
|
| 21 |
suspects_folder_id = '1N3RMhVD0OygeufLPYod6IYLtqzvlm3Jv'
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
def list_files_in_folder(folder_id):
|
| 24 |
+
print(f"Listing files in folder ID: {folder_id}")
|
| 25 |
query = f"'{folder_id}' in parents and trashed=false"
|
| 26 |
try:
|
| 27 |
+
results = drive_service.files().list(q=query, pageSize=1000, fields="files(id, name, mimeType, owners, parents)").execute()
|
| 28 |
files = results.get('files', [])
|
| 29 |
+
if not files:
|
| 30 |
+
print(f"No files found in folder ID: {folder_id}. Ensure that the folder has files and the service account has access.")
|
| 31 |
+
else:
|
| 32 |
+
print(f"Found {len(files)} files in folder ID: {folder_id}.")
|
| 33 |
return files
|
| 34 |
except Exception as e:
|
| 35 |
+
print(f"Error listing files in folder ID: {folder_id} - {e}")
|
| 36 |
+
return []
|
| 37 |
|
| 38 |
def download_file(file_id, file_name):
|
| 39 |
+
print(f"Attempting to download file: {file_name} (ID: {file_id})")
|
| 40 |
if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 41 |
file_name += ".jpg"
|
| 42 |
try:
|
|
|
|
| 49 |
file_io.seek(0)
|
| 50 |
with open(file_name, 'wb') as f:
|
| 51 |
f.write(file_io.read())
|
| 52 |
+
print(f"Successfully downloaded file: {file_name}")
|
| 53 |
return file_name
|
| 54 |
except Exception as e:
|
| 55 |
+
print(f"Error downloading file ID: {file_id} - {e}")
|
| 56 |
+
return None
|
| 57 |
|
| 58 |
+
def verify_and_fix_image(image_path):
|
| 59 |
try:
|
| 60 |
+
with Image.open(image_path) as img:
|
| 61 |
+
img.verify()
|
| 62 |
+
with Image.open(image_path) as img:
|
| 63 |
+
img.save(image_path)
|
| 64 |
+
print(f"Image verified and cleaned: {image_path}")
|
| 65 |
+
return True
|
| 66 |
except Exception as e:
|
| 67 |
+
print(f"Image verification failed for {image_path}. Error: {e}")
|
| 68 |
+
return False
|
| 69 |
+
|
| 70 |
+
def upload_file(file_path, folder_id):
|
| 71 |
+
print(f"Uploading file: {file_path} to folder ID: {folder_id}")
|
| 72 |
+
file_metadata = {'name': os.path.basename(file_path), 'parents': [folder_id]}
|
| 73 |
+
media = MediaFileUpload(file_path, resumable=True)
|
| 74 |
+
try:
|
| 75 |
+
file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 76 |
+
print(f"Uploaded file ID: {file.get('id')}")
|
| 77 |
+
return file.get('id')
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"Error uploading file {file_path} to folder ID: {folder_id} - {e}")
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
def process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
|
| 83 |
+
print("Starting processing of images...")
|
| 84 |
aadhar_files = list_files_in_folder(aadhar_folder_id)
|
| 85 |
cphotos_files = list_files_in_folder(cphotos_folder_id)
|
| 86 |
|
| 87 |
+
aadhar_embeddings = []
|
| 88 |
+
with ThreadPoolExecutor() as executor:
|
| 89 |
+
future_to_aadhar = {executor.submit(download_file, file['id'], file['name']): file['name'] for file in aadhar_files}
|
| 90 |
+
for future in future_to_aadhar:
|
| 91 |
+
file_name = future_to_aadhar[future]
|
|
|
|
| 92 |
try:
|
| 93 |
file_path = future.result()
|
| 94 |
if file_path:
|
| 95 |
+
embedding = DeepFace.represent(img_path=file_path, model_name='VGG-Face')
|
| 96 |
+
if embedding:
|
| 97 |
+
aadhar_embeddings.append((file_name, embedding))
|
| 98 |
except Exception as e:
|
| 99 |
+
print(f"Error processing Aadhar file {file_name}: {e}")
|
| 100 |
|
| 101 |
+
if not aadhar_embeddings:
|
| 102 |
+
print("No valid Aadhar face embeddings found. Exiting.")
|
| 103 |
return
|
| 104 |
|
|
|
|
| 105 |
unmatched_files = []
|
| 106 |
+
with ThreadPoolExecutor() as executor:
|
| 107 |
future_to_cphoto = {executor.submit(download_file, file['id'], file['name']): file for file in cphotos_files}
|
| 108 |
+
for future in future_to_cphoto:
|
| 109 |
file = future_to_cphoto[future]
|
| 110 |
try:
|
| 111 |
file_path = future.result()
|
| 112 |
if not file_path:
|
| 113 |
continue
|
| 114 |
|
| 115 |
+
embedding_cctv = DeepFace.represent(img_path=file_path, model_name='VGG-Face')
|
| 116 |
+
if not embedding_cctv:
|
| 117 |
continue
|
| 118 |
|
| 119 |
+
matched = False
|
| 120 |
+
for aadhar_name, aadhar_embedding in aadhar_embeddings:
|
| 121 |
+
result = DeepFace.verify(embedding_cctv, aadhar_embedding, model_name='VGG-Face')
|
| 122 |
+
if result['verified']:
|
| 123 |
+
matched = True
|
| 124 |
+
print(f"Match found for CCTV file: {file['name']} with Aadhar file: {aadhar_name}")
|
| 125 |
+
break
|
| 126 |
+
|
| 127 |
if not matched:
|
| 128 |
unmatched_files.append(file_path)
|
| 129 |
print(f"Unmatched image queued for upload: {file['name']}")
|
| 130 |
except Exception as e:
|
| 131 |
print(f"Error processing CCTV file {file['name']}: {e}")
|
| 132 |
|
| 133 |
+
with ThreadPoolExecutor() as executor:
|
| 134 |
+
for file_path in unmatched_files:
|
| 135 |
+
executor.submit(upload_file, file_path, suspects_folder_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
def main():
|
| 138 |
start_time = time.time()
|
| 139 |
+
print("Script started...")
|
| 140 |
+
process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id)
|
| 141 |
end_time = time.time()
|
| 142 |
print(f"Script completed in {end_time - start_time:.2f} seconds.")
|
| 143 |
|