Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,58 @@
|
|
1 |
-
import cv2
|
2 |
-
import gradio as gr
|
3 |
-
from huggingface_hub import hf_hub_download
|
4 |
-
|
5 |
-
from vision.ssd.mobilenet_v2_ssd_lite import (
|
6 |
-
create_mobilenetv2_ssd_lite,
|
7 |
-
create_mobilenetv2_ssd_lite_predictor,
|
8 |
-
)
|
9 |
-
|
10 |
-
MODEL_REPO = "fa0311/oita-ken-strawberries-mobilenet"
|
11 |
-
MODEL_FILENAME = "20250129_053504/mb2-ssd-lite-Epoch-55-Loss-1.508891262114048.pth"
|
12 |
-
LABELS_FILENAME = "20250129_053504/labels.txt"
|
13 |
-
|
14 |
-
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
|
15 |
-
label_path = hf_hub_download(repo_id=MODEL_REPO, filename=LABELS_FILENAME)
|
16 |
-
|
17 |
-
with open(label_path, "r") as f:
|
18 |
-
class_names = [name.strip() for name in f.readlines()]
|
19 |
-
|
20 |
-
net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
|
21 |
-
net.load(model_path)
|
22 |
-
predictor = create_mobilenetv2_ssd_lite_predictor(net, candidate_size=200)
|
23 |
-
|
24 |
-
|
25 |
-
def detect_objects(image, threshold):
|
26 |
-
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
27 |
-
boxes, labels, probs = predictor.predict(image, 10, threshold)
|
28 |
-
|
29 |
-
for i in range(boxes.size(0)):
|
30 |
-
box = list(map(int, boxes[i, :]))
|
31 |
-
cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4)
|
32 |
-
label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
|
33 |
-
cv2.putText(
|
34 |
-
image,
|
35 |
-
label,
|
36 |
-
(box[0] + 10, box[1] + 25),
|
37 |
-
cv2.FONT_HERSHEY_SIMPLEX,
|
38 |
-
0.8,
|
39 |
-
(255, 0, 255),
|
40 |
-
2,
|
41 |
-
)
|
42 |
-
|
43 |
-
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
44 |
-
|
45 |
-
|
46 |
-
iface = gr.Interface(
|
47 |
-
fn=detect_objects,
|
48 |
-
inputs=[
|
49 |
-
gr.Image(type="numpy"),
|
50 |
-
gr.Slider(0.1, 1.0, value=0.7, label="Detection Threshold"),
|
51 |
-
],
|
52 |
-
outputs=gr.Image(type="numpy"),
|
53 |
-
title="SSD Object Detection",
|
54 |
-
description="Upload an image of strawberries to detect objects using MobileNetV2-SSD-Lite.",
|
55 |
-
)
|
56 |
-
|
57 |
-
if __name__ == "__main__":
|
58 |
-
iface.launch()
|
|
|
1 |
+
import cv2
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
|
5 |
+
from vision.ssd.mobilenet_v2_ssd_lite import (
|
6 |
+
create_mobilenetv2_ssd_lite,
|
7 |
+
create_mobilenetv2_ssd_lite_predictor,
|
8 |
+
)
|
9 |
+
|
10 |
+
MODEL_REPO = "fa0311/oita-ken-strawberries-mobilenet"
|
11 |
+
MODEL_FILENAME = "20250129_053504/mb2-ssd-lite-Epoch-55-Loss-1.508891262114048.pth"
|
12 |
+
LABELS_FILENAME = "20250129_053504/labels.txt"
|
13 |
+
|
14 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
|
15 |
+
label_path = hf_hub_download(repo_id=MODEL_REPO, filename=LABELS_FILENAME)
|
16 |
+
|
17 |
+
with open(label_path, "r") as f:
|
18 |
+
class_names = [name.strip() for name in f.readlines()]
|
19 |
+
|
20 |
+
net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
|
21 |
+
net.load(model_path)
|
22 |
+
predictor = create_mobilenetv2_ssd_lite_predictor(net, candidate_size=200)
|
23 |
+
|
24 |
+
|
25 |
+
def detect_objects(image, threshold):
|
26 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
27 |
+
boxes, labels, probs = predictor.predict(image, 10, threshold)
|
28 |
+
|
29 |
+
for i in range(boxes.size(0)):
|
30 |
+
box = list(map(int, boxes[i, :]))
|
31 |
+
cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4)
|
32 |
+
label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
|
33 |
+
cv2.putText(
|
34 |
+
image,
|
35 |
+
label,
|
36 |
+
(box[0] + 10, box[1] + 25),
|
37 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
38 |
+
0.8,
|
39 |
+
(255, 0, 255),
|
40 |
+
2,
|
41 |
+
)
|
42 |
+
|
43 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
44 |
+
|
45 |
+
|
46 |
+
iface = gr.Interface(
|
47 |
+
fn=detect_objects,
|
48 |
+
inputs=[
|
49 |
+
gr.Image(type="numpy"),
|
50 |
+
gr.Slider(0.1, 1.0, value=0.7, label="Detection Threshold"),
|
51 |
+
],
|
52 |
+
outputs=gr.Image(type="numpy"),
|
53 |
+
title="SSD Object Detection - Strawberry quality classification",
|
54 |
+
description="Upload an image of strawberries to detect objects using MobileNetV2-SSD-Lite.",
|
55 |
+
)
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
iface.launch()
|