Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,52 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from torchvision import transforms
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
transforms.
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
gr.Interface(detect_objects, inputs, outputs, title="YOLOv7 Object Detection").launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
from models.experimental import attempt_load
|
| 5 |
+
from utils.general import non_max_suppression, plot_one_box
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Load YOLOv7 model
|
| 9 |
+
weights_path = "cattle.pt"
|
| 10 |
+
config_path = "yolov7.yaml"
|
| 11 |
+
|
| 12 |
+
# Initialize your YOLOv7 model here
|
| 13 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 14 |
+
net = attempt_load(weights_path, map_location=device)
|
| 15 |
+
|
| 16 |
+
# Define function to detect objects using YOLOv7
|
| 17 |
+
def detect_objects(image):
|
| 18 |
+
global net
|
| 19 |
+
|
| 20 |
+
# Perform any necessary preprocessing on the image
|
| 21 |
+
transform = transforms.Compose([
|
| 22 |
+
transforms.Resize((416, 416)), # Resize image to expected input size
|
| 23 |
+
transforms.ToTensor(), # Convert image to tensor
|
| 24 |
+
])
|
| 25 |
+
image_tensor = transform(image).unsqueeze(0)
|
| 26 |
+
|
| 27 |
+
# Perform object detection
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
# Forward pass through the network
|
| 30 |
+
outs = net(image_tensor)
|
| 31 |
+
|
| 32 |
+
# Apply non-maximum suppression
|
| 33 |
+
pred = non_max_suppression(outs, conf_thres=0.4, iou_thres=0.5, classes=None, agnostic=False)
|
| 34 |
+
|
| 35 |
+
# Process detection results and draw bounding boxes
|
| 36 |
+
object_count = 0
|
| 37 |
+
for i, det in enumerate(pred):
|
| 38 |
+
# Skip if no detections
|
| 39 |
+
if len(det):
|
| 40 |
+
# Increment object count
|
| 41 |
+
object_count += len(det)
|
| 42 |
+
# Loop over the detections
|
| 43 |
+
for *xyxy, conf, cls in reversed(det):
|
| 44 |
+
# Draw bounding box
|
| 45 |
+
plot_one_box(xyxy, image, label=f'{conf:.2f}', color=(255, 0, 0), line_thickness=3)
|
| 46 |
+
|
| 47 |
+
return image, object_count
|
| 48 |
+
|
| 49 |
+
# Create Gradio interface
|
| 50 |
+
inputs = gr.inputs.Image(label="Upload Image or Video")
|
| 51 |
+
outputs = [gr.outputs.Image(label="Output Image with Objects Detected"), gr.outputs.Text(label="Object Count")]
|
| 52 |
gr.Interface(detect_objects, inputs, outputs, title="YOLOv7 Object Detection").launch()
|