注:1.imageai目前(2020年03月23日)不支持tensorflow2.0,官方幫助文檔:https://imageai.readthedocs.io/en/latest/detection/index.html
2.網卡必須爲Navidia,並且計算能力爲3.0以上纔行。計算能力查詢:https://developer.nvidia.com/cuda-gpus#collapseOne
3.tensorflow-gpu版本與python版本、CUDA版本、cuDNN版本必須一一對應,否則無法調用。查詢地址:https://www.tensorflow.org/install/source_windows。(tensorflow-gpu與CUDA、cuDNN的對應關係可以自動處理,見下文環境配置章節第4步【安裝tensorflow-gpu=1.10.0】)
4.必須安裝驅動,驅動下載地址:https://www.nvidia.cn/Download/index.aspx?lang=cn
一、環境配置
下載並安裝驅動(略)
1.下載Anaconda3,我下載的是Anaconda3-2020.02-Windows-x86_64.exe版本,默認Python3.7.6。
2.點擊【開始】【Anaconda Prompt】。
3.創建及激活虛擬環境
添加國內鏡像源,下載包的速度更快
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
創建虛擬環境
conda create -n env2 python=3.6.1
激活虛擬環境
activate env2
後續的所有安裝都在env2環境下
4.安裝tensorflow-gpu=1.10.0
conda install tensorflow-gpu=1.10.0
此命令可自動完成cuda和cudnn的安裝。
5.安裝Keras==2.1.6
pip install Keras==2.1.6 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
6.安裝opencv-python
pip install opencv-python -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
7.安裝imageai
pip install imageai -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
8.打開Spyter,點擊菜單【Tools】【Preferences】,選擇左邊的【Python interpreter】,選擇新創建環境下的python.exe,點擊【OK】
這時大概率會出現Your Python environment or installation doesn’t have the spyder-kernels module
解決方法:
pip install spyder-kernels -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
至此,庫及開發環境就配置好了,可以編寫代碼了。
二、代碼編寫與調試
1.在Spyter的temp.py中輸入如下代碼
import os
from imageai.Detection import ObjectDetection
import cv2
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path , "yolo.h5"))
detector.loadModel()
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
x, y = frame.shape[0:2]
returned_image, detections = detector.detectObjectsFromImage(input_image=frame,input_type='array', output_type='array', minimum_percentage_probability=30)
cv2.imshow("video", returned_image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
2.下載yolo.h5(下載地址:https://imageai.readthedocs.io/en/latest/detection/index.html)
3.在temp.py上點擊右鍵,選中【Show in external file explorer】,打開temp.py所在文件夾,將下載的yolo.h5放入到文件夾。
4.運行程序,按q鍵可以退出。
附:
虛擬環境中庫及版本
Name |
Version |
Build |
Channel |
_tflow_select |
2.1.0 |
gpu |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
absl-py |
0.9.0 |
py36_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
astor |
0.8.0 |
py36_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
backcall |
0.1.0 |
pypi_0 |
pypi |
blas |
1 |
mkl |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
certifi |
2019.11.28 |
py36_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
cloudpickle |
1.3.0 |
pypi_0 |
pypi |
colorama |
0.4.3 |
pypi_0 |
pypi |
cudatoolkit |
9 |
1 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
cudnn |
7.6.5 |
cuda9.0_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
cycler |
0.10.0 |
pypi_0 |
pypi |
decorator |
4.4.2 |
pypi_0 |
pypi |
gast |
0.3.3 |
py_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
grpcio |
1.12.1 |
py36h1a1b453_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
h5py |
2.10.0 |
pypi_0 |
pypi |
icc_rt |
2019.0.0 |
h0cc432a_1 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
imageai |
2.1.5 |
pypi_0 |
pypi |
intel-openmp |
2020 |
166 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
ipykernel |
5.2.0 |
pypi_0 |
pypi |
ipython |
7.13.0 |
pypi_0 |
pypi |
ipython-genutils |
0.2.0 |
pypi_0 |
pypi |
jedi |
0.16.0 |
pypi_0 |
pypi |
jupyter-client |
6.1.0 |
pypi_0 |
pypi |
jupyter-core |
4.6.3 |
pypi_0 |
pypi |
keras |
2.1.6 |
pypi_0 |
pypi |
kiwisolver |
1.1.0 |
pypi_0 |
pypi |
libprotobuf |
3.6.0 |
h1a1b453_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
markdown |
3.1.1 |
py36_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
matplotlib |
3.2.1 |
pypi_0 |
pypi |
mkl |
2020 |
166 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
numpy |
1.14.2 |
py36h5c71026_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
opencv-python |
4.2.0.32 |
pypi_0 |
pypi |
parso |
0.6.2 |
pypi_0 |
pypi |
pickleshare |
0.7.5 |
pypi_0 |
pypi |
pillow |
7.0.0 |
pypi_0 |
pypi |
pip |
20.0.2 |
py36_1 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
prompt-toolkit |
3.0.4 |
pypi_0 |
pypi |
protobuf |
3.6.0 |
py36he025d50_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
pygments |
2.6.1 |
pypi_0 |
pypi |
pyparsing |
2.4.6 |
pypi_0 |
pypi |
python |
3.6.1 |
2 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free |
python-dateutil |
2.8.1 |
pypi_0 |
pypi |
pywin32 |
227 |
pypi_0 |
pypi |
pyyaml |
5.3.1 |
pypi_0 |
pypi |
pyzmq |
19.0.0 |
pypi_0 |
pypi |
scipy |
1.4.1 |
pypi_0 |
pypi |
setuptools |
46.0.0 |
py36_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
six |
1.14.0 |
py36_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
spyder-kernels |
1.9.0 |
pypi_0 |
pypi |
tensorboard |
1.10.0 |
py36he025d50_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
tensorflow |
1.10.0 |
gpu_py36h3514669_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
tensorflow-base |
1.10.0 |
gpu_py36h6e53903_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
tensorflow-gpu |
1.10.0 |
hf154084_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
termcolor |
1.1.0 |
py36_1 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
tornado |
6.0.4 |
pypi_0 |
pypi |
traitlets |
4.3.3 |
pypi_0 |
pypi |
vc |
14 |
0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free |
vs2015_runtime |
14.16.27012 |
hf0eaf9b_1 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
wcwidth |
0.1.8 |
pypi_0 |
pypi |
werkzeug |
1.0.0 |
py_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
wheel |
0.34.2 |
py36_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
wincertstore |
0.2 |
py36h7fe50ca_0 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
zlib |
1.2.11 |
h8395fce_2 |
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |