Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import face_recognition
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
import requests
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
st.title("AIMLJan24 - Face Recognition")
|
| 11 |
+
|
| 12 |
+
# create list of encoding of all images in photos folder
|
| 13 |
+
# Load images for face recognition
|
| 14 |
+
Images = [] # List to store Images
|
| 15 |
+
classnames = [] # List to store classnames
|
| 16 |
+
directory = "photos"
|
| 17 |
+
myList = os.listdir(directory)
|
| 18 |
+
|
| 19 |
+
st.write("Photographs found in folder : ")
|
| 20 |
+
for cls in myList:
|
| 21 |
+
if os.path.splitext(cls)[1] in [".jpg", ".jpeg"]:
|
| 22 |
+
img_path = os.path.join(directory, cls)
|
| 23 |
+
curImg = cv2.imread(img_path)
|
| 24 |
+
Images.append(curImg)
|
| 25 |
+
st.write(os.path.splitext(cls)[0])
|
| 26 |
+
classnames.append(os.path.splitext(cls)[0])
|
| 27 |
+
|
| 28 |
+
# Load images for face recognition
|
| 29 |
+
encodeListknown = [face_recognition.face_encodings(img)[0] for img in Images]
|
| 30 |
+
|
| 31 |
+
# camera to take photo of user in question
|
| 32 |
+
file_name = st.camera_input("Take a picture") #st.file_uploader("Upload image ")
|
| 33 |
+
|
| 34 |
+
# Function to update Aadhaar data
|
| 35 |
+
def update_data(name):
|
| 36 |
+
url = "https://aimljan24f1.glitch.me/adduserdata" #?rollno=222&name="+name
|
| 37 |
+
data = {'rollno':'222','name': name}
|
| 38 |
+
response = requests.post(url , data=data )
|
| 39 |
+
|
| 40 |
+
if response.status_code == 200:
|
| 41 |
+
st.success("Data updated on: " + url)
|
| 42 |
+
else:
|
| 43 |
+
st.warning("Data not updated")
|
| 44 |
+
|
| 45 |
+
if file_name is not None:
|
| 46 |
+
col1, col2 = st.columns(2)
|
| 47 |
+
|
| 48 |
+
test_image = Image.open(file_name)
|
| 49 |
+
image = np.asarray(test_image)
|
| 50 |
+
|
| 51 |
+
imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
|
| 52 |
+
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
|
| 53 |
+
facesCurFrame = face_recognition.face_locations(imgS)
|
| 54 |
+
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
|
| 55 |
+
|
| 56 |
+
name = "Unknown" # Default name for unknown faces
|
| 57 |
+
match_found = False # Flag to track if a match is found
|
| 58 |
+
|
| 59 |
+
# Checking if faces are detected
|
| 60 |
+
if len(encodesCurFrame) > 0:
|
| 61 |
+
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
|
| 62 |
+
# Assuming that encodeListknown is defined and populated in your code
|
| 63 |
+
matches = face_recognition.compare_faces(encodeListknown, encodeFace)
|
| 64 |
+
faceDis = face_recognition.face_distance(encodeListknown, encodeFace)
|
| 65 |
+
matchIndex = np.argmin(faceDis)
|
| 66 |
+
|
| 67 |
+
if matches[matchIndex]:
|
| 68 |
+
name = classnames[matchIndex].upper()
|
| 69 |
+
match_found = True # Set the flag to True
|
| 70 |
+
|
| 71 |
+
y1, x2, y2, x1 = faceLoc
|
| 72 |
+
y1, x2, y2, x1 = (y1 * 4), (x2 * 4), (y2 * 4) ,(x1 * 4)
|
| 73 |
+
|
| 74 |
+
# Make a copy of the image array before drawing on it
|
| 75 |
+
image_copy = image.copy()
|
| 76 |
+
|
| 77 |
+
cv2.rectangle(image_copy, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 78 |
+
cv2.rectangle(image_copy, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
|
| 79 |
+
cv2.putText(image_copy, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
|
| 80 |
+
|
| 81 |
+
# update the database
|
| 82 |
+
update_data(name)
|
| 83 |
+
|
| 84 |
+
st.image(image_copy, use_column_width=True, output_format="PNG")
|
| 85 |
+
else:
|
| 86 |
+
st.warning("No faces detected in the image. Face recognition failed.")
|
| 87 |
+
|
| 88 |
+
|