Ajay98 commited on
Commit
13a2ad8
·
verified ·
1 Parent(s): 72f5a0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -10,6 +10,7 @@ import numpy as np
10
  from PIL import Image
11
  from facenet_pytorch import MTCNN, InceptionResnetV1
12
  import json
 
13
 
14
  # Load Google Drive API credentials from the uploaded JSON file
15
  SERVICE_ACCOUNT_FILE = './.env' # Assuming the uploaded JSON file is named '.env'
@@ -48,7 +49,6 @@ def list_files_in_folder(folder_id):
48
 
49
  def download_file(file_id, file_name):
50
  print(f"Attempting to download file: {file_name} (ID: {file_id})")
51
- # Add extension if missing (assuming files are images)
52
  if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
53
  file_name += ".jpg"
54
  try:
@@ -70,8 +70,7 @@ def download_file(file_id, file_name):
70
  def verify_and_fix_image(image_path):
71
  try:
72
  with Image.open(image_path) as img:
73
- img.verify() # Verify if the image is corrupt
74
- # Reopen and save to ensure it's clean
75
  with Image.open(image_path) as img:
76
  img.save(image_path)
77
  print(f"Image verified and cleaned: {image_path}")
@@ -95,9 +94,9 @@ def upload_file(file_path, folder_id):
95
  def extract_face_embedding(image_path):
96
  try:
97
  image = Image.open(image_path)
98
- face = mtcnn(image) # Detect and crop the face
99
  if face is not None:
100
- embedding = resnet(face.unsqueeze(0)) # Get the face embedding
101
  return embedding
102
  else:
103
  print(f"No face detected in image: {image_path}")
@@ -107,7 +106,6 @@ def extract_face_embedding(image_path):
107
  return None
108
 
109
  def calculate_similarity(embedding1, embedding2):
110
- # Calculate cosine similarity between two embeddings
111
  return torch.nn.functional.cosine_similarity(embedding1, embedding2).item()
112
 
113
  def process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
@@ -115,7 +113,6 @@ def process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
115
  aadhar_files = list_files_in_folder(aadhar_folder_id)
116
  cphotos_files = list_files_in_folder(cphotos_folder_id)
117
 
118
- # Extract embeddings for all Aadhar images
119
  aadhar_embeddings = []
120
  for file in aadhar_files:
121
  print(f"Processing Aadhar file: {file['name']}")
@@ -125,7 +122,6 @@ def process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
125
  if embedding is not None:
126
  aadhar_embeddings.append((file['name'], embedding))
127
 
128
- # Compare each CCTV image with all Aadhar embeddings
129
  for file in cphotos_files:
130
  print(f"Processing CCTV file: {file['name']}")
131
  file_path = download_file(file['id'], file['name'])
@@ -139,7 +135,7 @@ def process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
139
  matched = False
140
  for aadhar_name, aadhar_embedding in aadhar_embeddings:
141
  similarity = calculate_similarity(embedding_cctv, aadhar_embedding)
142
- if similarity > 0.8: # Adjust the threshold as necessary
143
  matched = True
144
  print(f"Match found for file: {file['name']} with Aadhar file: {aadhar_name}, Similarity: {similarity:.2f}")
145
  break
@@ -150,12 +146,15 @@ def process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
150
 
151
  print("Processing of images completed.")
152
 
153
- def main():
154
  start_time = time.time()
155
  print("Script started...")
156
  process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id)
157
  end_time = time.time()
158
- print(f"Script completed in {end_time - start_time:.2f} seconds.")
 
 
 
159
 
160
  if __name__ == "__main__":
161
- main()
 
10
  from PIL import Image
11
  from facenet_pytorch import MTCNN, InceptionResnetV1
12
  import json
13
+ import gradio as gr
14
 
15
  # Load Google Drive API credentials from the uploaded JSON file
16
  SERVICE_ACCOUNT_FILE = './.env' # Assuming the uploaded JSON file is named '.env'
 
49
 
50
  def download_file(file_id, file_name):
51
  print(f"Attempting to download file: {file_name} (ID: {file_id})")
 
52
  if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
53
  file_name += ".jpg"
54
  try:
 
70
  def verify_and_fix_image(image_path):
71
  try:
72
  with Image.open(image_path) as img:
73
+ img.verify()
 
74
  with Image.open(image_path) as img:
75
  img.save(image_path)
76
  print(f"Image verified and cleaned: {image_path}")
 
94
  def extract_face_embedding(image_path):
95
  try:
96
  image = Image.open(image_path)
97
+ face = mtcnn(image)
98
  if face is not None:
99
+ embedding = resnet(face.unsqueeze(0))
100
  return embedding
101
  else:
102
  print(f"No face detected in image: {image_path}")
 
106
  return None
107
 
108
  def calculate_similarity(embedding1, embedding2):
 
109
  return torch.nn.functional.cosine_similarity(embedding1, embedding2).item()
110
 
111
  def process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id):
 
113
  aadhar_files = list_files_in_folder(aadhar_folder_id)
114
  cphotos_files = list_files_in_folder(cphotos_folder_id)
115
 
 
116
  aadhar_embeddings = []
117
  for file in aadhar_files:
118
  print(f"Processing Aadhar file: {file['name']}")
 
122
  if embedding is not None:
123
  aadhar_embeddings.append((file['name'], embedding))
124
 
 
125
  for file in cphotos_files:
126
  print(f"Processing CCTV file: {file['name']}")
127
  file_path = download_file(file['id'], file['name'])
 
135
  matched = False
136
  for aadhar_name, aadhar_embedding in aadhar_embeddings:
137
  similarity = calculate_similarity(embedding_cctv, aadhar_embedding)
138
+ if similarity > 0.8:
139
  matched = True
140
  print(f"Match found for file: {file['name']} with Aadhar file: {aadhar_name}, Similarity: {similarity:.2f}")
141
  break
 
146
 
147
  print("Processing of images completed.")
148
 
149
+ def run_script():
150
  start_time = time.time()
151
  print("Script started...")
152
  process_images(aadhar_folder_id, cphotos_folder_id, suspects_folder_id)
153
  end_time = time.time()
154
+ return f"Script completed in {end_time - start_time:.2f} seconds."
155
+
156
+ # Gradio interface
157
+ iface = gr.Interface(fn=run_script, inputs=[], outputs="text", description="Run the Image Processing Script")
158
 
159
  if __name__ == "__main__":
160
+ iface.launch()