|
import streamlit as st |
|
import cv2 |
|
import tempfile |
|
import numpy as np |
|
from ultralytics import YOLO, solutions |
|
|
|
|
|
model = YOLO("yolov8n-pose.pt") |
|
|
|
|
|
st.title("Workout Monitoring App") |
|
st.write("Upload a video to monitor your ab workout.") |
|
|
|
uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "mov", "avi"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
tfile = tempfile.NamedTemporaryFile(delete=False) |
|
tfile.write(uploaded_file.read()) |
|
tfile.close() |
|
|
|
|
|
cap = cv2.VideoCapture(tfile.name) |
|
assert cap.isOpened(), "Error reading video file" |
|
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS)) |
|
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
|
|
|
|
|
gym_object = solutions.AIGym( |
|
line_thickness=2, |
|
view_img=False, |
|
pose_type="abworkout", |
|
kpts_to_check=[6, 8, 10], |
|
) |
|
|
|
|
|
processed_frames = [] |
|
|
|
|
|
progress_bar = st.progress(0) |
|
frame_count = 0 |
|
|
|
|
|
st.write("Analyzing video... Please wait.") |
|
while cap.isOpened(): |
|
success, im0 = cap.read() |
|
if not success: |
|
break |
|
|
|
results = model.track(im0, verbose=False) |
|
im0 = gym_object.start_counting(im0, results) |
|
|
|
|
|
im0_resized = cv2.resize(im0, (1024, 1024)) |
|
|
|
|
|
processed_frames.append(im0_resized) |
|
|
|
|
|
frame_count += 1 |
|
progress_bar.progress(frame_count / total_frames) |
|
|
|
cap.release() |
|
cv2.destroyAllWindows() |
|
|
|
|
|
output_video_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') |
|
output_video_path = output_video_file.name |
|
|
|
|
|
video_writer = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (1024, 1024)) |
|
for frame in processed_frames: |
|
video_writer.write(frame) |
|
video_writer.release() |
|
|
|
|
|
st.write("Analysis complete. Displaying processed video:") |
|
st.video(output_video_path) |
|
|
|
|
|
st.write("Download the processed video:") |
|
with open(output_video_path, "rb") as video_file: |
|
st.download_button("Download", video_file, "workouts.mp4") |
|
|