admin commited on
Commit
7f341cf
·
1 Parent(s): e45575c
Files changed (4) hide show
  1. .gitignore +2 -1
  2. app.py +61 -51
  3. requirements.txt +2 -1
  4. utils.py +29 -2
.gitignore CHANGED
@@ -1,7 +1,8 @@
1
- __pycache__/
2
  _local/
3
  *.pyc
4
  local_models_*/
5
  rename.sh
6
  *.onnx
7
  simsun.ttc
 
 
1
+ *__pycache__*
2
  _local/
3
  *.pyc
4
  local_models_*/
5
  rename.sh
6
  *.onnx
7
  simsun.ttc
8
+ test.*
app.py CHANGED
@@ -4,75 +4,85 @@ import numpy as np
4
  import gradio as gr
5
  from PIL import Image
6
  from insectid import InsectDetector, InsectIdentifier
7
- from utils import MODEL_DIR
8
 
9
 
10
  def infer(filename: str):
11
- if not filename:
12
- None, "Please upload a picture"
 
 
 
13
 
14
- detector = InsectDetector()
15
- identifier = InsectIdentifier()
16
- image = khandy.imread(filename)
17
- if image is None:
18
- return None
19
 
20
- if max(image.shape[:2]) > 1280:
21
- image = khandy.resize_image_long(image, 1280)
22
 
23
- image_for_draw = image.copy()
24
- image_height, image_width = image.shape[:2]
25
- boxes, confs, classes = detector.detect(image)
26
- text = "Unknown"
27
- for box, _, _ in zip(boxes, confs, classes):
28
- box = box.astype(np.int32)
29
- box_width = box[2] - box[0] + 1
30
- box_height = box[3] - box[1] + 1
31
- if box_width < 30 or box_height < 30:
32
- continue
33
 
34
- cropped = khandy.crop_or_pad(image, box[0], box[1], box[2], box[3])
35
- results = identifier.identify(cropped)
36
- print(results[0])
37
- prob = results[0]["probability"]
38
- if prob >= 0.10:
39
- text = "{}: {:.2f}%".format(
40
- results[0]["latin_name"],
41
- 100.0 * results[0]["probability"],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  )
43
 
44
- position = [box[0] + 2, box[1] - 20]
45
- position[0] = min(max(position[0], 0), image_width)
46
- position[1] = min(max(position[1], 0), image_height)
47
- cv2.rectangle(
48
- image_for_draw,
49
- (box[0], box[1]),
50
- (box[2], box[3]),
51
- (0, 255, 0),
52
- 2,
53
- )
54
- image_for_draw = khandy.draw_text(
55
- image_for_draw,
56
- text,
57
- position,
58
- font=f"{MODEL_DIR}/simsun.ttc",
59
- font_size=15,
60
- )
61
 
62
- outxt = text.split(":")[0] if ":" in text else text
63
- return Image.fromarray(image_for_draw[:, :, ::-1], mode="RGB"), outxt
64
 
65
 
66
  if __name__ == "__main__":
67
  with gr.Blocks() as demo:
