Spaces:
Runtime error
Runtime error
added pages folder to run mutipage apps
Browse files- pages/1_📷️_Live_Stream.py +69 -0
- pages/2_ ⬆️_Upload_Video.py +121 -0
pages/1_📷️_Live_Stream.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import av
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from streamlit_webrtc import VideoHTMLAttributes, webrtc_streamer
|
| 6 |
+
from aiortc.contrib.media import MediaRecorder
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
BASE_DIR = os.path.abspath(os.path.join(__file__, '../../'))
|
| 10 |
+
sys.path.append(BASE_DIR)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
from utils import get_mediapipe_pose
|
| 14 |
+
from process_frame import ProcessFrame
|
| 15 |
+
from thresholds import get_thresholds_beginner, get_thresholds_pro
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
st.title('AI Fitness Trainer: Squats Analysis')
|
| 19 |
+
|
| 20 |
+
mode = st.radio('Select Mode', ['Beginner', 'Pro'], horizontal=True)
|
| 21 |
+
|
| 22 |
+
thresholds = None
|
| 23 |
+
|
| 24 |
+
if mode == 'Beginner':
|
| 25 |
+
thresholds = get_thresholds_beginner()
|
| 26 |
+
|
| 27 |
+
elif mode == 'Pro':
|
| 28 |
+
thresholds = get_thresholds_pro()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
live_process_frame = ProcessFrame(thresholds=thresholds, flip_frame=True)
|
| 32 |
+
# Initialize face mesh solution
|
| 33 |
+
pose = get_mediapipe_pose()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
output_video_file = f'output_live.flv'
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def video_frame_callback(frame: av.VideoFrame):
|
| 40 |
+
frame = frame.to_ndarray(format="rgb24") # Decode and get RGB frame
|
| 41 |
+
frame, _ = live_process_frame.process(frame, pose) # Process frame
|
| 42 |
+
return av.VideoFrame.from_ndarray(frame, format="rgb24") # Encode and return BGR frame
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def out_recorder_factory() -> MediaRecorder:
|
| 46 |
+
return MediaRecorder(output_video_file)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
ctx = webrtc_streamer(
|
| 50 |
+
key="Squats-pose-analysis",
|
| 51 |
+
video_frame_callback=video_frame_callback,
|
| 52 |
+
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}, # Add this config
|
| 53 |
+
media_stream_constraints={"video": {"width": {"min":800, "max": 1280}}, "audio": False},
|
| 54 |
+
video_html_attrs=VideoHTMLAttributes(autoPlay=True, controls=False, muted=False),
|
| 55 |
+
out_recorder_factory=out_recorder_factory
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if os.path.exists(output_video_file):
|
| 60 |
+
with open(output_video_file, 'rb') as op_vid:
|
| 61 |
+
st.download_button('Download Video', data = op_vid, file_name='output_live.flv')
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
pages/2_ ⬆️_Upload_Video.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import av
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import cv2
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
BASE_DIR = os.path.abspath(os.path.join(__file__, '../../'))
|
| 10 |
+
sys.path.append(BASE_DIR)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
from utils import get_mediapipe_pose
|
| 14 |
+
from process_frame import ProcessFrame
|
| 15 |
+
from thresholds import get_thresholds_beginner, get_thresholds_pro
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
st.title('AI Fitness Trainer: Squats Analysis')
|
| 20 |
+
|
| 21 |
+
mode = st.radio('Select Mode', ['Beginner', 'Pro'], horizontal=True)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
thresholds = None
|
| 26 |
+
|
| 27 |
+
if mode == 'Beginner':
|
| 28 |
+
thresholds = get_thresholds_beginner()
|
| 29 |
+
|
| 30 |
+
elif mode == 'Pro':
|
| 31 |
+
thresholds = get_thresholds_pro()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
upload_process_frame = ProcessFrame(thresholds=thresholds)
|
| 36 |
+
|
| 37 |
+
# Initialize face mesh solution
|
| 38 |
+
pose = get_mediapipe_pose()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
output_video_file = f'output_recorded.mp4'
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
with st.form('Upload', clear_on_submit=True):
|
| 45 |
+
up_file = st.file_uploader("Upload a Video", ['mp4','mov', 'avi'])
|
| 46 |
+
uploaded = st.form_submit_button("Upload")
|
| 47 |
+
|
| 48 |
+
stframe = st.empty()
|
| 49 |
+
|
| 50 |
+
ip_vid_str = '<p style="font-family:Helvetica; font-weight: bold; font-size: 16px;">Input Video</p>'
|
| 51 |
+
warning_str = '<p style="font-family:Helvetica; font-weight: bold; color: Red; font-size: 17px;">Please Upload a Video first!!!</p>'
|
| 52 |
+
|
| 53 |
+
warn = st.empty()
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
download_button = st.empty()
|
| 57 |
+
|
| 58 |
+
if up_file and uploaded:
|
| 59 |
+
|
| 60 |
+
download_button.empty()
|
| 61 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
warn.empty()
|
| 65 |
+
tfile.write(up_file.read())
|
| 66 |
+
|
| 67 |
+
vf = cv2.VideoCapture(tfile.name)
|
| 68 |
+
|
| 69 |
+
# --------------------- Write the processed video frame. --------------------
|
| 70 |
+
fps = int(vf.get(cv2.CAP_PROP_FPS))
|
| 71 |
+
width = int(vf.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 72 |
+
height = int(vf.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 73 |
+
frame_size = (width, height)
|
| 74 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 75 |
+
video_output = cv2.VideoWriter(output_video_file, fourcc, fps, frame_size)
|
| 76 |
+
# -----------------------------------------------------------------------------
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
txt = st.sidebar.markdown(ip_vid_str, unsafe_allow_html=True)
|
| 80 |
+
ip_video = st.sidebar.video(tfile.name)
|
| 81 |
+
|
| 82 |
+
while vf.isOpened():
|
| 83 |
+
ret, frame = vf.read()
|
| 84 |
+
if not ret:
|
| 85 |
+
break
|
| 86 |
+
|
| 87 |
+
# convert frame from BGR to RGB before processing it.
|
| 88 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 89 |
+
out_frame, _ = upload_process_frame.process(frame, pose)
|
| 90 |
+
stframe.image(out_frame)
|
| 91 |
+
video_output.write(out_frame[...,::-1])
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
vf.release()
|
| 95 |
+
video_output.release()
|
| 96 |
+
stframe.empty()
|
| 97 |
+
ip_video.empty()
|
| 98 |
+
txt.empty()
|
| 99 |
+
tfile.close()
|
| 100 |
+
|
| 101 |
+
except AttributeError:
|
| 102 |
+
warn.markdown(warning_str, unsafe_allow_html=True)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
if os.path.exists(output_video_file):
|
| 107 |
+
with open(output_video_file, 'rb') as op_vid:
|
| 108 |
+
download_button.download_button('Download Video', data = op_vid, file_name='output_recorded.mp4')
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|