1.环境配置

2.数据集获取

3.训练集获取

4.训练

5.调用测试训练结果

6.代码讲解

  本文是第五篇,讲解如何调用测试训练结果。

上一篇中我们输出了训练的模型,这一篇中我们通过调用训练好的模型来完成测试工作。

在object_detection目录下创建test.py并输入以下内容:

import os
import cv2
import numpy as np
import tensorflow as tf
import sys
sys.path.append("..")
from utils import label_map_util
from utils import visualization_utils as vis_util ENERMY = 2 # 1 代表蓝色方,2 代表红色方 ,设置蓝色方为敌人
DEBUG = False
THRE_VAL = 0.2 PATH_TO_CKPT ='/home/xueaoru/models/research/inference_graph_v2/frozen_inference_graph.pb'
PATH_TO_LABELS = '/home/xueaoru/models/research/object_detection/car_label_map.pbtxt'
NUM_CLASSES = 2
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)
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='') sess = tf.Session(graph=detection_graph)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
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') def video_test():
#cap = cv2.VideoCapture(1)
cap = cv2.VideoCapture("/home/xueaoru/下载/RoboMaster2.mp4")
while(1):
time = cv2.getTickCount()
ret, image = cap.read()
if ret!= True:
break
image_expanded = np.expand_dims(image, axis=0)#[1,w,h,3] (boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_expanded})
#print(np.squeeze(classes).astype(np.int32))
#print(np.squeeze(scores))
#print(np.squeeze(boxes))
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8,
min_score_thresh=0.4) cv2.imshow('Object detector', image)
key = cv2.waitKey(1)&0xff
time = cv2.getTickCount() - time
print("处理时间:"+str(time*1000/cv2.getTickFrequency()))
if key ==27:
break
cv2.destroyAllWindows()
def pic_test():
image = cv2.imread("/home/xueaoru/models/research/images/image12.jpg")
image_expanded = np.expand_dims(image, axis=0) # [1,w,h,3] (boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_expanded}) if DEBUG:
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8,
min_score_thresh=0.80)
else:
score = np.squeeze(scores)
max_index = np.argmax(score)
score = score[max_index]
detected_class = np.squeeze(classes).astype(np.int32)[max_index]
if score > THRE_VAL and detected_class == ENERMY:
box = np.squeeze(boxes)[max_index]#(ymin,xmin,ymax,xmax)
h,w,_ = image.shape
min_point = (int(box[1]*w),int(box[0]*h))
max_point = (int(box[3]*w),int(box[2]*h))
cv2.rectangle(image,min_point,max_point,(0,255,255),2) cv2.imshow('Object detector', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
video_test()

好了,暂时就先这样吧,最后一篇详细讲解包括通过这些识别到的框到最后计算炮台偏转角度的代码。这段代码的讲解也放在后面。

[神经网络]一步一步使用Mobile-Net完成视觉识别(五)的更多相关文章

  1. 一步一步理解word2Vec

    一.概述 关于word2vec,首先需要弄清楚它并不是一个模型或者DL算法,而是描述从自然语言到词向量转换的技术.词向量化的方法有很多种,最简单的是one-hot编码,但是one-hot会有维度灾难的 ...

  2. 如何一步一步用DDD设计一个电商网站(十二)—— 提交并生成订单

    阅读目录 前言 解决数据一致性的方案 回到DDD 设计 实现 结语 一.前言 之前的十一篇把用户购买商品并提交订单整个流程上的中间环节都过了一遍.现在来到了这最后一个环节,提交订单.单从业务上看,这个 ...

  3. 如何一步一步用DDD设计一个电商网站(十三)—— 领域事件扩展

    阅读目录 前言 回顾 本地的一致性 领域事件发布出现异常 订阅者处理出现异常 结语 一.前言 上篇中我们初步运用了领域事件,其中还有一些问题我们没有解决,所以实现是不健壮的,下面先来回顾一下. 二.回 ...

  4. NLP(二十九)一步一步,理解Self-Attention

      本文大部分内容翻译自Illustrated Self-Attention, Step-by-step guide to self-attention with illustrations and ...

  5. 如何一步一步用DDD设计一个电商网站(九)—— 小心陷入值对象持久化的坑

    阅读目录 前言 场景1的思考 场景2的思考 避坑方式 实践 结语 一.前言 在上一篇中(如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成),有一行注释的代码: public interfa ...

  6. 如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成

    阅读目录 前言 建模 实现 结语 一.前言 前面几篇已经实现了一个基本的购买+售价计算的过程,这次再让售价丰满一些,增加一个会员价的概念.会员价在现在的主流电商中,是一个不大常见的模式,其带来的问题是 ...

  7. 如何一步一步用DDD设计一个电商网站(十)—— 一个完整的购物车

     阅读目录 前言 回顾 梳理 实现 结语 一.前言 之前的文章中已经涉及到了购买商品加入购物车,购物车内购物项的金额计算等功能.本篇准备把剩下的购物车的基本概念一次处理完. 二.回顾 在动手之前我对之 ...

  8. 如何一步一步用DDD设计一个电商网站(七)—— 实现售价上下文

    阅读目录 前言 明确业务细节 建模 实现 结语 一.前言 上一篇我们已经确立的购买上下文和销售上下文的交互方式,传送门在此:http://www.cnblogs.com/Zachary-Fan/p/D ...

  9. 如何一步一步用DDD设计一个电商网站(六)—— 给购物车加点料,集成售价上下文

    阅读目录 前言 如何在一个项目中实现多个上下文的业务 售价上下文与购买上下文的集成 结语 一.前言 前几篇已经实现了一个最简单的购买过程,这次开始往这个过程中增加一些东西.比如促销.会员价等,在我们的 ...

  10. 如何一步一步用DDD设计一个电商网站(五)—— 停下脚步,重新出发

    阅读目录 前言 单元测试 纠正错误,重新出发 结语 一.前言 实际编码已经写了2篇了,在这过程中非常感谢有听到观点不同的声音,借着这个契机,今天这篇就把大家提出的建议一个个的过一遍,重新整理,重新出发 ...

随机推荐

  1. 7.12实习培训日志 Linux Docker

    Linux 管理 RHEL7 的用户和组 用户的属性修改 chage -l [username] #查看用户信息 usermod --expiredate=YYYY-MM-DD [username] ...

  2. POJ 2387 Til the Cows Come Home Dijkstra求最短路径

    Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...

  3. 3dmax室内设计,建筑视频

    第一教程篮球场 http://video.1kejian.com/video/?30800-0-1.html 第一课 元素级别-flip反转(直接看模型里面) 第二课 alt+r = ring crt ...

  4. Python 获取脚本路径以及脚本所在文件夹路径

    import os script_path = os.path.realpath(__file__) script_dir = os.path.dirname(script_path)

  5. 关于spring的简概

    一.Spring入门操作 IOC 创建对象 <bean id="user" class="com.itheima.domain.User">< ...

  6. Java基础笔记(一)——JDK、JRE、JVM

    JDK.JRE和JVM三者的关系 Java程序执行过程 JVM(java virtual machine) 注:由于各种操作系统(windows.linux等)支持的指令集(二进制可执行代码)不同,程 ...

  7. 字符串split函数

    https://www.cnblogs.com/hjhsysu/p/5700347.html 函数:split() Python中有split()和os.path.split()两个函数,具体作用如下 ...

  8. Luogu P2257 YY的GCD 莫比乌斯反演

    第一道莫比乌斯反演...$qwq$ 设$f(d)=\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)==d]$ $F(n)=\sum_{n|d}f(d)=\lfloor \frac{N ...

  9. POJ-3177-RedundantPaths(边联通分量,缩点)

    链接:https://vjudge.net/problem/POJ-3177#author=Dillydally 题意: 有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可 ...

  10. Uva12210-A Match Making Problem

    对于每个数字二分找到大于等于它的数,再暴力找到第一个小于它的数 #include<bits/stdc++.h> #define inf 0x3f3f3f3f ; using namespa ...