Ajay98 commited on
Commit
08b90e5
·
verified ·
1 Parent(s): b6e6a3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -52
app.py CHANGED
@@ -1,53 +1,47 @@
1
  import os
2
- from transformers import AutoModel, AutoTokenizer
3
- import torch
4
- from PIL import Image
5
-
6
- # Load pre-trained model and tokenizer
7
- model_name = "deepface/face-recognition"
8
- model = AutoModel.from_pretrained(model_name)
9
- tokenizer = AutoTokenizer.from_pretrained(model_name)
10
-
11
- # Define folders
12
- folder1 = "C:/Users/nikit/OneDrive/esktop/New folder (2)"
13
- folder2 = "C:/Users/nikit/OneDrive/Desktop/New folder"
14
-
15
- # Define threshold for matching (e.g., 0.5)
16
- threshold = 0.5
17
-
18
- # Function to extract face embeddings
19
- def extract_embeddings(folder):
20
- embeddings = []
21
- for file in os.listdir(folder):
22
- image = Image.open(os.path.join(folder, file))
23
- inputs = tokenizer(images=image, return_tensors='pt')
24
- outputs = model(**inputs)
25
- embeddings.append(outputs.last_hidden_state[:, 0, :].detach().numpy())
26
- return embeddings
27
-
28
- # Extract embeddings from both folders
29
- embeddings1 = extract_embeddings(folder1)
30
- embeddings2 = extract_embeddings(folder2)
31
-
32
- # Compare embeddings
33
- matches = []
34
- for embedding1 in embeddings1:
35
- for embedding2 in embeddings2:
36
- similarity = torch.cosine_similarity(torch.tensor(embedding1), torch.tensor(embedding2))
37
- if similarity > threshold:
38
- matches.append((embedding1, embedding2))
39
-
40
- # Print matches
41
- for match in matches:
42
- print(f"Match found: {match[0]} (Folder 1) and {match[1]} (Folder 2)")
43
-
44
- # Print non-matches
45
- non_matches = []
46
- for embedding1 in embeddings1:
47
- for embedding2 in embeddings2:
48
- similarity = torch.cosine_similarity(torch.tensor(embedding1), torch.tensor(embedding2))
49
- if similarity < threshold:
50
- non_matches.append((embedding1, embedding2))
51
-
52
- for non_match in non_matches:
53
- print(f"No match found: {non_match[0]} (Folder 1) and {non_match[1]} (Folder 2)")
 
1
  import os
2
+ import cv2
3
+ import numpy as np
4
+
5
+ def compare_images(image1, image2):
6
+ # Read the images
7
+ img1 = cv2.imread(r"C:\Users\nikit\OneDrive\Desktop\Test1")
8
+ img2 = cv2.imread(r"C:\Users\nikit\OneDrive\Desktop\Test2")
9
+
10
+ # Check if both images are loaded properly
11
+ if img1 is None or img2 is None:
12
+ return False
13
+
14
+ # Resize images to the same size
15
+ img1 = cv2.resize(img1, (100, 100))
16
+ img2 = cv2.resize(img2, (100, 100))
17
+
18
+ # Convert images to grayscale
19
+ gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
20
+ gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
21
+
22
+ # Calculate the Structural Similarity Index (SSI)
23
+ score, _ = cv2.compare_ssim(gray1, gray2, full=True)
24
+
25
+ # You can adjust the threshold based on your needs
26
+ return score >= 0.9 # Threshold for similarity
27
+
28
+ def compare_folders(staff_folder, unknown_folder):
29
+ for staff_image in os.listdir(staff_folder):
30
+ staff_image_path = os.path.join(staff_folder, staff_image)
31
+ found_match = False
32
+
33
+ for unknown_image in os.listdir(unknown_folder):
34
+ unknown_image_path = os.path.join(unknown_folder, unknown_image)
35
+
36
+ if compare_images(staff_image_path, unknown_image_path):
37
+ print(f"Match found: {staff_image} matches {unknown_image} (Staff)")
38
+ found_match = True
39
+ break
40
+
41
+ if not found_match:
42
+ print(f"No match found for {staff_image} (Unknown)")
43
+
44
+ # Usage
45
+ staff_folder_path = 'path/to/staff/images'
46
+ unknown_folder_path = 'path/to/unknown/images'
47
+ compare_folders(staff_folder_path, unknown_folder_path)