Medvira commited on
Commit
4fb6ca0
·
verified ·
1 Parent(s): ff34f09

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import tempfile
4
+ import numpy as np
5
+ from ultralytics import YOLO, solutions
6
+
7
+ # Load the YOLOv8 model
8
+ model = YOLO("yolov8n-pose.pt")
9
+
10
+ # Streamlit App
11
+ st.title("Workout Monitoring App")
12
+ st.write("Upload a video to monitor your ab workout.")
13
+
14
+ uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "mov", "avi"])
15
+
16
+ if uploaded_file is not None:
17
+ # Save the uploaded video to a temporary file
18
+ tfile = tempfile.NamedTemporaryFile(delete=False)
19
+ tfile.write(uploaded_file.read())
20
+ tfile.close()
21
+
22
+ # Load the video with OpenCV
23
+ cap = cv2.VideoCapture(tfile.name)
24
+ assert cap.isOpened(), "Error reading video file"
25
+ w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
26
+
27
+ # Initialize AIGym object
28
+ gym_object = solutions.AIGym(
29
+ line_thickness=2,
30
+ view_img=False, # Set to False since we are using Streamlit to display
31
+ pose_type="abworkout", # Use 'abworkout' as the pose type
32
+ kpts_to_check=[6, 8, 10],
33
+ )
34
+
35
+ # List to store processed frames
36
+ processed_frames = []
37
+
38
+ # Process the video frame by frame
39
+ st.write("Analyzing video... Please wait.")
40
+ while cap.isOpened():
41
+ success, im0 = cap.read()
42
+ if not success:
43
+ break
44
+
45
+ results = model.track(im0, verbose=False) # Tracking recommended
46
+ im0 = gym_object.start_counting(im0, results)
47
+
48
+ # Append the processed frame to the list
49
+ processed_frames.append(im0)
50
+
51
+ cap.release()
52
+ cv2.destroyAllWindows()
53
+
54
+ # Create a temporary file to save the processed video
55
+ output_video_file = tempfile.NamedTemporaryFile(delete=False, suffix='.avi')
56
+ output_video_path = output_video_file.name
57
+
58
+ # Write the processed frames to a video file
59
+ video_writer = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
60
+ for frame in processed_frames:
61
+ video_writer.write(frame)
62
+ video_writer.release()
63
+
64
+ # Display the processed video in Streamlit
65
+ st.write("Analysis complete. Displaying processed video:")
66
+ st.video(output_video_path)
67
+
68
+ # Provide a download link for the processed video
69
+ st.write("Download the processed video:")
70
+ with open(output_video_path, "rb") as video_file:
71
+ st.download_button("Download", video_file, "workouts.avi")