dvieri commited on
Commit
c2b7cdc
·
verified ·
1 Parent(s): 1f288fc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import streamlit as st
3
+ import tempfile
4
+ import torch
5
+ from torchvision import transforms
6
+ from mtcnn import MTCNN
7
+ from skimage.feature import hog
8
+ import joblib
9
+ import numpy as np
10
+
11
+ # Preprocessing for Siamese Model
12
+ def preprocess_image_siamese(img):
13
+ transform = transforms.Compose([
14
+ transforms.Resize((224, 224)),
15
+ transforms.ToTensor()
16
+ ])
17
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
18
+ return transform(img)
19
+
20
+ # Preprocessing for SVM model (converting to grayscale)
21
+ def preprocess_image_svm(img):
22
+ img = cv2.resize(img, (224, 224))
23
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
24
+ return img
25
+
26
+ # Extract HOG Features
27
+ def extract_hog_features(img):
28
+ hog_features = hog(img, orientations=9, pixels_per_cell=(16, 16), cells_per_block=(4, 4))
29
+ return hog_features
30
+
31
+ # Detect faces using MTCNN
32
+ def get_face(img):
33
+ detector = MTCNN()
34
+ faces = detector.detect_faces(img)
35
+ if faces:
36
+ x1, y1, w, h = faces[0]['box']
37
+ x1, y1 = abs(x1), abs(y1)
38
+ x2, y2 = x1 + w, y1 + h
39
+ return img[y1:y2, x1:x2]
40
+ return None
41
+
42
+ # Function to verify face (either HOG-SVM or Siamese model)
43
+ def verify(img1, img2, model_type, anchor_img):
44
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img1:
45
+ temp_img1.write(img1.read())
46
+ temp_img1_path = temp_img1.name
47
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img2:
48
+ temp_img2.write(img2.read())
49
+ temp_img2_path = temp_img2.name
50
+
51
+ img1p = cv2.imread(temp_img1_path)
52
+ img2p = cv2.imread(temp_img2_path)
53
+
54
+ face1 = get_face(img1p)
55
+ face2 = get_face(img2p)
56
+
57
+ if face1 is not None and face2 is not None:
58
+ st.image([face1, face2], caption=["Image 1", "Image 2"], width=200)
59
+
60
+ if model_type == "HOG-SVM":
61
+ with open('./svm.pkl', 'rb') as f:
62
+ svm = joblib.load(f)
63
+ with open('./pca.pkl', 'rb') as f:
64
+ pca = joblib.load(f)
65
+
66
+ face1 = preprocess_image_svm(face1)
67
+ face2 = preprocess_image_svm(face2)
68
+
69
+ hog1 = extract_hog_features(face1)
70
+ hog2 = extract_hog_features(face2)
71
+
72
+ hog1_pca = pca.transform([hog1])
73
+ hog2_pca = pca.transform([hog2])
74
+
75
+ pred1 = svm.predict(hog1_pca)
76
+ pred2 = svm.predict(hog2_pca)
77
+
78
+ if pred1 == 1 and pred2 == 1:
79
+ st.write("Matched")
80
+ else:
81
+ st.write("Not Matched")
82
+ else:
83
+ st.write("Face not detected in one or both images")
84
+
85
+ # Main function to handle Streamlit interaction
86
+ def main():
87
+ st.title("Real-time Face Verification App")
88
+
89
+ model_type = st.selectbox("Select Model", ["Siamese", "HOG-SVM"])
90
+ anchor_img = st.file_uploader("Select Anchor Image", type=["jpg", "png"])
91
+
92
+ if model_type == "Siamese":
93
+ # Implement Siamese model choice logic here if needed
94
+ st.write("Using Siamese Network")
95
+
96
+ elif model_type == "HOG-SVM":
97
+ # Implement HOG-SVM logic here
98
+ st.write("Using HOG-SVM")
99
+
100
+ # Camera Input for Face Detection
101
+ run_detection = st.checkbox("Start Camera")
102
+
103
+ if run_detection:
104
+ cap = cv2.VideoCapture(0) # Start camera
105
+
106
+ while True:
107
+ ret, frame = cap.read()
108
+ if not ret:
109
+ st.write("Failed to grab frame.")
110
+ break
111
+
112
+ # Detect face in the current frame
113
+ face = get_face(frame)
114
+ if face is not None:
115
+ # Draw bounding box around detected face
116
+ x1, y1, x2, y2 = face[0], face[1], face[2], face[3] # Update face coordinates
117
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
118
+
119
+ # Show bounding box
120
+ st.image(frame, channels="BGR", use_column_width=True)
121
+
122
+ # Stop camera when ESC is pressed
123
+ key = cv2.waitKey(1) & 0xFF
124
+ if key == 27: # ESC key
125
+ break
126
+
127
+ cap.release()
128
+ cv2.destroyAllWindows()
129
+
130
+ if __name__ == "__main__":
131
+ main()