Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -77,67 +77,77 @@ def get_face(img):
|
|
77 |
return img[y1:y2, x1:x2]
|
78 |
return None
|
79 |
|
80 |
-
def verify(image, model, person):
|
81 |
|
82 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_image:
|
83 |
temp_image.write(image.read())
|
84 |
temp_image_path = temp_image.name
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
face = get_face(image)
|
89 |
-
|
90 |
-
temp_face_path = tempfile.mktemp(suffix=".jpg")
|
91 |
-
cv2.imwrite(temp_face_path, face)
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
siamese.eval()
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
face = preprocess_image_svm(face)
|
120 |
-
|
121 |
-
hog = extract_hog_features(face)
|
122 |
-
|
123 |
-
hog_pca = pca.transform([hog])
|
124 |
-
|
125 |
-
pred = svm.predict(hog_pca)
|
126 |
-
|
127 |
-
if pred == 1:
|
128 |
-
st.write("Match")
|
129 |
-
else:
|
130 |
-
st.write("Not Match")
|
131 |
|
132 |
def main():
|
133 |
st.title("Face Verification")
|
134 |
|
135 |
model = st.selectbox("Select Model", ["Siamese", "HOG-SVM"])
|
136 |
person = st.selectbox("Select Person", ["Theo", "Deverel", "Justin"])
|
|
|
|
|
137 |
enable = st.checkbox("Enable camera")
|
138 |
captured_image = st.camera_input("Take a picture", disabled=not enable)
|
139 |
|
140 |
-
if captured_image:
|
|
|
|
|
141 |
verify(captured_image, model, person)
|
142 |
|
143 |
if __name__ == "__main__":
|
|
|
77 |
return img[y1:y2, x1:x2]
|
78 |
return None
|
79 |
|
80 |
+
def verify(image, model, person, validation_image=None):
|
81 |
|
82 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_image:
|
83 |
temp_image.write(image.read())
|
84 |
temp_image_path = temp_image.name
|
85 |
|
86 |
+
image = cv2.imread(temp_image_path)
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
face = get_face(image)
|
89 |
+
|
90 |
+
temp_face_path = tempfile.mktemp(suffix=".jpg")
|
91 |
+
cv2.imwrite(temp_face_path, face)
|
|
|
92 |
|
93 |
+
if face is not None:
|
94 |
+
if model == "Siamese":
|
95 |
+
siamese = SiameseNetwork()
|
96 |
+
siamese.load_state_dict(torch.load(f'siamese_{person.lower()}.pth'))
|
97 |
+
siamese.eval()
|
98 |
+
|
99 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as validation_temp_image:
|
100 |
+
validation_temp_image.write(validation_image.read())
|
101 |
+
validation_temp_image_path = validation_temp_image.name
|
102 |
+
|
103 |
+
|
104 |
+
face = preprocess_image_siamese(temp_face_path)
|
105 |
+
validation_face = preprocess_image_siamese(validation_temp_image_path)
|
106 |
+
|
107 |
+
with torch.no_grad():
|
108 |
+
output = siamese(face, validation_face)
|
109 |
+
probability = output.item()
|
110 |
+
pred = 1.0 if probability > 0.7 else 0.0
|
111 |
+
|
112 |
+
if pred == 1:
|
113 |
+
st.write("Match")
|
114 |
+
else:
|
115 |
+
st.write("Not Match")
|
116 |
+
|
117 |
+
elif model == "HOG-SVM":
|
118 |
+
with open(f'./svm_{person.lower()}.pkl', 'rb') as f:
|
119 |
+
svm = joblib.load(f)
|
120 |
+
with open(f'./pca_{person.lower()}.pkl', 'rb') as f:
|
121 |
+
pca = joblib.load(f)
|
122 |
+
|
123 |
+
face = cv2.imread(temp_face_path)
|
124 |
|
125 |
+
face = preprocess_image_svm(face)
|
126 |
+
|
127 |
+
hog = extract_hog_features(face)
|
128 |
+
|
129 |
+
hog_pca = pca.transform([hog])
|
130 |
+
|
131 |
+
pred = svm.predict(hog_pca)
|
132 |
+
|
133 |
+
if pred == 1:
|
134 |
+
st.write("Match")
|
135 |
+
else:
|
136 |
+
st.write("Not Match")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
def main():
|
139 |
st.title("Face Verification")
|
140 |
|
141 |
model = st.selectbox("Select Model", ["Siamese", "HOG-SVM"])
|
142 |
person = st.selectbox("Select Person", ["Theo", "Deverel", "Justin"])
|
143 |
+
if model == "Siamese":
|
144 |
+
uploaded_image = st.file_uploader("Upload Validation Image (Siamese)", type=["jpg", "png"])
|
145 |
enable = st.checkbox("Enable camera")
|
146 |
captured_image = st.camera_input("Take a picture", disabled=not enable)
|
147 |
|
148 |
+
if captured_image and model == "Siamese":
|
149 |
+
verify(captured_image, model, person, uploaded_image)
|
150 |
+
elif captured_image and model == "HOG-SVM":
|
151 |
verify(captured_image, model, person)
|
152 |
|
153 |
if __name__ == "__main__":
|