花了一天写了个小代码来绘制出GroundTruth的位置,以方便做单张图片测试时的比较应用。先睡觉了,注释和细节优化再说吧!
8.23修改保存图片,去除了保存图片的白边并使之尽量与原图一样大小。注意pad_inches和dpi参数可能只是适应500*375,其他尺寸仍需要微调。可以说是最大的败笔了,要怪就怪plt的机制非得留白。要想彻底解决这个问题大概要换一种绘图方式了
import _init_paths
import os
from datasets.imdb import imdb
import datasets.ds_utils as ds_utils
import xml.etree.ElementTree as ET
import numpy as np
import scipy.sparse
import scipy.io as sio
import utils.cython_bbox
import cPickle
import subprocess
import uuid
from fast_rcnn.config import cfg
from datasets.pascal_voc import pascal_voc
import pandas as pd
import matplotlib.pylab as plt
import cv2
from PIL import Image
CLASSES = ('__background__',
'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor')
def get_image_annotation(imdb,index):
filename = os.path.join(imdb._data_path, 'Annotations', index + '.xml')
tree = ET.parse(filename)
objs = tree.findall('object')
num_objs = len(objs)
boxes = np.zeros((num_objs, 4), dtype=np.uint16)
gt_classes = np.zeros((num_objs), dtype=np.int32)
# Load object bounding boxes into a data dict.
for ix, obj in enumerate(objs):
bbox = obj.find('bndbox')
# Make pixel indexes 0-based
x1 = float(bbox.find('xmin').text) - 1
y1 = float(bbox.find('ymin').text) - 1
x2 = float(bbox.find('xmax').text) - 1
y2 = float(bbox.find('ymax').text) - 1
cls = imdb._class_to_ind[obj.find('name').text.lower().strip()]
boxes[ix, :] = [x1, y1, x2, y2]
gt_classes[ix] = cls
dets={'boxes': boxes,'gt_classes':gt_classes}
return dets
def draw(gt, image_name):
"""Detect object classes in an image using pre-computed object proposals."""
# Load the demo image
im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
im = cv2.imread(im_file)
inds=len(gt['gt_classes'])
boxes=gt['boxes']
clses=np.empty(inds,dtype='S16')
for cls in gt['gt_classes']:
clses[np.where(gt['gt_classes']==cls)]= CLASSES[int(cls)]#Load cls_name according to cls index
im = im[:, :, (2, 1, 0)]
fig, ax = plt.subplots()
ax.imshow(im, aspect='equal')
for i in range(inds):
bbox = boxes[i, :4]
ax.add_patch(
plt.Rectangle((bbox[0], bbox[1]),
bbox[2] - bbox[0],
bbox[3] - bbox[1], fill=False,
edgecolor='red', linewidth=1.5)
)
ax.text(bbox[0]+6, bbox[1] +16,
'{:s}'.format(clses[i]),
bbox=dict(facecolor='y',alpha=0.3),
fontsize=8, color='white')
plt.axis('off')
fig = plt.gcf()
w=float(im.shape[1])/100
h=float(im.shape[0])/100
fig.set_size_inches(w,h)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top = 0.02, bottom = 0, right = 0.01, left = 0, hspace = 0, wspace = 0)#tight the bbox
plt.margins(0,0)
plt.tight_layout()
plt.draw()
if __name__ == '__main__':
imdb=pascal_voc("test","2007")#Get imdb data
im_names = ['000456.jpg','000542.jpg', '001150.jpg',
'001763.jpg', '004545.jpg']
for im_name in im_names:
print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
im=im_name.split(".")[0]#Get im index
gt=get_image_annotation(imdb,im)#Get Groundtruth annotation
draw(gt,im_name)
plt.savefig("/home/dl/deeplearning/pylearn/zsy/testfigs/" + im_name,dpi =110, bbox_inches = "tight",pad_inches=-0.015)
print im_name+" saved."