LovnishVerma
commited on
Commit
•
9befc9d
1
Parent(s):
4164ff5
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.set_page_config(
|
12 |
+
page_title="Attendance System Using Face Recognition",
|
13 |
+
page_icon="📷",
|
14 |
+
layout="centered",
|
15 |
+
initial_sidebar_state="collapsed"
|
16 |
+
)
|
17 |
+
st.title("Attendance System Using Face Recognition 📷")
|
18 |
+
st.markdown("This app recognizes faces in an image and updates attendance records with current timestamp & Location.")
|
19 |
+
|
20 |
+
# Load images for face recognition
|
21 |
+
Images = []
|
22 |
+
classnames = []
|
23 |
+
directory = "photos"
|
24 |
+
|
25 |
+
myList = os.listdir(directory)
|
26 |
+
|
27 |
+
for cls in myList:
|
28 |
+
if os.path.splitext(cls)[1] in [".jpg", ".jpeg"]:
|
29 |
+
img_path = os.path.join(directory, cls)
|
30 |
+
curImg = cv2.imread(img_path)
|
31 |
+
Images.append(curImg)
|
32 |
+
classnames.append(os.path.splitext(cls)[0])
|
33 |
+
|
34 |
+
def findEncodings(Images):
|
35 |
+
encodeList = []
|
36 |
+
for img in Images:
|
37 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
38 |
+
encode = face_recognition.face_encodings(img)[0]
|
39 |
+
encodeList.append(encode)
|
40 |
+
return encodeList
|
41 |
+
|
42 |
+
encodeListknown = findEncodings(Images)
|
43 |
+
|
44 |
+
# Take picture using the camera
|
45 |
+
img_file_buffer = st.camera_input("Take a picture")
|
46 |
+
if img_file_buffer is not None:
|
47 |
+
test_image = Image.open(img_file_buffer)
|
48 |
+
image = np.asarray(test_image)
|
49 |
+
|
50 |
+
imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
|
51 |
+
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
|
52 |
+
facesCurFrame = face_recognition.face_locations(imgS)
|
53 |
+
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
|
54 |
+
|
55 |
+
name = "Unknown" # Default name for unknown faces
|
56 |
+
|
57 |
+
if len(encodesCurFrame) > 0:
|
58 |
+
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
|
59 |
+
matches = face_recognition.compare_faces(encodeListknown, encodeFace)
|
60 |
+
faceDis = face_recognition.face_distance(encodeListknown, encodeFace)
|
61 |
+
matchIndex = np.argmin(faceDis)
|
62 |
+
|
63 |
+
if matches[matchIndex]:
|
64 |
+
name = classnames[matchIndex].upper()
|
65 |
+
|
66 |
+
y1, x2, y2, x1 = faceLoc
|
67 |
+
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
|
68 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
69 |
+
cv2.rectangle(image, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
|
70 |
+
cv2.putText(image, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
|
71 |
+
|
72 |
+
if name != "Unknown":
|
73 |
+
url = "https://mrvishal7705.000webhostapp.com"
|
74 |
+
url1 = "/update.php"
|
75 |
+
data1 = {'name': name}
|
76 |
+
response = requests.post(url + url1, data=data1)
|
77 |
+
|
78 |
+
if response.status_code == 200:
|
79 |
+
st.success("Data updated on: " + url)
|
80 |
+
else:
|
81 |
+
st.warning("Data not updated")
|
82 |
+
|
83 |
+
# Apply styling with CSS
|
84 |
+
st.markdown('<style>img { animation: pulse 2s infinite; }</style>', unsafe_allow_html=True)
|
85 |
+
st.image(image, use_column_width=True, output_format="PNG")
|
86 |
+
|
87 |
+
if name == "Unknown":
|
88 |
+
st.info("Face not detected. Please try again.")
|