randomshit11 commited on
Commit
170fd84
·
verified ·
1 Parent(s): df5c099

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +8 -23
main.py CHANGED
@@ -2,23 +2,13 @@ import gradio as gr
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,
@@ -37,17 +27,12 @@ def process(video_path, pose_type):
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"
@@ -57,17 +42,17 @@ 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,
69
- cache_examples=True,
70
- )
71
 
72
  # Launch the demo!
73
  demo.launch(show_api=True)
 
2
  from ultralytics import YOLO
3
  import ai_gym
4
  import cv2
 
 
 
5
 
 
 
 
 
 
6
  def process(video_path, pose_type):
 
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,
 
27
  print("Video processing has been successfully completed.")
28
  break
29
  frame_count += 1
30
+ results = model.track(im0, verbose=False) # Tracking recommended
31
+ im0 = gym_object.start_counting(im0, results, frame_count)
32
+ video_writer.write(im0)
 
 
 
 
 
 
33
 
34
  cap.release()
35
+ video_writer.release()
36
  cv2.destroyAllWindows()
37
 
38
  return "output_video.mp4"
 
42
  inputs = [gr.Video(label='Input Video'),
43
  gr.Radio(["pullups", "pushups", "absworkout"], label="Pose Type")]
44
  outputs = gr.Video(label='Output Video')
 
45
 
46
+ example_list = [['Examples/PULL-UPS.mp4'],['Examples/PUSH-UPS.mp4']]
47
  # Create the Gradio demo
48
  demo = gr.Interface(fn=process,
49
  inputs=inputs,
50
  outputs=outputs,
51
  title=title,
52
+ description=description,
53
  examples=example_list,
54
+ cache_examples=True
55
+ )
56
 
57
  # Launch the demo!
58
  demo.launch(show_api=True)