Ajay98 commited on
Commit
a63d8bc
·
verified ·
1 Parent(s): 40ca95d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -31
app.py CHANGED
@@ -1,33 +1,152 @@
1
- from simple_salesforce import Salesforce
2
- import requests
3
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Salesforce credentials
6
- sf = Salesforce(username='alwaysajay411@gmail.com',
7
- password='Anusha@310819',
8
- security_token='GsGDq6g2ft5klfVfD53NFG4kA',
9
- domain='login')
10
-
11
- # Query Salesforce custom object for image file details
12
- query = "SELECT Id, Name, ContentDocumentId FROM CustomerData__c"
13
- response = sf.query(query)
14
- records = response['records']
15
-
16
- # Create a folder to save images locally if it doesn't exis
17
- local_folder = 'salesforce_images'
18
- if not os.path.exists(local_folder):
19
- os.makedirs(local_folder)
20
- # Iterate through records and download images
21
- for record in records:
22
- file_id = record['ContentDocumentId']
23
- file_details = sf.ContentDocument.get(file_id)
24
- file_name = file_details['Title'] + '.' + file_details['FileExtension']
25
- file_url = f'/services/data/v50.0/sobjects/ContentVersion/{file_id}/VersionData'
26
-
27
- # Download file content
28
- file_response = requests.get(sf.base_url + file_url, headers={'Authorization': 'Bearer ' + sf.session_id})
29
-
30
- # Save the image file locally
31
- with open(os.path.join(local_folder, file_name), 'wb') as f:
32
- f.write(file_response.content)
33
- print(f'Downloaded {file_name}')
 
1
+
2
+
3
  import os
