from matplotlib import pyplot as plt
import numpy as np
import os import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
import glob
from collections import defaultdict
from io import StringIO from PIL import Image
import DrawBox
import cv2
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util SaveFilename='D:/FasterR-CNNImageTest/11/'
if not os.path.exists(SaveFilename):
os.mkdir(SaveFilename)
with tf.device('/cpu:0'):
cap = cv2.VideoCapture(0)
PATH_TO_CKPT = 'pb/frozen_inference_graph.pb'
PATH_TO_LABELS = os.path.join('dataset', 'pascal_label_map.pbtxt')
NUM_CLASSES = 2
# Load a (frozen) Tensorflow model into memory.
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) 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) # # Detection
cnt = 0
PATH_TO_TEST_IMAGES_DIR = 'E:/PythonOpenCVCode/BalanceGeneratePicture/TestSetSaveImage/Test/JPEGImages' with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
# while True: # for image_path in TEST_IMAGE_PATHS: #changed 20170825
# Definite input and output Tensors for detection_graph
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.
detection_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.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
for pidImage in glob.glob(PATH_TO_TEST_IMAGES_DIR + "/*.jpg"):
TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, pidImage)]
# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# Visualization of the results of a detection.
# print(boxes)
DrawBox.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
max_boxes_to_draw=400,
use_normalized_coordinates=True,
groundtruth_box_visualization_color='red',
line_thickness=8)
#plt.figure(figsize=IMAGE_SIZE)
cv2.imwrite(SaveFilename + os.path.basename(image_path), image_np)
# plt.imshow(image_np)
cnt = cnt + 1
print(image_path)

tensorflow高效地推导pb模型,完整代码的更多相关文章

  1. 使用redis的zset实现高效分页查询(附完整代码)

    一.需求 移动端系统里有用户和文章,文章可设置权限对部分用户开放.现要实现的功能是,用户浏览自己能看的最新文章,并可以上滑分页查看. 二.数据库表设计 涉及到的数据库表有:用户表TbUser.文章表T ...

  2. tensorflow学习笔记——模型持久化的原理,将CKPT转为pb文件,使用pb模型预测

    由题目就可以看出,本节内容分为三部分,第一部分就是如何将训练好的模型持久化,并学习模型持久化的原理,第二部分就是如何将CKPT转化为pb文件,第三部分就是如何使用pb模型进行预测. 一,模型持久化 为 ...

  3. 【5】TensorFlow光速入门-图片分类完整代码

    本文地址:https://www.cnblogs.com/tujia/p/13862364.html 系列文章: [0]TensorFlow光速入门-序 [1]TensorFlow光速入门-tenso ...

  4. tensorflow C++接口调用图像分类pb模型代码

    #include <fstream> #include <utility> #include <Eigen/Core> #include <Eigen/Den ...

  5. tensorflow C++接口调用目标检测pb模型代码

    #include <iostream> #include "tensorflow/cc/ops/const_op.h" #include "tensorflo ...

  6. 导出pb模型之后测试的python代码

    链接:https://blog.csdn.net/thriving_fcl/article/details/75213361 saved_model模块主要用于TensorFlow Serving.T ...

  7. 查看tensorflow pb模型文件的节点信息

    查看tensorflow pb模型文件的节点信息: import tensorflow as tf with tf.Session() as sess: with open('./quantized_ ...

  8. tensorflow c++ API加载.pb模型文件并预测图片

    tensorflow  python创建模型,训练模型,得到.pb模型文件后,用c++ api进行预测 #include <iostream> #include <map> # ...

  9. 将keras的h5模型转换为tensorflow的pb模型

    h5_to_pb.py from keras.models import load_model import tensorflow as tf import os import os.path as ...

随机推荐

  1. 使用maven搭建Hibernate

    使用maven搭建Hibernate框架(web项目) create table USERS ( ID NUMBER not null primary key, NAME ), PASSWORD ), ...

  2. springboot整合TinyMCE文件上传回显

    今天想尝试TinyMCE富文本,准备着手搭建自己的博客,发现springboot上传文件,如果把文件放在static文件夹不能即时回显,百度了下,说是要刷新文件夹才能解决. 有问题就有解决办法 方法1 ...

  3. <python基础>封装,继承,多态,重写,重载

    什么是封装? 所谓的面向对象就是将我们的程序模块化,对象化,把具体事物的特性属性和通过这些属性来实现一些动作的具体方法放到一个类里面,这就是封装.封装是我们所说的面相对象编程的特征之一.除此之外还有继 ...

  4. TIB、TEB 信息

    https://en.wikipedia.org/wiki/Win32_Thread_Information_Block 这是重点 Position Length Windows Versions D ...

  5. nginx 配置文件备份 nginx.conf and vhosts

    bogon:vhosts xingchong$ brew services restart nginx Stopping `nginx`... (might take a while) ==> ...

  6. 显示和隐藏(display属性)none或block

    显示和隐藏(display属性) 网页中经常会看到显示和隐藏的效果,可通过display属性来设置. 语法: Object.style.display = value 注意:Object是获取的元素对 ...

  7. Android开发 EditText按回车按键后出现 focus search returned a view that wasn't able to take focus! 错误

    问题描述 将EditText这个View成为了ListView或者RecyclerView的item时,在按输入法的回车/下一步/next时会出现的 focus search returned a v ...

  8. 第七章 Odoo 12开发之记录集 - 使用模型数据

    在上一篇文章中,我们概览了模型创建以及如何从模型中载入和导出数据.现在我们已有数据模型和相关数据,是时候学习如何编程与其进行交互 了.模型的 ORM(Object-Relational Mapping ...

  9. 移动端,fixed bottom问题

    //不显示 .bar { position:fixed; bottom:0; z-index:99; } //显示 .bar{ position:fixed; bottom:calc(90vh); / ...

  10. [POI2014]KAR-Cards

    题目链接: 传送门 题目分析: 线段树妙题,感觉思路奇奇怪怪的,虽然对我来说不是"线段树菜题"(\(ldx\)神仙\(blog\)原话)\(QAQ\) 考虑怎么样维护可合并的信息解 ...