Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import av
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
import streamlit as st
|
5 |
+
import cv2
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
dir = os.path.abspath(os.path.join(__file__, '../../'))
|
9 |
+
sys.path.append(dir)
|
10 |
+
|
11 |
+
from utils import get_mediapipe_pose
|
12 |
+
from process_frame import ProcessFrame
|
13 |
+
from thresholds import get_thresholds_beginner, get_thresholds_pro
|
14 |
+
|
15 |
+
st.title('Exercise tracking Demo')
|
16 |
+
|
17 |
+
diffuculty = st.radio('Select Mode', ['Beginners Squat', 'Pro Squat', 'Push up'], horizontal=True)
|
18 |
+
|
19 |
+
thresholds = None
|
20 |
+
|
21 |
+
if diffuculty == 'Beginners':
|
22 |
+
thresholds = get_thresholds_beginner()
|
23 |
+
|
24 |
+
elif diffuculty == 'Professionals':
|
25 |
+
thresholds = get_thresholds_pro()
|
26 |
+
|
27 |
+
elif difficulty == 'Push up':
|
28 |
+
thresholds = get_pushups()
|
29 |
+
|
30 |
+
ProcessUpload = ProcessFrame(thresholds=thresholds)
|
31 |
+
|
32 |
+
# Initialize face mesh solution
|
33 |
+
pose = get_mediapipe_pose()
|
34 |
+
|
35 |
+
|
36 |
+
download = None
|
37 |
+
|
38 |
+
if 'download' not in st.session_state:
|
39 |
+
st.session_state['download'] = False
|
40 |
+
|
41 |
+
|
42 |
+
Video_Output = f'output_recorded.mp4'
|
43 |
+
|
44 |
+
if os.path.exists(Video_Output):
|
45 |
+
os.remove(Video_Output)
|
46 |
+
|
47 |
+
|
48 |
+
with st.form('Upload', clear_on_submit=True):
|
49 |
+
up_file = st.file_uploader("Upload a Video", ['mp4','mov', 'avi'])
|
50 |
+
uploaded_File = st.form_submit_button("Upload")
|
51 |
+
|
52 |
+
stframe = st.empty()
|
53 |
+
|
54 |
+
ip_vid_str = '<p style="font-family:Helvetica; font-weight: bold; font-size: 16px;">Input Video</p>'
|
55 |
+
warn_str = '<p style="font-family:Helvetica; font-weight: bold; color: Red; font-size: 17px;">Please Upload a Video first!!!</p>'
|
56 |
+
|
57 |
+
warn = st.empty()
|
58 |
+
|
59 |
+
|
60 |
+
download_btn = st.empty()
|
61 |
+
|
62 |
+
if up_file and uploaded_File:
|
63 |
+
|
64 |
+
download_btn.empty()
|
65 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
66 |
+
|
67 |
+
try:
|
68 |
+
warn.empty()
|
69 |
+
tfile.write(up_file.read())
|
70 |
+
|
71 |
+
vf = cv2.VideoCapture(tfile.name)
|
72 |
+
|
73 |
+
# --------------------- Write the processed video frame. --------------------
|
74 |
+
Frames_per_sec = int(vf.get(cv2.cap_FPS))
|
75 |
+
width = int(vf.get(cv2.cap_FrameWidth))
|
76 |
+
height = int(vf.get(cv2.cap_FrameHeight))
|
77 |
+
Frame_Size = (width, height)
|
78 |
+
four_cc = cv2.VideoWriter_four_cc(*'mp4v')
|
79 |
+
video_output = cv2.VideoWriter(Video_Output, four_cc, Frames_per_sec, Frame_Size)
|
80 |
+
# -----------------------------------------------------------------------------
|
81 |
+
|
82 |
+
txt = st.sidebar.markdown(ip_vid_str, unsafe_allow_html=True)
|
83 |
+
ip_vid = st.sidebar.video(tfile.name)
|
84 |
+
|
85 |
+
while vf.isOpened():
|
86 |
+
ret, frame = vf.read()
|
87 |
+
if not ret:
|
88 |
+
break
|
89 |
+
|
90 |
+
# convert frame from BGR to RGB before processing it.
|
91 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
92 |
+
out_frame, _ = ProcessUpload.process(frame, pose)
|
93 |
+
stframe.image(out_frame)
|
94 |
+
video_output.write(out_frame[...,::-1])
|
95 |
+
|
96 |
+
vf.release()
|
97 |
+
video_output.release()
|
98 |
+
stframe.empty()
|
99 |
+
ip_vid.empty()
|
100 |
+
txt.empty()
|
101 |
+
tfile.close()
|
102 |
+
|
103 |
+
except AttributeError:
|
104 |
+
warn.markdown(warn_str, unsafe_allow_html=True)
|
105 |
+
|
106 |
+
if os.path.exists(Video_Output):
|
107 |
+
with open(Video_Output, 'rb') as op_vid:
|
108 |
+
download = download_btn.download_btn('Download Video', data = op_vid, file_name='output.mp4')
|
109 |
+
|
110 |
+
if download:
|
111 |
+
st.session_state['download'] = True
|
112 |
+
|
113 |
+
if os.path.exists(Video_Output) and st.session_state['download']:
|
114 |
+
os.remove(Video_Output)
|
115 |
+
st.session_state['download'] = False
|
116 |
+
download_btn.empty()
|