-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenpose.py
65 lines (49 loc) · 1.74 KB
/
openpose.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
import cv2
import mediapipe as mp
from flask import Flask, render_template, Response
from flask_socketio import SocketIO
from threading import Thread
import datetime, time
import os, sys
import numpy as np
import atexit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'istilldontknowwhatthisis'
socketio = SocketIO(app)
cap = cv2.VideoCapture(0)
#cap.set(cv2.CAP_PROP_FPS, 20)
mpPose = mp.solutions.pose
pose = mpPose.Pose()
mpDraw = mp.solutions.drawing_utils
@socketio.on('connect')
def handle_connect():
print('connected')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/video_container')
def video_container():
return Response(process_frames(),mimetype='multipart/x-mixed-replace; boundary=frame')
def process_frames():
while True:
success, frame = cap.read()
if success:
# Convert frame to RGB format (MediaPipe requires RGB input)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Detect pose landmarks
results = pose.process(rgb_frame)
# Draw pose landmarks on the frame
if results.pose_landmarks:
mpDraw.draw_landmarks(frame, results.pose_landmarks, mpPose.POSE_CONNECTIONS)
# Convert the frame to JPEG format to send over the socket
_, buffer = cv2.imencode('.jpg', frame)
frame_bytes = buffer.tobytes()
#socketio.emit('video_frame', frame_bytes)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
#socketio.emit('video_frame', frame_bytes)
if __name__ == '__main__':
thread = Thread(target=process_frames)
thread.daemon = True
thread.start()
socketio.run(app)