Spaces:
Running
Running
MuGeminorum
commited on
Commit
•
70c99ee
1
Parent(s):
bea80a9
add output txt
Browse files
app.py
CHANGED
@@ -18,13 +18,13 @@ def download_model(url, local_path):
|
|
18 |
response = requests.get(url, stream=True)
|
19 |
|
20 |
# Get the total file size in bytes
|
21 |
-
total_size = int(response.headers.get(
|
22 |
|
23 |
# Initialize the tqdm progress bar
|
24 |
-
progress_bar = tqdm(total=total_size, unit=
|
25 |
|
26 |
# Open a local file with write-binary mode
|
27 |
-
with open(local_path,
|
28 |
for data in response.iter_content(chunk_size=1024):
|
29 |
# Update the progress bar
|
30 |
progress_bar.update(len(data))
|
@@ -40,7 +40,7 @@ def download_model(url, local_path):
|
|
40 |
|
41 |
def inference(filename):
|
42 |
if not filename:
|
43 |
-
filename =
|
44 |
|
45 |
detector = InsectDetector()
|
46 |
identifier = InsectIdentifier()
|
@@ -55,6 +55,7 @@ def inference(filename):
|
|
55 |
image_for_draw = image.copy()
|
56 |
image_height, image_width = image.shape[:2]
|
57 |
boxes, confs, classes = detector.detect(image)
|
|
|
58 |
|
59 |
for box, _, _ in zip(boxes, confs, classes):
|
60 |
box = box.astype(np.int32)
|
@@ -67,36 +68,26 @@ def inference(filename):
|
|
67 |
cropped = khandy.crop_or_pad(image, box[0], box[1], box[2], box[3])
|
68 |
results = identifier.identify(cropped)
|
69 |
print(results[0])
|
70 |
-
prob = results[0][
|
71 |
-
|
72 |
-
if prob
|
73 |
-
text =
|
74 |
-
|
75 |
-
text = '{}: {:.2f}%'.format(
|
76 |
-
results[0]['latin_name'],
|
77 |
-
100.0 * results[0]['probability']
|
78 |
)
|
79 |
|
80 |
position = [box[0] + 2, box[1] - 20]
|
81 |
position[0] = min(max(position[0], 0), image_width)
|
82 |
position[1] = min(max(position[1], 0), image_height)
|
83 |
cv2.rectangle(
|
84 |
-
image_for_draw,
|
85 |
-
(box[0], box[1]),
|
86 |
-
(box[2], box[3]),
|
87 |
-
(0, 255, 0),
|
88 |
-
2
|
89 |
)
|
90 |
|
91 |
image_for_draw = khandy.draw_text(
|
92 |
-
image_for_draw,
|
93 |
-
text,
|
94 |
-
position,
|
95 |
-
font='simsun.ttc',
|
96 |
-
font_size=15
|
97 |
)
|
98 |
|
99 |
-
|
|
|
100 |
|
101 |
|
102 |
domain = "https://huggingface.co/MuGeminorum/insecta/resolve/main/"
|
@@ -104,27 +95,30 @@ domain_zh = "https://www.modelscope.cn/api/v1/models/MuGeminorum/insecta/repo?Re
|
|
104 |
try:
|
105 |
download_model(
|
106 |
f"{domain}quarrying_insect_detector.onnx",
|
107 |
-
"./insectid/models/quarrying_insect_detector.onnx"
|
108 |
)
|
109 |
download_model(
|
110 |
f"{domain}quarrying_insect_identifier.onnx",
|
111 |
-
"./insectid/models/quarrying_insect_identifier.onnx"
|
112 |
)
|
113 |
except Exception:
|
114 |
download_model(
|
115 |
f"{domain_zh}quarrying_insect_detector.onnx",
|
116 |
-
"./insectid/models/quarrying_insect_detector.onnx"
|
117 |
)
|
118 |
download_model(
|
119 |
f"{domain_zh}quarrying_insect_identifier.onnx",
|
120 |
-
"./insectid/models/quarrying_insect_identifier.onnx"
|
121 |
)
|
122 |
|
123 |
iface = gr.Interface(
|
124 |
fn=inference,
|
125 |
-
inputs=gr.Image(label=
|
126 |
-
outputs=
|
127 |
-
|
|
|
|
|
|
|
128 |
)
|
129 |
|
130 |
iface.launch()
|
|
|
18 |
response = requests.get(url, stream=True)
|
19 |
|
20 |
# Get the total file size in bytes
|
21 |
+
total_size = int(response.headers.get("content-length", 0))
|
22 |
|
23 |
# Initialize the tqdm progress bar
|
24 |
+
progress_bar = tqdm(total=total_size, unit="B", unit_scale=True)
|
25 |
|
26 |
# Open a local file with write-binary mode
|
27 |
+
with open(local_path, "wb") as file:
|
28 |
for data in response.iter_content(chunk_size=1024):
|
29 |
# Update the progress bar
|
30 |
progress_bar.update(len(data))
|
|
|
40 |
|
41 |
def inference(filename):
|
42 |
if not filename:
|
43 |
+
filename = "./examples/butterfly.jpg"
|
44 |
|
45 |
detector = InsectDetector()
|
46 |
identifier = InsectIdentifier()
|
|
|
55 |
image_for_draw = image.copy()
|
56 |
image_height, image_width = image.shape[:2]
|
57 |
boxes, confs, classes = detector.detect(image)
|
58 |
+
text = "Unknown"
|
59 |
|
60 |
for box, _, _ in zip(boxes, confs, classes):
|
61 |
box = box.astype(np.int32)
|
|
|
68 |
cropped = khandy.crop_or_pad(image, box[0], box[1], box[2], box[3])
|
69 |
results = identifier.identify(cropped)
|
70 |
print(results[0])
|
71 |
+
prob = results[0]["probability"]
|
72 |
+
|
73 |
+
if prob >= 0.10:
|
74 |
+
text = "{}: {:.2f}%".format(
|
75 |
+
results[0]["latin_name"], 100.0 * results[0]["probability"]
|
|
|
|
|
|
|
76 |
)
|
77 |
|
78 |
position = [box[0] + 2, box[1] - 20]
|
79 |
position[0] = min(max(position[0], 0), image_width)
|
80 |
position[1] = min(max(position[1], 0), image_height)
|
81 |
cv2.rectangle(
|
82 |
+
image_for_draw, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2
|
|
|
|
|
|
|
|
|
83 |
)
|
84 |
|
85 |
image_for_draw = khandy.draw_text(
|
86 |
+
image_for_draw, text, position, font="simsun.ttc", font_size=15
|
|
|
|
|
|
|
|
|
87 |
)
|
88 |
|
89 |
+
outxt = text.split(":")[0] if ":" in text else text
|
90 |
+
return Image.fromarray(image_for_draw[:, :, ::-1], mode="RGB"), outxt
|
91 |
|
92 |
|
93 |
domain = "https://huggingface.co/MuGeminorum/insecta/resolve/main/"
|
|
|
95 |
try:
|
96 |
download_model(
|
97 |
f"{domain}quarrying_insect_detector.onnx",
|
98 |
+
"./insectid/models/quarrying_insect_detector.onnx",
|
99 |
)
|
100 |
download_model(
|
101 |
f"{domain}quarrying_insect_identifier.onnx",
|
102 |
+
"./insectid/models/quarrying_insect_identifier.onnx",
|
103 |
)
|
104 |
except Exception:
|
105 |
download_model(
|
106 |
f"{domain_zh}quarrying_insect_detector.onnx",
|
107 |
+
"./insectid/models/quarrying_insect_detector.onnx",
|
108 |
)
|
109 |
download_model(
|
110 |
f"{domain_zh}quarrying_insect_identifier.onnx",
|
111 |
+
"./insectid/models/quarrying_insect_identifier.onnx",
|
112 |
)
|
113 |
|
114 |
iface = gr.Interface(
|
115 |
fn=inference,
|
116 |
+
inputs=gr.Image(label="Upload insect photo", type="filepath"),
|
117 |
+
outputs=[
|
118 |
+
gr.Image(label="Detection result"),
|
119 |
+
gr.Textbox(label="Most probable species", show_copy_button=True),
|
120 |
+
],
|
121 |
+
examples=["./examples/butterfly.jpg", "./examples/beetle.jpg"],
|
122 |
)
|
123 |
|
124 |
iface.launch()
|