keffy commited on
Commit
08ef8fa
·
verified ·
1 Parent(s): af96a4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -22
app.py CHANGED
@@ -1,34 +1,62 @@
1
  import gradio as gr
2
  import cv2
3
- import numpy as np
4
  from ultralytics import YOLO
 
 
5
 
6
- # Load the YOLOv8 model
7
- model = YOLO("best.pt") # Ensure this file is in the same directory as app.py on Hugging Face
8
 
9
  # Define the inference function
10
- def predict(image):
11
- # Convert the input image from RGB to BGR (OpenCV format)
12
- image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
13
-
14
- # Run the model on the input image
15
- results = model(image_bgr)
16
-
17
- # Extract the result image with detections
18
- annotated_image = results[0].plot() # Returns a BGR image with annotations
19
-
20
- # Convert the image back to RGB for displaying in Gradio
21
- annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
22
-
23
- return annotated_image_rgb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Define the Gradio interface
26
  interface = gr.Interface(
27
- fn=predict,
28
- inputs=gr.Image(type="numpy", label="Upload an Image"),
29
- outputs=gr.Image(type="numpy", label="Detected Objects"),
30
- title="YOLOv8 Object Detection",
31
- description="Upload an image to detect objects with YOLOv8 model."
32
  )
33
 
34
  # Launch the app
 
1
  import gradio as gr
2
  import cv2
 
3
  from ultralytics import YOLO
4
+ import tempfile
5
+ import os
6
 
7
+ # Load the YOLO model
8
+ model = YOLO("best.pt") # Replace with the path to your model
9
 
10
  # Define the inference function
11
+ def yolo_inference(input_file):
12
+ # Check if the input is an image or a video
13
+ if input_file.endswith((".jpg", ".jpeg", ".png")):
14
+ # Process as an image
15
+ img = cv2.imread(input_file)
16
+ results = model(img)
17
+ annotated_img = results[0].plot()
18
+
19
+ # Save the annotated image
20
+ output_path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
21
+ cv2.imwrite(output_path, annotated_img)
22
+ return output_path
23
+
24
+ elif input_file.endswith((".mp4", ".avi", ".mov")):
25
+ # Process as a video
26
+ cap = cv2.VideoCapture(input_file)
27
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
28
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
29
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
30
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
31
+
32
+ # Create a temporary output video path
33
+ output_video_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
34
+ out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
35
+
36
+ while cap.isOpened():
37
+ ret, frame = cap.read()
38
+ if not ret:
39
+ break
40
+
41
+ # Run YOLO on each frame
42
+ results = model(frame)
43
+ annotated_frame = results[0].plot()
44
+ out.write(annotated_frame)
45
+
46
+ cap.release()
47
+ out.release()
48
+ return output_video_path
49
+
50
+ else:
51
+ raise ValueError("Unsupported file format. Please upload an image or video.")
52
 
53
  # Define the Gradio interface
54
  interface = gr.Interface(
55
+ fn=yolo_inference,
56
+ inputs=gr.File(label="Upload an Image or Video"),
57
+ outputs=gr.File(label="Processed Image/Video"),
58
+ title="YOLO Object Detection",
59
+ description="Upload an image or video for object detection using YOLO."
60
  )
61
 
62
  # Launch the app