dvieri commited on
Commit
af18329
·
verified ·
1 Parent(s): 6ba851a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -66
app.py CHANGED
@@ -40,45 +40,35 @@ def get_face(img):
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
 
@@ -86,45 +76,13 @@ def verify(img1, img2, model_type, anchor_img):
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
  if __name__ == "__main__":
130
  main()
 
40
  return None
41
 
42
  # Function to verify face (either HOG-SVM or Siamese model)
43
+ def verify(image, model, person):
44
+
45
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_image:
46
+ temp_image.write(image.read())
47
+ temp_image_path = temp_image.name
48
+
49
+ image = cv2.imread(temp_image_path)
 
 
 
 
 
 
50
 
51
+ face = get_face(image)
 
52
 
53
+ if face is not None:
54
  if model_type == "HOG-SVM":
55
+ with open(f'./svm_{lower(person)}.pkl', 'rb') as f:
56
  svm = joblib.load(f)
57
+ with open(f'./pca_{lower(person)}.pkl', 'rb') as f:
58
  pca = joblib.load(f)
59
 
60
+ face = preprocess_image_svm(face)
 
61
 
62
+ hog = extract_hog_features(face)
 
63
 
64
+ hog_pca = pca.transform([hog])
 
65
 
66
+ pred = svm.predict(hog_pca)
 
67
 
68
+ if pred == 1:
69
+ st.write("Match")
70
  else:
71
+ st.write("Not Match")
72
  else:
73
  st.write("Face not detected in one or both images")
74
 
 
76
  def main():
77
  st.title("Real-time Face Verification App")
78
 
79
+ model = st.selectbox("Select Model", ["Siamese", "HOG-SVM"])
80
+ person = st.selectbox("Select Person", ["Theo"])
81
+ enable = st.checkbox("Enable camera")
82
+ captured_image = st.camera_input("Take a picture", disabled=not enable)
83
 
84
+ if captured_image:
85
+ verify(captured_image, model, person)
 
 
 
 
 
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  if __name__ == "__main__":
88
  main()