Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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(
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 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 |
-
|
| 58 |
-
st.image([face1, face2], caption=["Image 1", "Image 2"], width=200)
|
| 59 |
|
|
|
|
| 60 |
if model_type == "HOG-SVM":
|
| 61 |
-
with open('./
|
| 62 |
svm = joblib.load(f)
|
| 63 |
-
with open('./
|
| 64 |
pca = joblib.load(f)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
face2 = preprocess_image_svm(face2)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
hog2 = extract_hog_features(face2)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
hog2_pca = pca.transform([hog2])
|
| 74 |
|
| 75 |
-
|
| 76 |
-
pred2 = svm.predict(hog2_pca)
|
| 77 |
|
| 78 |
-
if
|
| 79 |
-
st.write("
|
| 80 |
else:
|
| 81 |
-
st.write("Not
|
| 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 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
if
|
| 93 |
-
|
| 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()
|