Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,42 @@
|
|
1 |
-
import os
|
2 |
-
import subprocess
|
3 |
-
|
4 |
-
# Clone the yolov5 repository and install its requirements
|
5 |
-
if not os.path.exists('yolov5'):
|
6 |
-
subprocess.run(['git', 'clone', 'https://github.com/ultralytics/yolov5'], check=True)
|
7 |
-
subprocess.run(['pip', 'install', '-r', 'yolov5/requirements.txt'], check=True)
|
8 |
-
|
9 |
import torch
|
10 |
-
import torchvision
|
11 |
-
from torchvision.transforms import functional as F
|
12 |
-
from PIL import Image
|
13 |
import cv2
|
14 |
-
import gradio as gr
|
15 |
import numpy as np
|
16 |
-
|
17 |
-
from
|
18 |
from yolov5.utils.general import non_max_suppression
|
|
|
|
|
19 |
|
|
|
20 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
21 |
-
print(f"Using device: {device}")
|
22 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
|
23 |
model.eval()
|
24 |
-
print("Model loaded successfully")
|
25 |
|
26 |
def preprocess_image(image):
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
print(f"Preprocessed image tensor: {image_tensor.shape}")
|
31 |
-
return image_tensor
|
32 |
-
except Exception as e:
|
33 |
-
print(f"Error in preprocessing image: {e}")
|
34 |
-
return None
|
35 |
|
36 |
def draw_boxes(image, outputs, threshold=0.3):
|
37 |
-
|
38 |
-
|
39 |
-
h, w, _ = image.shape
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
cv2.putText(image, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
49 |
|
50 |
-
|
51 |
-
except Exception as e:
|
52 |
-
print(f"Error in drawing boxes: {e}")
|
53 |
-
return image
|
54 |
|
55 |
def detect_objects(image):
|
56 |
image_tensor = preprocess_image(image)
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
print(f"Model raw outputs: {outputs}")
|
62 |
-
outputs = non_max_suppression(outputs, conf_thres=0.25, iou_thres=0.45)
|
63 |
-
if outputs is None or len(outputs[0]) == 0:
|
64 |
-
print("No objects detected.")
|
65 |
-
return image
|
66 |
-
print(f"Filtered outputs: {outputs[0]}")
|
67 |
-
result_image = draw_boxes(image, outputs[0].cpu().numpy())
|
68 |
-
return result_image
|
69 |
-
except Exception as e:
|
70 |
-
print(f"Error in detecting objects: {e}")
|
71 |
-
return image
|
72 |
|
73 |
iface = gr.Interface(
|
74 |
fn=detect_objects,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
|
|
|
|
|
|
2 |
import cv2
|
|
|
3 |
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from torchvision.transforms import functional as F
|
6 |
from yolov5.utils.general import non_max_suppression
|
7 |
+
from yolov5.models.yolo import Model
|
8 |
+
import gradio as gr
|
9 |
|
10 |
+
# Load YOLOv5 model
|
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 |
+
image = Image.fromarray(image)
|
17 |
+
image_tensor = F.to_tensor(image).unsqueeze(0).to(device)
|
18 |
+
return image_tensor
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def draw_boxes(image, outputs, threshold=0.3):
|
21 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
22 |
+
h, w, _ = image.shape
|
|
|
23 |
|
24 |
+
for box in outputs:
|
25 |
+
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()
|
26 |
+
if score > threshold:
|
27 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
28 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
29 |
+
text = f"{model.names[label]}: {score:.2f}"
|
30 |
+
cv2.putText(image, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
|
|
31 |
|
32 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
|
33 |
|
34 |
def detect_objects(image):
|
35 |
image_tensor = preprocess_image(image)
|
36 |
+
outputs = model(image_tensor)
|
37 |
+
outputs = non_max_suppression(outputs)[0]
|
38 |
+
result_image = draw_boxes(image, outputs.cpu().numpy())
|
39 |
+
return result_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
iface = gr.Interface(
|
42 |
fn=detect_objects,
|