Ajay98 commited on
Commit
34a4d41
·
verified ·
1 Parent(s): 8bfd723

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -44
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import os
2
  import json
3
- import logging
4
  import time
5
  from concurrent.futures import ThreadPoolExecutor, as_completed
6
  from google.oauth2 import service_account
@@ -9,11 +8,7 @@ from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
9
  from PIL import Image
10
  import torch
11
  import io
12
- from deepface import DeepFace
13
- from scipy.spatial.distance import cosine # Import for cosine similarity calculation
14
-
15
- # Set up logging
16
- logging.basicConfig(level=logging.DEBUG)
17
 
18
  # Load Google Drive API credentials
19
  SERVICE_ACCOUNT_FILE = './.env' # Assuming the uploaded JSON file is named '.env'
@@ -21,21 +16,18 @@ SCOPES = ['https://www.googleapis.com/auth/drive']
21
 
22
  try:
23
  if not os.path.exists(SERVICE_ACCOUNT_FILE):
24
- logging.error(f"Service account file '{SERVICE_ACCOUNT_FILE}' not found.")
25
  raise FileNotFoundError(f"Service account file '{SERVICE_ACCOUNT_FILE}' not found.")
26
 
27
  with open(SERVICE_ACCOUNT_FILE, 'r') as json_file:
28
  credentials_info = json.load(json_file)
29
  credentials = service_account.Credentials.from_service_account_info(credentials_info, scopes=SCOPES)
30
  drive_service = build('drive', 'v3', credentials=credentials)
31
- logging.debug("Google Drive API credentials loaded successfully.")
32
  except json.JSONDecodeError as e:
33
- logging.error(f"Error parsing JSON service account file: {e}")
34
  except FileNotFoundError as e:
35
- logging.error(f"Service account file missing: {e}")
36
  except Exception as e:
37
- logging.error(f"Unexpected error while loading credentials: {e}")
38
- raise
39
 
40
  # Folder IDs
41
  aadhar_folder_id = '1Qtb5DYzSFE67Mbb5ZgDIqWUtdJaDD2F4'
@@ -43,25 +35,18 @@ cphotos_folder_id = '1DGeRqRbCPcfLDdEgP0h5fyX-MF8EQ8AH'
43
  suspects_folder_id = '1N3RMhVD0OygeufLPYod6IYLtqzvlm3Jv'
44
 
45
  # Limit the number of threads for scalability
46
- MAX_THREADS = 3
47
 
48
  def list_files_in_folder(folder_id):
49
- logging.debug(f"Listing files in folder ID: {folder_id}")
50
  query = f"'{folder_id}' in parents and trashed=false"
51
  try:
52
  results = drive_service.files().list(q=query, pageSize=50, fields="files(id, name, mimeType)").execute()
53
  files = results.get('files', [])
54
- if not files:
55
- logging.warning(f"No files found in folder ID: {folder_id}. Ensure that the folder has files and the service account has access.")
56
- else:
57
- logging.debug(f"Found {len(files)} files in folder ID: {folder_id}.")
58
  return files
59
  except Exception as e:
60
- logging.error(f"Error listing files in folder ID: {folder_id} - {e}")
61
- return []
62
 
63
  def download_file(file_id, file_name):
64
- logging.debug(f"Attempting to download file: {file_name} (ID: {file_id})")
65
  if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
66
  file_name += ".jpg"
67
  try:
@@ -74,11 +59,9 @@ def download_file(file_id, file_name):
74
  file_io.seek(0)
75
  with open(file_name, 'wb') as f:
76
  f.write(file_io.read())
77
- logging.debug(f"Successfully downloaded file: {file_name}")
78
  return file_name
79
  except Exception as e:
80
- logging.error(f"Error downloading file ID: {file_id} - {e}")
81
- return None
82
 
83
  def extract_face_embedding(image_path):
84
  try:
@@ -86,25 +69,21 @@ def extract_face_embedding(image_path):
86
  embedding = DeepFace.represent(img_path=image_path, model_name='VGG-Face')[0]["embedding"]
87
  return embedding
88
  except Exception as e:
89
- logging.error(f"Error extracting face encoding for {image_path}. Error: {e}")
90
- return None
91
 
