Tensorflow实战目标检测

首先到github下载相应的Tensorflow模型,以及配置好环境。具体的可参考这篇博客

或者参考Github上,TensorFlow models/research/object_detection里的安装教程

这里给出一个视频里面的目标检测代码:

import os
import time
import argparse
import multiprocessing
import numpy as np
import tensorflow as tf
import tarfile
from matplotlib import pyplot as plt

from object_detection.utils import label_map_util

from object_detection.utils import visualization_utils as vis_util
‘‘‘
    视频目标追踪
‘‘‘
#1.得到模型 (这里首先下载流模型并在解压在path/to/models/research/object_detection里面)
MODEL_NAME = ‘ssd_mobilenet_v1_coco_2017_11_17‘
PATH_TO_CKPT = os.path.join(MODEL_NAME, ‘frozen_inference_graph.pb‘)

PATH_TO_LABELS = os.path.join(‘data‘, ‘mscoco_label_map.pbtxt‘)

print(‘Loading model...‘)

#load frozen of tensorflow to memeory
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_CKPT, ‘rb‘) as fid: #文本操作句柄,类似python里面的open()
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name=‘‘)  #将图像从od_graph_def导入当前的默认Graph

#label map to class name 如预测为5,知道它是对应飞机
NUM_CLASS = 90

print("Loading label map...")
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)  #得到label map proto
categories = label_map_util.convert_label_map_to_categories(label_map, NUM_CLASS) #得到类别
category_index = label_map_util.create_category_index(categories) 

#2.对视频进行物体检测
def detect_objects(image_np, sess, detection_graph):
    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‘)

    #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)
    return image_np

from moviepy.editor import VideoFileClip
from IPython.display import HTML

def process_image(image):
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            image_process = detect_objects(image, sess, detection_graph)
            return image_process

white_output = ‘/home/magic/111_out.mp4‘
clip1 = VideoFileClip("/home/magic/111.avi")
white_clip = clip1.fl_image(process_image)  #This function expects color images!
white_clip.write_videofile(white_output, audio=False)
#等待一段时间后,得到111_out.mp4,可以去查看效果  我的测试结果如下


原文地址:https://www.cnblogs.com/logo-88/p/9542862.html

时间: 2024-07-31 12:44:44

Tensorflow实战目标检测的相关文章

caffe框架下目标检测——faster-rcnn实战篇操作

原有模型 1.下载fasrer-rcnn源代码并安装 git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git 1)  经常断的话,可以采取两步: git clone https://github.com/rbgirshick/py-faster-rcnn.git 2)  到py-faster-rcnn中,继续下载caffe-faster-rcnn,采取后台跑: git submodule update --in

目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练

将目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练. import xml.etree.ElementTree as ET import numpy as np import os import tensorflow as tf from PIL import Image classes = ["aeroplane", "bicycle", "bird", "boat", &quo

YOLOv3目标检测实战:交通标志识别

在无人驾驶中,交通标志识别是一项重要的任务.本项目以美国交通标志数据集LISA为训练对象,采用YOLOv3目标检测方法实现实时交通标志识别. 具体项目过程包括包括:安装Darknet.下载LISA交通标志数据集.数据集格式转换.修改配置文件.训练LISA数据集.测试训练出的网络模型.性能统计(mAP计算和画出PR曲线)和先验框聚类. YOLOv3可以实时地进行端到端的目标检测,以速度快见长.本课程将手把手地教大家使用YOLOv3实现交通标志识别.本课程的YOLOv3使用Darknet,在Ubun

10 行Python 代码,实现 AI 目标检测技术,真给力!

只需10行Python代码,我们就能实现计算机视觉中目标检测. from imageai.Detection import ObjectDetection import os execution_path = os.getcwd() detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path , "resnet50_coco_b

TensorFlow实战之Softmax Regression识别手写数字

     关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2018年02月21日 23:10:04所撰写内容(http://blog.csdn.net/qq_37608890/article/details/79343860).        本文根据最近学习TensorFlow书籍网络文章的情况,特将一些学习心得做了总结,详情如下.如有不当之处,请各位大拿多多指点,在此谢过. 一.相关概念 1.MNIST MNIST(Mixed

第三十四节,目标检测之谷歌Object Detection API源码解析

我们在第三十二节,使用谷歌Object Detection API进行目标检测.训练新的模型(使用VOC 2012数据集)那一节我们介绍了如何使用谷歌Object Detection API进行目标检测,以及如何使用谷歌提供的目标检测模型训练自己的数据.在训练自己的数据集时,主要包括以下几步: 制作自己的数据集,注意这里数据集在进行标注时,需要按照一定的格式.然后调object_detection\dataset_tools下对应的脚本生成tfrecord文件.如下图,如果我们想调用create

第三十六节,目标检测之yolo源码解析

在一个月前,我就已经介绍了yolo目标检测的原理,后来也把tensorflow实现代码仔细看了一遍.但是由于这个暑假事情比较大,就一直搁浅了下来,趁今天有时间,就把源码解析一下.关于yolo目标检测的原理请参考前面一篇文章:第三十五节,目标检测之YOLO算法详解 在讲解源码之前,我们需要做一些准备工作: 下载源码,本文所使用的yolo源码来源于网址:https://github.com/hizhangp/yolo_tensorflow 下载训练所使用的数据集,我们仍然使用以VOC 2012数据集

一文带你学会使用YOLO及Opencv完成图像及视频流目标检测(上)|附源码

计算机视觉领域中,目标检测一直是工业应用上比较热门且成熟的应用领域,比如人脸识别.行人检测等,国内的旷视科技.商汤科技等公司在该领域占据行业领先地位.相对于图像分类任务而言,目标检测会更加复杂一些,不仅需要知道这是哪一类图像,而且要知道图像中所包含的内容有什么及其在图像中的位置,因此,其工业应用比较广泛.那么,今天将向读者介绍该领域中表现优异的一种算算法--"你只需要看一次"(you only look once,yolo),提出该算法的作者风趣幽默可爱,其个人主页及论文风格显示了其性

深度学习之目标检测常用算法原理+实践精讲

第1章 课程介绍本章节主要介绍课程的主要内容.核心知识点.课程涉及到的应用案例.深度学习算法设计通用流程.适应人群.学习本门课程的前置条件.学习后达到的效果等,帮助大家从整体上了解本门课程的整体脉络. 第2章 目标检测算法基础介绍本章节主要介绍目标检测算法的基本概念.传统的目标检测算法.目前深度学习目标检测主流方法(one-stage.two-stage.多任务网络).相关算法的基本流程.算法性能的评价指标.不同算法的优缺点和性能比较等,并结合实际的应用场景和案例来介绍目标检测算法的重要性和实用