- Error1:
[root@AIcentos7 code3]# python3 face_rec.py
Traceback (most recent call last):
File "face_rec.py", line 5, in <module>
import cv2
ImportError: No module named 'cv2'
solution1:
[root@AIcentos7 code3]# pip3 install opencv-python
Collecting opencv-python
Downloading https://files.pythonhosted.org/packages/59/de/208f66a8f57a8b32536c5f7ca5e883cb15ddae8032164ea192fa103d50f6/opencv_python-4.0.0.21-cp35-cp35m-manylinux1_x86_64.whl (25.4MB)
100% |████████████████████████████████| 25.4MB 534kB/s
Requirement already satisfied: numpy>=1.11.1 in /opt/python3.5/lib/python3.5/site-packages (from opencv-python) (1.15.2)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.0.0.21
error2 :matplotlib 图坐标中文显示乱码
参考:
https://blog.csdn.net/u012150360/article/details/72921855
https://blog.csdn.net/huuinn/article/details/78968966
亲测有效解决办法:
解决思路
- 准备中文字体
- matplotlib指定字体为中文字体
- 清空matplotlib使配置生效
Python版本及matplotlib配置文件位置查询
[root@AIcentos7 share]# python3
Python 3.5.1 (default, Oct 8 2018, 15:12:06)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/opt/python3.5/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc'
>>> quit()
[root@AIcentos7 share]#
准备中文字体
C:\Windows\winsxs\amd64_microsoft-windows-font-truetype-simhei_31bf3856ad364e35_6.1.7600.16385_none_501ca8058dc5e9fb\simhei.ttf
传递到/opt/python3.5/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf
[root@AIcentos7 ttf]# pwd
/opt/python3.5/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf
[root@AIcentos7 ttf]# ls -t
simhei.ttf LICENSE_DEJAVU STIXNonUniIta.ttf STIXGeneralBol.ttf
DejaVuSansDisplay.ttf STIXGeneralBolIta.ttf STIXSizFiveSymReg.ttf DejaVuSans.ttf
cmb10.ttf DejaVuSerif-Italic.ttf DejaVuSansMono-Oblique.ttf DejaVuSerifDisplay.ttf
DejaVuSansMono-Bold.ttf DejaVuSerif-Bold.ttf cmtt10.ttf DejaVuSans-Oblique.ttf
DejaVuSansMono.ttf STIXSizOneSymBol.ttf STIXNonUni.ttf STIXSizFourSymBol.ttf
STIXSizThreeSymBol.ttf DejaVuSans-BoldOblique.ttf DejaVuSansMono-BoldOblique.ttf STIXSizFourSymReg.ttf
cmr10.ttf DejaVuSerif.ttf STIXSizThreeSymReg.ttf STIXSizOneSymReg.ttf
DejaVuSans-Bold.ttf LICENSE_STIX local.conf STIXSizTwoSymBol.ttf
cmss10.ttf cmex10.ttf STIXNonUniBol.ttf STIXNonUniBolIta.ttf
cmsy10.ttf STIXGeneralItalic.ttf STIXSizTwoSymReg.ttf
STIXGeneral.ttf cmmi10.ttf DejaVuSerif-BoldItalic.ttf
matplotlib指定字体为中文字体
[root@AIcentos7 ttf]#vi matplotlibrc
# 搜索font.family配置项,将其#注释去掉,并将:号后面的值改为字段对应的名字。
font.family : SimHei
# 搜索axes.unicode_minus配置项,将其#注释去掉,并将:号后面的值改为False
axes.unicode_minus : False
[root@AIcentos7 mpl-data]# diff matplotlibrc matplotlibrc.org
193c193
< font.family :simhei
---
> #font.family : sans-serif
204,205c204,205
< font.serif : DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
< font.sans-serif : DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
---
> #font.serif : DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
> #font.sans-serif : DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
320c320
< axes.unicode_minus : False ## use unicode for the minus symbol
---
> #axes.unicode_minus : True ## use unicode for the minus symbol
[root@AIcentos7 mpl-data]# pwd
/opt/python3.5/lib/python3.5/site-packages/matplotlib/mpl-data
[root@AIcentos7 mpl-data]#
清空matplotlib使配置生效
[root@AIcentos7 mpl-data]# rm ~/.cache/matplotlib -R
rm: descend into directory ‘/root/.cache/matplotlib’? y
rm: remove regular file ‘/root/.cache/matplotlib/fontlist-v300.json’? y
rm: remove directory ‘/root/.cache/matplotlib/tex.cache’? y
rm: remove directory ‘/root/.cache/matplotlib’? y
[root@AIcentos7 mpl-data]#
测试最终效果图
[root@AIcentos7 matplotlib]# cat zhtest.py
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y = x * 2
plt.title("一元一次函数")
plt.plot(x, y)
plt.show()
[root@AIcentos7 matplotlib]#