isLinXu commited on
Commit
3684411
·
1 Parent(s): d887cc3

update app

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system("pip install opencv-python")
3
+ os.system("pip install tensorflow")
4
+ os.system("pip install modelscope")
5
+
6
+ import gradio as gr
7
+ import cv2
8
+ import PIL.Image as Image
9
+ import numpy as np
10
+ import torch
11
+ from PIL import ImageFont
12
+ from modelscope.pipelines import pipeline
13
+ from modelscope.utils.constant import Tasks
14
+
15
+ import warnings
16
+ warnings.filterwarnings("ignore")
17
+
18
+ def recognize_image(img):
19
+ # 加载模型
20
+ general_recognition = pipeline(Tasks.general_recognition, model='damo/cv_resnest101_general_recognition')
21
+ img = np.array(img)
22
+ # 模型推理
23
+ result = general_recognition(img)
24
+ print(result)
25
+ # 绘制分数和类别信息
26
+ scores = result['scores']
27
+ labels = result['labels']
28
+
29
+ # 读取中文字体文件
30
+ font = cv2.FONT_HERSHEY_SIMPLEX
31
+ font_scale = 1
32
+ font_color = (0, 0, 255)
33
+ thickness = 2
34
+
35
+ for i in range(len(scores)):
36
+ score = scores[i]
37
+ label = labels[i]
38
+ img = cv2.putText(img, f"{label}: {score:.2f}", (10, 30), font, font_scale, font_color, thickness)
39
+
40
+ img_pil = Image.fromarray(np.uint8(img))
41
+ return img_pil
42
+
43
+
44
+ def download_test_image():
45
+ # Images
46
+ torch.hub.download_url_to_file(
47
+ 'https://pailitao-image-recog.oss-cn-zhangjiakou.aliyuncs.com/mufan/img_data/maas_test_data/dog.png',
48
+ 'dog.png')
49
+
50
+
51
+ download_test_image()
52
+ input_image = gr.inputs.Image(type='pil', label="输入图片")
53
+ output_image = gr.outputs.Image(type='pil', label="输出图片")
54
+ examples = [["dog.png"]]
55
+ title = "万物识别-中文-通用领域 web demo"
56
+ interface = gr.Interface(fn=recognize_image,
57
+ inputs=input_image,
58
+ outputs=output_image,
59
+ examples=examples,
60
+ title=title)
61
+
62
+ interface.launch()