codewithRiz commited on
Commit
6d18b9c
·
verified ·
1 Parent(s): 3297191

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -138
app.py CHANGED
@@ -1,138 +1,44 @@
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()
 
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()