利用yolo进行目标识别时,输出的是英文标签,如下图,不便于用户阅读,因此需要转换为中文标签,我采取的办法是对yolo predefined的80类进行了“汉化”,以实现输出中文标签。
不知道是否有更便捷的方法,只是提供了一种思路,供大家参考。
首先获取LOYO中label名称
打开LOYO文件coco.nameslabel,获取这80类label名称,并译为中文
predefined_En=['person','bicycle','car','motorbike','aeroplane','bus','train','truck','boat','traffic light',
'fire hydrant','stop sign','parking meter','bench','bird','cat','dog','horse','sheep','cow',
'elephant','bear','zebra','giraffe','backpack','umbrella','handbag','tie','suitcase','frisbee',
'skis','snowboard','sports ball','kite','baseball bat','baseball glove','skateboard','surfboard','tennis racket','bottle',
'wine glass','cup','fork','knife','spoon','bowl','banana','apple','sandwich','orange',
'broccoli','carrot','hot dog','pizza','donut','cake','chair','sofa','pottedplant','bed',
'diningtable','toilet','tvmonitor','laptop','mouse','remote','keyboard','cell phone','microwave','oven',
'toaster','sink','refrigerator','book','clock','vase','scissors','teddy bear','hair drier','toothbrush']
predefined_CN=['人','自行车','汽车','摩托车','飞机','公共汽车','火车','卡车','船','红绿灯',
'消防栓','停止标志','停车收费表','板凳','鸟','猫','狗','马','羊','牛',
'大象','熊','斑马','长颈鹿','背包','雨伞','手提包','领带','手提箱','飞盘',
'滑雪板','单板滑雪','运动球类','风筝','棒球棒','棒球手套','滑板','冲浪板','网球拍','瓶子',
'红酒杯','杯子','叉子','刀','勺子','碗','香蕉','苹果','三明治','橘子',
'西兰花','胡萝卜','热狗','比萨','甜甜圈','蛋糕','椅子','沙发','盆栽','床',
'餐桌','厕所','电视监视器','笔记本电脑','老鼠','遥控器','键盘','手机','微波炉','烤箱',
'烤面包机','水槽','冰箱','书','时钟','花瓶','剪刀','泰迪熊','吹风机','牙刷']
输出中文标签
OpenCV中可以利用putText()在图片上添加英文,但是添加中文则会出现乱码,因此需要:
1)将OpenCV图片格式转换成PIL的图片格式;
2)使用PIL绘制文字;
3)PIL图片格式转换成OpenCV的图片格式;
def cv2ImgAddText(img, text, left, top, textColor=(255, 0, 0), textSize=20):
if (isinstance(img, np.ndarray)): # 判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img)
# 字体的格式
fontStyle = ImageFont.truetype(
"font/simsun.ttc", textSize, encoding="utf-8")
# 绘制文本
draw.text((left, top), text, textColor, font=fontStyle)
# 转换回OpenCV格式
result=cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
return result
此方法转载自:https://blog.youkuaiyun.com/baidu_37366055/article/details/81627185
完整代码
from test import video_demo
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
def E2C(label):
predefined_En=['person','bicycle','car','motorbike','aeroplane','bus','train','truck','boat','traffic light',
'fire hydrant','stop sign','parking meter','bench','bird','cat','dog','horse','sheep','cow',
'elephant','bear','zebra','giraffe','backpack','umbrella','handbag','tie','suitcase','frisbee',
'skis','snowboard','sports ball','kite','baseball bat','baseball glove','skateboard','surfboard','tennis racket','bottle',
'wine glass','cup','fork','knife','spoon','bowl','banana','apple','sandwich','orange',
'broccoli','carrot','hot dog','pizza','donut','cake','chair','sofa','pottedplant','bed',
'diningtable','toilet','tvmonitor','laptop','mouse','remote','keyboard','cell phone','microwave','oven',
'toaster','sink','refrigerator','book','clock','vase','scissors','teddy bear','hair drier','toothbrush']
predefined_CN=['人','自行车','汽车','摩托车','飞机','公共汽车','火车','卡车','船','红绿灯',
'消防栓','停止标志','停车收费表','板凳','鸟','猫','狗','马','羊','牛',
'大象','熊','斑马','长颈鹿','背包','雨伞','手提包','领带','手提箱','飞盘',
'滑雪板','单板滑雪','运动球类','风筝','棒球棒','棒球手套','滑板','冲浪板','网球拍','瓶子',
'红酒杯','杯子','叉子','刀','勺子','碗','香蕉','苹果','三明治','橘子',
'西兰花','胡萝卜','热狗','比萨','甜甜圈','蛋糕','椅子','沙发','盆栽','床',
'餐桌','厕所','电视监视器','笔记本电脑','老鼠','遥控器','键盘','手机','微波炉','烤箱',
'烤面包机','水槽','冰箱','书','时钟','花瓶','剪刀','泰迪熊','吹风机','牙刷']
#找到英文label名称在list中的位置
loc = predefined_En.index(label)
#显示对应位置的中文名称
label_CN=predefined_CN[loc]
return label_CN
def cv2ImgAddText(img, text, left, top, textColor=(255, 0, 0), textSize=20):
if (isinstance(img, np.ndarray)): # 判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img)
# 字体的格式
fontStyle = ImageFont.truetype(
"font/simsun.ttc", textSize, encoding="utf-8")
# 绘制文本
draw.text((left, top), text, textColor, font=fontStyle)
# 转换回OpenCV格式
result=cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
return result
if __name__ == '__main__':
#待检测的图片
file='H:/dog.jpg'
#进行LOYO识别
image, info = video_demo(file)
imgA=cv2.imread(file)
#YOLO返回的坐标值和标签、置信度
locx1 = [q1['xtop'] for q1 in info] # 左上角x坐标
locx2 = [q2['ytop'] for q2 in info] # 左上角y坐标
locy1 = [q3['xbottom'] for q3 in info] # 右下角x坐标
locy2 = [q4['ybottom'] for q4 in info] # 右下角y坐标
lable_str = [q5['label'] for q5 in info] # 标签
confidence = [q6['confidence'] for q6 in info] # 置信度
B = len(locx1)
for l in range(0, B):
x2 = (locx1[l], locx2[l])
y2 = (locy1[l], locy2[l])
#英文标签转换为中文标签
label_CN = E2C(lable_str[l])
#输出中文标签和置信度
text = "{}: {}".format(label_CN, confidence[l])
#在imgA上画出矩形
cv2.rectangle(imgA, x2, y2, (0, 0, 255), 2)
#在imgA上显示中文标签+置信度
imgA = cv2ImgAddText(imgA, text, locx1[l], locx2[l])
cv2.imshow('Image', imgA)
cv2.waitKey(0)