Clementapa commited on
Commit
55c5649
β€’
1 Parent(s): 180ea55

Change inference video

Browse files
Files changed (1) hide show
  1. app.py +45 -13
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import os.path as osp
3
  from typing import List
4
 
 
5
  import gradio as gr
6
  import numpy as np
7
  import supervision as sv
@@ -16,7 +17,7 @@ MARKDOWN = """
16
  ## About the model πŸ‘οΈ
17
  This is a demo for my YOLOv8 nano trained for orang outan detection.\\
18
  The model was trained using [this dataset](https://images.cv/dataset/orangutan-image-classification-dataset)
19
- for orang outan images and [this dataset](https://www.kaggle.com/datasets/slothkong/10-monkey-species/data) as background images. Annotations were obtained using zero shot object detection method GroundingDino.\\
20
 
21
  The code can be found on my github repository: https://github.com/clementapa/orang-outan-image-video-detection.
22
 
@@ -25,9 +26,8 @@ Because to habitat destruction, illicit poaching, and the pet trade, orangutans
25
 
26
  ## AI for good 🌍
27
  Artificial Intelligence (AI) has unquestionable power in the realm of innovation and technology. Even though artificial intelligence (AI) has frequently been used for commercial advantage, it is important to stress that AI can also be used for more noble purposes, such as protecting the environment and the planet's future. We can build a more promising and sustainable future if we reorient AI's focus from business to improving our planet.
28
-
29
-
30
  """
 
31
  EXAMPLES = []
32
 
33
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
@@ -82,11 +82,13 @@ def inference_image(image_rgb_pil: Image.Image, confidence: float) -> List[Image
82
  )
83
 
84
 
85
- def process_frame(frame: np.ndarray, _) -> np.ndarray:
86
  output = YOLO_MODEL(frame, imgsz=640, verbose=False)[0]
87
 
88
  detections = sv.Detections.from_ultralytics(output)
89
 
 
 
90
  labels = [
91
  f"{output.names[class_id]} {confidence:0.2f}"
92
  for _, _, confidence, class_id, _ in detections
@@ -112,13 +114,43 @@ def process_frame(frame: np.ndarray, _) -> np.ndarray:
112
  return annotated_frame
113
 
114
 
115
- def inference_video(path_video):
116
  path_output_video = "temp.mp4"
117
- sv.process_video(
118
- source_path=path_video,
119
- target_path=path_output_video,
120
- callback=process_frame,
 
 
 
 
 
 
 
 
 
 
121
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  return path_output_video
123
 
124
 
@@ -174,9 +206,9 @@ with gr.Blocks(theme=custom_theme, css="style.css") as demo:
174
  label="Examples (click one of the images below to start)",
175
  examples_per_page=10,
176
  )
177
- # confidence_video_slider = gr.Slider(
178
- # label="Confidence", minimum=0.1, maximum=1.0, step=0.05, value=0.6
179
- # )
180
  submit_button_video = gr.Button("Let's find orang outans 🦧 !")
181
  output_video = gr.Video(label="Results")
182
 
@@ -189,7 +221,7 @@ with gr.Blocks(theme=custom_theme, css="style.css") as demo:
189
 
190
  submit_button_video.click(
191
  inference_video,
192
- inputs=[input_video],
193
  outputs=output_video,
194
  queue=True,
195
  )
 
2
  import os.path as osp
3
  from typing import List
4
 
5
+ import cv2
6
  import gradio as gr
7
  import numpy as np
8
  import supervision as sv
 
17
  ## About the model πŸ‘οΈ
18
  This is a demo for my YOLOv8 nano trained for orang outan detection.\\
19
  The model was trained using [this dataset](https://images.cv/dataset/orangutan-image-classification-dataset)
20
+ for orang outan images and [this dataset](https://www.kaggle.com/datasets/slothkong/10-monkey-species/data) as background images. Annotations were obtained using zero shot object detection method GroundingDino.\
21
 
22
  The code can be found on my github repository: https://github.com/clementapa/orang-outan-image-video-detection.
23
 
 
26
 
27
  ## AI for good 🌍
28
  Artificial Intelligence (AI) has unquestionable power in the realm of innovation and technology. Even though artificial intelligence (AI) has frequently been used for commercial advantage, it is important to stress that AI can also be used for more noble purposes, such as protecting the environment and the planet's future. We can build a more promising and sustainable future if we reorient AI's focus from business to improving our planet.
 
 
29
  """
30
+
31
  EXAMPLES = []
32
 
33
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
 
82
  )
83
 
84
 
85
+ def process_frame(frame: np.ndarray, confidence: float) -> np.ndarray:
86
  output = YOLO_MODEL(frame, imgsz=640, verbose=False)[0]
87
 
88
  detections = sv.Detections.from_ultralytics(output)
89
 
90
+ detections = detections[detections.confidence >= confidence]
91
+
92
  labels = [
93
  f"{output.names[class_id]} {confidence:0.2f}"
94
  for _, _, confidence, class_id, _ in detections
 
114
  return annotated_frame
115
 
116
 
117
+ def inference_video(path_video, confidence):
118
  path_output_video = "temp.mp4"
119
+ video_capture = cv2.VideoCapture(path_video)
120
+
121
+ # Check if the video file was successfully opened
122
+ if not video_capture.isOpened():
123
+ print("Error: Could not open video file.")
124
+ exit()
125
+
126
+ frame_width = int(video_capture.get(3))
127
+ frame_height = int(video_capture.get(4))
128
+ frame_rate = int(video_capture.get(5))
129
+
130
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v") # You can change the codec as needed
131
+ out = cv2.VideoWriter(
132
+ path_output_video, fourcc, frame_rate, (frame_width, frame_height)
133
  )
134
+
135
+ while True:
136
+ # Read a frame from the video
137
+ ret, frame = video_capture.read()
138
+
139
+ # Check if the video has ended
140
+ if not ret:
141
+ break
142
+
143
+ # Do something with the frame (e.g., display it or process it)
144
+ # For example, you can display the frame in a window
145
+ annotated_frame = process_frame(frame, confidence=confidence)
146
+
147
+ out.write(annotated_frame)
148
+
149
+ # Release the video capture object and close any open windows
150
+ video_capture.release()
151
+ out.release()
152
+ cv2.destroyAllWindows()
153
+
154
  return path_output_video
155
 
156
 
 
206
  label="Examples (click one of the images below to start)",
207
  examples_per_page=10,
208
  )
209
+ confidence_video_slider = gr.Slider(
210
+ label="Confidence", minimum=0.1, maximum=1.0, step=0.05, value=0.6
211
+ )
212
  submit_button_video = gr.Button("Let's find orang outans 🦧 !")
213
  output_video = gr.Video(label="Results")
214
 
 
221
 
222
  submit_button_video.click(
223
  inference_video,
224
+ inputs=[input_video, confidence_video_slider],
225
  outputs=output_video,
226
  queue=True,
227
  )