1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| ''' In this example, we will load a RefineDet model and use it to detect objects. ''' import argparse import os import sys import numpy as np import skimage.io as io import cv2
caffe_root = './' os.chdir(caffe_root) sys.path.insert(0, os.path.join(caffe_root, 'python')) import caffe
classes = ['background', 'person']
def filter_boxs(boxs, threshold=0.4): """ boxs: 500*6 (xmin,ymin,xmax,ymax,confidence,class_index) class_index: 0 background, 1 person confidence: 0-1 return: new_boxs `list` [b1,b2,b3,...] """ new_boxs = [] for i in range(0, boxs.shape[0]): xmin,ymin,xmax,ymax,confidence,class_index = boxs[i] if int(class_index)>0 and confidence >= threshold: box = [int(xmin),int(ymin),int(xmax),int(ymax),confidence, int(class_index)] new_boxs.append(box) return new_boxs
def save_results(counter, image_file, boxs, save_fig=False):
img = cv2.imread(image_file) for i in range(0, len(boxs)): xmin,ymin,xmax,ymax,confidence,class_index = boxs[i]
name = classes[class_index] coords = (xmin, ymin), xmax - xmin, ymax - ymin
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 0, 255), 3) display_text = '%.2f' % (confidence) cv2.putText(img, display_text, (xmin, ymin-5), cv2.FONT_HERSHEY_SIMPLEX, 1, color=(0,0,255), thickness=2) if save_fig: image_filepath = 'output/{0}_results.jpg'.format(counter) cv2.imwrite(image_filepath, img) print('Saved: ' + image_filepath)
def single(): caffe.set_device(0) caffe.set_mode_gpu()
save_dir = "./output" if not os.path.exists(save_dir): os.mkdir(save_dir)
model_def = 'models/ResNet/coco/refinedet_resnet101_512x512/deploy.prototxt' model_weights = 'models/ResNet/coco/refinedet_resnet101_512x512/coco_refinedet_resnet101_512x512_iter_75000.caffemodel' net = caffe.Net(model_def, model_weights, caffe.TEST)
img_resize = 512 net.blobs['data'].reshape(1, 3, img_resize, img_resize) data_shape = net.blobs['data'].data.shape print("data_shape=", data_shape) transformer = caffe.io.Transformer({'data':data_shape}) transformer.set_transpose('data', (2, 0, 1)) transformer.set_channel_swap('data', (2, 1, 0)) transformer.set_raw_scale('data', 255) transformer.set_mean('data', np.array([104, 117, 123])) files = ["./images/1.png", "./images/2.png"] for index,image_file in enumerate(files): print("image_file=", image_file) image = caffe.io.load_image(image_file) print("image.shape=", image.shape)
transformed_image = transformer.preprocess('data', image) print("transformed_image.shape=", transformed_image.shape)
net.blobs['data'].data[...] = transformed_image
detections = net.forward()['detection_out'] print("detections.shape = ",detections.shape) det_label = detections[0, 0, :, 1] det_conf = detections[0, 0, :, 2] det_xmin = detections[0, 0, :, 3] * image.shape[1] det_ymin = detections[0, 0, :, 4] * image.shape[0] det_xmax = detections[0, 0, :, 5] * image.shape[1] det_ymax = detections[0, 0, :, 6] * image.shape[0] boxs = np.column_stack([det_xmin, det_ymin, det_xmax, det_ymax, det_conf, det_label]) print("boxs = ", boxs.shape)
new_boxs = filter_boxs(boxs) print("new_boxs = ", len(new_boxs))
save_results(index, image_file, new_boxs, save_fig=True)
def batch(): caffe.set_device(0) caffe.set_mode_gpu()
save_dir = "./output" if not os.path.exists(save_dir): os.mkdir(save_dir)
model_def = 'models/ResNet/coco/refinedet_resnet101_512x512/deploy.prototxt' model_weights = 'models/ResNet/coco/refinedet_resnet101_512x512/coco_refinedet_resnet101_512x512_iter_75000.caffemodel' net = caffe.Net(model_def, model_weights, caffe.TEST)
box_count_per_image = 500 files = ["./images/1.png", "./images/2.png"] batch_size = len(files) img_resize = 512 net.blobs['data'].reshape(batch_size, 3, img_resize, img_resize) data_shape = net.blobs['data'].data.shape print("data_shape=", data_shape) transformer = caffe.io.Transformer({'data':data_shape}) transformer.set_transpose('data', (2, 0, 1)) transformer.set_channel_swap('data', (2, 1, 0)) transformer.set_raw_scale('data', 255) transformer.set_mean('data', np.array([104, 117, 123])) for i in range(len(files)): image_file = files[i] print("image_file=", image_file) image = caffe.io.load_image(image_file) print("image.shape=", image.shape)
transformed_image = transformer.preprocess('data', image) print("transformed_image.shape=", transformed_image.shape)
net.blobs['data'].data[i,:,:,:] = transformed_image
detections = net.forward()['detection_out'] print("detections.shape = ",detections.shape)
for i in range(batch_size): start = i * box_count_per_image end = (i+1) * box_count_per_image print("start-end: ",start, end) det_label = detections[0, 0, start:end, 1] print(det_label[:10]) det_conf = detections[0, 0, start:end, 2] det_xmin = detections[0, 0, start:end, 3] * image.shape[1] det_ymin = detections[0, 0, start:end, 4] * image.shape[0] det_xmax = detections[0, 0, start:end, 5] * image.shape[1] det_ymax = detections[0, 0, start:end, 6] * image.shape[0] boxs = np.column_stack([det_xmin, det_ymin, det_xmax, det_ymax, det_conf, det_label]) print("boxs = ", boxs.shape)
new_boxs = filter_boxs(boxs) print("new_boxs = ", len(new_boxs))
save_results(i, image_file, new_boxs, save_fig=True)
if __name__ == '__main__': batch()
|