dnth commited on
Commit
0f07c40
1 Parent(s): 1646797

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -27
app.py CHANGED
@@ -1,11 +1,4 @@
1
- import subprocess
2
- import sys
3
-
4
- print("Reinstalling mmcv")
5
- subprocess.check_call([sys.executable, "-m", "pip", "uninstall", "-y", "mmcv-full==1.3.17"])
6
- subprocess.check_call([sys.executable, "-m", "pip", "install", "mmcv-full==1.3.17", "-f", "https://download.openmmlab.com/mmcv/dist/cpu/torch1.10.0/index.html"])
7
- print("mmcv install complete")
8
-
9
  from icevision.all import *
10
  from icevision.models.checkpoint import *
11
  import PIL
@@ -13,7 +6,7 @@ import gradio as gr
13
  import os
14
 
15
  # Load model
16
- checkpoint_path = 'models/model_checkpoint.pth'
17
  checkpoint_and_model = model_from_checkpoint(checkpoint_path)
18
 
19
  model = checkpoint_and_model["model"]
@@ -25,40 +18,81 @@ img_size = checkpoint_and_model["img_size"]
25
  valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(img_size), tfms.A.Normalize()])
26
 
27
 
28
- for root, dirs, files in os.walk(r'sample_images/'):
29
  for filename in files:
30
  print(filename)
31
 
32
- examples = ["sample_images/"+file for file in files]
33
- article="<p style='text-align: center'><a href='https://dicksonneoh.com/' target='_blank'>Blog post</a></p>"
34
- enable_queue=True
35
-
36
-
37
- #examples = [['sample_images/3.jpg']]
38
- examples = [["sample_images/"+file] for file in files]
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  def show_preds(input_image, display_label, display_bbox, detection_threshold):
41
 
42
- if detection_threshold==0: detection_threshold=0.5
 
 
 
43
 
44
- img = PIL.Image.fromarray(input_image, 'RGB')
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- pred_dict = model_type.end2end_detect(img, valid_tfms, model, class_map=class_map, detection_threshold=detection_threshold,
47
- display_label=display_label, display_bbox=display_bbox, return_img=True,
48
- font_size=16, label_color="#FF59D6")
49
 
50
- return pred_dict['img']
51
 
52
  # display_chkbox = gr.inputs.CheckboxGroup(["Label", "BBox"], label="Display", default=True)
53
- display_chkbox_label = gr.inputs.Checkbox(label="Label", default=True)
54
  display_chkbox_box = gr.inputs.Checkbox(label="Box", default=True)
55
 
56
- detection_threshold_slider = gr.inputs.Slider(minimum=0, maximum=1, step=0.1, default=0.5, label="Detection Threshold")
 
 
57
 
58
- outputs = gr.outputs.Image(type="pil")
 
 
 
59
 
60
  # Option 1: Get an image from local drive
61
- gr_interface = gr.Interface(fn=show_preds, inputs=["image", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, title='Microalgae Detection', article=article, examples=examples)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  # # Option 2: Grab an image from a webcam
64
  # gr_interface = gr.Interface(fn=show_preds, inputs=["webcam", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, title='IceApp - COCO', live=False)
 
1
+ from gradio.outputs import Label
 
 
 
 
 
 
 
2
  from icevision.all import *
3
  from icevision.models.checkpoint import *
4
  import PIL
 
6
  import os
7
 
8
  # Load model
9
+ checkpoint_path = "models/model_checkpoint.pth"
10
  checkpoint_and_model = model_from_checkpoint(checkpoint_path)
11
 
12
  model = checkpoint_and_model["model"]
 
18
  valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(img_size), tfms.A.Normalize()])
19
 
20
 
21
+ for root, dirs, files in os.walk(r"sample_images/"):
22
  for filename in files:
23
  print(filename)
24
 
25
+ examples = ["sample_images/" + file for file in files]
26
+ article = "<p style='text-align: center'><a href='https://dicksonneoh.com/' target='_blank'>Blog post</a></p>"
27
+ enable_queue = True
28
+
29
+ # Populate examples in Gradio interface
30
+ example_images = [["sample_images/" + file] for file in files]
31
+
32
+ # Columns: Input Image | Label | Box | Detection Threshold
33
+ examples = [
34
+ [example_images[0], False, True, 0.5],
35
+ [example_images[1], True, True, 0.5],
36
+ [example_images[2], False, True, 0.7],
37
+ [example_images[3], True, True, 0.7],
38
+ [example_images[4], False, True, 0.5],
39
+ [example_images[5], False, True, 0.5],
40
+ [example_images[6], False, True, 0.5],
41
+ [example_images[7], False, True, 0.5],
42
+ ]
43
 
44
  def show_preds(input_image, display_label, display_bbox, detection_threshold):
45
 
46
+ if detection_threshold == 0:
47
+ detection_threshold = 0.5
48
+
49
+ img = PIL.Image.fromarray(input_image, "RGB")
50
 
51
+ pred_dict = model_type.end2end_detect(
52
+ img,
53
+ valid_tfms,
54
+ model,
55
+ class_map=class_map,
56
+ detection_threshold=detection_threshold,
57
+ display_label=display_label,
58
+ display_bbox=display_bbox,
59
+ return_img=True,
60
+ font_size=16,
61
+ label_color="#FF59D6",
62
+ )
63
 
64
+ return pred_dict["img"], len(pred_dict["detection"]["bboxes"])
 
 
65
 
 
66
 
67
  # display_chkbox = gr.inputs.CheckboxGroup(["Label", "BBox"], label="Display", default=True)
68
+ display_chkbox_label = gr.inputs.Checkbox(label="Label", default=False)
69
  display_chkbox_box = gr.inputs.Checkbox(label="Box", default=True)
70
 
71
+ detection_threshold_slider = gr.inputs.Slider(
72
+ minimum=0, maximum=1, step=0.1, default=0.5, label="Detection Threshold"
73
+ )
74
 
75
+ outputs = [
76
+ gr.outputs.Image(type="pil", label="RetinaNet Inference"),
77
+ gr.outputs.Textbox(type='number', label='Microalgae Count')
78
+ ]
79
 
80
  # Option 1: Get an image from local drive
81
+ gr_interface = gr.Interface(
82
+ fn=show_preds,
83
+ inputs=[
84
+ "image",
85
+ display_chkbox_label,
86
+ display_chkbox_box,
87
+ detection_threshold_slider,
88
+ ],
89
+ outputs=outputs,
90
+ title="Microalgae Detector with RetinaNet",
91
+ description="This RetinaNet model counts microalgaes on a given image. Upload an image or click an example image below to use.",
92
+ article=article,
93
+ examples=examples,
94
+ )
95
+
96
 
97
  # # Option 2: Grab an image from a webcam
98
  # gr_interface = gr.Interface(fn=show_preds, inputs=["webcam", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, title='IceApp - COCO', live=False)