Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,138 +1,44 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
#
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# # Draw box + label
|
| 47 |
-
# cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 48 |
-
# cv2.putText(
|
| 49 |
-
# image,
|
| 50 |
-
# label,
|
| 51 |
-
# (x1, y1 - 10),
|
| 52 |
-
# cv2.FONT_HERSHEY_SIMPLEX,
|
| 53 |
-
# 0.7,
|
| 54 |
-
# (0, 255, 0),
|
| 55 |
-
# 2
|
| 56 |
-
# )
|
| 57 |
-
|
| 58 |
-
# # Convert back BGR → RGB
|
| 59 |
-
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 60 |
-
# return image
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# # -------------------------
|
| 64 |
-
# # Gradio UI
|
| 65 |
-
# # -------------------------
|
| 66 |
-
# app = gr.Interface(
|
| 67 |
-
# fn=predict,
|
| 68 |
-
# inputs=gr.Image(type="numpy", label="Upload Deer Image"),
|
| 69 |
-
# outputs=gr.Image(type="numpy", label="Prediction"),
|
| 70 |
-
# title="Buck Tracker AI – Deer Detection & Classification",
|
| 71 |
-
# description="Upload a trail cameras image. The system detects deer and classifies Buck/Doe using a multi-stage YOLO pipeline."
|
| 72 |
-
# )
|
| 73 |
-
|
| 74 |
-
# # -------------------------
|
| 75 |
-
# # Launch
|
| 76 |
-
# # -------------------------
|
| 77 |
-
# if __name__ == "__main__":
|
| 78 |
-
# app.launch()
|
| 79 |
-
from ultralytics import YOLO
|
| 80 |
-
import cv2
|
| 81 |
-
import gradio as gr
|
| 82 |
-
import numpy as np
|
| 83 |
-
|
| 84 |
-
# -------------------------
|
| 85 |
-
# Load detection model
|
| 86 |
-
# -------------------------
|
| 87 |
-
det_model = YOLO(r"models\buck_vs_doe_Detection_best.pt")
|
| 88 |
-
|
| 89 |
-
# -------------------------
|
| 90 |
-
# Inference function
|
| 91 |
-
# -------------------------
|
| 92 |
-
def predict(image):
|
| 93 |
-
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 94 |
-
|
| 95 |
-
results = det_model(image)
|
| 96 |
-
|
| 97 |
-
for r in results:
|
| 98 |
-
for box in r.boxes:
|
| 99 |
-
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 100 |
-
conf = float(box.conf[0])
|
| 101 |
-
cls_id = int(box.cls[0])
|
| 102 |
-
|
| 103 |
-
# ✅ Auto class name from Ultralytics
|
| 104 |
-
class_name = det_model.names[cls_id]
|
| 105 |
-
|
| 106 |
-
label = f"{class_name} ({conf:.2f})"
|
| 107 |
-
|
| 108 |
-
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 109 |
-
cv2.putText(
|
| 110 |
-
image,
|
| 111 |
-
label,
|
| 112 |
-
(x1, y1 - 10),
|
| 113 |
-
cv2.FONT_HERSHEY_SIMPLEX,
|
| 114 |
-
0.7,
|
| 115 |
-
(0, 255, 0),
|
| 116 |
-
2
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 120 |
-
return image
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# -------------------------
|
| 124 |
-
# Gradio UI
|
| 125 |
-
# -------------------------
|
| 126 |
-
app = gr.Interface(
|
| 127 |
-
fn=predict,
|
| 128 |
-
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 129 |
-
outputs=gr.Image(type="numpy", label="Detection Result"),
|
| 130 |
-
title="Buck Tracker AI – Deer Detection",
|
| 131 |
-
description="YOLO-based deer detection with automatic class labels from the model."
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
# -------------------------
|
| 135 |
-
# Launch
|
| 136 |
-
# -------------------------
|
| 137 |
-
if __name__ == "__main__":
|
| 138 |
-
app.launch()
|
|
|
|
| 1 |
+
mport gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# -------------------------
|
| 6 |
+
# Load detection model
|
| 7 |
+
# -------------------------
|
| 8 |
+
model = YOLO("buck_vs_doe_Detection_best.pt")
|
| 9 |
+
|
| 10 |
+
# -------------------------
|
| 11 |
+
# Inference function
|
| 12 |
+
# -------------------------
|
| 13 |
+
def predict(image):
|
| 14 |
+
# Run inference (YOLO accepts numpy RGB directly)
|
| 15 |
+
results = model(image)
|
| 16 |
+
|
| 17 |
+
# Take first result (single image)
|
| 18 |
+
r = results[0]
|
| 19 |
+
|
| 20 |
+
# Plot results (BGR numpy array)
|
| 21 |
+
im_bgr = r.plot()
|
| 22 |
+
|
| 23 |
+
# Convert BGR → RGB for Gradio
|
| 24 |
+
im_rgb = im_bgr[..., ::-1]
|
| 25 |
+
|
| 26 |
+
return im_rgb
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# -------------------------
|
| 30 |
+
# Gradio UI
|
| 31 |
+
# -------------------------
|
| 32 |
+
app = gr.Interface(
|
| 33 |
+
fn=predict,
|
| 34 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 35 |
+
outputs=gr.Image(type="numpy", label="Detection Result"),
|
| 36 |
+
title="Buck Tracker AI – Deer Detection",
|
| 37 |
+
description="YOLO-based buck vs doe detection using Ultralytics native plotting."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# -------------------------
|
| 41 |
+
# Launch
|
| 42 |
+
# -------------------------
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|