CV2.putText不支持中文,在实时视频流上写入汉字的方法
#coding=utf-8
import cv2
import numpy
from PIL import Image, ImageDraw, ImageFont
def cv2ImgAddText(img, text):
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
fillColor=(255,0,0)
fontStyle = ImageFont.truetype("font/simsun.ttc", 40, encoding='utf-8')
draw.text((0, 0), text, font=fontStyle ,fill=fillColor)
return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR) # 转换回OpenCV格式
def main():
cap = cv2.VideoCapture(0)
frame_count = 1
success = True
while(success):
success, frame = cap.read()
frame=cv2ImgAddText(frame, "这是一段中文")
cv2.imshow("Image", frame)
# Press 'q' to quit
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__=="__main__":
#draw_img()
main()