bunnyroshan commited on
Commit
44a02e2
·
verified ·
1 Parent(s): 531e06b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -41
app.py CHANGED
@@ -1,63 +1,56 @@
1
  import gradio as gr
2
  import torch
3
- from ultralytics import YOLO
4
  import cv2
5
- import tempfile
 
6
 
7
- # Load the trained YOLOv8 model
8
- model = YOLO('best.pt')
9
 
10
- def predict(image):
11
  results = model(image)
12
- # You might want to process results to return bounding boxes, class labels, etc.
13
- annotated_image = results[0].plot() # plot the results on the image
14
- return annotated_image
15
 
16
- def predict_video(video):
17
- # Read the video file
18
  cap = cv2.VideoCapture(video)
19
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
20
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
21
- fps = int(cap.get(cv2.CAP_PROP_FPS))
22
-
23
- # Create a temporary file to save the output video
24
- out_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
25
- out_path = out_file.name
26
-
27
- # Define the codec and create VideoWriter object
28
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
29
- out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
30
 
31
- while cap.isOpened():
32
  ret, frame = cap.read()
33
  if not ret:
34
  break
35
  results = model(frame)
36
- annotated_frame = results[0].plot() # plot the results on the frame
37
- out.write(annotated_frame)
38
-
39
  cap.release()
40
- out.release()
41
 
42
- return out_path
 
 
 
 
 
 
 
 
 
43
 
44
  # Create Gradio interface
45
- interface = gr.Interface(
46
- fn=lambda img, vid: (predict(img), predict_video(vid)),
47
- inputs=[
48
- gr.inputs.Image(type="numpy", label="Input Image"),
49
- gr.inputs.Video(label="Input Video")
50
- ],
51
- outputs=[
52
- gr.outputs.Image(type="numpy", label="Output Image"),
53
- gr.outputs.Video(label="Output Video")
54
- ],
55
- title="YOLOv8 Object Detection",
56
- description="Upload an image or a video and get the object detection results using a YOLOv8 model."
57
- )
58
 
59
  if __name__ == "__main__":
60
- interface.launch()
61
 
62
 
63
 
 
1
  import gradio as gr
2
  import torch
 
3
  import cv2
4
+ import numpy as np
5
+ from PIL import Image
6
 
7
+ # Load the YOLOv8 model
8
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True) # Adjust to yolov8 if needed
9
 
10
+ def process_image(image):
11
  results = model(image)
12
+ return results.render()[0] # Returns an image with boxes drawn
 
 
13
 
14
+ def process_video(video):
 
15
  cap = cv2.VideoCapture(video)
16
+ frames = []
 
 
 
 
 
 
 
 
 
 
17
 
18
+ while(cap.isOpened()):
19
  ret, frame = cap.read()
20
  if not ret:
21
  break
22
  results = model(frame)
23
+ frames.append(results.render()[0])
24
+
 
25
  cap.release()
 
26
 
27
+ # Convert frames back to a video format
28
+ height, width, layers = frames[0].shape
29
+ video_out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 30, (width, height))
30
+
31
+ for frame in frames:
32
+ video_out.write(frame)
33
+
34
+ video_out.release()
35
+
36
+ return 'output.mp4'
37
 
38
  # Create Gradio interface
39
+ image_input = gr.inputs.Image(type="numpy", label="Upload an image")
40
+ video_input = gr.inputs.Video(type="mp4", label="Upload a video")
41
+
42
+ image_output = gr.outputs.Image(type="numpy", label="Detected image")
43
+ video_output = gr.outputs.Video(type="mp4", label="Detected video")
44
+
45
+ iface = gr.Interface(fn={'image': process_image, 'video': process_video},
46
+ inputs=[image_input, video_input],
47
+ outputs=[image_output, video_output],
48
+ live=True,
49
+ title="YOLOv8 Object Detection",
50
+ description="Upload an image or video to detect objects using YOLOv8.")
 
51
 
52
  if __name__ == "__main__":
53
+ iface.launch()
54
 
55
 
56