Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 import hog
|
| 8 |
+
import pickle
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
from torchvision.models import resnet50
|
| 11 |
+
|
| 12 |
+
def preprocess_image_siamese(img):
|
| 13 |
+
transform = transforms.Compose([
|
| 14 |
+
transforms.Resize((224, 224)),
|
| 15 |
+
transforms.ToTensor()
|
| 16 |
+
])
|
| 17 |
+
|
| 18 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 19 |
+
return transform(img)
|
| 20 |
+
|
| 21 |
+
def preprocess_image_svm(img):
|
| 22 |
+
img= cv2.resize(img, (224, 224))
|
| 23 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 24 |
+
|
| 25 |
+
return img
|
| 26 |
+
|
| 27 |
+
def extract_hog_features(img):
|
| 28 |
+
hog_features = hog(img, orientations=9,
|
| 29 |
+
pixels_per_cell=(8, 8),
|
| 30 |
+
cells_per_block=(2, 2))
|
| 31 |
+
return hog_features
|
| 32 |
+
|
| 33 |
+
def get_face(img):
|
| 34 |
+
detector = MTCNN()
|
| 35 |
+
faces = detector.detect_faces(img)
|
| 36 |
+
|
| 37 |
+
x1,y1,w,h = faces[0]['box']
|
| 38 |
+
x1, y1 = abs(x1), abs(y1)
|
| 39 |
+
x2 = abs(x1+w)
|
| 40 |
+
y2 = abs(y1+h)
|
| 41 |
+
|
| 42 |
+
store_face = img[y1:y2,x1:x2]
|
| 43 |
+
|
| 44 |
+
return store_face
|
| 45 |
+
|
| 46 |
+
def verify(img1, img2, model_type):
|
| 47 |
+
|
| 48 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img1:
|
| 49 |
+
temp_img1.write(img1.read())
|
| 50 |
+
temp_img1_path = temp_img1.name
|
| 51 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img2:
|
| 52 |
+
temp_img2.write(img2.read())
|
| 53 |
+
temp_img2_path = temp_img2.name
|
| 54 |
+
|
| 55 |
+
img1p = cv2.imread(temp_img1_path)
|
| 56 |
+
img2p = cv2.imread(temp_img2_path)
|
| 57 |
+
|
| 58 |
+
face1 = get_face(img1p)
|
| 59 |
+
face2 = get_face(img2p)
|
| 60 |
+
|
| 61 |
+
if face1 and face2:
|
| 62 |
+
|
| 63 |
+
st.image([face1, face2], caption=["Image 1", "Image 2"], width=200)
|
| 64 |
+
|
| 65 |
+
# if model_type == "Siamese":
|
| 66 |
+
# face1 = preprocess_image_siamese(face1)
|
| 67 |
+
# face2 = preprocess_image_siamese(face2)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# # Get predictions
|
| 71 |
+
# with torch.no_grad():
|
| 72 |
+
# prediction = model(face1_tensor, face2_tensor)
|
| 73 |
+
|
| 74 |
+
# # Threshold decision
|
| 75 |
+
# if prediction.item() > 0.5:
|
| 76 |
+
# st.write("Matched")
|
| 77 |
+
# else:
|
| 78 |
+
# st.write("Not Matched")
|
| 79 |
+
# st.write(f"Confidence: {prediction.item():.4f}")
|
| 80 |
+
|
| 81 |
+
if model_type == "HOG-SVM":
|
| 82 |
+
with open('svm_model.pkl', 'rb') as f:
|
| 83 |
+
svm = pickle.load(f)
|
| 84 |
+
with open('pca_model.pkl', 'rb') as f:
|
| 85 |
+
pca = pickle.load(f)
|
| 86 |
+
|
| 87 |
+
face1 = preprocess_image_svm(face1)
|
| 88 |
+
face2 = preprocess_image_svm(face2)
|
| 89 |
+
|
| 90 |
+
hog1 = extract_hog_features(face1)
|
| 91 |
+
hog2 = extract_hog_features(face2)
|
| 92 |
+
|
| 93 |
+
hog1_pca = pca.transform([hog1])
|
| 94 |
+
hog2_pca = pca.transform([hog2])
|
| 95 |
+
|
| 96 |
+
pred1 = svm.predict(hog1_pca)
|
| 97 |
+
pred2 = svm.predict(hog2_pca)
|
| 98 |
+
|
| 99 |
+
if pred1 == 1 and pred2 == 1:
|
| 100 |
+
st.write("Matched")
|
| 101 |
+
else:
|
| 102 |
+
st.write("Not Matched")
|
| 103 |
+
else:
|
| 104 |
+
st.write("Face not detected in one or both images")
|
| 105 |
+
|
| 106 |
+
def main():
|
| 107 |
+
st.title("Face Verification App")
|
| 108 |
+
|
| 109 |
+
model_type = st.selectbox("Select Model", ["Siamese", "HOG-SVM"])
|
| 110 |
+
|
| 111 |
+
uploaded_img1 = st.file_uploader("Upload Image 1", type=["jpg", "png"])
|
| 112 |
+
uploaded_img2 = st.file_uploader("Upload Image 2", type=["jpg", "png"])
|
| 113 |
+
|
| 114 |
+
if uploaded_img1 and uploaded_img2:
|
| 115 |
+
if st.button("Verify Faces"):
|
| 116 |
+
verify(uploaded_img1, uploaded_img2, model_type)
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
main()
|