Python深度學習筆記(二):使用TensorFlow API進行圖像辨識
6 min readMay 6, 2019
前置作業
- 閱讀 TensorFlow Object Detection API installation documentation
- 下載 git for Windows.
- 下載 tensorflow-model repository.
下載 tensorflow-model repository
git clone https://github.com/tensorflow/models.git#下載到指定資料夾(D:\tensorflow)
安裝TensorFlow
# For CPU
pip install tensorflow
# For GPU
pip install tensorflow-gpu
安裝相關模組
pip install pillow
pip install lxml
pip install jupyter
pip install matplotlib
下載COCO API
git clone https://github.com/cocodataset/cocoapi.git並將檔案移動到 tensorflow/models/research的目錄當中
下載 Google Protobuf
https://github.com/google/protobuf/releases/tag/v3.4.0選擇 “protoc-3.4.0-win32.zip”將Protobuf解壓縮到 C:\Program Files\
將下列路徑加到環境變數當中
系統-> 進階系統設定 -> 環境變數 -> 編輯-> 新增下列路徑
path\Anaconda3path\Anaconda3\Scriptspath\Anaconda3\Library\binpath-to-tensorflow\models\researchpath-to-tensorflow\models\research\slimpath-to-tensorflow\models\research\object_detection
重新開機
重新開機讓環境變數生效
使用CMD
cd tensorflow\models\research“C:\Program Files\protoc-3.4.0-win32\bin\protoc.exe” object_detection/protos/*.proto --python_out=.到 object_detection/protos 檢查是否有 .py 檔案
測試是否成功
python object_detection/builders/model_builder_test.py
如果出現 ImportError: No module named ‘object_detection’
在Anaconda\Lib\site-packages資料夾中,寫一個tensorflow_model.pth。內容為兩條路徑D:\tensor-flow\models\research
D:\tensor-flow\models\research\slim
開啟object_detection_tutorial.ipynb
在D:\tensor-flow\models\research\object_detection目錄底下執行object_detection_tutorial.ipynb
官方範例:
使用自己的照片辨識
替換D:\tensor-flow\models\research\object_detection\test_images的照片
且必須以image1,image2,image3,.....為檔名修改object_detection_tutorial.ipynb中的程式Detection的部分 #有多張照片就要修改range範圍
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ]或者也可自訂程式,做更彈性的辨識