92
  def calculate_similarity(encoding1, encoding2):
93
- # Use cosine similarity directly
94
- distance = cosine(encoding1, encoding2)
95
- similarity = 1 - distance
96
- return similarity
97
 
98
  def compare_faces_with_encodings(encoding_cctv, aadhar_encodings, threshold=0.7):
99
  for aadhar_name, aadhar_encoding in aadhar_encodings:
100
  similarity = calculate_similarity(encoding_cctv, aadhar_encoding)
101
  if similarity > threshold:
102
- logging.info(f"Match found: Similarity = {similarity:.2f} with Aadhar file: {aadhar_name}")
103
  return True
104
  return False
105
 
106
  def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
107
- logging.debug("Starting processing of images...")
108
  aadhar_files = list_files_in_folder(aadhar_folder_id)
109
  cphotos_files = list_files_in_folder(cphotos_folder_id)
110
 
@@ -121,10 +100,10 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
121
  if encoding is not None:
122
  aadhar_encodings.append((file['name'], encoding))
123
  except Exception as e:
124
- logging.error(f"Error processing Aadhar file {file['name']}: {e}")
125
 
126
  if not aadhar_encodings:
127
- logging.warning("No valid Aadhar face encodings found. Exiting.")
128
  return
129
 
130
  # Extract CCTV encodings and compare concurrently
@@ -145,9 +124,9 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
145
  matched = compare_faces_with_encodings(encoding_cctv, aadhar_encodings)
146
  if not matched:
147
  unmatched_files.append(file_path)
148
- logging.info(f"Unmatched image queued for upload: {file['name']}")
149
  except Exception as e:
150
- logging.error(f"Error processing CCTV file {file['name']}: {e}")
151
 
152
  # Upload unmatched files concurrently
153
  with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
@@ -155,28 +134,24 @@ def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id
155
  for future in as_completed(upload_futures):
156
  try:
157
  future.result()
158
- logging.info(f"Successfully uploaded unmatched image to suspects folder.")
159
  except Exception as e:
160
- logging.error(f"Error uploading unmatched file: {e}")
161
 
162
  def upload_file(file_path, folder_id):
163
- logging.debug(f"Uploading file: {file_path} to folder ID: {folder_id}")
164
  file_metadata = {'name': os.path.basename(file_path), 'parents': [folder_id]}
165
  media = MediaFileUpload(file_path, resumable=True)
166
  try:
167
  file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
168
- logging.debug(f"Uploaded file ID: {file.get('id')}")
169
  return file.get('id')
170
  except Exception as e:
171
- logging.error(f"Error uploading file {file_path} to folder ID: {folder_id} - {e}")
172
- return None
173
 
174
  def main():
175
  start_time = time.time()
176
- logging.debug("Script started...")
177
  batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id)
178
  end_time = time.time()
179
- logging.debug(f"Script completed in {end_time - start_time:.2f} seconds.")
180
 
181
  if __name__ == "__main__":
182
  main()
 
1
  import os
2
  import json
 
3
  import time
4
  from concurrent.futures import ThreadPoolExecutor, as_completed
5
  from google.oauth2 import service_account
 
8
  from PIL import Image
9
  import torch
10
  import io
11
+ from deepface import DeepFace # Use DeepFace for advanced face recognition
 
 
 
 
12
 
13
  # Load Google Drive API credentials
14
  SERVICE_ACCOUNT_FILE = './.env' # Assuming the uploaded JSON file is named '.env'
 
16
 
17
  try:
18
  if not os.path.exists(SERVICE_ACCOUNT_FILE):
 
19
  raise FileNotFoundError(f"Service account file '{SERVICE_ACCOUNT_FILE}' not found.")
20
 
21
  with open(SERVICE_ACCOUNT_FILE, 'r') as json_file:
22
  credentials_info = json.load(json_file)
23
  credentials = service_account.Credentials.from_service_account_info(credentials_info, scopes=SCOPES)
24
  drive_service = build('drive', 'v3', credentials=credentials)
 
25
  except json.JSONDecodeError as e:
26
+ raise Exception(f"Error parsing JSON service account file: {e}")
27
  except FileNotFoundError as e:
28
+ raise Exception(f"Service account file missing: {e}")
29
  except Exception as e:
