Skip to content

Commit 3c87493

Browse files
committed
新增图片添加中文字符串支持
1 parent aa37431 commit 3c87493

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Code segment are often used in deep learning algorithms(pytorch/numpy)
3232

3333
### 2 img_show
3434

35-
​ opencv写的图片显示功能,支持单张图、多张图、多张图合并、多张图自定义行列数显示功能;支持bbox显示
35+
​ opencv写的图片显示功能,支持单张图、多张图、多张图合并、多张图自定义行列数显示功能;支持bbox显示;支持中文显示
3636

3737
### 3 pr_roc
3838

docs/detail.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ ImageHelper.show_bbox(img, bbox_list, color=(255, 0, 0), font_scale=0.2, thickne
3636
ImageHelper.show_bbox(img, bbox_list, color=(255, 0, 0), font_scale=0.2, thickness=1,is_without_mask=True)
3737
```
3838

39+
支持中文
40+
41+
```python
42+
img = ImageHelper.cv2ImgAddText(img, '这是一个测试中文代码', 10, 10, textSize=8, font='./NotoSansCJK-Bold')
43+
```
44+
45+
需要注意的是需要字体库支持,我采用的是NotoSansCJK-Bold,后缀是.ttc,ubuntu系统下有,可以查找出来。
46+
47+
48+
3949
## 3 pr_roc
50+
4051
[c3_pr_roc](../library/c3_pr_roc.py)
4152

4253
​ 分类问题中,对于类别不平衡问题,采用acc指标是不够的,pr曲线和roc曲线绘制来评估模型性能比较关键。

library/c2_img_show.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ def convert_gray_to_bgr(img):
200200
bgr_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
201201
return bgr_img
202202

203+
@staticmethod
204+
def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20, font='./NotoSansCJK-Bold.ttc'):
205+
from PIL import Image, ImageDraw, ImageFont
206+
if isinstance(img, np.ndarray): # 判断是否OpenCV图片类型
207+
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
208+
draw = ImageDraw.Draw(img)
209+
fontText = ImageFont.truetype(font, textSize, encoding="utf-8")
210+
draw.text((left, top), text, textColor, font=fontText)
211+
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
212+
203213
@staticmethod
204214
def show_img(imgs, window_names=None, wait_time_ms=0, is_merge=False, row_col_num=(1, -1)):
205215
"""
@@ -333,4 +343,4 @@ def show_bbox(image, bboxs_list, color=None,
333343
font, font_scale, (255, 255, 255), thickness=thickness, lineType=cv2.LINE_AA)
334344
if is_show:
335345
ImageHelper.show_img(image_copy, names, wait_time_ms)
336-
return image_copy
346+
return image_copy

tools/c2_img_show.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ def demo_img_show():
2929
ImageHelper.show_img([img, img2, img3, img4, img5], is_merge=True, row_col_num=(2, 3)) # 合并显示,指定数目
3030

3131

32+
# 显示中文,需要字体库
33+
# 输入图片默认是opencv读出来的bgr格式
34+
img = ImageHelper.cv2ImgAddText(img, '这是一个测试中文代码', 10, 10, textSize=8, font='./NotoSansCJK-Bold')
35+
ImageHelper.show_img(img)
36+
37+
38+
3239
def demo_bbox_show():
3340
input_shape = [400, 400, 3]
3441
img = np.zeros(input_shape, np.uint8)

0 commit comments

Comments
 (0)