Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455566 unmodified lines63646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160import osimport jsonimport timefrom concurrent.futures import ThreadPoolExecutor, as_completedfrom google.oauth2 import service_accountfrom googleapiclient.discovery import buildfrom googleapiclient.http import MediaIoBaseDownload, MediaFileUploadfrom PIL import Imageimport ioimport numpy as npfrom deepface import DeepFacefrom scipy.spatial.distance import cosine # Import cosine similarity calculation# Disable GPU to avoid cuDNN and cuBLAS errorsos.environ["CUDA_VISIBLE_DEVICES"] = "-1"# Load Google Drive API credentialsSERVICE_ACCOUNT_FILE = './.env' # Assuming the uploaded JSON file is named '.env'SCOPES = ['https://www.googleapis.com/auth/drive']try:if not os.path.exists(SERVICE_ACCOUNT_FILE):raise FileNotFoundError(f"Service account file '{SERVICE_ACCOUNT_FILE}' not found.")with open(SERVICE_ACCOUNT_FILE, 'r') as json_file:credentials_info = json.load(json_file)credentials = service_account.Credentials.from_service_account_info(credentials_info, scopes=SCOPES)drive_service = build('drive', 'v3', credentials=credentials)except json.JSONDecodeError as e:raise Exception(f"Error parsing JSON service account file: {e}")except FileNotFoundError as e:raise Exception(f"Service account file missing: {e}")except Exception as e:raise Exception(f"Unexpected error while loading credentials: {e}")# Folder IDsaadhar_folder_id = '1Qtb5DYzSFE67Mbb5ZgDIqWUtdJaDD2F4'cphotos_folder_id = '1DGeRqRbCPcfLDdEgP0h5fyX-MF8EQ8AH'suspects_folder_id = '1N3RMhVD0OygeufLPYod6IYLtqzvlm3Jv'# Limit the number of threads for scalabilityMAX_THREADS = 5def list_files_in_folder(folder_id):query = f"'{folder_id}' in parents and trashed=false"try:results = drive_service.files().list(q=query, pageSize=50, fields="files(id, name, mimeType)").execute()files = results.get('files', [])return filesexcept Exception as e:raise Exception(f"Error listing files in folder ID: {folder_id} - {e}")def download_file(file_id, file_name):if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):file_name += ".jpg"try:6 unmodified linesfile_io.seek(0)with open(file_name, 'wb') as f:f.write(file_io.read())return file_nameexcept Exception as e:raise Exception(f"Error downloading file ID: {file_id} - {e}")def extract_face_embedding(image_path):try:# Use DeepFace to extract embeddingsembedding = DeepFace.represent(img_path=image_path, model_name='VGG-Face')[0]["embedding"]return embeddingexcept Exception as e:raise Exception(f"Error extracting face encoding for {image_path}. Error: {e}")def calculate_similarity(encoding1, encoding2):# Use cosine similarity for comparisondistance = cosine(encoding1, encoding2)similarity = 1 - distancereturn similaritydef compare_faces_with_encodings(encoding_cctv, aadhar_encodings, threshold=0.7):for aadhar_name, aadhar_encoding in aadhar_encodings:similarity = calculate_similarity(encoding_cctv, aadhar_encoding)if similarity > threshold:return Truereturn Falsedef batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):aadhar_files = list_files_in_folder(aadhar_folder_id)cphotos_files = list_files_in_folder(cphotos_folder_id)# Extract Aadhar encodings concurrentlyaadhar_encodings = []with ThreadPoolExecutor(max_workers=MAX_THREADS) 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:encoding = extract_face_embedding(file_path)if encoding is not None:aadhar_encodings.append((file['name'], encoding))except Exception as e:print(f"Error processing Aadhar file {file['name']}: {e}")if not aadhar_encodings:print("No valid Aadhar face encodings found. Exiting.")return# Extract CCTV encodings and compare concurrentlyunmatched_files = []with ThreadPoolExecutor(max_workers=MAX_THREADS) 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:continueencoding_cctv = extract_face_embedding(file_path)if encoding_cctv is None:continuematched = compare_faces_with_encodings(encoding_cctv, aadhar_encodings)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 files concurrentlywith ThreadPoolExecutor(max_workers=MAX_THREADS) 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 upload_file(file_path, folder_id):file_metadata = {'name': os.path.basename(file_path), 'parents': [folder_id]}media = MediaFileUpload(file_path, resumable=True)try:file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()return file.get('id')except Exception as e:raise Exception(f"Error uploading file {file_path} to folder ID: {folder_id} - {e}")def main():start_time = time.time()batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id)end_time = time.time()print(f"Script completed in {end_time - start_time:.2f} seconds.")1234567891011121314151617181920212223242526272829303132333435363738394041426 unmodified lines495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143import osimport cv2from google.oauth2 import service_accountfrom googleapiclient.discovery import buildfrom googleapiclient.http import MediaIoBaseDownload, MediaFileUploadimport ioimport timefrom PIL import Imagefrom concurrent.futures import ThreadPoolExecutorfrom deepface import DeepFace # Advanced face recognition for better accuracy# Load Google Drive API credentialsSCOPES = ['https://www.googleapis.com/auth/drive']SERVICE_ACCOUNT_FILE = './salesforce-api-439514-d6b432a2e20e.json' # Assuming file in current directorycredentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)drive_service = build('drive', 'v3', credentials=credentials)# Folder IDsaadhar_folder_id = '1Qtb5DYzSFE67Mbb5ZgDIqWUtdJaDD2F4'cphotos_folder_id = '1DGeRqRbCPcfLDdEgP0h5fyX-MF8EQ8AH'suspects_folder_id = '1N3RMhVD0OygeufLPYod6IYLtqzvlm3Jv'def list_files_in_folder(folder_id):print(f"Listing files in folder ID: {folder_id}")query = f"'{folder_id}' in parents and trashed=false"try:results = drive_service.files().list(q=query, pageSize=1000, fields="files(id, name, mimeType, owners, parents)").execute()files = results.get('files', [])if not files:print(f"No files found in folder ID: {folder_id}. Ensure that the folder has files and the service account has access.")else:print(f"Found {len(files)} files in folder ID: {folder_id}.")return filesexcept Exception as e:print(f"Error listing files in folder ID: {folder_id} - {e}")return []def download_file(file_id, file_name):print(f"Attempting to download file: {file_name} (ID: {file_id})")if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):file_name += ".jpg"try:6 unmodified linesfile_io.seek(0)with open(file_name, 'wb') as f:f.write(file_io.read())print(f"Successfully downloaded file: {file_name}")return file_nameexcept Exception as e:print(f"Error downloading file ID: {file_id} - {e}")return Nonedef verify_and_fix_image(image_path):try:with Image.open(image_path) as img:img.verify()with Image.open(image_path) as img:img.save(image_path)print(f"Image verified and cleaned: {image_path}")return Trueexcept Exception as e:print(f"Image verification failed for {image_path}. Error: {e}")return Falsedef upload_file(file_path, folder_id):print(f"Uploading file: {file_path} to folder ID: {folder_id}")file_metadata = {'name': os.path.basename(file_path), 'parents': [folder_id]}media = MediaFileUpload(file_path, resumable=True)try:file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()print(f"Uploaded file ID: {file.get('id')}")return file.get('id')except Exception as e:print(f"Error uploading file {file_path} to folder ID: {folder_id} - {e}")return Nonedef process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):print("Starting processing of images...")aadhar_files = list_files_in_folder(aadhar_folder_id)cphotos_files = list_files_in_folder(cphotos_folder_id)aadhar_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 future_to_aadhar:file_name = future_to_aadhar[future]try:file_path = future.result()if file_path:embedding = DeepFace.represent(img_path=file_path, model_name='VGG-Face')if embedding:aadhar_embeddings.append((file_name, embedding))except Exception as e:print(f"Error processing Aadhar file {file_name}: {e}")if not aadhar_embeddings:print("No valid Aadhar face embeddings found. Exiting.")returnunmatched_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 future_to_cphoto:file = future_to_cphoto[future]try:file_path = future.result()if not file_path:continueembedding_cctv = DeepFace.represent(img_path=file_path, model_name='VGG-Face')if not embedding_cctv:continuematched = Falsefor aadhar_name, aadhar_embedding in aadhar_embeddings:result = DeepFace.verify(embedding_cctv, aadhar_embedding, model_name='VGG-Face')if result['verified']:matched = Trueprint(f"Match found for CCTV file: {file['name']} with Aadhar file: {aadhar_name}")breakif 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}")with ThreadPoolExecutor() as executor:for file_path in unmatched_files:executor.submit(upload_file, file_path, suspects_folder_id)def main():start_time = time.time()print("Script started...")process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id)end_time = time.time()print(f"Script completed in {end_time - start_time:.2f} seconds.")
app.py+72-89