Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
directory = "facerecognition"
|
| 34 |
+
myList = os.listdir(directory)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
for cls in myList:
|
| 38 |
+
if os.path.splitext(cls)[1] in [".jpg", ".jpeg",".png"]:
|
| 39 |
+
img_path = os.path.join(directory, cls)
|
| 40 |
+
curImg = cv2.imread(img_path)
|
| 41 |
+
Images.append(curImg)
|
| 42 |
+
classnames.append(os.path.splitext(cls)[0])
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# Load images for face recognition
|
| 46 |
+
encodeListknown = [face_recognition.face_encodings(img)[0] for img in Images]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
####### Section 3 ###################
|
| 50 |
+
# Take picture using the camera
|
| 51 |
+
img_file_buffer = st.camera_input("Take Your picture")
|
| 52 |
+
|
| 53 |
+
# recognise the face in the uploaded image
|
| 54 |
+
if img_file_buffer is not None:
|
| 55 |
+
test_image = Image.open(img_file_buffer)
|
| 56 |
+
image = np.asarray(test_image)
|
| 57 |
+
image = image.copy()
|
| 58 |
+
|
| 59 |
+
imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
|
| 60 |
+
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
|
| 61 |
+
facesCurFrame = face_recognition.face_locations(imgS)
|
| 62 |
+
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
|
| 63 |
+
faceMatchedflag = 0
|
| 64 |
+
# run looop to find match in encodeListknown list
|
| 65 |
+
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
|
| 66 |
+
# Assuming that encodeListknown is defined and populated in your code
|
| 67 |
+
matches = face_recognition.compare_faces(encodeListknown, encodeFace)
|
| 68 |
+
faceDis = face_recognition.face_distance(encodeListknown, encodeFace)
|
| 69 |
+
matchIndex = np.argmin(faceDis)
|
| 70 |
+
|
| 71 |
+
if matches[matchIndex]:
|
| 72 |
+
name = classnames[matchIndex].upper()
|
| 73 |
+
#st.write (name)
|
| 74 |
+
# show the name on image to user
|
| 75 |
+
y1, x2, y2, x1 = faceLoc
|
| 76 |
+
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
|
| 77 |
+
cv2.rectangle(image , (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 78 |
+
cv2.rectangle(image , (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
|
| 79 |
+
cv2.putText(image , name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
|
| 80 |
+
|
| 81 |
+
########## update website
|
| 82 |
+
# using Get Method
|
| 83 |
+
# Construct the URL
|
| 84 |
+
url = "https://fc11.glitch.me/submit?email=pm&message=faceReco&name="
|
| 85 |
+
url = url + name
|
| 86 |
+
st.write("Constructed URL:", url)
|
| 87 |
+
|
| 88 |
+
# Try to send the request and handle errors
|
| 89 |
+
# try:
|
| 90 |
+
# res = urllib.request.urlopen(url)
|
| 91 |
+
# response = requests.post(url)
|
| 92 |
+
# st.write("Data updated successfully.")
|
| 93 |
+
# except urllib.error.URLError as e:
|
| 94 |
+
# st.error(f"Failed to open URL: {e}")
|
| 95 |
+
# except requests.exceptions.RequestException as e:
|
| 96 |
+
# st.error(f"Request failed: {e}")
|
| 97 |
+
|
| 98 |
+
url = "https://fc11.glitch.me/submit?email=pm&message=faceReco&name="
|
| 99 |
+
url = url + name
|
| 100 |
+
st.write(url)
|
| 101 |
+
res = urllib.request.urlopen(url)
|
| 102 |
+
response = requests.post(url )
|
| 103 |
+
|
| 104 |
+
# # using post method
|
| 105 |
+
# url = "https://aimljul24f.glitch.me/submit?email=pm&message=faceReco&name="
|
| 106 |
+
# url1 = "/save"
|
| 107 |
+
# data = {'rollno': '99','name': name, 'email': 'p@g.com','pwd': '**' }
|
| 108 |
+
# response = requests.post(url +url1 , data=data)
|
| 109 |
+
# if response.status_code == 200:
|
| 110 |
+
# st.success("Data updated on: " + "https://aimljul24f.glitch.me/")
|
| 111 |
+
# else:
|
| 112 |
+
# st.warning("Data not updated")
|
| 113 |
+
########### end update website
|
| 114 |
+
faceMatchedflag = 1
|
| 115 |
+
|
| 116 |
+
st.image(image , use_column_width=True, output_format="PNG")
|
| 117 |
+
|
| 118 |
+
if(faceMatchedflag == 0) :
|
| 119 |
+
st.warning("No faces detected in the image.")
|