-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
142 lines (113 loc) · 4.51 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from flask import Flask, render_template, Response
import cv2
import face_recognition
import numpy as np
import boto3
import os
from datetime import datetime
from urllib import request
app=Flask(__name__)
# Archivo de prueba local de reconocimiento facial
# Credenciales de Amazon
# Requisitos: Tener un bucket disponible, en este caso el bucket es "unida"
ACCESS_KEY = "..."
SECRET_KEY = "..."
# Cliente s3 y listado de objetos del bucket
client = boto3.client(
"s3",
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
)
response = client.list_objects_v2(Bucket="unida")
lista = response["Contents"]
# Obtener solo el "key" de los objects del bucket seleccionado y descargarlo en una ruta local ./img
for fichero in lista:
classNames2 = fichero["Key"]
# print(f"classNames2 :{classNames2} \n")
client.download_file('unida', f'{classNames2}', f'./img/{classNames2}')
# Listar los objetos descargados y obtener un array de cada objeto además de su nombre
path = './img'
images = []
classNames = []
myList = os.listdir(path)
print(myList)
for cl in myList:
# print(cl)
curImg = cv2.imread(f'{path}/{cl}')
# print(curImg)
images.append(curImg)
classNames.append(os.path.splitext(cl)[0])
# print(classNames)
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# print(f"img: {img} \n")
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
# print(encodeList)
return encodeList
def markAttendance(name):
with open('./Attendance.csv', 'r+') as f:
myDataList = f.readlines()
nameList = []
for line in myDataList:
entry = line.split(',')
nameList.append(entry[0])
if name not in nameList:
now = datetime.now()
dtString = now.strftime('%H:%M:%S')
f.writelines(f'\n{name}, {dtString}')
encodeListKnown = findEncodings(images)
print('Encoding Complete')
def gen_frames():
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
imgS = cv2.resize(img, (0, 0), fx=0.25, fy=0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facesCurFrame = face_recognition.face_locations(imgS)
encodeCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
face_names = []
for encodeFace in encodeCurFrame:
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
name = 'Unknown'
faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)
# print(faceDis)
matchIndex = np.argmin(faceDis)
# if matches[matchIndex]:
# name = classNames[matchIndex].upper()
# print(name)
# y1,x2,y2,x1 = faceLoc
# x1,y1,x2,y2 = x1*4, y1*4, x2*4, y2*4
# cv2.rectangle(img, (x1,y1), (x2,y2), (0, 255, 0), 2)
# cv2.rectangle(img, (x1, y2-35), (x2,y2), (0,255,0), cv2.FILLED)
# cv2.putText(img, name, (x1+6, y2-6), cv2.FONT_HERSHEY_COMPLEX, 1, (255,255,255),2)
# markAttendance(name)
if matches[matchIndex] :
name = classNames[matchIndex]
face_names.append(name)
# Display the results
for (top, right, bottom, left), name in zip(facesCurFrame, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw a box around the face
cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
cv2.rectangle(img, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(img, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
ret, buffer = cv2.imencode('.jpg', img)
img = buffer.tobytes()
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + img + b'\r\n')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__=='__main__':
app.run(debug=True)