Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -2,13 +2,23 @@ import gradio as gr
|
|
2 |
from ultralytics import YOLO
|
3 |
import ai_gym
|
4 |
import cv2
|
|
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
model = YOLO("yolov8n-pose.pt")
|
8 |
cap = cv2.VideoCapture(video_path)
|
9 |
assert cap.isOpened(), "Error reading video file"
|
10 |
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
|
11 |
|
|
|
12 |
video_writer = cv2.VideoWriter("output_video.mp4",
|
13 |
cv2.VideoWriter_fourcc(*'mp4v'),
|
14 |
fps,
|
@@ -17,7 +27,7 @@ def process(video_path):
|
|
17 |
gym_object = ai_gym.AIGym() # init AI GYM module
|
18 |
gym_object.set_args(line_thickness=2,
|
19 |
view_img=False, # Set view_img to False to prevent displaying the video in real-time
|
20 |
-
pose_type=
|
21 |
kpts_to_check=[6, 8, 10])
|
22 |
|
23 |
frame_count = 0
|
@@ -27,26 +37,32 @@ def process(video_path):
|
|
27 |
print("Video processing has been successfully completed.")
|
28 |
break
|
29 |
frame_count += 1
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
cap.release()
|
35 |
-
video_writer.release()
|
36 |
cv2.destroyAllWindows()
|
37 |
|
38 |
return "output_video.mp4"
|
39 |
|
40 |
title = "Push-up Counter"
|
41 |
description = "This app counts the number of push-ups in a video."
|
42 |
-
|
43 |
-
|
|
|
44 |
example_list = ['Examples/PULL-UPS.mp4','Examples/PUSH-UPS.mp4']
|
45 |
|
46 |
# Create the Gradio demo
|
47 |
demo = gr.Interface(fn=process,
|
48 |
-
inputs=
|
49 |
-
outputs=
|
50 |
title=title,
|
51 |
description=description,
|
52 |
examples=example_list,
|
@@ -55,9 +71,3 @@ demo = gr.Interface(fn=process,
|
|
55 |
|
56 |
# Launch the demo!
|
57 |
demo.launch(show_api=True)
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
2 |
from ultralytics import YOLO
|
3 |
import ai_gym
|
4 |
import cv2
|
5 |
+
import tempfile
|
6 |
+
from PIL import Image
|
7 |
+
import subprocess
|
8 |
|
9 |
+
# Function to upgrade pip
|
10 |
+
def upgrade_pip():
|
11 |
+
subprocess.run(['pip', 'install', '--upgrade', 'pip'])
|
12 |
+
|
13 |
+
# Process video function
|
14 |
+
def process(video_path, pose_type):
|
15 |
+
upgrade_pip() # Upgrade pip before executing the main function
|
16 |
model = YOLO("yolov8n-pose.pt")
|
17 |
cap = cv2.VideoCapture(video_path)
|
18 |
assert cap.isOpened(), "Error reading video file"
|
19 |
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
|
20 |
|
21 |
+
temp_dir = tempfile.mkdtemp() # Create a temporary directory to store processed frames
|
22 |
video_writer = cv2.VideoWriter("output_video.mp4",
|
23 |
cv2.VideoWriter_fourcc(*'mp4v'),
|
24 |
fps,
|
|
|
27 |
gym_object = ai_gym.AIGym() # init AI GYM module
|
28 |
gym_object.set_args(line_thickness=2,
|
29 |
view_img=False, # Set view_img to False to prevent displaying the video in real-time
|
30 |
+
pose_type=pose_type,
|
31 |
kpts_to_check=[6, 8, 10])
|
32 |
|
33 |
frame_count = 0
|
|
|
37 |
print("Video processing has been successfully completed.")
|
38 |
break
|
39 |
frame_count += 1
|
40 |
+
if frame_count % 5 == 0: # Process every 5th frame
|
41 |
+
results = model.track(im0, verbose=False) # Tracking recommended
|
42 |
+
im0 = gym_object.start_counting(im0, results, frame_count)
|
43 |
+
# Save processed frame as an image in the temporary directory
|
44 |
+
cv2.imwrite(f"{temp_dir}/{frame_count}.jpg", im0)
|
45 |
+
|
46 |
+
# Use PIL to create the final video from the processed frames
|
47 |
+
images = [Image.open(f"{temp_dir}/{i}.jpg") for i in range(1, frame_count + 1)]
|
48 |
+
images[0].save("output_video.mp4", save_all=True, append_images=images[1:], duration=1000/fps, loop=0)
|
49 |
|
50 |
cap.release()
|
|
|
51 |
cv2.destroyAllWindows()
|
52 |
|
53 |
return "output_video.mp4"
|
54 |
|
55 |
title = "Push-up Counter"
|
56 |
description = "This app counts the number of push-ups in a video."
|
57 |
+
inputs = [gr.Video(label='Input Video'),
|
58 |
+
gr.Radio(["pullups", "pushups", "absworkout"], label="Pose Type")]
|
59 |
+
outputs = gr.Video(label='Output Video')
|
60 |
example_list = ['Examples/PULL-UPS.mp4','Examples/PUSH-UPS.mp4']
|
61 |
|
62 |
# Create the Gradio demo
|
63 |
demo = gr.Interface(fn=process,
|
64 |
+
inputs=inputs,
|
65 |
+
outputs=outputs,
|
66 |
title=title,
|
67 |
description=description,
|
68 |
examples=example_list,
|
|
|
71 |
|
72 |
# Launch the demo!
|
73 |
demo.launch(show_api=True)
|
|
|
|
|
|
|
|
|
|
|
|