Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,67 @@
|
|
1 |
import glob
|
2 |
import os
|
|
|
3 |
|
4 |
import gradio as gr
|
5 |
import yolov5
|
6 |
-
from PIL import Image
|
7 |
model = yolov5.load('model/dango.pt')
|
8 |
|
9 |
-
|
|
|
10 |
|
11 |
def inference(gr_input):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
x2 = boxes[:, 2].tolist()
|
32 |
-
y2 = boxes[:, 3].tolist()
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
42 |
|
43 |
-
img = im.open(gr_input)
|
44 |
-
img_crop = img.crop((x1, y1, x2, y2))
|
45 |
-
img_name = os.path.basename(image)
|
46 |
-
# pngで保存
|
47 |
-
|
48 |
-
results.save(save_dir='results/')
|
49 |
-
img_crop.save(f"{output_folder}/{img_name}", quality=95)
|
50 |
-
img_list = [glob.glob("result/*.png")]
|
51 |
-
return img_list
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
gr.Markdown(
|
57 |
-
"# <center> ダンゴムシ捕捉\n"
|
58 |
-
"## <center> ダンゴムシの腹側からの画像を、機械学習で判別できるモデルです\n"
|
59 |
-
)
|
60 |
-
inputs = gr.Image()
|
61 |
-
output = gr.Gallery(label="結果")
|
62 |
-
btn = gr.Button("judge")
|
63 |
-
btn.click(inference, inputs, output)
|
64 |
-
app.launch()
|
|
|
1 |
import glob
|
2 |
import os
|
3 |
+
import shutil
|
4 |
|
5 |
import gradio as gr
|
6 |
import yolov5
|
7 |
+
from PIL import Image as im
|
8 |
model = yolov5.load('model/dango.pt')
|
9 |
|
10 |
+
# てすと
|
11 |
+
|
12 |
|
13 |
def inference(gr_input):
|
14 |
+
# set model parameters
|
15 |
+
model.conf = 0.45 # NMS confidence threshold
|
16 |
+
model.iou = 0.45 # NMS IoU threshold
|
17 |
+
model.agnostic = False # NMS class-agnostic
|
18 |
+
model.multi_label = False # NMS multiple labels per box
|
19 |
+
model.max_det = 1 # maximum number of detections per image
|
20 |
+
results = model(gr_input)
|
21 |
+
output_folder = "results"
|
22 |
+
shutil.rmtree(output_folder)
|
23 |
+
if not os.path.exists(output_folder):
|
24 |
+
os.makedirs(output_folder)
|
25 |
+
# ディレクトリを空にする
|
26 |
+
|
27 |
+
# parse results
|
28 |
+
predictions = results.pred[0]
|
29 |
+
boxes = predictions[:, :4] # x1, y1, x2, y2
|
30 |
+
scores = predictions[:, 4]
|
31 |
+
categories = predictions[:, 5]
|
32 |
+
|
33 |
+
# boxesからx1,y1,x2,y2を取り出す
|
34 |
+
x1 = boxes[:, 0].tolist()
|
35 |
+
y1 = boxes[:, 1].tolist()
|
36 |
+
x2 = boxes[:, 2].tolist()
|
37 |
+
y2 = boxes[:, 3].tolist()
|
38 |
|
39 |
+
# 4つの座標のうち、一つでも入っていなかったら、その画像はスキップ
|
40 |
+
if x1 == [] or y1 == [] or x2 == [] or y2 == []:
|
41 |
+
return
|
|
|
|
|
42 |
|
43 |
+
x1 = int(x1[0])
|
44 |
+
y1 = int(y1[0])
|
45 |
+
x2 = int(x2[0])
|
46 |
+
y2 = int(y2[0])
|
47 |
+
img = im.open(gr_input)
|
48 |
+
img_crop = img.crop((x1, y1, x2, y2))
|
49 |
+
img_name = os.path.basename(gr_input)
|
50 |
+
# pngで保存
|
51 |
|
52 |
+
results.save(save_dir='results/')
|
53 |
+
img_crop.save(f"{output_folder}/crop_{img_name}", quality=95)
|
54 |
+
img_list = glob.glob("results/*.jpg")
|
55 |
+
img_list.append(f"{output_folder}/crop_{img_name}")
|
56 |
+
return img_list
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
iface = gr.Interface(
|
60 |
+
fn=inference,
|
61 |
+
inputs=gr.File(label="画像を選択"),
|
62 |
+
outputs=gr.Gallery(label="結果"),
|
63 |
+
title="YOLO"
|
64 |
+
)
|
65 |
|
66 |
+
if __name__ == "__main__":
|
67 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|