brian25 commited on
Commit
a84f7fb
·
verified ·
1 Parent(s): 8b0c9a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -9
app.py CHANGED
@@ -1,14 +1,39 @@
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- from ultralytics import YOLO
 
 
 
4
 
5
- model = YOLO("best.pt")
 
 
6
 
7
- def greet(source):
8
- model = YOLO("best.pt")
9
- results = model.predict(source, imgsz=1080, conf=0.25)
10
- result = results[0]
11
- return result.plot()
 
 
 
12
 
13
- demo = gr.Interface(fn=greet, inputs="image", outputs="image")
14
- demo.launch()
 
 
1
+
2
+
3
+ import torch
4
+ import cv2
5
  import gradio as gr
6
+ from pathlib import Path
7
+
8
+ # Load the YOLOv5 model
9
+ MODEL_PATH = "best.pt" # Path to your custom model
10
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
11
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path=MODEL_PATH, force_reload=True).to(device)
12
+
13
+ # Define the prediction function
14
+ def predict(image):
15
+ # Convert the input image (numpy) to a compatible format
16
+ img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
17
+ results = model(img_bgr) # Run inference
18
 
19
+ # Render results
20
+ annotated_image = results.render()[0]
21
+ annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
22
+ return annotated_image_rgb
23
 
24
+ # Gradio Interface
25
+ title = "Custom YOLOv5 Model Deployment"
26
+ description = "Upload an image to get predictions using a custom YOLOv5 model trained with your dataset."
27
 
28
+ interface = gr.Interface(
29
+ fn=predict, # Prediction function
30
+ inputs=gr.Image(type="numpy", label="Upload Image"),
31
+ outputs=gr.Image(type="numpy", label="Detected Objects"),
32
+ title=title,
33
+ description=description,
34
+ live=True # Enable live processing
35
+ )
36
 
37
+ # Launch the Gradio app
38
+ if __name__ == "__main__":
39
+ interface.launch()