Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,45 @@
|
|
1 |
-
|
2 |
-
from fastapi.responses import JSONResponse
|
3 |
import torch
|
|
|
|
|
4 |
from PIL import Image
|
5 |
from torchvision.transforms import functional as F
|
6 |
-
from yolov5.models.yolo import Model
|
7 |
from yolov5.utils.general import non_max_suppression
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
12 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
|
13 |
model.eval()
|
14 |
|
15 |
def preprocess_image(image):
|
16 |
-
|
17 |
-
|
|
|
18 |
|
19 |
-
def draw_boxes(outputs, threshold=0.3):
|
20 |
-
|
|
|
21 |
for box in outputs:
|
22 |
score, label, x1, y1, x2, y2 = box[4].item(), int(box[5].item()), box[0].item(), box[1].item(), box[2].item(), box[3].item()
|
23 |
if score > threshold:
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
return boxes
|
30 |
|
31 |
-
|
32 |
-
async def predict(file: UploadFile = File(...)):
|
33 |
-
image = Image.open(file.file)
|
34 |
image_tensor = preprocess_image(image)
|
35 |
outputs = model(image_tensor)
|
36 |
outputs = non_max_suppression(outputs)[0]
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
from PIL import Image
|
6 |
from torchvision.transforms import functional as F
|
|
|
7 |
from yolov5.utils.general import non_max_suppression
|
8 |
|
9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
10 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
|
11 |
model.eval()
|
12 |
|
13 |
def preprocess_image(image):
|
14 |
+
image = image.convert("RGB")
|
15 |
+
image_tensor = F.to_tensor(image).unsqueeze(0).to(device)
|
16 |
+
return image_tensor
|
17 |
|
18 |
+
def draw_boxes(image, outputs, threshold=0.3):
|
19 |
+
image = np.array(image)
|
20 |
+
h, w, _ = image.shape
|
21 |
for box in outputs:
|
22 |
score, label, x1, y1, x2, y2 = box[4].item(), int(box[5].item()), box[0].item(), box[1].item(), box[2].item(), box[3].item()
|
23 |
if score > threshold:
|
24 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
25 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
26 |
+
text = f"{model.names[label]}: {score:.2f}"
|
27 |
+
cv2.putText(image, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
28 |
+
return Image.fromarray(image)
|
|
|
29 |
|
30 |
+
def detect_objects(image):
|
|
|
|
|
31 |
image_tensor = preprocess_image(image)
|
32 |
outputs = model(image_tensor)
|
33 |
outputs = non_max_suppression(outputs)[0]
|
34 |
+
return draw_boxes(image, outputs)
|
35 |
+
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=detect_objects,
|
38 |
+
inputs=gr.Image(type="pil"),
|
39 |
+
outputs=gr.Image(type="pil"),
|
40 |
+
title="YOLO Object Detector",
|
41 |
+
description="Upload an image to detect objects using YOLOv5."
|
42 |
+
)
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
iface.launch()
|