在进行人脸识别的时候,执行下列代码,使用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)
# 在脸部区域下面绘制人名