-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
74 lines (50 loc) · 1.77 KB
/
app.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
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 28 19:19:44 2021
@author: Sonu
"""
import time
import cv2
from flask import Flask, render_template, Response
import mediapipe as mp
app = Flask(__name__)
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html')
def gen():
previous_time = 0
# creating our model to draw landmarks
mpDraw = mp.solutions.drawing_utils
# creating our model to detected our pose
my_pose = mp.solutions.pose
pose = my_pose.Pose()
"""Video streaming generator function."""
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
# converting image to RGB from BGR cuz mediapipe only work on RGB
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = pose.process(imgRGB)
# print(result.pose_landmarks)
if result.pose_landmarks:
mpDraw.draw_landmarks(img, result.pose_landmarks, my_pose.POSE_CONNECTIONS)
# checking video frame rate
current_time = time.time()
fps = 1 / (current_time - previous_time)
previous_time = current_time
# Writing FrameRate on video
cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
#cv2.imshow("Pose detection", img)
frame = cv2.imencode('.jpg', img)[1].tobytes()
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
key = cv2.waitKey(20)
if key == 27:
break
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__=="__main__":
app.run(debug=True)