(windows) tensorflow-object-detection-api 教程

执行过程中出现任何问题,可下方评论,看到会回复
准备:
1.python3.6
2.tensorflow

环境搭建:

1.下载模型

从github上下载模型,下载地址:https://github.com/tensorflow/models
这里写图片描述

2.Protobuf 安装:

https://github.com/google/protobuf/releases下载win版的工具,如下图:
这里写图片描述

下载完成之后解压,例如我的路径为:D:\Tensorflow\ObjectDetection, 在D:\Tensorflow\ObjectDetection\protoc-3.5.1-win32\bin 目录下可看到可执行文件protoc.exe. 为了方便使用,将D:\Tensorflow\ObjectDetection\protoc-3.5.1-win32\bin设置为环境变量,输入命令:protoc,不报错,表示安装成功

注:若使用3.6.1版本的protoc,在运行的时候会出现:__init_() got an unexpected keyword argument ‘serialized_options’的错误

3.protoc 编译:

用protoc可执行文件编译目录object_detection/protos下的proto文件,生成Python文件。
git终端移动到 models\research目录下执行命令:

protoc object_detection/protos/*.proto --python_out=.

不报错,即编译成功,可查看protos下的.proto文件,都生成了一份.py文件。

4.object-detection环境变量设置:

window下,环境变量需通过对应目录下的setup.py 进行设置。
终端移动到models\research目录下,依次执行

python setup.py build
python setup.py install

5.测试

终端移动到models\research目录下,执行:

python object_detection\builders\model_builder_test.py

需要等待几秒钟,若出现 “OK”,则表示环境准备成功。
若出现: ” no moduel named ‘nets’ “

终端移动到models\research\slim 目录下,再重新执行一次

python setup.py build
python setup.py install

若出现 “build已经存在的问题 ”, 将该文件夹下的build文件删掉,重新执行上面的命令即可。

再次执行:

python object_detection\builders\model_builder_test.py

6.运行tensorflow-object-detection-api.

有两种形式可以执行,先介绍官方给的示例。
官方示例:

终端移动到models\research目录下
终端输入:jupyter notebook
浏览器会默认打开一个 http://localhost:8888/tree的网址
打开文件object_detection_tutorial.ipynb, 运行demo
这里写图片描述

在Cell中点击 Run All
这里写图片描述

耐心等待一小会儿, 出现下图结果表示环境安装成功。
这里写图片描述

若想修改识别的图片,可修改以下位置的代码,根据自己的需求进行修改。

为了便于后期使用,我在官方示例的基础上,做了一点点的修改
1.可在编辑器中直接运行该程序。
2.文件夹中的图片不用指定名称与数量,有多少图片,就识别多少图片。
3.识别完成的图片自动保存至指定文件夹中。

在models\research\object_detection 文件夹下,新建一个py文件,暂时命名为test.py.
然后执行 test.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

sys.path.append("..")
from object_detection.utils import ops as utils_ops
if tf.__version__ <</span> '1.4.0':
    raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')

from utils import label_map_util
from utils import visualization_utils as vis_util

# 选择要下载的model
MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
MODEL_FILE = MODEL_NAME + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'

# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

NUM_CLASSES = 90

#下载model,若是执行过一次,本地已经存在model,可将下面这些下载model的代码注释掉
opener = urllib.request.URLopener()
opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
tar_file = tarfile.open(MODEL_FILE)
for file in tar_file.getmembers():
    file_name = os.path.basename(file.name)
    if 'frozen_inference_graph.pb' in file_name:
        tar_file.extract(file, os.getcwd())


#=================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_FROZEN_GRAPH, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')


#==============================Loading label map==========================
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============================
# 将要筛选的图片放到 test_images 文件夹下,选取test_images文件夹下的所有图片
# 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, 5) ]
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, '{}'.format(i)) for i in os.listdir("./test_images")]

# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)

def run_inference_for_single_image(image, graph):
  with graph.as_default():
    with tf.Session() as sess:
      # Get handles to input and output tensors
      ops = tf.get_default_graph().get_operations()
      all_tensor_names = {output.name for op in ops for output in op.outputs}
      # print('==============', all_tensor_names)
      tensor_dict = {}
      for key in [
          'num_detections', 'detection_boxes', 'detection_scores',
          'detection_classes', 'detection_masks'
      ]:
        tensor_name = key + ':0'
        # print('====', tensor_name, all_tensor_names)
        if tensor_name in all_tensor_names:
          tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
              tensor_name)
      if 'detection_masks' in tensor_dict:
        # The following processing is only for single image
        detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
        detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
        # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
        real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
        detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
        detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
        detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
            detection_masks, detection_boxes, image.shape[0], image.shape[1])
        detection_masks_reframed = tf.cast(
            tf.greater(detection_masks_reframed, 0.5), tf.uint8)
        # Follow the convention by adding back the batch dimension
        tensor_dict['detection_masks'] = tf.expand_dims(
            detection_masks_reframed, 0)
      image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')

      # Run inference
      output_dict = sess.run(tensor_dict,
                             feed_dict={image_tensor: np.expand_dims(image, 0)})

      # all outputs are float32 numpy arrays, so convert types as appropriate
      output_dict['num_detections'] = int(output_dict['num_detections'][0])
      output_dict['detection_classes'] = output_dict[
          'detection_classes'][0].astype(np.uint8)
      output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
      output_dict['detection_scores'] = output_dict['detection_scores'][0]
      if 'detection_masks' in output_dict:
        output_dict['detection_masks'] = output_dict['detection_masks'][0]
  return output_dict

image_cut_size = []
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)
  # Actual detection.
  print("检测图像.....", image_path)
  output_dict = run_inference_for_single_image(image_np, detection_graph)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks'),
      use_normalized_coordinates=True,
      line_thickness=8)

  plt.figure(figsize=IMAGE_SIZE)
  image_name = image_path.split("\\")[1]
  fig = plt.gcf()
  plt.imshow(image_np)
  fig.savefig("./test_images_detection/"+image_name, dpi=100)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值