google object detection API 实现教程

本文详细介绍如何在Ubuntu 14.04环境下使用TensorFlow 1.4.0搭建目标检测环境,包括安装必要库、下载并编译Google Object Detection API源码等步骤,并通过SSD MobileNet v1模型演示了实际图像的目标检测过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,环境(配置参照上一篇教程):

ubuntu14.04

tensorflow_gpu-1.4.0

二,安装相应的库

sudo apt-get install protobuf-compiler python-pil python-lxml python-tk
sudo pip install jupyter
sudo pip install matplotlib

三,下载google object detection api源码

四,编译proto文件为python文件

之前装的protoc软件在这里有问题,这时我们手动去网上下载

wget https://github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-linux-x86_64.zip  
下载比较慢也可以这里下载:  
https://www.witsrc.com/download  
然后解压到tensorflow/models/research/     目录
bin/protoc object_detection/protos/*.proto --python_out=.     #进行编译

五,添加slim环境变量

# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

六,最后到tensorflow/models/research/object_detection目录

运行如下代码(先去网上下载ssd_mobilenet_v_coco_11_0模型解压提取其中的frozen_inference_graph.pb到tensorflow/models/research/object_detection目录)

import sys
sys.path.append('..')
import os
import time
import tensorflow as tf
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt

from utils import label_map_util
from utils import visualization_utils as vis_util

from collections import defaultdict
from io import StringIO

PATH_TEST_IMAGE = sys.argv[1]
PATH_TO_CKPT = 'frozen_inference_graph.pb'
PATH_TO_LABELS = 'data/pascal_label_map.pbtxt'
NUM_CLASSES = 90 #21
IMAGE_SIZE = (12, 8)

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)

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='')

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)

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        start_time = time.time()
        print(time.ctime())
        image = Image.open(PATH_TEST_IMAGE)
        image_np = load_image_into_numpy_array(image)
        image_np_expanded = np.expand_dims(image_np, axis=0)
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        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')
        (boxes, scores, classes, num_detections) = sess.run(
            [boxes, scores, classes, num_detections],
            feed_dict={image_tensor: image_np_expanded})
        print('{} elapsed time: {:.3f}s'.format(time.ctime(), time.time() - start_time))
        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()

保存为infer.py

python3 infer.py test_images/image2.jpg

运行程序




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值