Ajay98 commited on
Commit
7efeb77
·
verified ·
1 Parent(s): 4f0f342

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -8
app.py CHANGED
@@ -1,8 +1,145 @@
1
- google-api-python-client==2.52.0
2
- google-auth==2.12.0
3
- google-auth-oauthlib==0.5.3
4
- oauth2client==4.1.3
5
- opencv-python==4.5.5.64
6
- numpy==1.21.4
7
- Pillow==9.2.0
8
- deepface==0.0.75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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:
43
+ request = drive_service.files().get_media(fileId=file_id)
44
+ file_io = io.BytesIO()
45
+ downloader = MediaIoBaseDownload(file_io, request)
46
+ done = False
47
+ while not done:
48
+ _, done = downloader.next_chunk()
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
+
144
+ if __name__ == "__main__":
145
+ main()