解决openCV的cv2.putText函数不能显示中文问题

在进行人脸识别的时候,执行下列代码,使用cv2.putText()函数标注人脸名字,但中文始终显示???,尝试了很多种方法依旧无果,询问ai,它说使用ecode函数把name转为‘utf-8’,但报错说putText需要传入一个字符串,于是我把putText函数中的name改为str(name),代码如下,中文就可以正常显示了

中文显示乱码的源代码:

    for (top, right, bottom, left), name in zip(face_locations, face_names):
        if not name:
            continue
        # 绘制脸部区域框
        cv2.rectangle(frame,(left,top),(right,bottom), (0, 0, 255), 2)
        # 在脸部区域下面绘制人名
        cv2.rectangle(frame, (left, bottom - 25),
                      (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame,name, (left + 6, bottom - 6), font, 1, (255, 255, 255), 1)

中文正常显示的代码:

    for (top, right, bottom, left), name in zip(face_locations, face_names):
        if not name:
            continue
        # 绘制脸部区域框
        cv2.rectangle(frame,(left,top),(right,bottom), (0, 0, 255), 2)
        # 在脸部区域下面绘制人名
        cv2.rectangle(frame, (left, bottom - 25),
                      (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame,str(name), (left + 6, bottom - 6), font, 1, (255, 255, 255), 1)

注:传入face_names是一个中文的列表,例如:face_names=['人名1', '人名2', '人名3']

但name本身的类型就是str,str(name)之后也依旧是str:

虽然解决中文显示乱码的问题,但其中原理我没有查到,希望有懂的佬可以解答一下,感恩!!!也希望可以帮到各位

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值