Windows7 64位+python3.6环境下安装opencv的方法
1 windows 下安装python
安装后,如果需要配置环境变量,则配置如下两条
C:\Users\24738\AppData\Local\Programs\Python\Python36
C:\Users\24738\AppData\Local\Programs\Python\Python36\Scripts
D:\__software\人工智能>python
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
2 安装opencv 的库
OpenCV
, a real time computer vision library.
- opencv_python‑2.4.13.2‑cp27‑cp27m‑win32.whl
- opencv_python‑2.4.13.2‑cp27‑cp27m‑win_amd64.whl
- opencv_python‑3.1.0‑cp27‑cp27m‑win32.whl
- opencv_python‑3.1.0‑cp27‑cp27m‑win_amd64.whl
- opencv_python‑3.1.0‑cp34‑cp34m‑win32.whl
- opencv_python‑3.1.0‑cp34‑cp34m‑win_amd64.whl
- opencv_python‑3.2.0+contrib‑cp35‑cp35m‑win32.whl
- opencv_python‑3.2.0+contrib‑cp35‑cp35m‑win_amd64.whl
- opencv_python‑3.2.0+contrib‑cp36‑cp36m‑win32.whl
- opencv_python‑3.2.0+contrib‑cp36‑cp36m‑win_amd64.whl
- opencv_python‑3.2.0‑cp35‑cp35m‑win32.whl
- opencv_python‑3.2.0‑cp35‑cp35m‑win_amd64.whl
- opencv_python‑3.2.0‑cp36‑cp36m‑win32.whl
- opencv_python‑3.2.0‑cp36‑cp36m‑win_amd64.whl
D:\__software\人工智能>
pip install opencv_python-3.2.0-cp36-cp36m-win_amd64.whl
Processing d:\__software\人工智能\opencv_python-3.2.0-cp36-cp36m-win_amd64.whl
Installing collected packages: opencv-python
Successfully installed opencv-python-3.2.0
>>> import cv2
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import
>>>
D:\__software\人工智能>
pip install numpy
Collecting numpy
Downloading numpy-1.13.1-cp36-none-win_amd64.whl (7.8MB)
100% |████████████████████████████████| 7.8MB 132kB/s
Installing collected packages: numpy
Successfully installed numpy-1.13.1
高版本安装后,里面好像没有识别特征目录,又安了个低版本
特征文件如:haarcascade_frontalface_default
.xml
opencv-2.4.13.2-vc14.exe
导出文件夹要移动到
C:\Users\24738\AppData\Local\Programs\Python\Python36\Lib\site-packages\
安装成功
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
4 安装PIL
Pillow
, a replacement for
PIL
, the Python Image Library, which provides image processing functionality and supports many file formats.
Use `
from PIL import Image
` instead of `import Image`.
- Pillow‑3.4.2‑cp36‑cp36m‑win32.whl
- Pillow‑3.4.2‑cp36‑cp36m‑win_amd64.whl
- Pillow‑4.2.1‑cp27‑cp27m‑win32.whl
- Pillow‑4.2.1‑cp27‑cp27m‑win_amd64.whl
- Pillow‑4.2.1‑cp34‑cp34m‑win32.whl
- Pillow‑4.2.1‑cp34‑cp34m‑win_amd64.whl
- Pillow‑4.2.1‑cp35‑cp35m‑win32.whl
- Pillow‑4.2.1‑cp35‑cp35m‑win_amd64.whl
- Pillow‑4.2.1‑cp36‑cp36m‑win32.whl
- Pillow‑4.2.1‑cp36‑cp36m‑win_amd64.whl
- Pillow‑4.2.1‑pp258‑pypy_41‑win32.whl
D:\__software\人工智能>
pip install Pillow-4.2.1-cp36-cp36m-win_amd64.whl
Processing d:\__software\人工智能\pillow-4.2.1-cp36-cp36m-win_amd64.whl
Collecting olefile (from Pillow==4.2.1)
Downloading olefile-0.44.zip (74kB)
100% |████████████████████████████████| 81kB 322kB/s
Installing collected packages: olefile, Pillow
Running setup.py install for olefile ... done
Successfully installed Pillow-4.2.1 olefile-0.44
运行脚本测试
E:\script>python face_detection.py
OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file D:\Build\OpenCV\opencv-3.2.0\modules\objdetect\src\cascadedetect.cpp, line 1681
Traceback (most recent call last):
File "face_detection.py", line 119, in <module>
File "face_detection.py", line 51, in drawFaces
File "face_detection.py", line 24, in detectFaces
cv2.error: D:\Build\OpenCV\opencv-3.2.0\modules\objdetect\src\cascadedetect.cpp:1681: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
cv2.CascadeClassifier("
E:\\script\\haarcascade_frontalface_default.xml")
改成如下两行:
CascadeClassifier里面的目录是对应的安装目录
face_cascade = cv2.CascadeClassifier("
haarcascade_frontalface_default.xml")
face_cascade.load('
C:/Users/24738/AppData/Local/Programs/Python/Python36/Lib/site-packages/opencv/build/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
####################测试脚本####################
import cv2
filename='1.jpg'
face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
face_cascade.load('C:/Users/24738/AppData/Local/Programs/Python/Python36/Lib/site-packages/opencv/build/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
img=cv2.imread(filename)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray,1.3,5)
for (x,y,h,w) in faces:
img=cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.namedWindow('faces Detected!')
cv2.imshow('faces Detected!',img)
cv2.imwrite('faces.jpg',img)
cv2.waitKey(0)
############################################################
没有记录下借鉴哪些资料,抱歉。