環境
Windows7 x64
conda 4.3.30
1、TensorFlow安裝
首先在conda中建立TensorFlow環境
conda create -n tensorflow python=3.6.2
激活tensorflow環境
activate tensorflow
安裝tensorflow
pip install tensorflow==1.12.0
安裝完成後會在anaconda安裝路徑envs檔案下自動建立tensorflow目錄

2、tensorflow object detection API安裝
下載下傳安裝包:https://github.com/xizhonghuai/tensorflow_object_detection_demo
下載下傳完成後解壓,将tensorflow_models檔案夾拷貝到不含中文任意路徑下,如:
在anaconda安裝路徑下找到剛才新建立的tensorflow環境目錄
在\Lib\site-packages下建立tensorflow_model.pth檔案,内容如下:
E:\python\workspace\tensorflow_models\research
E:\python\workspace\tensorflow_models\research\slim
進入E:\python\workspace\tensorflow_models\research\slim檔案夾下分别執行一下指令:
python setup.py build
python setup.py install
運作後如果出現error: could not create ‘build’,請删掉\slim檔案夾下BUILD檔案,在重新執行。
進入E:\python\workspace\tensorflow_models\research檔案夾下分别執行一下指令:
python setup.py build
python setup.py install
這個過程時間稍長,若出現報錯情況,請仔細檢視報錯資訊是否提示缺少相關依賴,手動通過pip安裝。
測試tensorflow object detection API是否安裝成功,在E:\python\workspace\tensorflow_models\research下執行
object_detection/builders/model_builder_test.py
無報錯表示安裝成功。
說明:
報錯資訊提示找不到相關子產品,請解除安裝tensorflow,重新安裝其他版本。
報錯資訊提示DLL相關錯誤,請解除安裝tensorflow,在以下連結中安裝 https://github.com/fo40225/tensorflow-windows-wheel
3、測試
下載下傳官方已訓練好的模型到本地,這裡我們使用ssd_mobilenet_v1_coco(模型包含90種常見物體的識别)
https://github.com/xizhonghuai/tensorflow_object_detection_demo/blob/master/tensorflow_models/research/object_detection/g3doc/detection_model_zoo.md
點選下載下傳ssd_mobilenet_v1_coco模型你解壓到本地。
打開iead,建立python工程,建立com包,在com包下分别建立,data、test_images、model檔案夾
将剛才解壓模型包中的frozen_inference_graph.pb檔案拷貝到model檔案下。
在E:\python\workspace\tensorflow_models\research\object_detection\data_back_up檔案夾下拷貝mscoco_label_map.pbtxt檔案到data檔案下。
在網上随便找一張圖檔重命名為image1.jpg存放到test_images下
項目結構如下:
在com包下建立py代碼:
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from collections import defaultdict
from io import StringIO
# from matplotlib import pyplot as plt
from PIL import Image
## Env setup
# This is needed to display the images.
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
# Model preparation
## Variables
#Any model exported using the `export_inference_graph.py` tool can be loaded here #simply by changing `PATH_TO_CKPT` to point to a new .pb file.
#By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/object_detection/g3doc/detection_mo#del_zoo.md) for a list of other models that can be run out-of-the-box with varying #speeds and accuracies.
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = 'model/frozen_inference_graph.pb'
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
NUM_CLASSES = 1
## Load a (frozen) Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
print(PATH_TO_CKPT)
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
## Helper code
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
# Detection
# For the sake of simplicity we will use only 2 images:
# image1.jpg
# image2.jpg
# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = 'test_images'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 2) ]
#TEST_IMAGE_PATHS = ['test_images']
# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
# plt.figure(figsize=IMAGE_SIZE) # plt.imshow(image_np)
# plt.show()
im = Image.fromarray(image_np)
im.save("ret.jpeg")
print("OK")
測試:
執行python代碼,運作成功後将生成識别結果并儲存圖檔到com包下。