tensorflow入门教程(三十七)Object Detection API+OpenCV实现动态目标检测

#
#作者:韦访
#博客:https://blog.csdn.net/rookie_wei
#微信:1007895847
#添加微信的备注一下是CSDN的
#欢迎大家一起学习
#

1、概述

最近在解析Object Detaction API的源码,确实有点难啃啊。刚好新入职的工作也是做目标检测的,用的是微软的Azure。源码解析的博客还没更新那么快,那就玩些应用调解一下气氛吧。还是用以前博客的源码稍微改一点点就可以实现了。

参考博客:https://blog.csdn.net/rookie_wei/article/details/81210499

2、思路

都不好意思说思路了,太简单了,OpenCV读取摄像头数据,然后,用Object Detaction API解析数据,一帧一帧的读,一帧一帧的解析就可以了。没有装opencv的请先安装一下哈。安装命令如下,

sudo pip3 install opencv-python

3、直接上代码

只是调节一下气氛而已,比较简单,咱也就豪放点直接上代码好了,有不懂的就看以前的博客吧。

# encoding:utf-8
import tensorflow as tf
import numpy as np
import cv2
import os

from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_utils


# 下载下来的模型的目录
MODEL_DIR = 'object_detection/ssd_mobilenet_v1_coco_2018_01_28'
# 下载下来的模型的文件
MODEL_CHECK_FILE = os.path.join(MODEL_DIR, 'frozen_inference_graph.pb')
# 数据集对于的label
MODEL_LABEL_MAP = os.path.join('object_detection/data', 'mscoco_label_map.pbtxt')
# 数据集分类数量,可以打开mscoco_label_map.pbtxt文件看看
MODEL_NUM_CLASSES = 90

tf.reset_default_graph()

# 将模型读取到默认的图中
with tf.gfile.GFile(MODEL_CHECK_FILE, 'rb') as fd:
    _graph = tf.GraphDef()
    _graph.ParseFromString(fd.read())
    tf.import_graph_def(_graph, name='')

# 加载COCO数据标签,将mscoco_label_map.pbtxt的内容转换成
# {1: {'id': 1, 'name': u'person'}...90: {'id': 90, 'name': u'toothbrush'}}格式
label_map = label_map_util.load_labelmap(MODEL_LABEL_MAP)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=MODEL_NUM_CLASSES)
category_index = label_map_util.create_category_index(categories)


# 将图片转化成numpy数组形式
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)

#用opencv打开摄像头
capture = cv2.VideoCapture(0)

# 在图中开始计算
detection_graph = tf.get_default_graph()
with tf.Session(graph=detection_graph) as sess:
    while (True):
        # 一帧一帧的读
        ret, frame = capture.read()

        # 增加一个维度
        frame_expanded = np.expand_dims(frame, 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: frame_expanded})
        # 得到可视化结果
        vis_utils.visualize_boxes_and_labels_on_image_array(
            frame,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8
        )

        # 显示
        cv2.imshow('frame', frame)

        #按 q 退出
        if cv2.waitKey(1) == ord('q'):
            break

运行结果:

这摄像头像素都快成马赛克了,淘宝十几块的货也差不多这样了~~

如果您感觉本篇博客对您有帮助,请打开支付宝,领个红包支持一下,祝您扫到99元,谢谢~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值