Vikas01's picture
Update app.py
3657998
raw
history blame
3.35 kB
import face_recognition
import cv2
import numpy as np
import csv
from datetime import datetime
###########################
from flask import Flask,render_template
from flask_socketio import SocketIO,emit
import base64
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socket = SocketIO(app,async_mode="eventlet")
###################################
video_capture=cv2.VideoCapture(1)
if not video_capture.isOpened():
print("Failed to open the video capture.")
exit()
sir_image=face_recognition.load_image_file("photos/sir.jpeg")
sir_encoding=face_recognition.face_encodings(sir_image)[0]
vikas_image=face_recognition.load_image_file("photos/vikas.jpg")
vikas_encoding=face_recognition.face_encodings(vikas_image)[0]
known_face_encoding=[sir_encoding,vikas_encoding]
known_faces_names=["Sarwan Sir","Vikas"]
students=known_faces_names.copy()
face_locations=[]
face_encodings=[]
face_names=[]
s=True
now=datetime.now()
current_date=now.strftime("%Y-%m-%d")
f=open(current_date+'.csv','w+',newline='')
lnwriter=csv.writer(f)
############################
def base64_to_image(base64_string):
# Extract the base64 encoded binary data from the input string
base64_data = base64_string.split(",")[1]
# Decode the base64 data to bytes
image_bytes = base64.b64decode(base64_data)
# Convert the bytes to numpy array
image_array = np.frombuffer(image_bytes, dtype=np.uint8)
# Decode the numpy array as an image using OpenCV
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
return image
@socket.on("connect")
def test_connect():
print("Connected")
emit("my response", {"data": "Connected"})
@socket.on("image")
def receive_image(image):
return render_template("index.html" , myimage = image , cname = class_name )
while True:
# _,frame=camera.read()
frame=base64_to_image(image)
small_frame=cv2.resize(frame,(0,0),fx=0.25,fy=0.25)
rgb_small_frame=small_frame[:,:,::-1]
if s:
face_locations=face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names=[]
for face_encoding in face_encodings:
matches=face_recognition.compare_faces(known_face_encoding,face_encoding)
name=""
face_distance=face_recognition.face_distance(known_face_encoding,face_encoding)
best_match_index=np.argmin(face_distance)
if matches[best_match_index]:
name=known_faces_names[best_match_index]
face_names.append(name)
if name in known_faces_names:
if name in students:
students.remove(name)
print(students)
current_time=now.strftime("%H-%M-%S")
lnwriter.writerow([name,current_time,"Present"])
cv2.imshow("attendence system",frame)
if cv2.waitKey(1) & 0xFF==ord('q'):
break
f.close()
#######################################
@app.route("/")
def home():
return render_template("index.html")
if __name__ == '__main__':
# app.run(debug=True)
socket.run(app,host="0.0.0.0", port=7860)
#######################################