在进行人脸识别的时候,执行下列代码,使用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:
![]()
虽然解决中文显示乱码的问题,但其中原理我没有查到,希望有懂的佬可以解答一下,感恩!!!也希望可以帮到各位
2185

被折叠的 条评论
为什么被折叠?



