-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamera.py
68 lines (54 loc) · 2.2 KB
/
camera.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
import cv2
import mediapipe as mp
import numpy as np
import pickle
# Load the pre-trained model
model_dict = pickle.load(open('model.p', 'rb'))
model = model_dict['model']
# Initialize MediaPipe Hands
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(min_detection_confidence=0.8, min_tracking_confidence=0.8)
mp_drawing = mp.solutions.drawing_utils
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
self.stopped = False # Flag to control video streaming
def __del__(self):
self.video.release()
def stop_stream(self):
self.stopped = True
def get_frame(self):
while not self.stopped:
ret, frame = self.video.read()
if not ret:
break
data_aux = []
x_ = []
y_ = []
H, W, _ = frame.shape
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_rgb = cv2.flip(frame_rgb, 1)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame_rgb,
hand_landmarks,
mp_hands.HAND_CONNECTIONS
)
for landmark in hand_landmarks.landmark:
x_.append(landmark.x)
y_.append(landmark.y)
data_aux.append(landmark.x)
data_aux.append(landmark.y)
x1 = int(min(x_) * W) - 10
y1 = int(min(y_) * H) - 10
x2 = int(max(x_) * W) - 10
y2 = int(max(y_) * H) - 10
prediction = model.predict([np.array(data_aux)[:42]])[0]
cv2.rectangle(frame_rgb, (x1, y1 - 10), (x2, y2), (255, 99, 173), 6)
cv2.putText(frame_rgb, prediction, (x1, y1), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
ret, jpeg = cv2.imencode('.jpg', frame_rgb)
frame = jpeg.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')