ryo2 commited on
Commit
d7e066c
·
verified ·
1 Parent(s): a3ff2fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -49
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 as im
7
  model = yolov5.load('model/dango.pt')
8
 
9
- #てすと
 
10
 
11
  def inference(gr_input):
12
- # set model parameters
13
- model.conf = 0.45 # NMS confidence threshold
14
- model.iou = 0.45 # NMS IoU threshold
15
- model.agnostic = False # NMS class-agnostic
16
- model.multi_label = False # NMS multiple labels per box
17
- model.max_det = 1 # maximum number of detections per image
18
- results = model(gr_input)
19
- output_folder = "results"
20
- if not os.path.exists(output_folder):
21
- os.makedirs(output_folder)
22
- # parse results
23
- predictions = results.pred[0]
24
- boxes = predictions[:, :4] # x1, y1, x2, y2
25
- scores = predictions[:, 4]
26
- categories = predictions[:, 5]
 
 
 
 
 
 
 
 
 
27
 
28
- # boxesからx1,y1,x2,y2を取り出す
29
- x1 = boxes[:, 0].tolist()
30
- y1 = boxes[:, 1].tolist()
31
- x2 = boxes[:, 2].tolist()
32
- y2 = boxes[:, 3].tolist()
33
 
34
- # 4つの座標のうち、一つでも入っていなかったら、その画像はスキップ
35
- if x1 == [] or y1 == [] or x2 == [] or y2 == []:
36
- return
 
 
 
 
 
37
 
38
- x1 = int(x1[0])
39
- y1 = int(y1[0])
40
- x2 = int(x2[0])
41
- y2 = int(y2[0])
 
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
- with gr.Blocks() as app:
55
- gr.Markdown('<img id="visitor-badge" alt="visitor badge" src="https://visitor-badge.glitch.me/badge?page_id=alrab222.Cinderella" />')
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()