-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (51 loc) · 2.22 KB
/
Copy pathapp.py
File metadata and controls
65 lines (51 loc) · 2.22 KB
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
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import cv2
import face_recognition
app = Flask(__name__)
socketio = SocketIO(app)
def get_face_encodings(frame):
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
return face_locations, face_encodings
# Load known faces and labels
known_faces = []
known_labels = ["orang", "ojan", "obam", "arya"]
for i in range(1, 5):
known_image_path = f"{i}.jpg" # Replace with the correct path
known_image = cv2.imread(known_image_path)
_, known_encoding = get_face_encodings(known_image)
known_faces.append(known_encoding[0])
# Open a connection to the webcam (you may need to change the index based on your system)
video_capture = cv2.VideoCapture(1)
@socketio.on('connect')
def handle_connect():
print('Client connected')
emit('response', {'data': 'Connected'})
@socketio.on('disconnect')
def handle_disconnect():
print('Client disconnected')
@socketio.on('request_prediction')
def process_frames():
while True:
ret, frame = video_capture.read()
# Find face locations and face encodings in the current frame
unknown_locations, unknown_encodings = get_face_encodings(frame)
# Iterate through each unknown face and check if it matches any known face
for unknown_face_location, unknown_face_encoding in zip(unknown_locations, unknown_encodings):
matches = face_recognition.compare_faces(known_faces, unknown_face_encoding)
name = "Unknown"
for i, match in enumerate(matches):
if match:
name = known_labels[i]
break
# Emit the processed frame and name to the connected clients
_, buffer = cv2.imencode('.jpg', frame)
frame_bytes = buffer.tobytes()
socketio.emit('prediction', {'image': frame_bytes, 'name': name, 'confidence': 100})
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
socketio.run(app, debug=True)