Spaces:
Build error
Build error
Update app.py
Browse files9 unmodified lines1011121314151632 unmodified lines4950515152535415 unmodified lines70717273747374757617 unmodified lines949596989798100991001011023 unmodified lines1061071081101091101111 unmodified line1131141151181161171183 unmodified lines1221231241281251261277 unmodified lines1351361371421381391401414 unmodified lines1461471481531491501511521531581541551561571581591611609 unmodified linesfrom PIL import Imagefrom facenet_pytorch import MTCNN, InceptionResnetV1import jsonimport gradio as gr
# Load Google Drive API credentials from the uploaded JSON fileSERVICE_ACCOUNT_FILE = './.env' # Assuming the uploaded JSON file is named '.env'32 unmodified lines
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:15 unmodified linesdef 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 clean img.verify() with Image.open(image_path) as img: img.save(image_path) print(f"Image verified and cleaned: {image_path}")17 unmodified linesdef extract_face_embedding(image_path): try: image = Image.open(image_path) face = mtcnn(image) # Detect and crop the face face = mtcnn(image) if face is not None: embedding = resnet(face.unsqueeze(0)) # Get the face embedding embedding = resnet(face.unsqueeze(0)) return embedding else: print(f"No face detected in image: {image_path}")3 unmodified lines return None
def calculate_similarity(embedding1, embedding2): # Calculate cosine similarity between two embeddings return torch.nn.functional.cosine_similarity(embedding1, embedding2).item()
def process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):1 unmodified line aadhar_files = list_files_in_folder(aadhar_folder_id) cphotos_files = list_files_in_folder(cphotos_folder_id)
# Extract embeddings for all Aadhar images aadhar_embeddings = [] for file in aadhar_files: print(f"Processing Aadhar file: {file['name']}")3 unmodified lines if embedding is not None: aadhar_embeddings.append((file['name'], embedding))
# Compare each CCTV image with all Aadhar embeddings for file in cphotos_files: print(f"Processing CCTV file: {file['name']}") file_path = download_file(file['id'], file['name'])7 unmodified lines matched = False for aadhar_name, aadhar_embedding in aadhar_embeddings: similarity = calculate_similarity(embedding_cctv, aadhar_embedding) if similarity > 0.8: # Adjust the threshold as necessary if similarity > 0.8: matched = True print(f"Match found for file: {file['name']} with Aadhar file: {aadhar_name}, Similarity: {similarity:.2f}") break4 unmodified lines
print("Processing of images completed.")
def main():def run_script(): 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.") return f"Script completed in {end_time - start_time:.2f} seconds."
# Gradio interfaceiface = gr.Interface(fn=run_script, inputs=[], outputs="text", description="Run the Image Processing Script")
if __name__ == "__main__": main() iface.launch()app.py+11-12