-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (46 loc) · 1.61 KB
/
app.py
File metadata and controls
57 lines (46 loc) · 1.61 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
from flask import Flask, render_template, Response
from flask_socketio import SocketIO, emit
import cv2
import HandTrackingModule as htm
app = Flask(__name__)
socketio = SocketIO(app)
detector = htm.HandDetector(max_num_hands=1)
cap = cv2.VideoCapture(0)
def gen_frames():
while True:
success, frame = cap.read()
if not success:
break
else:
img = detector.findHands(frame)
lmList, bbox = detector.findPosition(img)
if lmList:
x1, y1 = lmList[8][1:3]
x2, y2 = lmList[12][1:3]
fingers = detector.fingersUp()
socketio.emit('gesture_data', {'x': x1, 'y': y1, 'fingers': fingers})
ret, buffer = cv2.imencode('.jpg', img)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + 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')
@socketio.on('frame')
def handle_frame(data):
x, y = data['x'], data['y']
emit('response', {'action': 'move', 'x': x + 50, 'y': y + 50})
@socketio.on('webcam_status')
def handle_webcam_status(data):
if data['status'] == 'started':
print("Webcam has started.")
elif data['status'] == 'stopped':
print("Webcam has stopped.")
if __name__ == '__main__':
try:
socketio.run(app, debug=True)
finally:
cap.release()