Spaces:
Runtime error
Runtime error
Upload facereco.py
Browse files- facereco.py +107 -0
facereco.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import face_recognition
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
|
| 11 |
+
st.title('Face RECOGNITION')
|
| 12 |
+
|
| 13 |
+
index = st.sidebar.selectbox(
|
| 14 |
+
'Toma lista',
|
| 15 |
+
(0, 1, 2)
|
| 16 |
+
)
|
| 17 |
+
lista = ["/Users/hectorgonzalez/Documents/CLOUD/streamlit/Video/Josue.mp4",
|
| 18 |
+
"/Users/hectorgonzalez/Documents/CLOUD/streamlit/Video/rudy.mp4", "/Users/hectorgonzalez/Documents/CLOUD/streamlit/Video/video.mp4"]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
st.write(f'You selected: {lista[index]}')
|
| 22 |
+
|
| 23 |
+
path = "ImagesAttendance"
|
| 24 |
+
images = []
|
| 25 |
+
classNames = []
|
| 26 |
+
myList = os.listdir(path)
|
| 27 |
+
print(myList)
|
| 28 |
+
for cl in myList:
|
| 29 |
+
curImg = cv2.imread(f'{path}/{cl}')
|
| 30 |
+
images.append(curImg)
|
| 31 |
+
classNames.append(os.path.splitext(cl)[0])
|
| 32 |
+
print(classNames)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def findEncodings(images):
|
| 36 |
+
encodeList = []
|
| 37 |
+
for img in images:
|
| 38 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 39 |
+
encode = face_recognition.face_encodings(img)[0]
|
| 40 |
+
encodeList.append(encode)
|
| 41 |
+
|
| 42 |
+
return encodeList
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def markAttendance(name):
|
| 46 |
+
with open('Attendance.csv', 'r+') as f:
|
| 47 |
+
myDataList = f.readlines()
|
| 48 |
+
nameList = []
|
| 49 |
+
for line in myDataList:
|
| 50 |
+
entry = line.split(',')
|
| 51 |
+
nameList.append(entry[0])
|
| 52 |
+
if name not in nameList:
|
| 53 |
+
now = datetime.now()
|
| 54 |
+
dtString = now.strftime('%H:%M:%S')
|
| 55 |
+
f.writelines(f'\n{name},{dtString}, {now}')
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
encodeListKnown = findEncodings(images)
|
| 59 |
+
print('Encoding Complete')
|
| 60 |
+
|
| 61 |
+
# Videos sections
|
| 62 |
+
# Rudys one /Users/hectorgonzalez/Documents/CLOUD/streamlit/Video/vid.mp4
|
| 63 |
+
|
| 64 |
+
videoLoaded = (
|
| 65 |
+
lista[index])
|
| 66 |
+
|
| 67 |
+
video_file = open(
|
| 68 |
+
videoLoaded, 'rb')
|
| 69 |
+
video_bytes = video_file.read()
|
| 70 |
+
st.video(video_bytes)
|
| 71 |
+
|
| 72 |
+
cap = cv2.VideoCapture(videoLoaded)
|
| 73 |
+
|
| 74 |
+
while True:
|
| 75 |
+
success, img = cap.read()
|
| 76 |
+
if success == False:
|
| 77 |
+
print("No image")
|
| 78 |
+
break
|
| 79 |
+
|
| 80 |
+
imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
|
| 81 |
+
#imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2GRAY)
|
| 82 |
+
|
| 83 |
+
facesCurFrame = face_recognition.face_locations(imgS)
|
| 84 |
+
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
|
| 85 |
+
|
| 86 |
+
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
|
| 87 |
+
matches = face_recognition.compare_faces(
|
| 88 |
+
encodeListKnown, encodeFace)
|
| 89 |
+
faceDis = face_recognition.face_distance(
|
| 90 |
+
encodeListKnown, encodeFace)
|
| 91 |
+
matchIndex = np.argmin(faceDis)
|
| 92 |
+
|
| 93 |
+
if matches[matchIndex]:
|
| 94 |
+
name = classNames[matchIndex].upper()
|
| 95 |
+
y1, x2, y2, x1 = faceLoc
|
| 96 |
+
y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4
|
| 97 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 98 |
+
cv2.rectangle(img, (x1, y2-35), (x2, y2), (0, 255, 0), cv2.FILLED)
|
| 99 |
+
cv2.putText(img, name, (x1+6, y2-6),
|
| 100 |
+
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
|
| 101 |
+
markAttendance(name)
|
| 102 |
+
print(name)
|
| 103 |
+
st.error(f"Lista de alumnos {classNames}", icon="🚨")
|
| 104 |
+
st.success(name, icon="✅")
|
| 105 |
+
|
| 106 |
+
cv2.imshow('Webcam', img)
|
| 107 |
+
cv2.waitKey()
|