tensorflow object detection API+windows10 usb摄像头实时目标检测

一、工程环境

Windows10
python 3.6
tensorflow-GPU =1.8.0(在我另一台私人电脑上也跑了tensorflow-cpu的版本,tensorflow-cpu 1.14?记不太清了)
opencv4.2.0
普通usb摄像头(笔记本的话用自带的就可以)
版本配置
显卡要求:英伟达 gtx1050ti。如果暂时只是想跑通一下看看效果,那显卡什么的不用太在意;后期自己训练的话肯定是英伟达显卡性能越高越好。AMD的显卡我没测试过,不敢说。

二、tensorflow object detection API的安装

安装过程已经在我上一篇博客中给出了一些建议、博客参考链接和一些相关资源的网盘。点击进入上一篇博客链接
这个API的安装主要还是GitHub官网上的版本一直在更新,老版本的资源我在上一篇博客中已经给出。
(我自己还是要养成随手截图记录的习惯,,,,,,,不然后期整理的时候真的无奈)
切记,如果你和我一样是新手,建议去下载老版本的API,新的我没测试成功,老版本的API的百度网盘链接我在上一篇博客中已经给出。

三、实时视频的检测

先跑通官网的demo,会自动下载ssd_mobile的模型,跑出那两张图片之后把模型文件夹保存好。
这里简单说一下,做测试肯定需要训练好的模型,我去GitHub上下载那些最新模型文件后解压发现少了一些东西(少了pb、pbtxt,不确定什么原因),所以想出了这个办法,把跑demo下载的模型保存好作为我们视频测试的模型。
在你的models/research/object_detection文件夹下添加.py文件,名字随意,我的是avtest.py
代码如下:

from object_detection.utils import visualization_utils as vis_util
from object_detection.utils import label_map_util
from distutils.version import StrictVersion
import tensorflow as tf
import numpy as np
import os
import cv2

#if StrictVersion(tf.__version__) < StrictVersion('1.9.0'):
    #raise ImportError('Please upgrade your TensorFlow installation to v1.9.* or later!')

# 开启摄像头
cap = cv2.VideoCapture(0)

# 添加模型位置和标签配置文件位置
PATH_TO_FROZEN_GRAPH = 'D:/BaiduNetdiskDownload/models/research/object_detection/ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb'
PATH_TO_LABELS = os.path.join('data','mscoco_label_map.pbtxt')

# 载入模型
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)

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        while True:
            ret, image_np = cap.read()
            # 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)

            cv2.imshow('object detection', image_np)
            if cv2.waitKey(25) & 0xFF == ord('q'):
                cv2.destroyAllWindows()
                break
cap.release()
cv2.destroyAllWindows()

代码其实也没什么难懂的。我稍微说几下:
(1)开头库的导入,官方的demo和我的有一点区别,我把matplotlib等一些给去了,因为做视频检测用不到;
(2)#if StrictVersion(tf.__version__) < StrictVersion('1.9.0'): #raise ImportError('Please upgrade your TensorFlow installation to v1.9.* or later!')
这一块demo中是有的,目的是让你下载指定的tensorflow版本,注释掉或者删了就行了,不影响。
(3)PATH_TO_FROZEN_GRAPH=…
PATH_TO_LABELS = os.path.join(…)
这两个地方就是要填入你自己的模型文件位置

在这里插入图片描述

最后祝大家早日成功。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值