Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
####### Section 1 ###################
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
import requests
|
6 |
+
import face_recognition
|
7 |
+
import os
|
8 |
+
import streamlit as st
|
9 |
+
import urllib.request
|
10 |
+
|
11 |
+
####### Section 2 ###################
|
12 |
+
p1 = "sarwan.jpg"
|
13 |
+
p2 = "rattantata.png"
|
14 |
+
p3 = "Ravinder.jpg"
|
15 |
+
|
16 |
+
st.title("Face Recognition ")
|
17 |
+
Images = []
|
18 |
+
classnames = []
|
19 |
+
|
20 |
+
# read images and train the face_recognition package
|
21 |
+
img1 = cv2.imread(p1)
|
22 |
+
Images.append(img1)
|
23 |
+
classnames.append("Sarwan")
|
24 |
+
|
25 |
+
img2 = cv2.imread(p2)
|
26 |
+
Images.append(img2)
|
27 |
+
classnames.append("RattanTata")
|
28 |
+
|
29 |
+
img3 = cv2.imread(p3)
|
30 |
+
Images.append(img3)
|
31 |
+
classnames.append("RavinderKaur")
|
32 |
+
|
33 |
+
# Load images for face recognition
|
34 |
+
encodeListknown = [face_recognition.face_encodings(img)[0] for img in Images]
|
35 |
+
|
36 |
+
|
37 |
+
####### Section 3 ###################
|
38 |
+
# Take picture using the camera
|
39 |
+
img_file_buffer = st.camera_input("Take Your picture")
|
40 |
+
|
41 |
+
# recognise the face in the uploaded image
|
42 |
+
if img_file_buffer is not None:
|
43 |
+
test_image = Image.open(img_file_buffer)
|
44 |
+
image = np.asarray(test_image)
|
45 |
+
image = image.copy()
|
46 |
+
|
47 |
+
imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
|
48 |
+
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
|
49 |
+
facesCurFrame = face_recognition.face_locations(imgS)
|
50 |
+
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
|
51 |
+
faceMatchedflag = 0
|
52 |
+
# run looop to find match in encodeListknown list
|
53 |
+
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
|
54 |
+
# Assuming that encodeListknown is defined and populated in your code
|
55 |
+
matches = face_recognition.compare_faces(encodeListknown, encodeFace)
|
56 |
+
faceDis = face_recognition.face_distance(encodeListknown, encodeFace)
|
57 |
+
matchIndex = np.argmin(faceDis)
|
58 |
+
|
59 |
+
if matches[matchIndex]:
|
60 |
+
name = classnames[matchIndex].upper()
|
61 |
+
#st.write (name)
|
62 |
+
# show the name on image to user
|
63 |
+
y1, x2, y2, x1 = faceLoc
|
64 |
+
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
|
65 |
+
cv2.rectangle(image , (x1, y1), (x2, y2), (0, 255, 0), 2)
|
66 |
+
cv2.rectangle(image , (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
|
67 |
+
cv2.putText(image , name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
|
68 |
+
|
69 |
+
########## update website
|
70 |
+
# using Get Method
|
71 |
+
url = "https://fc11.glitch.me/submit?email=pm&message=faceReco&name="
|
72 |
+
url = url + name
|
73 |
+
st.write(url)
|
74 |
+
res = urllib.request.urlopen(url)
|
75 |
+
response = requests.post(url )
|
76 |
+
|
77 |
+
# # using post method
|
78 |
+
# url = "https://aimljul24f.glitch.me"
|
79 |
+
# url1 = "/save"
|
80 |
+
# data = {'rollno': '99','name': name, 'email': '[email protected]','pwd': '**' }
|
81 |
+
# response = requests.post(url +url1 , data=data)
|
82 |
+
# if response.status_code == 200:
|
83 |
+
# st.success("Data updated on: " + "https://aimljul24f.glitch.me/")
|
84 |
+
# else:
|
85 |
+
# st.warning("Data not updated")
|
86 |
+
########### end update website
|
87 |
+
faceMatchedflag = 1
|
88 |
+
|
89 |
+
st.image(image , use_column_width=True, output_format="PNG")
|
90 |
+
|
91 |
+
if(faceMatchedflag == 0) :
|
92 |
+
st.warning("No faces detected in the image.")
|
93 |
+
|