代码放在tensorflow 提供的modles中的 /models/research/object_detection/models  中,指定的模型要提前下到文件夹,

使用了opencv中从指定的ip地址视频流中截取图片,将识别结果中的第一个写到了制动文件夹中。搭建的环境是Ubuntu16.04

import numpy as np
import os
import sys
import tarfile
import tensorflow as tf
import cv2
import timefrom collections import defaultdict# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("../..")from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util# What model to download.
MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09'
#MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
#MODEL_NAME = 'faster_rcnn_inception_resnet_v2_atrous_lowproposals_coco_2018_01_28'
#MODEL_NAME = 'faster_rcnn_resnet50_coco_2018_01_28'
#MODEL_NAME = 'ssd_inception_v2_coco_11_06_2017'
MODEL_FILE = MODEL_NAME + '.tar.gz'# 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('/home/che/Desktop/models/research/object_detection/data', 'mscoco_label_map.pbtxt')#extract the ssd_mobilenet
start = time.clock()
NUM_CLASSES = 90
#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())
end= time.clock()
print('load the model',(end-start))
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='')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)cap = cv2.VideoCapture('http://192.168.12.1:8080/?action=stream')
with detection_graph.as_default():with tf.Session(graph=detection_graph) as sess:writer = tf.summary.FileWriter("logs/", sess.graph)sess.run(tf.global_variables_initializer())while(1):start = time.clock()ret, frame = cap.read()if cv2.waitKey(1) & 0xFF == ord('q'):breakimage_np=frame# the array based representation of the image will be used later in order to prepare the# result image with boxes and labels on it.# 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})class_name = category_index[np.squeeze(classes).astype(np.int32)[0]]['name']f = open('/home/che/Desktop/du.txt','w')print (class_name)f.write(class_name)f.close()# 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=6)end = time.clock()print ('One frame detect take time:',end - start)cv2.imshow("capture", image_np)cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()

tensorflow object detect API 使用,并修改一部分相关推荐

  1. 深度学习tensorflow object detect 之识别社会人小猪佩奇

    今天我们来做一个有趣的事情,识别"社会人",我们现在就准备用tensorflow object detect api来检测它 代码仓库:https://github.com/san ...

  2. Tensorflow object detection API 搭建属于自己的物体识别模型(转载修改)

    Tensorflow object detection API 搭建属于自己的物体识别模型 电脑配置信息 开始搭建环境 测试自带案例 准备训练图片 配置文件与模型 开始训练模型 生成最终的训练文件 测 ...

  3. Tensorflow object detection API 搭建自己的目标检测模型并迁移到Android上

    参考链接:https://blog.csdn.net/dy_guox/article/details/79111949 之前参考上述一系列博客在Windows10下面成功运行了TensorFlow A ...

  4. TensorFlow学习——Tensorflow Object Detection API(win10,CPU)

    英文链接地址:https://github.com/tensorflow/models/tree/master/object_detection 确保安装了如下的库: Tensorflow Objec ...

  5. 使用Tensorflow Object Detection API进行集装箱识别并对集装箱号进行OCR识别

    使用Tensorflow Object Detection API进行集装箱识别并对集装箱号进行OCR识别 两年多之前我在"ex公司"的时候,有一个明确的项目需求是集装箱识别并计数 ...

  6. 使用Tensorflow Object Detection API对集装箱号进行OCR识别

    玄念 两年多之前我在"ex公司"的时候,有一个明确的项目需求是集装箱识别并计数,然后通过OCR识别出之前计数的每一个集装箱号,与其余业务系统的数据进行交换,以实现特定的整体需求.当 ...

  7. 谷歌开放的TensorFlow Object Detection API 效果如何?对业界有什么影响

    ? 谷歌开放了一个 Object Detection API: Supercharge your C 写个简单的科普帖吧. 熟悉TensorFlow的人都知道,tf在Github上的主页是:tenso ...

  8. ssd目标检测训练自己的数据_目标检测Tensorflow object detection API之训练自己的数据集...

    构建自己的模型之前,推荐先跑一下Tensorflow object detection API的demo JustDoIT:目标检测Tensorflow object detection API​zh ...

  9. 关于使用tensorflow object detection API训练自己的模型-补充部分(代码,数据标注工具,训练数据,测试数据)

    之前分享过关于tensorflow object detection API训练自己的模型的几篇博客,后面有人陆续碰到一些问题,问到了我解决方法.所以在这里补充点大家可能用到的东西.声明一下,本人专业 ...

  10. 使用tensorflow object detection API 训练自己的目标检测模型 (三)

    在上一篇博客"使用tensorflow object detection API 训练自己的目标检测模型 (二)"中介绍了如何使用LabelImg标记数据集,生成.xml文件,经过 ...

最新文章

  1. Exchange 2016 先决条件
  2. golang map的定义语法
  3. Zookeeper系列四:Zookeeper实现分布式锁、Zookeeper实现配置中心
  4. java判断日期是否是同一周_JAVA里面怎样判断一个日期是否是星期六或者星期天,给出原代码,多谢!!!!!!!!!!...
  5. 顺序表删除重复元素(完整代码的实现)
  6. 如何使用 Laravel Facades ?
  7. Facebook怎样开发软件:工程师驱动的文化
  8. 程序员:时间就是最好的创意
  9. 计算机程序设计流程图循环,流程图循环画法_流程图用什么办公软件
  10. 字节跳动“蚕食”B端,搅局企业服务?
  11. 五线谱音名和组别对照表_五线谱简谱对照表(五线谱1234567表示图)
  12. [含lw+源码等]S2SH+mysql的报刊订阅系统[包运行成功]Java毕业设计计算机毕设
  13. js练习之--数组方法,数组实现文字内容高亮、替换!
  14. 要依赖于抽象,不要依赖于具体
  15. python+tkinter仿制win10标准型计算器(中文版)完美教学
  16. redis-发布与订阅
  17. Scrapy使用MailSender发送邮件
  18. charles的一个基本的使用
  19. quartus II使用
  20. 本月,我最推荐的意外保险排行榜

热门文章

  1. 32岁了,我有机会转行做程序员吗?——Leo网上答疑(1)
  2. 自己常用的分页SQL
  3. 如何判断两物体加速度相等_高中物理知识点总结解题技巧:关联物体问题的求解方法...
  4. dataframe groupby_python pandas获取groupby之后的数据
  5. Java简单语法与访问权限修饰符
  6. shell 函数定义及应用
  7. Apache Shiro学习笔记(七)IniWebEnvironment
  8. 特殊情形的Riemann引理
  9. MySQL 定时任务event
  10. 异常错误 - MySQL导入时错误