Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import face_recognition
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
import requests
|
7 |
+
import os
|
8 |
+
|
9 |
+
st.title("AIMLJan24 - Face Recognition")
|
10 |
+
|
11 |
+
# create list of encoding of all images in photos folder
|
12 |
+
# Load images for face recognition
|
13 |
+
Images = [] # List to store Images
|
14 |
+
classnames = [] # List to store classnames
|
15 |
+
directory = "photos"
|
16 |
+
myList = os.listdir(directory)
|
17 |
+
|
18 |
+
st.write("Photographs found in folder : ")
|
19 |
+
for cls in myList:
|
20 |
+
if os.path.splitext(cls)[1] in [".jpg", ".jpeg"]:
|
21 |
+
img_path = os.path.join(directory, cls)
|
22 |
+
curImg = cv2.imread(img_path)
|
23 |
+
Images.append(curImg)
|
24 |
+
st.write(os.path.splitext(cls)[0])
|
25 |
+
classnames.append(os.path.splitext(cls)[0])
|
26 |
+
|
27 |
+
# Load images for face recognition
|
28 |
+
encodeListknown = [face_recognition.face_encodings(img)[0] for img in Images]
|
29 |
+
|
30 |
+
# camera to take photo of user in question
|
31 |
+
file_name = st.camera_input("Take a picture") #st.file_uploader("Upload image ")
|
32 |
+
|
33 |
+
# Function to update Aadhaar data
|
34 |
+
def update_data(name):
|
35 |
+
# url = "https://attendanceviaface.000webhostapp.com"
|
36 |
+
# url1 = "/update.php"
|
37 |
+
# data = {'name': name, 'aadhaar': '998877'}
|
38 |
+
# response = requests.post(url + url1, data=data)
|
39 |
+
|
40 |
+
url = "https://aimljan24f1.glitch.me/adduserdata" #?rollno=222&name="+name
|
41 |
+
data = {'rollno':'222','name': name}
|
42 |
+
response = requests.post(url , data=data )
|
43 |
+
|
44 |
+
if response.status_code == 200:
|
45 |
+
st.success("Data updated on: " + url)
|
46 |
+
else:
|
47 |
+
st.warning("Data not updated")
|
48 |
+
|
49 |
+
if file_name is not None:
|
50 |
+
col1, col2 = st.columns(2)
|
51 |
+
|
52 |
+
test_image = Image.open(file_name)
|
53 |
+
image = np.asarray(test_image)
|
54 |
+
|
55 |
+
imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
|
56 |
+
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
|
57 |
+
facesCurFrame = face_recognition.face_locations(imgS)
|
58 |
+
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
|
59 |
+
|
60 |
+
name = "Unknown" # Default name for unknown faces
|
61 |
+
match_found = False # Flag to track if a match is found
|
62 |
+
|
63 |
+
# Checking if faces are detected
|
64 |
+
if len(encodesCurFrame) > 0:
|
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 |
+
match_found = True # Set the flag to True
|
74 |
+
|
75 |
+
y1, x2, y2, x1 = faceLoc
|
76 |
+
y1, x2, y2, x1 = (y1 * 4), (x2 * 4), (y2 * 4) ,(x1 * 4)
|
77 |
+
|
78 |
+
# Make a copy of the image array before drawing on it
|
79 |
+
image_copy = image.copy()
|
80 |
+
|
81 |
+
cv2.rectangle(image_copy, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
82 |
+
cv2.rectangle(image_copy, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
|
83 |
+
cv2.putText(image_copy, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
|
84 |
+
|
85 |
+
# update the database
|
86 |
+
update_data(name)
|
87 |
+
|
88 |
+
st.image(image_copy, use_column_width=True, output_format="PNG")
|
89 |
+
else:
|
90 |
+
st.warning("No faces detected in the image. Face recognition failed.")
|
91 |
+
|
92 |
+
# image = Image.open(file_name)
|
93 |
+
# col1.image(image, use_column_width=True)
|
94 |
+
|
95 |
+
# pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
96 |
+
|
97 |
+
# st.title("AIMLJan24 First App on Hugging face - Hot Dog? Or Not?")
|
98 |
+
|
99 |
+
# file_name = st.file_uploader("Upload the test image to find is this hot dog ! ")
|
100 |
+
|
101 |
+
# if file_name is not None:
|
102 |
+
# col1, col2 = st.columns(2)
|
103 |
+
|
104 |
+
# image = Image.open(file_name)
|
105 |
+
# col1.image(image, use_column_width=True)
|
106 |
+
# predictions = pipeline(image)
|
107 |
+
|
108 |
+
# col2.header("Probabilities")
|
109 |
+
# for p in predictions:
|
110 |
+
# col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")
|
111 |
+
|
112 |
+
|
113 |
+
# # my first app
|
114 |
+
# import streamlit as st
|
115 |
+
|
116 |
+
# x = st.slider('Select a value')
|
117 |
+
# st.write(x, 'squared is', x * x)
|