randomshit11 commited on
Commit
afaf3bd
·
verified ·
1 Parent(s): bfba840

Rename test.py to main.py

Browse files
Files changed (1) hide show
  1. test.py → main.py +65 -47
test.py → main.py RENAMED
@@ -1,5 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # from ultralytics import YOLO
2
- # import ai_gym
3
  # import cv2
4
 
5
  # model = YOLO("yolov8n-pose.pt")
@@ -7,65 +61,29 @@
7
  # assert cap.isOpened(), "Error reading video file"
8
  # w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
9
 
10
- # video_writer = cv2.VideoWriter("workouts.avi",
11
- # cv2.VideoWriter_fourcc(*'mp4v'),
12
- # fps,
13
- # (w, h))
14
 
15
  # gym_object = ai_gym.AIGym() # init AI GYM module
16
  # gym_object.set_args(line_thickness=2,
17
- # view_img=True,
18
- # pose_type="pullup",
19
  # kpts_to_check=[6, 8, 10])
20
 
21
  # frame_count = 0
22
  # while cap.isOpened():
23
  # success, im0 = cap.read()
24
  # if not success:
25
- # print("Video frame is empty or video processing has been successfully completed.")
26
- # break
27
  # frame_count += 1
28
  # results = model.track(im0, verbose=False) # Tracking recommended
29
  # #results = model.predict(im0) # Prediction also supported
30
  # im0 = gym_object.start_counting(im0, results, frame_count)
31
  # video_writer.write(im0)
32
 
33
- # cv2.destroyAllWindows()
34
  # video_writer.release()
35
-
36
-
37
- from ultralytics import YOLO
38
- from ultralytics.solutions import ai_gym
39
- import cv2
40
-
41
- model = YOLO("yolov8n-pose.pt")
42
- cap = cv2.VideoCapture("pullups.mp4")
43
- assert cap.isOpened(), "Error reading video file"
44
- w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
45
-
46
- video_writer = cv2.VideoWriter("output_video.avi",
47
- cv2.VideoWriter_fourcc(*'mp4v'),
48
- fps,
49
- (w, h))
50
-
51
- gym_object = ai_gym.AIGym() # init AI GYM module
52
- gym_object.set_args(line_thickness=2,
53
- view_img=False, # Set view_img to False to prevent displaying the video in real-time
54
- pose_type="pushup",
55
- kpts_to_check=[6, 8, 10])
56
-
57
- frame_count = 0
58
- while cap.isOpened():
59
- success, im0 = cap.read()
60
- if not success:
61
- print("Video frame is empty or video processing has been successfully completed.")
62
- break
63
- frame_count += 1
64
- results = model.track(im0, verbose=False) # Tracking recommended
65
- #results = model.predict(im0) # Prediction also supported
66
- im0 = gym_object.start_counting(im0, results, frame_count)
67
- video_writer.write(im0)
68
-
69
- cap.release()
70
- video_writer.release()
71
- cv2.destroyAllWindows()
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ from ultralytics.solutions import ai_gym
4
+ import cv2
5
+ import tempfile
6
+ import os
7
+
8
+ # Initialize YOLO model
9
+ model = YOLO("yolov8n-pose.pt")
10
+
11
+ # Initialize AIGym object
12
+ gym_object = ai_gym.AIGym()
13
+
14
+ def count_workouts(input_video):
15
+ # Temporary file to store output video
16
+ output_path = tempfile.NamedTemporaryFile(suffix='.avi').name
17
+
18
+ # Open input video
19
+ cap = cv2.VideoCapture(input_video.name)
20
+ assert cap.isOpened(), "Error reading video file"
21
+ w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
22
+
23
+ # Initialize video writer for output video
24
+ video_writer = cv2.VideoWriter(output_path,
25
+ cv2.VideoWriter_fourcc(*'mp4v'),
26
+ fps,
27
+ (w, h))
28
+
29
+ frame_count = 0
30
+ while cap.isOpened():
31
+ success, im0 = cap.read()
32
+ if not success:
33
+ print("Video frame is empty or video processing has been successfully completed.")
34
+ break
35
+ frame_count += 1
36
+ results = model.track(im0, verbose=False) # Tracking recommended
37
+ im0 = gym_object.start_counting(im0, results, frame_count)
38
+ video_writer.write(im0)
39
+
40
+ cap.release()
41
+ video_writer.release()
42
+ cv2.destroyAllWindows()
43
+
44
+ return output_path
45
+
46
+ # Gradio Interface
47
+ inputs = gr.inputs.Video(label="Upload a video")
48
+ outputs = gr.outputs.Video(label="Output Video")
49
+
50
+ gr.Interface(count_workouts, inputs, outputs, title="Workout Counter",
51
+ description="Upload a video and get a video with workout counting annotations.").launch()
52
+
53
+
54
+
55
  # from ultralytics import YOLO
56
+ # from ultralytics.solutions import ai_gym
57
  # import cv2
58
 
59
  # model = YOLO("yolov8n-pose.pt")
 
61
  # assert cap.isOpened(), "Error reading video file"
62
  # w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
63
 
64
+ # video_writer = cv2.VideoWriter("output_video.avi",
65
+ # cv2.VideoWriter_fourcc(*'mp4v'),
66
+ # fps,
67
+ # (w, h))
68
 
69
  # gym_object = ai_gym.AIGym() # init AI GYM module
70
  # gym_object.set_args(line_thickness=2,
71
+ # view_img=False, # Set view_img to False to prevent displaying the video in real-time
72
+ # pose_type="pushup",
73
  # kpts_to_check=[6, 8, 10])
74
 
75
  # frame_count = 0
76
  # while cap.isOpened():
77
  # success, im0 = cap.read()
78
  # if not success:
79
+ # print("Video frame is empty or video processing has been successfully completed.")
80
+ # break
81
  # frame_count += 1
82
  # results = model.track(im0, verbose=False) # Tracking recommended
83
  # #results = model.predict(im0) # Prediction also supported
84
  # im0 = gym_object.start_counting(im0, results, frame_count)
85
  # video_writer.write(im0)
86
 
87
+ # cap.release()
88
  # video_writer.release()
89
+ # cv2.destroyAllWindows()