MuGeminorum commited on
Commit
1c7433a
1 Parent(s): 98515ad
app.py CHANGED
@@ -72,14 +72,12 @@ with gr.Blocks() as demo:
72
  with gr.Row():
73
  image_input = gr.Image(
74
  type='filepath',
75
- label="Input Image",
76
- source="upload"
77
  )
78
 
79
  image_output = gr.Image(
80
  type='pil',
81
- label="Output Image",
82
- source="canvas"
83
  )
84
 
85
  text_button = gr.Button("Detect")
 
72
  with gr.Row():
73
  image_input = gr.Image(
74
  type='filepath',
75
+ label="Input Image"
 
76
  )
77
 
78
  image_output = gr.Image(
79
  type='pil',
80
+ label="Output Image"
 
81
  )
82
 
83
  text_button = gr.Button("Detect")
insectid/detector.py CHANGED
@@ -1,8 +1,6 @@
1
  import os
2
-
3
  import khandy
4
  import numpy as np
5
-
6
  from .base import OnnxModel
7
  from .base import check_image_dtype_and_shape
8
 
@@ -10,15 +8,16 @@ from .base import check_image_dtype_and_shape
10
  class InsectDetector(OnnxModel):
11
  def __init__(self):
12
  current_dir = os.path.dirname(os.path.abspath(__file__))
13
- model_path = os.path.join(current_dir, 'models/quarrying_insect_detector.onnx')
 
14
  self.input_width = 640
15
  self.input_height = 640
16
  super(InsectDetector, self).__init__(model_path)
17
 
18
  def _preprocess(self, image):
19
  check_image_dtype_and_shape(image)
20
-
21
- # image size normalization
22
  image, scale, pad_left, pad_top = khandy.letterbox_image(
23
  image, self.input_width, self.input_height, 0, return_scale=True)
24
  # image channel normalization
@@ -26,17 +25,18 @@ class InsectDetector(OnnxModel):
26
  # image dtype normalization
27
  image = khandy.rescale_image(image, 'auto', np.float32)
28
  # to tensor
29
- image = np.transpose(image, (2,0,1))
30
  image = np.expand_dims(image, axis=0)
31
  return image, scale, pad_left, pad_top
32
-
33
  def _post_process(self, outputs_list, scale, pad_left, pad_top, conf_thresh, iou_thresh):
34
  pred = outputs_list[0][0]
35
  pass_t = pred[:, 4] > conf_thresh
36
  pred = pred[pass_t]
37
-
38
  boxes = khandy.convert_boxes_format(pred[:, :4], 'cxcywh', 'xyxy')
39
- boxes = khandy.unletterbox_2d_points(boxes, scale, pad_left, pad_top, False)
 
40
  confs = np.max(pred[:, 5:] * pred[:, 4:5], axis=-1)
41
  classes = np.argmax(pred[:, 5:] * pred[:, 4:5], axis=-1)
42
  keep = khandy.non_max_suppression(boxes, confs, iou_thresh)
@@ -46,11 +46,10 @@ class InsectDetector(OnnxModel):
46
  image, scale, pad_left, pad_top = self._preprocess(image)
47
  outputs_list = self.forward(image)
48
  boxes, confs, classes = self._post_process(
49
- outputs_list,
50
- scale=scale,
51
  pad_left=pad_left,
52
  pad_top=pad_top,
53
- conf_thresh=conf_thresh,
54
  iou_thresh=iou_thresh)
55
  return boxes, confs, classes
56
-
 
1
  import os
 
2
  import khandy
3
  import numpy as np
 
4
  from .base import OnnxModel
5
  from .base import check_image_dtype_and_shape
6
 
 
8
  class InsectDetector(OnnxModel):
9
  def __init__(self):
10
  current_dir = os.path.dirname(os.path.abspath(__file__))
11
+ model_path = os.path.join(
12
+ current_dir, './models/quarrying_insect_detector.onnx')
13
  self.input_width = 640
14
  self.input_height = 640
15
  super(InsectDetector, self).__init__(model_path)
16
 
17
  def _preprocess(self, image):
18
  check_image_dtype_and_shape(image)
19
+
20
+ # image size normalization
21
  image, scale, pad_left, pad_top = khandy.letterbox_image(
22
  image, self.input_width, self.input_height, 0, return_scale=True)
23
  # image channel normalization
 
25
  # image dtype normalization
26
  image = khandy.rescale_image(image, 'auto', np.float32)
27
  # to tensor
28
+ image = np.transpose(image, (2, 0, 1))
29
  image = np.expand_dims(image, axis=0)
30
  return image, scale, pad_left, pad_top
31
+
32
  def _post_process(self, outputs_list, scale, pad_left, pad_top, conf_thresh, iou_thresh):
33
  pred = outputs_list[0][0]
34
  pass_t = pred[:, 4] > conf_thresh
35
  pred = pred[pass_t]
36
+
37
  boxes = khandy.convert_boxes_format(pred[:, :4], 'cxcywh', 'xyxy')
38
+ boxes = khandy.unletterbox_2d_points(
39
+ boxes, scale, pad_left, pad_top, False)
40
  confs = np.max(pred[:, 5:] * pred[:, 4:5], axis=-1)
41
  classes = np.argmax(pred[:, 5:] * pred[:, 4:5], axis=-1)
42
  keep = khandy.non_max_suppression(boxes, confs, iou_thresh)
 
46
  image, scale, pad_left, pad_top = self._preprocess(image)
47
  outputs_list = self.forward(image)