68
  gr.Interface(
69
  fn=infer,
70
- inputs=gr.Image(label="Upload insect picture", type="filepath"),
71
  outputs=[
72
- gr.Image(label="Recognition result", show_share_button=False),
73
- gr.Textbox(label="Best match", show_copy_button=True),
 
74
  ],
75
- title="Image file format support PNG, JPG, JPEG and BMP, and the file size does not exceed 10M.",
76
  examples=[
77
  f"{MODEL_DIR}/examples/butterfly.jpg",
78
  f"{MODEL_DIR}/examples/beetle.jpg",
 
4
  import gradio as gr
5
  from PIL import Image
6
  from insectid import InsectDetector, InsectIdentifier
7
+ from utils import _L, MODEL_DIR, EN_US
8
 
9
 
10
  def infer(filename: str):
11
+ status = "Success"
12
+ result = outxt = None
13
+ try:
14
+ if not filename:
15
+ raise ValueError("请上传图片")
16
 
17
+ detector = InsectDetector()
18
+ identifier = InsectIdentifier()
19
+ image = khandy.imread(filename)
20
+ if image is None:
21
+ raise ValueError("图片读取失败")
22
 
23
+ if max(image.shape[:2]) > 1280:
24
+ image = khandy.resize_image_long(image, 1280)
25
 
26
+ image_for_draw = image.copy()
27
+ image_height, image_width = image.shape[:2]
28
+ boxes, confs, classes = detector.detect(image)
29
+ text = _L("未知")
30
+ for box, _, _ in zip(boxes, confs, classes):
31
+ box = box.astype(np.int32)
32
+ box_width = box[2] - box[0] + 1
33
+ box_height = box[3] - box[1] + 1
34
+ if box_width < 30 or box_height < 30:
35
+ continue
36
 
37
+ cropped = khandy.crop_or_pad(image, box[0], box[1], box[2], box[3])
38
+ results = identifier.identify(cropped)
39
+ print(results[0])
40
+ prob = results[0]["probability"]
41
+ if prob >= 0.10:
42
+ text = "{} {}: {:.2f}%".format(
43
+ results[0]["chinese_name"],
44
+ results[0]["latin_name"],
45
+ 100.0 * results[0]["probability"],
46
+ )
47
+
48
+ position = [box[0] + 2, box[1] - 20]
49
+ position[0] = min(max(position[0], 0), image_width)
50
+ position[1] = min(max(position[1], 0), image_height)
51
+ cv2.rectangle(
52
+ image_for_draw,
53
+ (box[0], box[1]),
54
+ (box[2], box[3]),
55
+ (0, 255, 0),
56
+ 2,
57
+ )
58
+ image_for_draw = khandy.draw_text(
59
+ image_for_draw,
60
+ text,
61
+ position,
62
+ font=None if EN_US else f"{MODEL_DIR}/simsun.ttc",
63
+ font_size=15,
64
  )
65
 
66
+ outxt = text.split(":")[0] if ":" in text else text
67
+ result = Image.fromarray(image_for_draw[:, :, ::-1], mode="RGB")
68
+
69
+ except Exception as e:
70
+ status = f"{e}"
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
+ return status, result, outxt
 
73
 
74
 
75
  if __name__ == "__main__":
76
  with gr.Blocks() as demo:
77
  gr.Interface(
78
  fn=infer,
79
+ inputs=gr.Image(label=_L("上传昆虫照片"), type="filepath"),
80
  outputs=[
81
+ gr.Textbox(label=_L("状态栏"), show_copy_button=True),
82
+ gr.Image(label=_L("识别结果"), show_share_button=False),
83
+ gr.Textbox(label=_L("最可能的物种"), show_copy_button=True),
84
  ],
85
+ title=_L("图像文件格式支持 PNG, JPG, JPEG BMP, 且文件大小不超过 10M"),
86
  examples=[
87
  f"{MODEL_DIR}/examples/butterfly.jpg",
88
  f"{MODEL_DIR}/examples/beetle.jpg",
requirements.txt CHANGED
@@ -3,4 +3,5 @@ Pillow
3
  requests
4
  onnxruntime
5
  numpy>=1.11.1
6
- opencv-python>=4.5
 
 
3
  requests
4
  onnxruntime
5
  numpy>=1.11.1
6
+ opencv-python>=4.5
7
+ modelscope[framework]==1.21
utils.py CHANGED
@@ -1,3 +1,30 @@
1
- from huggingface_hub import snapshot_download
 
 
2
 
3
- MODEL_DIR = snapshot_download("Genius-Society/insecta", cache_dir="./__pycache__")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import modelscope
3
+ import huggingface_hub
4
 
5
+ EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
6
+
7
+ MODEL_DIR = (
8
+ huggingface_hub.snapshot_download(
9
+ "Genius-Society/insecta",
10
+ cache_dir="./__pycache__",
11
+ )
12
+ if EN_US
13
+ else modelscope.snapshot_download(
14
+ "Genius-Society/insecta",
15
+ cache_dir="./__pycache__",
16
+ )
17
+ )
18
+
19
+ ZH2EN = {
20
+ "上传昆虫照片": "Upload insect picture",
21
+ "状态栏": "Status",
22
+ "识别结果": "Recognition result",
23
+ "最可能的物种": "Best match",
24
+ "图像文件格式支持 PNG, JPG, JPEG 和 BMP, 且文件大小不超过 10M": "Image file format support PNG, JPG, JPEG and BMP, and the file size does not exceed 10M.",
25
+ "未知": "Unknown",
26
+ }
27
+
28
+
29
+ def _L(zh_txt: str):
30
+ return ZH2EN[zh_txt] if EN_US else zh_txt