paddleDetection:OpenCV检测框转中文

注:OpenCV不能直接显示中文,通过PIL转换会损失一部分算力性能

修改源码(可视化)

./deploy/python/visualize.py

增加导入字体库和字体文件

1
2
3
from PIL import Image, ImageDraw, ImageFile, ImageFont

font_file = '/exp/work/video/PaddleDetection/deploy/pipeline/SourceHanSansCN-Medium.otf'

visualize_attr

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
def visualize_attr(im, results, boxes=None, is_mtmct=False):
if isinstance(im, str):
im = Image.open(im)
im = np.ascontiguousarray(np.copy(im))
im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
else:
im = np.ascontiguousarray(np.copy(im))

line_inter = im.shape[0] / 40.
text_scale = max(0.5, im.shape[0] / 100.)
# 将nparray图像转PIL图像
im = Image.fromarray(im)

for i, res in enumerate(results):
print(i, res)
if boxes is None:
text_w = 3
text_h = 1
elif is_mtmct:
box = boxes[i] # multi camera, bbox shape is x,y, w,h
text_w = int(box[0]) + 3
text_h = int(box[1])
else:
box = boxes[i] # single camera, bbox shape is 0, 0, x,y, w,h
text_w = int(box[2]) + 3
text_h = int(box[3])
for text in res:
text_h += int(line_inter)
text_loc = (text_w, text_h)
# 写入
draw = ImageDraw.Draw(im)
draw.text(
text_loc,
text,
font=ImageFont.truetype(font_file, size=int(text_scale)), # 字体位置
fill=(0, 255, 255))
# 还原连续存储数组
im = np.ascontiguousarray(np.copy(im))
return im

visualize_vehicleplate

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
def visualize_vehicleplate(im, results, boxes=None):
if isinstance(im, str):
im = Image.open(im)
im = np.ascontiguousarray(np.copy(im))
im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
else:
im = np.ascontiguousarray(np.copy(im))

text_scale = max(1.0, im.shape[0] / 400.)
line_inter = im.shape[0] / 40.
# 将nparray图像转PIL图像
im = Image.fromarray(im)
for i, res in enumerate(results):
if boxes is None:
text_w = 3
text_h = 1
else:
box = boxes[i]
text = res
if text == "":
continue
text_w = int(box[2])
text_h = int(box[5] + box[3])
text_loc = (text_w, text_h)
# 写入
draw = ImageDraw.Draw(im)
draw.text(
text_loc,
text,
font=ImageFont.truetype(font_file, size=int(text_scale)), # 字体位置
fill=(0, 255, 255))
# 还原连续存储数组
im = np.ascontiguousarray(np.copy(im))
return im

visualize_action

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
def visualize_action(im,
mot_boxes,
action_visual_collector=None,
action_text="",
video_action_score=None,
video_action_text=""):
im = cv2.imread(im) if isinstance(im, str) else im
im_h, im_w = im.shape[:2]

text_scale = max(1, im.shape[1] / 40.)
text_thickness = 2
# 将nparray图像转PIL图像
im = Image.fromarray(im)
if action_visual_collector:
id_action_dict = {}
for collector, action_type in zip(action_visual_collector, action_text):
id_detected = collector.get_visualize_ids()
for pid in id_detected:
id_action_dict[pid] = id_action_dict.get(pid, [])
id_action_dict[pid].append(action_type)
for mot_box in mot_boxes:
# mot_box is a format with [mot_id, class, score, xmin, ymin, w, h]
if mot_box[0] in id_action_dict:
text_position = (int(mot_box[3] + mot_box[5] * 0.75),
int(mot_box[4] - 10))
display_text = ', '.join(id_action_dict[mot_box[0]])

draw = ImageDraw.Draw(im)
draw.text(
text_position,
display_text,
font=ImageFont.truetype(size=int(text_scale)), # 字体位置
fill=(0, 0, 255))

if video_action_score:
draw = ImageDraw.Draw(im)
draw.text(
(int(im_w / 2), int(15 * text_scale) + 5),
video_action_text + ': %.2f' % video_action_score,
font=ImageFont.truetype(font_file, size=int(text_scale)), # 字体位置
fill=(0, 0, 255))
# 还原连续存储数组
im = np.ascontiguousarray(np.copy(im))
return im

修改源码(属性)

修改对应中文模块

./deploy/pipeline/

  • pipeline.py

./deploy/pipeline/ppvehicle

  • vehicle_attr.py
  • vehicle_plate.py

./deploy/pipeline/pphuman

  • attr_infer.py

执行脚本

1
2
3
4
5
# pphuman
python deploy/pipeline/pipeline.py --config deploy/pipeline/config/infer_cfg_pphuman.yml --video_file=demo_input/act1.mp4 --device=gpu --output_dir=demo_output

# ppvehicle
python deploy/pipeline/pipeline.py --config deploy/pipeline/config/infer_cfg_ppvehicle.yml --video_file=demo_input/car_t1.mp4 --device=gpu --output_dir=demo_output

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2024 青域 All Rights Reserved.

UV : | PV :