30
+ raise Exception(f"Unexpected error while loading credentials: {e}")
 
31
 
32
  # Folder IDs
33
  aadhar_folder_id = '1Qtb5DYzSFE67Mbb5ZgDIqWUtdJaDD2F4'
 
35
  suspects_folder_id = '1N3RMhVD0OygeufLPYod6IYLtqzvlm3Jv'
36
 
37
  # Limit the number of threads for scalability
38
+ MAX_THREADS = 5
39
 
40
  def list_files_in_folder(folder_id):
 
41
  query = f"'{folder_id}' in parents and trashed=false"
42
  try:
43
  results = drive_service.files().list(q=query, pageSize=50, fields="files(id, name, mimeType)").execute()
44
  files = results.get('files', [])
 
 
 
 
45
  return files
46
  except Exception as e:
47
+ raise Exception(f"Error listing files in folder ID: {folder_id} - {e}")
 
48
 
49
  def download_file(file_id, file_name):
 
50
  if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
51
  file_name += ".jpg"
52
  try:
 
59
  file_io.seek(0)
60
  with open(file_name, 'wb') as f:
61
  f.write(file_io.read())
 
62
  return file_name
63
  except Exception as e:
64
+ raise Exception(f"Error downloading file ID: {file_id} - {e}")
 
65
 
66
  def extract_face_embedding(image_path):
67
  try:
 
69
  embedding = DeepFace.represent(img_path=image_path, model_name='VGG-Face')[0]["embedding"]
70
  return embedding
71
  except Exception as e:
72
+ raise Exception(f"Error extracting face encoding for {image_path}. Error: {e}")
 
73
 
74
  def calculate_similarity(encoding1, encoding2):
75
+ # Use cosine similarity for comparison
76
+ similarity = DeepFace.find(img_path1=encoding1, img_path2=encoding2, distance_metric='cosine')[0]
77
+ return 1 - similarity # Convert distance to similarity
 
78
 
79
  def compare_faces_with_encodings(encoding_cctv, aadhar_encodings, threshold=0.7):
80
  for aadhar_name, aadhar_encoding in aadhar_encodings:
81
  similarity = calculate_similarity(encoding_cctv, aadhar_encoding)
82
  if similarity > threshold:
 
83
  return True
84
  return False
85
 
86
  def batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
 
87
  aadhar_files = list_files_in_folder(aadhar_folder_id)
88
  cphotos_files = list_files_in_folder(cphotos_folder_id)
89
 
 
100
  if encoding is not None:
101
  aadhar_encodings.append((file['name'], encoding))
102
  except Exception as e:
103
+ print(f"Error processing Aadhar file {file['name']}: {e}")
104
 
105
  if not aadhar_encodings:
106
+ print("No valid Aadhar face encodings found. Exiting.")
107
  return
108
 
109
  # Extract CCTV encodings and compare concurrently
 
124
  matched = compare_faces_with_encodings(encoding_cctv, aadhar_encodings)
125
  if not matched:
126
  unmatched_files.append(file_path)
127
+ print(f"Unmatched image queued for upload: {file['name']}")
128
  except Exception as e:
129
+ print(f"Error processing CCTV file {file['name']}: {e}")
130
 
131
  # Upload unmatched files concurrently
132
  with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
 
134
  for future in as_completed(upload_futures):
135
  try:
136
  future.result()
137
+ print(f"Successfully uploaded unmatched image to suspects folder.")
138
  except Exception as e:
139
+ print(f"Error uploading unmatched file: {e}")
140
 
141
  def upload_file(file_path, folder_id):
 
142
  file_metadata = {'name': os.path.basename(file_path), 'parents': [folder_id]}
143
  media = MediaFileUpload(file_path, resumable=True)
144
  try:
145
  file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
 
146
  return file.get('id')
147
  except Exception as e:
148
+ raise Exception(f"Error uploading file {file_path} to folder ID: {folder_id} - {e}")
 
149
 
150
  def main():
151
  start_time = time.time()
 
152
  batch_process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id)
153
  end_time = time.time()
154
+ print(f"Script completed in {end_time - start_time:.2f} seconds.")
155
 
156
  if __name__ == "__main__":
157
  main()