前言
本机环境
windows 7
python 3.7.1rc1
pycharm 2018.2.4
第三次尝试最终成功,效果图
![](https://img.haomeiwen.com/i2855784/3ac9c1d0c2f6a11c.jpg)
![](https://img.haomeiwen.com/i2855784/1685602c699d218b.jpg)
尝试
按照官方文档
python的版本要求3.5.1以上
安装tensorflow,要求tensorflow1.4以上
pip3 install --upgrade tensorflow
第一次尝试
C:\Users\hackertao>python -V
Python 3.7.1rc1
C:\Users\hackertao>pip -V
pip 10.0.1 from c:\users\hackertao\appdata\local\programs\python\python37\lib\site-packages\pip (python 3.7)
C:\Users\hackertao>pip install --upgrade tensorflow
Collecting tensorflow
Could not find a version that satisfies the requirement tensorflow (fromversions: )
No matching distribution found for tensorflow
第二次尝试
根据网上说明从https://www.lfd.uci.edu/~gohlke/pythonlibs/#tensorflow下载tensorflow‑1.9.0‑cp37‑cp37m‑win_amd64.whl,安装成功,但运行报错。
第三次尝试
重新下载安装python3.6,并修改系统变量指向新安装的路径
C:\Users\hackertao>python -V
Python 3.6.8
C:\Users\hackertao>pip -V
pip 19.0 from c:\users\hackertao\appdata\local\programs\python\python36\lib\site
n 3.6)
C:\Users\hackertao>pip list
Package Version
---------- -------
pip 19.0
setuptools 40.6.2
C:\Users\hackertao>d:
D:\>cd tools
D:\tools>mkdir testai
D:\tools>dir
驱动器 D 中的卷是 NewDisk
卷的序列号是 2061-1ACA
D:\tools 的目录
2019/01/24 05:53 <DIR> .
2019/01/24 05:53 <DIR> ..
2019/01/24 05:53 <DIR> testai
D:\tools>cd testai
D:\tools\testai>python -m venv venv
D:\tools\testai>dir
驱动器 D 中的卷是 NewDisk
卷的序列号是 2061-1ACA
D:\tools\testai 的目录
2019/01/24 05:54 <DIR> .
2019/01/24 05:54 <DIR> ..
2019/01/24 05:54 <DIR> venv
0 个文件 0 字节
3 个目录 371,119,284,224 可用字节
D:\tools\testai>venv\Scripts\activate.bat
#弹出新的窗口
(venv) D:\tools\testai>python -V
Python 3.6.8
(venv) D:\tools\testai>pip list
Package Version
---------- -------
pip 18.1
setuptools 40.6.2
You are using pip version 18.1, however version 19.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(venv) D:\tools\testai>python -m pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/46/dc/7fd5df840efb3e56c8b4f768793a237ec4ee5989
1959d6a215d63f727023/pip-19.0.1-py2.py3-none-any.whl (1.4MB)
238kB/s eta 0:00:01
100% |████████████████████████████████| 1.4MB 234kB/s
Installing collected packages: pip
Found existing installation: pip 18.1
Uninstalling pip-18.1:
Successfully uninstalled pip-18.1
Successfully installed pip-19.0.1
新建一个文件requirement.txt,内容如下
tensorflow==1.12.0
numpy==1.16.0
scipy==1.2.0
opencv-python==4.0.0.21
Pillow==5.4.1
matplotlib==3.0.2
h5py==2.9.0
Keras==2.2.4
继续上面的操作
(venv) D:\tools\testai>dir
D:\tools\testai 的目录
2019/01/24 06:04 <DIR> .
2019/01/24 06:04 <DIR> ..
2019/01/23 06:11 135 requirement.txt
2019/01/24 05:54 <DIR> venv
(venv) D:\tools\testai>pip install -r requirement.txt
#等安装完成即可
(venv) D:\tools\testai>pip list
Package Version
------------------- --------
absl-py 0.7.0
astor 0.7.1
cycler 0.10.0
gast 0.2.2
grpcio 1.18.0
h5py 2.9.0
Keras 2.2.4
Keras-Applications 1.0.6
Keras-Preprocessing 1.0.5
kiwisolver 1.0.1
Markdown 3.0.1
matplotlib 3.0.2
numpy 1.16.0
opencv-python 4.0.0.21
Pillow 5.4.1
pip 19.0.1
protobuf 3.6.1
pyparsing 2.3.1
python-dateutil 2.7.5
PyYAML 3.13
scipy 1.2.0
setuptools 40.6.2
six 1.12.0
tensorboard 1.12.2
tensorflow 1.12.0
termcolor 1.1.0
Werkzeug 0.14.1
wheel 0.32.3
(venv) D:\tools\testai>pip install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.2/
imageai-2.0.2-py3-none-any.whl
Collecting imageai==2.0.2 from https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.2/imag
eai-2.0.2-py3-none-any.whl
Downloading https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.2/imageai-2.0.2-py3-non
e-any.whl (151kB)
100% |████████████████████████████████| 153kB 176kB/s
Installing collected packages: imageai
Successfully installed imageai-2.0.2
下载https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_coco_best_v2.0.1.h5放到项目根路径下
新建FirstDection.py
# coding:utf-8
from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
detector = detector.detectObjectsFromImage(input_image=os.path.join(execution_path, "timg.jpg"), output_image_path=os.path.join(execution_path, "timgnew.jpg"))
for eachObject in detector:
print(eachObject["name"] , " : " , eachObject["percentage_probability"], " : ", eachObject["box_points"] )
到网上下载一张图片并重名名为timg.jpg,或者修改FirstDection.py里的文件名
![](https://img.haomeiwen.com/i2855784/3ac9c1d0c2f6a11c.jpg)
(venv) D:\tools\testai>dir
D:\tools\testai 的目录
2019/01/24 06:13 <DIR> .
2019/01/24 06:13 <DIR> ..
2019/01/23 07:34 551 FirstDection.py
2019/01/23 06:11 135 requirement.txt
2019/01/23 07:17 152,661,008 resnet50_coco_best_v2.0.1.h5
2019/01/23 05:31 38,260 timg.jpg
2019/01/24 05:54 <DIR> venv
(venv) D:\tools\testai>python FirstDection.py
Using TensorFlow backend.
2019-01-24 06:20:36.800210: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports i
nstructions that this TensorFlow binary was not compiled to use: AVX2
car : 59.85320806503296 : [141 136 167 158]
truck : 55.627310276031494 : [ 76 125 104 154]
bus : 56.11525774002075 : [177 130 211 163]
person : 72.99579977989197 : [101 133 120 198]
person : 74.8095691204071 : [121 129 141 196]
car : 74.34787154197693 : [ 0 137 24 201]
truck : 64.91233706474304 : [ 1 97 60 168]
car : 71.6873824596405 : [323 133 408 198]
truck : 53.766125440597534 : [424 127 499 213]
truck : 50.21634101867676 : [214 56 330 201]
person : 86.6084635257721 : [325 135 467 330]
(venv) D:\tools\testai>dir
D:\tools\testai 的目录
2019/01/24 06:17 <DIR> .
2019/01/24 06:17 <DIR> ..
2019/01/24 06:20 585 FirstDection.py
2019/01/23 06:11 135 requirement.txt
2019/01/23 07:17 152,661,008 resnet50_coco_best_v2.0.1.h5
2019/01/23 05:31 38,260 timg.jpg
2019/01/24 06:17 101,934 timgnew.jpg
2019/01/24 05:54 <DIR> venv
![](https://img.haomeiwen.com/i2855784/1685602c699d218b.jpg)
总结
这只是图片识别的示例代码,更多的请大家移步github仓库或中文网站的说明,主要在搭建环境中耗费很多的时间,官方文档在安装这块的说明不严谨导致的。
网友评论