4
+ import cv2
5
+ from google.oauth2 import service_account
6
+ from googleapiclient.discovery import build
7
+ from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
8
+ import io
9
+ import time
10
+ from PIL import Image
11
+
12
+ # Load Google Drive API credentials
13
+ SCOPES = ['https://www.googleapis.com/auth/drive']
14
+ SERVICE_ACCOUNT_FILE = r'C:\Users\Ajaya\Downloads\salesforce-api-439514-d6b432a2e20e.json' # Use raw string to avoid Unicode escape issues
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 = '1jXklVbxTni1gnduuKgJ7zI6zVks6s0LP'
20
+ cphotos_folder_id = '1JVJMtHuIHxZyxAekT6ZQFkEWePOimIlX'
21
+ suspects_folder_id = '1LJE5aw_g5Jvqd5cLZ7UiJ8LyR36JkBKP'
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
+ for file in files:
34
+ print(f"Found file: Name={file['name']}, ID={file['id']}, Type={file['mimeType']}, Owner={file['owners'][0]['displayName']}, Parent Folder ID={file['parents']}")
35
+ return files
36
+ except Exception as e:
37
+ print(f"Error listing files in folder ID: {folder_id} - {e}")
38
+ return []
39
+
40
+ def download_file(file_id, file_name):
41
+ print(f"Attempting to download file: {file_name} (ID: {file_id})")
42
+ # Add extension if missing (assuming files are images)
43
+ if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
44
+ file_name += ".jpg"
45
+ try:
46
+ request = drive_service.files().get_media(fileId=file_id)
47
+ file_io = io.BytesIO()
48
+ downloader = MediaIoBaseDownload(file_io, request)
49
+ done = False
50
+ while not done:
51
+ _, done = downloader.next_chunk()
52
+ file_io.seek(0)
53
+ with open(file_name, 'wb') as f:
54
+ f.write(file_io.read())
55
+ print(f"Successfully downloaded file: {file_name}")
56
+ return file_name
57
+ except Exception as e:
58
+ print(f"Error downloading file ID: {file_id} - {e}")
59
+ return None
60
+
61
+ def verify_and_fix_image(image_path):
62
+ try:
63
+ with Image.open(image_path) as img:
64
+ img.verify() # Verify if the image is corrupt
65
+ # Reopen and save to ensure it's clean
66
+ with Image.open(image_path) as img:
67
+ img.save(image_path)
68
+ print(f"Image verified and cleaned: {image_path}")
69
+ return True
70
+ except Exception as e:
71
+ print(f"Image verification failed for {image_path}. Error: {e}")
72
+ return False
73
+
74
+ def upload_file(file_path, folder_id):
75
+ print(f"Uploading file: {file_path} to folder ID: {folder_id}")
76
+ file_metadata = {'name': os.path.basename(file_path), 'parents': [folder_id]}
77
+ media = MediaFileUpload(file_path, resumable=True)
78
+ try:
79
+ file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
80
+ print(f"Uploaded file ID: {file.get('id')}")
81
+ return file.get('id')
82
+ except Exception as e:
83
+ print(f"Error uploading file {file_path} to folder ID: {folder_id} - {e}")
84
+ return None
85
+
86
+ def detect_face(image_path):
87
+ print(f"Detecting faces in image: {image_path}")
88
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
89
+ image = cv2.imread(image_path)
90
+ if image is None:
91
+ print(f"Error reading image: {image_path}. The file may not be a valid image.")
92
+ return False
93
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
94
+ faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
95
+ face_detected = len(faces) > 0
96
+ print(f"Faces detected: {face_detected} in image: {image_path}")
97
+ return face_detected
98
+
99
+ def process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
100
+ print("Starting processing of images...")
101
+ aadhar_files = list_files_in_folder(aadhar_folder_id)
102
+ cphotos_files = list_files_in_folder(cphotos_folder_id)
103
+
104
+ aadhar_faces = []
105
+ for file in aadhar_files:
106
+ print(f"Processing Aadhar file: {file['name']}")
107
+ file_path = download_file(file['id'], file['name'])
108
+ if file_path and verify_and_fix_image(file_path) and detect_face(file_path):
109
+ img = cv2.imread(file_path)
110
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
111
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
112
+ faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
113
+ if len(faces) > 0:
114
+ aadhar_faces.append((file['name'], faces, img))
115
+
116
+ for file in cphotos_files:
117
+ print(f"Processing CCTV file: {file['name']}")
118
+ file_path = download_file(file['id'], file['name'])
119
+ if not file_path or not verify_and_fix_image(file_path):
120
+ continue
121
+ matched = False
122
+ if detect_face(file_path):
123
+ img_c = cv2.imread(file_path)
124
+ gray_c = cv2.cvtColor(img_c, cv2.COLOR_BGR2GRAY)
125
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
126
+ faces_c = face_cascade.detectMultiScale(gray_c, scaleFactor=1.1, minNeighbors=5)
127
+ if len(faces_c) > 0:
128
+ for aadhar_name, aadhar_face_regions, aadhar_img in aadhar_faces:
129
+ for (x, y, w, h) in aadhar_face_regions:
130
+ aadhar_face = aadhar_img[y:y+h, x:x+w]
131
+ for (xc, yc, wc, hc) in faces_c:
132
+ cctv_face = img_c[yc:yc+hc, xc:xc+wc]
133
+ if aadhar_face.shape == cctv_face.shape and not (cv2.subtract(aadhar_face, cctv_face).any()):
134
+ matched = True
135
+ print(f"Match found for file: {file['name']} with Aadhar file: {aadhar_name}")
136
+ break
137
+ if matched:
138
+ break
139
+ if not matched:
140
+ upload_file(file_path, suspects_folder_id)
141
+ print(f"Unmatched image uploaded: {file['name']}")
142
+ print("Processing of images completed.")
143
+
144
+ def main():
145
+ start_time = time.time()
146
+ print("Script started...")
147
+ process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id)
148
+ end_time = time.time()
149
+ print(f"Script completed in {end_time - start_time:.2f} seconds.")
150
 
151
+ if __name__ == "__main__":
152
+ main()