1、開發環境搭建
①、安裝Anaconda
建議選擇 Anaconda3-5.0.1 版本,已經集成大多數庫,並將其作為默認python版本(3.6.3),配置好環境變量(Anaconda安裝則已經配好)。也可以直接安裝python,安裝各種包比較麻煩,因此直接裝了Anaconda集成環境。
安裝完Anaconda后,打開Anaconda Prompt,逐個輸入conda --version和python --version,出現下圖所示內容則安裝成功。
②、安裝TensorFlow
如果是初學者,我們安裝cpu版本的tensorflow足夠使用。安裝TensorFlow-cpu很簡單,打開Anaconda Prompt,輸入pip install tensorflow。稍等一會就安裝成功。通過輸入以下代碼,檢測是否安裝成功。
python
如果輸入import tensorflow as tf出現如下警告:
FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.from ._conv import register_converters as _register_converters。則需要打開Anaconda Prompt,輸入pip install h5py==2.8.0rc1解決。
如果輸入 sess = tf.Session()出現Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2,則表明TensorFlow版本太低了,需要打開Anaconda Prompt,輸入pip install --upgrade tensorflow解決。
③、下載Tensorflow object detection API模型,從https://github.com/tensorflow/models下載。解壓文件到磁盤指定目錄,例如C:\Users\CFF\Desktop,重命名為models(此包內包括各種內容,我們所用到的object_detection文件夾在C:\Users\CFF\Desktop\models\research文件夾下)
④、Protobuf 編譯。從https://github.com/google/protobuf/releases下載windows版的工具,如protoc-3.6.1-win32.zip,解壓到C:\Users\CFF\Desktop,生成:bin, include兩個文件夾。打開Anaconda Prompt,cd C:\Users\CFF\Desktop\models\research ,輸入C:\Users\CFF\Desktop\bin\protoc ,編譯結果如下說明可以開始編譯。
Protobuf 編譯,用protoc可執行文件編譯目錄object_detection/protos下的proto文件,生成Python文件。如:C:\Users\CFF\Desktop\bin\protoc object_detection/protos/*.proto --python_out=.。如何出現object_detection/protos/*.proto目錄沒有發現,可以將*號改成對應的文件逐個編譯,生成對應的Python文件。
⑤、測試。打開Anaconda Prompt,cd C:\Users\CFF\Desktop\models\research目錄,輸入jupyter notebook,跳轉到網頁界面。
在cell中選擇runAll,正常的話稍等一會兒就會有結果:
到此,已經成功完成了環境搭建與測試。
⑥、用來測試自己的圖片,改變PATH_TO_TEST_IMAGES_DIR 的路徑,我的如下所示。測試圖片命名為image1.jpg image2.jpg... 相應的數量for i in range(1, 3)也要根據自己的圖片數量進行改變。
運行后,輸出結果如下: