目标检测实例ssd_detect.py
来源:互联网 发布:海量数据存储 编辑:程序博客网 时间:2024/06/05 19:43
# coding: utf-8# Note: this file is expected to be in {caffe_root}/examples# ### 1. Setupfrom __future__ import print_functionimport numpy as npimport matplotlib.pyplot as pltimport pylabplt.rcParams['figure.figsize'] = (10, 10)plt.rcParams['image.interpolation'] = 'nearest'plt.rcParams['image.cmap'] = 'gray'caffe_root = '../'import osos.chdir(caffe_root)import syssys.path.insert(0, '/ssda/software/caffe/python')import caffefrom google.protobuf import text_formatfrom caffe.proto import caffe_pb2caffe.set_device(0)caffe.set_mode_gpu()labelmap_file = '/ssda/software/caffe/data/BIRD_MEDIUM2017/labelmap_voc.prototxt'file = open(labelmap_file, 'r')labelmap = caffe_pb2.LabelMap()text_format.Merge(str(file.read()), labelmap)def get_labelname(labelmap, labels): num_labels = len(labelmap.item) labelnames = [] if type(labels) is not list: labels = [labels] for label in labels: found = False for i in xrange(0, num_labels): if label == labelmap.item[i].label: found = True labelnames.append(labelmap.item[i].display_name) break assert found == True return labelnamesmodel_def = '/ssda/software/caffe/models/VGGNet/BIRD_MEDIUM2017/SSD_300x300/deploy.prototxt'model_weights = '/ssda/software/caffe/models/VGGNet/BIRD_MEDIUM2017/SSD_300x300/BIRD2017_SSD_300x300_iter_90000.caffemodel'net = caffe.Net(model_def, model_weights, caffe.TEST)# input preprocessing: 'data' is the name of the input blob == net.inputs[0]transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})transformer.set_transpose('data', (2, 0, 1))transformer.set_mean('data', np.array([104, 117, 123])) # mean pixeltransformer.set_raw_scale( 'data', 255) # the reference model operates on images in [0,255] range instead of [0,1]transformer.set_channel_swap( 'data', (2, 1, 0)) # the reference model has channels in BGR order instead of RGB# ### 2. SSD detection# Load an image.image_resize = 300net.blobs['data'].reshape(1, 3, image_resize, image_resize)image = caffe.io.load_image('/ssda/software/caffe/examples/images/bird.jpg')plt.imshow(image)# Run the net and examine the top_k resultstransformed_image = transformer.preprocess('data', image)net.blobs['data'].data[...] = transformed_image# Forward pass.detections = net.forward()['detection_out']# Parse the outputs.det_label = detections[0, 0, :, 1]det_conf = detections[0, 0, :, 2]det_xmin = detections[0, 0, :, 3]det_ymin = detections[0, 0, :, 4]det_xmax = detections[0, 0, :, 5]det_ymax = detections[0, 0, :, 6]# Get detections with confidence higher than 0.6.top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6]top_conf = det_conf[top_indices]top_label_indices = det_label[top_indices].tolist()top_labels = get_labelname(labelmap, top_label_indices)top_xmin = det_xmin[top_indices]top_ymin = det_ymin[top_indices]top_xmax = det_xmax[top_indices]top_ymax = det_ymax[top_indices]# Plot the boxescolors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()currentAxis = plt.gca()for i in xrange(top_conf.shape[0]): # bbox value xmin = int(round(top_xmin[i] * image.shape[1])) ymin = int(round(top_ymin[i] * image.shape[0])) xmax = int(round(top_xmax[i] * image.shape[1])) ymax = int(round(top_ymax[i] * image.shape[0])) # score score = top_conf[i] # label label = int(top_label_indices[i]) label_name = top_labels[i] # display info: label score xmin ymin xmax ymax display_txt = '%s: %.2f %d %d %d %d' % (label_name, score, xmin, ymin, xmax, ymax) # display_bbox_value = '%d %d %d %d' % (xmin, ymin, xmax, ymax) coords = (xmin, ymin), xmax - xmin + 1, ymax - ymin + 1 color = colors[label] currentAxis.add_patch( plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2)) currentAxis.text( xmin, ymin, display_txt, bbox={'facecolor': color, 'alpha': 0.5}) # currentAxis.text((xmin+xmax)/2, (ymin+ymax)/2, display_bbox_value, bbox={'facecolor': color, 'alpha': 0.5})plt.imshow(image)pylab.show()
阅读全文
0 0
- 目标检测实例ssd_detect.py
- [目标检测] faster-rcnn demo.py 解析
- 【深度学习:目标检测】 py-faster-rcnn demo.py解析
- 目标检测算法SSD源码解读~~~~~~~~~~ssd_pascal.py
- 利用py-faster-rcnn训练目标检测模型
- 【修改anchor】目标检测框架py-faster-rcnn修改anchor_box
- 目标检测实例-SSD/examples/detection.ipynb
- 【深度学习:目标检测】 py-faster-rcnn_caffemodel对人脸进行标注
- py-faster-rcnn制作自己的数据集做目标检测
- 目标检测
- 目标检测
- 目标检测
- 目标检测
- 目标检测
- 目标检测
- 目标检测
- 目标检测
- 目标检测
- 获取iOS应用信息
- Vue.js相关资料地址
- 使用ffmpeg可以生成视频截图
- 机器学习之numpy和matplotlib学习(五)
- Linux中VMware虚拟机增加磁盘空间的扩容操作[图文]
- 目标检测实例ssd_detect.py
- 所有的进制都是十进制
- C#中的条件判断(?,??等等)
- mybatis执行批量更新update 的方法oracle
- JAVA学习第二天
- DB2-添加外键的SQL语句
- monkey测试
- Struts2的文件下载
- Android 4.4 KitKat NotificationManagerService使用详解与原理分析(一)__使用详解