codewithRiz commited on
Commit
3297191
Β·
verified Β·
1 Parent(s): 84bbec2

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from ultralytics import YOLO
2
+ # import cv2
3
+ # import gradio as gr
4
+ # import numpy as np
5
+
6
+ # # -------------------------
7
+ # # Load models (once)
8
+ # # -------------------------
9
+ # det_model = YOLO(
10
+ # "models/detect/best_yolov8s.onnx"
11
+ # )
12
+
13
+ # cls_model = YOLO(
14
+ # "models/classify/Buck_classification_epoch_26_best.onnx",
15
+ # task="classify"
16
+ # )
17
+
18
+ # # -------------------------
19
+ # # Inference function
20
+ # # -------------------------
21
+ # def predict(image):
22
+ # # Convert RGB (Gradio) β†’ BGR (OpenCV)
23
+ # image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
24
+
25
+ # det_results = det_model(image)
26
+
27
+ # for r in det_results:
28
+ # for box in r.boxes:
29
+ # x1, y1, x2, y2 = map(int, box.xyxy[0])
30
+
31
+ # crop = image[y1:y2, x1:x2]
32
+ # if crop.size == 0:
33
+ # continue
34
+
35
+ # # Classification
36
+ # cls_results = cls_model(crop)
37
+ # probs = cls_results[0].probs
38
+
39
+ # cls_id = probs.top1
40
+ # cls_conf = probs.top1conf
41
+ # cls_name = cls_results[0].names[cls_id]
42
+
43
+ # # Labels
44
+ # label = f"Deer | {cls_name} ({cls_conf:.2f})"
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()