Learning record of object_detection under tensorflw/models (0)-first use of object_detection

Preface

object_detection provides many useful target detection models, and we can easily perform migration learning on the basis of them.

installation

First, you need to prepare the models module

The second is a very troublesome step in Windows.
First, you need to download the corresponding Windows version from https://github.com/protocolbuffers/protobuf/releases, mine is 64-bit,
Insert picture description here
and you need to add the bin in the installation directory to the environment variable
Insert picture description here
protoc object_detection/protos/*.proto --python_out=.is linux The command below: If
a single compilation is made, there are many files related to each other, and the compilation is unsuccessful. After consulting the information, I found that using the shift+right button to open Windows powershell in the models folder and
Insert picture description here
using the following command to compile all:
Get-ChildItem object_detection/protos/*.proto |Resolve-Path -Relative | %{protoc $_ --python_out=.}
After completion, the following files will appear

Demo

download

Create a new download.py file and write the following code. I changed the method of downloading files in python2 to python3.

import os
import tarfile
import urllib
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')
# PYTHON3
urllib.request.urlretrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
# PYTHON2
# opener = urllib.request
# 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())

If you do not modify it, the following error will be reported

  opener = urllib.request.URLopener()
AttributeError: module 'urllib' has no attribute 'request'

It takes a while to download, and the following files will appear after completion.
Insert picture description here

Detect

Need to pay attention to the installation of the PIL library, because PIL (Python Imaging Library) is a powerful image processing library in Python, but currently it only supports Python2.7
pillow is a branch of PIL, although it is a branch, it also has the same features as PIL Very strong image processing library.

import os, sys
import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt
# 只支持PYTHON2,3中通过pip install Pillow安装
from PIL import Image
# 只需修改前面PYTHON的路径即可
ROOTPATH = "D:\\Python\\Python37\\Lib\\site-packages\\tensorflow\\models\\research"
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append(ROOTPATH)
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
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(ROOTPATH+'\\object_detection\\data', 'mscoco_label_map.pbtxt')

# 封装加载数据的方法
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)

# 封装检测的代码
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}
      tensor_dict = {}
      for key in [
          'num_detections', 'detection_boxes', 'detection_scores',
          'detection_classes', 'detection_masks'
      ]:
        tensor_name = key + ':0'
        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[1], image.shape[2])
        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: image})
	 # 将结果封装起来返回,以供其他工具方法使用
      # 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.int64)
      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

# 加载图到内存当中
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='')
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)


# 构建测试图片路径列表
PATH_TO_TEST_IMAGES_DIR = ROOTPATH+'\\object_detection\\test_images'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ]

# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)
# 遍历之前构建的列表,一一进行预测
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.
  # 获得检测的结果
  output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
  # 用工具方法将检测结果标在图中
  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)
  plt.imshow(image_np)
  plt.show()

Guess you like

Origin blog.csdn.net/weixin_44112790/article/details/95935846