-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
134 lines (104 loc) · 4.9 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import argparse
import gradio as gr
import numpy as np
import torch
from ram_pipeline import RAM
from PIL import Image
import time
parser = argparse.ArgumentParser(prog=__file__)
# parser.add_argument('--input', type=str, default='./test_imgs', help='path of input')
# parser.add_argument('--dev_id', type=int, default=0, help='device id')
# parser.add_argument('--conf_thresh', type=float, default=0.25, help='det confidence threshold')
# parser.add_argument('--nms_thresh', type=float, default=0.7, help='det nms threshold')
# parser.add_argument('--single_output', action='store_true', help='det confidence threshold')
args = parser.parse_args()
ram = RAM()
def recognize_anything(img_path, return_anno_img=False):
if img_path is None or return_anno_img is None:
return '[ INVALID INPUT ]', None
ram_res = ram(img_path, return_bbox=return_anno_img)
tags = [ram_res['tag_ch'][i]+ram_res['tag_en'][i] for i in range(len(ram_res['tag_ch']))]
print(tags)
return ' || '.join(tags), ram_res['img_res']
def dino_annotate(img_path, text_description):
if img_path is None or text_description is None or text_description.strip()=="":
return '[ INVALID INPUT ]', None
dino_res = ram.get_bbox(img_path, [text_description])
return "[ SUCCESS ]", dino_res
# Description
title = f"<center><strong><font size='8'>万物检测⭐powered by 1684x <font></strong></center>"
description_e = f"""### 这是在1684X上部署[Recognize Anything Model (RAM)](https://github.com/xinyu1205/recognize-anything)+GroundingDINO实现万物检测的示例。
"""
default_example = ["./resources/image/demo1.jpg", "./resources/image/demo3.jpg"]
css = "h1 { text-align: center } .about { text-align: justify; padding-left: 10%; padding-right: 10%; }"
with gr.Blocks(css=css, title="万物检测") as demo:
with gr.Row():
with gr.Column(scale=1):
# Title
gr.Markdown(title)
gr.Markdown(description_e)
with gr.Tab('检测一切'):
description_p = """ # 使用方法
1. 上传需要检测的图像。
2. 若需要框出物体位置,请勾选“标注位置”。
3. 点击“检测”。
"""
with gr.Row():
with gr.Column():
img_inputs = gr.Image(label="选择图片", value=default_example[0], sources=['upload'], type='filepath')
using_dino = gr.Checkbox(label="标注位置", value=True)
with gr.Column():
tags_area = gr.Textbox(label='标签', interactive=False)
annotated_img = gr.Image(label="检测结果", interactive=False)
# Submit & Clear
with gr.Row():
with gr.Column():
with gr.Row():
with gr.Column():
det_btn_p = gr.Button(
"检测", variant="primary"
)
clear_btn_p = gr.Button("清空", variant="secondary")
with gr.Column():
# Description
gr.Markdown(description_p)
det_btn_p.click(
recognize_anything, inputs=[img_inputs, using_dino], outputs=[tags_area, annotated_img]#, json_str]
)
def clear():
return [None, None, None]
clear_btn_p.click(clear, outputs=[img_inputs, tags_area, annotated_img])
with gr.Tab('检测【指定物】'):
description_p = """ # 使用方法
1. 上传需要检测的图像。
2. 填写物体描述【英文】。
3. 点击“检测”。
"""
with gr.Row():
with gr.Column():
dino_img_input = gr.Image(label="选择图片", value=default_example[0], sources=['upload'], type='filepath')
text_input = gr.Text(label="需检测物体的描述")
with gr.Column():
msg_area = gr.Textbox(label='Log', interactive=False)
annotated_img = gr.Image(label="检测结果", interactive=False)
# Submit & Clear
with gr.Row():
with gr.Column():
with gr.Row():
with gr.Column():
det_btn_p = gr.Button(
"检测", variant="primary"
)
clear_btn_p = gr.Button("清空", variant="secondary")
with gr.Column():
# Description
gr.Markdown(description_p)
det_btn_p.click(
dino_annotate, inputs=[dino_img_input, text_input], outputs=[msg_area, annotated_img]#, json_str]
)
def clear():
return [None, None, None]
clear_btn_p.click(clear, outputs=[dino_img_input, annotated_img, msg_area])
demo.queue()
demo.launch(ssl_verify=False, server_name="0.0.0.0")