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