Spaces:
Runtime error
Runtime error
Commit
·
4d45f61
1
Parent(s):
70dd265
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import requests
|
| 5 |
+
import face_recognition
|
| 6 |
+
import os
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
import streamlit as st
|
| 9 |
+
|
| 10 |
+
# Set page title and description
|
| 11 |
+
st.title("Face Recognition App")
|
| 12 |
+
st.markdown("This app recognizes faces in an image and updates attendance.")
|
| 13 |
+
|
| 14 |
+
# Load images for face recognition
|
| 15 |
+
Images = []
|
| 16 |
+
classnames = []
|
| 17 |
+
directory = "photos"
|
| 18 |
+
|
| 19 |
+
myList = os.listdir(directory)
|
| 20 |
+
|
| 21 |
+
for cls in myList:
|
| 22 |
+
if os.path.splitext(cls)[1] in [".jpg", ".jpeg"]:
|
| 23 |
+
img_path = os.path.join(directory, cls)
|
| 24 |
+
curImg = cv2.imread(img_path)
|
| 25 |
+
Images.append(curImg)
|
| 26 |
+
classnames.append(os.path.splitext(cls)[0])
|
| 27 |
+
|
| 28 |
+
def findEncodings(Images):
|
| 29 |
+
encodeList = []
|
| 30 |
+
for img in Images:
|
| 31 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 32 |
+
encode = face_recognition.face_encodings(img)[0]
|
| 33 |
+
encodeList.append(encode)
|
| 34 |
+
return encodeList
|
| 35 |
+
|
| 36 |
+
encodeListknown = findEncodings(Images)
|
| 37 |
+
|
| 38 |
+
# Take picture using the camera
|
| 39 |
+
img_file_buffer = st.camera_input("Take a picture")
|
| 40 |
+
if img_file_buffer is not None:
|
| 41 |
+
test_image = Image.open(img_file_buffer)
|
| 42 |
+
image = np.asarray(test_image)
|
| 43 |
+
|
| 44 |
+
imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
|
| 45 |
+
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
|
| 46 |
+
facesCurFrame = face_recognition.face_locations(imgS)
|
| 47 |
+
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
|
| 48 |
+
|
| 49 |
+
name = "Unknown" # Default name for unknown faces
|
| 50 |
+
|
| 51 |
+
if len(encodesCurFrame) > 0:
|
| 52 |
+
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
|
| 53 |
+
matches = face_recognition.compare_faces(encodeListknown, encodeFace)
|
| 54 |
+
faceDis = face_recognition.face_distance(encodeListknown, encodeFace)
|
| 55 |
+
matchIndex = np.argmin(faceDis)
|
| 56 |
+
|
| 57 |
+
if matches[matchIndex]:
|
| 58 |
+
name = classnames[matchIndex].upper()
|
| 59 |
+
|
| 60 |
+
y1, x2, y2, x1 = faceLoc
|
| 61 |
+
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
|
| 62 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 63 |
+
cv2.rectangle(image, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
|
| 64 |
+
cv2.putText(image, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
|
| 65 |
+
|
| 66 |
+
if name != "Unknown":
|
| 67 |
+
url = "https://attendanceviaface.000webhostapp.com"
|
| 68 |
+
url1 = "/update.php"
|
| 69 |
+
data1 = {'name': name}
|
| 70 |
+
response = requests.post(url + url1, data=data1)
|
| 71 |
+
|
| 72 |
+
if response.status_code == 200:
|
| 73 |
+
st.success("Data updated on: " + url)
|
| 74 |
+
else:
|
| 75 |
+
st.warning("Data not updated")
|
| 76 |
+
|
| 77 |
+
st.image(image, caption="Detected Face", use_column_width=True)
|
| 78 |
+
|
| 79 |
+
if name == "Unknown":
|
| 80 |
+
st.info("Face not detected. Please try again.")
|