OpenCV添加中文文字
代码如下,随机在摄像头中添加文字
import cv2
import numpy as np
from random import randint
from PIL import Image, ImageDraw, ImageFont
COLOR = (255,0,0)
font_size = 24
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret,frame = cap.read()
width,height = frame.shape[:2]
w = randint(40,width-40)
h = randint(40,height-40)
img = Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",font_size)
draw.text((w,h),"不明物体",COLOR,font=font)
pil_image = np.array(img)
frame = cv2.cvtColor(pil_image, cv2.COLOR_RGB2BGR)
cv2.imshow("win",frame)
key = cv2.waitKey(1000//30)
if (key & 0xFF == ord("q")):
break
cap.release()
cv2.destroyAllWindows()
# https://docs.opencv.org/4.8.0/d9/dfa/classcv_1_1freetype_1_1FreeType2.html
# https://blog.51cto.com/u_12865/10721866
Linux上使用fc-list :lang=zh
列举中文字体地址,如/usr/share/fonts/truetype/wqy/wqy-microhei.ttc。
另外还可以参考
https://stackoverflow.com/questions/37191008/load-truetype-font-to-opencv
https://docs.opencv.org/4.1.1/d9/dfa/classcv_1_1freetype_1_1FreeType2.html
https://docs.opencv.org/4.x/d4/dfc/group__freetype.html
https://docs.opencv.org/4.x/d5/de5/tutorial_py_setup_in_windows.html
https://www.cnblogs.com/touch-skyer/p/14343791.html
https://forum.opencv.org/t/module-cv2-has-no-attribute-freetype/12489
https://github.com/BabaGodPikin/Build-OpenCv-for-Python-with-Extra-Modules-Windows-10
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
apt-get install libharfbuzz-dev libfreetype-dev python3-numpy libgtk2.0-dev
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ -DBUILD_opencv_legacy=OFF -D PYTHON3_PACKAGES_PATH=\((python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D PYTHON3_NUMPY_INCLUDE_DIRS=\)(python3 -c "import numpy; print(numpy.get_include())") ..
sudo make install
参数
-D PYTHON3_EXECUTABLE=\((which python3) \
-D PYTHON_INCLUDE_DIR=\)(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")
-D PYTHON_INCLUDE_DIR2=\((python3 -c "from os.path import dirname; from distutils.sysconfig import get_config_h_filename; print(dirname(get_config_h_filename()))") \
-D PYTHON_LIBRARY=\)(python3 -c "from distutils.sysconfig import get_config_var;from os.path import dirname,join ; print(join(dirname(get_config_var('LIBPC')),get_config_var('LDLIBRARY')))")
-D PYTHON3_NUMPY_INCLUDE_DIRS=\((python3 -c "import numpy; print(numpy.get_include())") \
-D PYTHON3_PACKAGES_PATH=\)(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
参考文章:
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc
>>> ft = cv2.freetype.createFreeType2()
>>> ft.loadFontData(fontFileName="/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",idx=0)
>>> ft.putText(img,text="天气真好",org=(100,200),fontHeight=32,color=(0,0,255),thickness=-1,line_type=cv2.LINE_AA,bottomLeftOrigin=True)
>>> cv2.imwrite("~/test.jpg",img)