Spaces:
Build error
Build error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233from simple_salesforce import Salesforceimport requestsimport os# Salesforce credentialssf = Salesforce(username='alwaysajay411@gmail.com',password='Anusha@310819',security_token='GsGDq6g2ft5klfVfD53NFG4kA',domain='login')# Query Salesforce custom object for image file detailsquery = "SELECT Id, Name, ContentDocumentId FROM CustomerData__c"response = sf.query(query)records = response['records']# Create a folder to save images locally if it doesn't exislocal_folder = 'salesforce_images'if not os.path.exists(local_folder):os.makedirs(local_folder)# Iterate through records and download imagesfor record in records:file_id = record['ContentDocumentId']file_details = sf.ContentDocument.get(file_id)file_name = file_details['Title'] + '.' + file_details['FileExtension']file_url = f'/services/data/v50.0/sobjects/ContentVersion/{file_id}/VersionData'# Download file contentfile_response = requests.get(sf.base_url + file_url, headers={'Authorization': 'Bearer ' + sf.session_id})# Save the image file locallywith open(os.path.join(local_folder, file_name), 'wb') as f:f.write(file_response.content)print(f'Downloaded {file_name}')123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152import osimport cv2from google.oauth2 import service_accountfrom googleapiclient.discovery import buildfrom googleapiclient.http import MediaIoBaseDownload, MediaFileUploadimport ioimport timefrom PIL import Image# Load Google Drive API credentialsSCOPES = ['https://www.googleapis.com/auth/drive']SERVICE_ACCOUNT_FILE = r'C:\Users\Ajaya\Downloads\salesforce-api-439514-d6b432a2e20e.json' # Use raw string to avoid Unicode escape issuescredentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)drive_service = build('drive', 'v3', credentials=credentials)# Folder IDsaadhar_folder_id = '1jXklVbxTni1gnduuKgJ7zI6zVks6s0LP'cphotos_folder_id = '1JVJMtHuIHxZyxAekT6ZQFkEWePOimIlX'suspects_folder_id = '1LJE5aw_g5Jvqd5cLZ7UiJ8LyR36JkBKP'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}.")for file in files:print(f"Found file: Name={file['name']}, ID={file['id']}, Type={file['mimeType']}, Owner={file['owners'][0]['displayName']}, Parent Folder ID={file['parents']}")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})")# Add extension if missing (assuming files are images)if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):file_name += ".jpg"try:request = drive_service.files().get_media(fileId=file_id)file_io = io.BytesIO()downloader = MediaIoBaseDownload(file_io, request)done = Falsewhile not done:_, done = downloader.next_chunk()file_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() # Verify if the image is corrupt# Reopen and save to ensure it's cleanwith 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 detect_face(image_path):print(f"Detecting faces in image: {image_path}")face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')image = cv2.imread(image_path)if image is None:print(f"Error reading image: {image_path}. The file may not be a valid image.")return Falsegray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)face_detected = len(faces) > 0print(f"Faces detected: {face_detected} in image: {image_path}")return face_detecteddef 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_faces = []for file in aadhar_files:print(f"Processing Aadhar file: {file['name']}")file_path = download_file(file['id'], file['name'])if file_path and verify_and_fix_image(file_path) and detect_face(file_path):img = cv2.imread(file_path)gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)if len(faces) > 0:aadhar_faces.append((file['name'], faces, img))for file in cphotos_files:print(f"Processing CCTV file: {file['name']}")file_path = download_file(file['id'], file['name'])if not file_path or not verify_and_fix_image(file_path):continuematched = Falseif detect_face(file_path):img_c = cv2.imread(file_path)gray_c = cv2.cvtColor(img_c, cv2.COLOR_BGR2GRAY)face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')faces_c = face_cascade.detectMultiScale(gray_c, scaleFactor=1.1, minNeighbors=5)if len(faces_c) > 0:for aadhar_name, aadhar_face_regions, aadhar_img in aadhar_faces:for (x, y, w, h) in aadhar_face_regions:aadhar_face = aadhar_img[y:y+h, x:x+w]for (xc, yc, wc, hc) in faces_c:cctv_face = img_c[yc:yc+hc, xc:xc+wc]if aadhar_face.shape == cctv_face.shape and not (cv2.subtract(aadhar_face, cctv_face).any()):matched = Trueprint(f"Match found for file: {file['name']} with Aadhar file: {aadhar_name}")breakif matched:breakif not matched:upload_file(file_path, suspects_folder_id)print(f"Unmatched image uploaded: {file['name']}")print("Processing of images completed.")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.")if __name__ == "__main__":main()
app.py+150-31