48
  boxes, confs, classes = self._post_process(
49
+ outputs_list,
50
+ scale=scale,
51
  pad_left=pad_left,
52
  pad_top=pad_top,
53
+ conf_thresh=conf_thresh,
54
  iou_thresh=iou_thresh)
55
  return boxes, confs, classes
 
insectid/identifier.py CHANGED
@@ -1,23 +1,24 @@
1
  import os
2
  import copy
3
- from collections import OrderedDict
4
-
5
  import khandy
6
  import numpy as np
7
-
8
  from .base import OnnxModel
 
9
  from .base import check_image_dtype_and_shape
10
 
11
 
12
  class InsectIdentifier(OnnxModel):
13
  def __init__(self):
14
  current_dir = os.path.dirname(os.path.abspath(__file__))
15
- model_path = os.path.join(current_dir, 'models/quarrying_insect_identifier.onnx')
16
- label_map_path = os.path.join(current_dir, 'models/quarrying_insectid_label_map.txt')
 
 
17
  super(InsectIdentifier, self).__init__(model_path)
18
-
19
  self.label_name_dict = self._get_label_name_dict(label_map_path)
20
- self.names = [self.label_name_dict[i]['chinese_name'] for i in range(len(self.label_name_dict))]
 
21
  self.num_classes = len(self.label_name_dict)
22
 
23
  @staticmethod
@@ -26,14 +27,14 @@ class InsectIdentifier(OnnxModel):
26
  label_name_dict = {}
27
  for record in records:
28
  label, chinese_name, latin_name = record.split(',')
29
- label_name_dict[int(label)] = OrderedDict([('chinese_name', chinese_name),
30
  ('latin_name', latin_name)])
31
  return label_name_dict
32
-
33
  @staticmethod
34
  def _preprocess(image):
35
  check_image_dtype_and_shape(image)
36
-
37
  # image size normalization
38
  image = khandy.letterbox_image(image, 224, 224)
39
  # image channel normalization
@@ -43,21 +44,21 @@ class InsectIdentifier(OnnxModel):
43
  mean, stddev = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
44
  image = khandy.normalize_image_value(image, mean, stddev, 'auto')
45
  # to tensor
46
- image = np.transpose(image, (2,0,1))
47
  image = np.expand_dims(image, axis=0)
48
  return image
49
-
50
  def predict(self, image):
51
  inputs = self._preprocess(image)
52
  logits = self.forward(inputs)
53
  probs = khandy.softmax(logits)
54
  return probs
55
-
56
  def identify(self, image, topk=5):
57
  assert isinstance(topk, int)
58
  if topk <= 0 or topk > self.num_classes:
59
  topk = self.num_classes
60
-
61
  probs = self.predict(image)
62
  topk_probs, topk_indices = khandy.top_k(probs, topk)
63
 
@@ -67,5 +68,3 @@ class InsectIdentifier(OnnxModel):
67
  one_result['probability'] = prob
68
  results.append(one_result)
69
  return results
70
-
71
-
 
1
  import os
2
  import copy
 
 
3
  import khandy
4
  import numpy as np
 
5
  from .base import OnnxModel
6
+ from collections import OrderedDict
7
  from .base import check_image_dtype_and_shape
8
 
9
 
10
  class InsectIdentifier(OnnxModel):
11
  def __init__(self):
12
  current_dir = os.path.dirname(os.path.abspath(__file__))
13
+ model_path = os.path.join(
14
+ current_dir, './models/quarrying_insect_identifier.onnx')
15
+ label_map_path = os.path.join(
16
+ current_dir, './models/quarrying_insectid_label_map.txt')
17
  super(InsectIdentifier, self).__init__(model_path)
18
+
19
  self.label_name_dict = self._get_label_name_dict(label_map_path)
20
+ self.names = [self.label_name_dict[i]['chinese_name']
21
+ for i in range(len(self.label_name_dict))]
22
  self.num_classes = len(self.label_name_dict)
23
 
24
  @staticmethod
 
27
  label_name_dict = {}
28
  for record in records:
29
  label, chinese_name, latin_name = record.split(',')
30
+ label_name_dict[int(label)] = OrderedDict([('chinese_name', chinese_name),
31
  ('latin_name', latin_name)])
32
  return label_name_dict
33
+
34
  @staticmethod
35
  def _preprocess(image):
36
  check_image_dtype_and_shape(image)
37
+
38
  # image size normalization
39
  image = khandy.letterbox_image(image, 224, 224)
40
  # image channel normalization
 
44
  mean, stddev = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
45
  image = khandy.normalize_image_value(image, mean, stddev, 'auto')
46
  # to tensor
47
+ image = np.transpose(image, (2, 0, 1))
48
  image = np.expand_dims(image, axis=0)
49
  return image
50
+
51
  def predict(self, image):
52
  inputs = self._preprocess(image)
53
  logits = self.forward(inputs)
54
  probs = khandy.softmax(logits)
55
  return probs
56
+
57
  def identify(self, image, topk=5):
58
  assert isinstance(topk, int)
59
  if topk <= 0 or topk > self.num_classes:
60
  topk = self.num_classes
61
+
62
  probs = self.predict(image)
63
  topk_probs, topk_indices = khandy.top_k(probs, topk)
64
 
 
68
  one_result['probability'] = prob
69
  results.append(one_result)
70
  return results
 
 
insectid/models/quarrying_insect_detector.onnx DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:3d1c0615c8dc604248d1b8c48c8414a7b7a84d653ce54ba612ff928ba8f38745
3
- size 28315428
 
 
 
 
insectid/models/quarrying_insect_identifier.onnx DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:3e9c9c498633cbb7797560009e1826b34a6ed2aa5b95c9e7b1e184ad8cbb2355
3
- size 22675272