(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程

一、(pycharm)Tensorflow object detection API 物体检测模型  (一)   配置教程

二、(pycharm)tensorflow object detection API  物体检测模型 (二)  labelImg的安装配置程

三、Tensorflow objection detection api  物体检测模型 (三)   从识别的物体中抠出特定物体进行保存

1、开发环境搭建

在自己笔记本上完成。

操作系统:windows 7    64位

默认已经安装好了: pycharm、python3.6、tensorflow

第一步:

下载Tensorflow object detection API 模型:https://github.com/tensorflow/models

第二步:

下载Protoc:https://github.com/protocolbuffers/protobuf/releases

protoc的作用是将Tensorflow object detection API模型文件中的.pro文件编译成python文件。

进去链接一直往下拉,看到与自己电脑对应的下载就可以。window下下载的版本可以是:

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第1张图片

下载后解压到任意文件夹,可以看到目录如下:

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第2张图片

然后把bin文件夹的路径添加到环境变量:

步骤:

计算机-属性-高级系统设置-环境变量-

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第3张图片

找到path,然后点击编辑,把bin文件夹的路径添加到变量值,记得用英文的分号;隔间开(切记:之前的环境变量不要删除!!!)

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第4张图片

下面是bin的路径

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第5张图片

放到环境变量中就可以了:

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第6张图片

打开cmd,输入protoc:  输出如下信息说明添加环境变量成功:

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第7张图片

第三步:将.proto文件编译成py文件

将下载的tensorflow object detection文件解压, 文件名可改为models:打开models\research\object_detection\protos,会看到里面有很多的.proto文件,利用Protoc将这些.proto文件编译成py文件,下面介绍具体的做法:

在models\research文件下打开cmd命令窗口(shift+鼠标右键可以在当前文件夹下打开cmd)

输入:protoc ./object_detection/protos/*.proto --python_out=. 就可以快速编译所有文件, *.proto相当于匹配所有的.proto文件。

在这一步有时候会出错,可以尝试把/*.proto 这部分改成protos文件夹下具体的文件名,一个一个试,每运行一个,文件夹下应该出现对应的.py结尾的文件。不报错即可

编译完成后如下图所示:

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第8张图片

第四步:添加环境变量

将下面两个文件夹所在路径添加到环境变量中

\models\research

 \models\research\slim

到这里,环境搭建就算完成了,下面测试一下;

2、测试(参考:https://blog.csdn.net/zj1131190425/article/details/80711857)

我这里直接用pycharm打开models项目:整个目录如下图所示

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第9张图片

在research/object_detection文件夹下新建一个python文件,命名为object_detection_tutorial

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第10张图片

文件内容如下:

(关于下面代码的讲解可参考这个博客的分析代码结构部分:https://blog.csdn.net/dy_guox/article/details/79111949)

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
 
 
# # This is needed to display the images.
# %matplotlib inline
 
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
 
# from utils import label_map_util
# from utils import visualization_utils as vis_util
from research.object_detection.utils import label_map_util
from research.object_detection.utils import visualization_utils as vis_util
 
# What model to download.
MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'
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_CKPT = 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
 
# download 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_CKPT, '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)
 
 
# 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, 3)]
 
# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)
 
with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        # Definite input and output Tensors for detection_graph
        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.
        detection_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.
        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')
        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()

运行该object_detection_tutorial.py文件,运行时间可能比较久,耐心等待后会出现如下结果:

(pycharm)Tensorflow object detection API 物体检测模型 (一) 配置教程_第11张图片

到这里就已经可以了!下面就介绍如何训练自己的模型!

你可能感兴趣的:(python笔记)