def vis_all_detection(im_array, detections, imdb_classes=None, thresh=0.): """ visualize all detections in one image :param im_array: [b=1 c h w] in rgb :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ] :param imdb_classes: list of names in imdb :param thresh: threshold for valid detections :return: """ import matplotlib.pyplot as plt import random im = image_processing.transform_inverse(im_array, config.PIXEL_MEANS) plt.imshow(im) for j in range(1, len(imdb_classes)): color = (random.random(), random.random(), random.random()) # generate a random color dets = detections[j] for i in range(dets.shape[0]): bbox = dets[i, :4] score = dets[i, -1] if score > thresh: rect = plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False, edgecolor=color, linewidth=2) plt.gca().add_patch(rect) plt.gca().annotate('{} {:.3f}'.format(imdb_classes[j], score), rect.get_xy(), color='w') plt.show()
def vis_all_detection(im_array, detections, imdb_classes=None, thresh=0.7): """ visualize all detections in one image :param im_array: [b=1 c h w] in rgb :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ] :param imdb_classes: list of names in imdb :param thresh: threshold for valid detections :return: """ import matplotlib.pyplot as plt import random im = image_processing.transform_inverse(im_array, config.PIXEL_MEANS) plt.imshow(im) for j in range(1, len(imdb_classes)): color = (random.random(), random.random(), random.random()) # generate a random color dets = detections[j] for i in range(dets.shape[0]): bbox = dets[i, :4] score = dets[i, -1] if score > thresh: rect = plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False, edgecolor=color, linewidth=3.5) plt.gca().add_patch(rect) plt.gca().text(bbox[0], bbox[1] - 2, '{:s} {:.3f}'.format(imdb_classes[j], score), bbox=dict(facecolor=color, alpha=0.5), fontsize=12, color='white') plt.show()
def save_all_detection(im_array, detections, imdb_classes=None, thresh=0.7): """ save all detections in one image with result.png :param im_array: [b=1 c h w] in rgb :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ] :param imdb_classes: list of names in imdb :param thresh: threshold for valid detections :return: """ import random im = image_processing.transform_inverse(im_array, config.PIXEL_MEANS) im = im[:, :, ::-1].copy() # back to b,g,r for j in range(1, len(imdb_classes)): color = (255 * random.random(), 255 * random.random(), 255 * random.random()) # generate a random color dets = detections[j] for i in range(dets.shape[0]): bbox = dets[i, :4] score = dets[i, -1] if score > thresh: cv2.rectangle(im, (int(round(bbox[0])), int(round(bbox[1]))), (int(round(bbox[2])), int(round(bbox[3]))), color, 2) cv2.putText(im, '%s' % imdb_classes[j], (bbox[0], bbox[1]), cv2.FONT_HERSHEY_SIMPLEX, 1.0, color, 2) cv2.imwrite("result.jpg", im)
def vis_detection(im, dets, thresh=0.): """ draw detected bounding boxes :param im: [b, c, h, w] oin rgb :param dets: only one class, [N * [4 coordinates score]] :param thresh: thresh for valid detections :return: """ from rcnn.config import config from helper.processing.image_processing import transform_inverse import matplotlib.pyplot as plt inds = np.where(dets[:, -1] >= thresh)[0] if len(inds) == 0: return inds = np.argsort(dets[:, -1])[::-1] inds = inds[:20] class_name = 'obj' fig, ax = plt.subplots(figsize=(12, 12)) im = transform_inverse(im, config.PIXEL_MEANS) ax.imshow(im, aspect='equal') for i in inds: bbox = dets[i, :4] score = dets[i, -1] rect = plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False, edgecolor='red', linewidth=3.5) ax.add_patch(rect) ax.text(bbox[0], bbox[1] - 2, '{:s} {:3f}'.format(class_name, score), bbox=dict(facecolor='blue', alpha=0.5), fontsize=14, color='white') ax.set_title('{} detections with p({} | box) >= {:.1f}'.format( class_name, class_name, thresh), fontsize=14) plt.axis('off') plt.tight_layout() plt.draw() plt.show()
def save_all_detection(im_array, detections, imdb_classes=None, thresh=0.7): """ save all detections in one image with result.png :param im_array: [b=1 c h w] in rgb :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ] :param imdb_classes: list of names in imdb :param thresh: threshold for valid detections :return: """ import random im = image_processing.transform_inverse(im_array, config.PIXEL_MEANS) im = im[:, :, ::-1].copy() # back to b,g,r for j in range(1, len(imdb_classes)): color = (255*random.random(), 255*random.random(), 255*random.random()) # generate a random color dets = detections[j] for i in range(dets.shape[0]): bbox = dets[i, :4] score = dets[i, -1] if score > thresh: cv2.rectangle(im, (int(round(bbox[0])), int(round(bbox[1]))), (int(round(bbox[2])), int(round(bbox[3]))), color, 2) cv2.imwrite("result.jpg", im)
def vis_detection(im, dets, thresh=0.): """ draw detected bounding boxes :param im: [b, c, h, w] oin rgb :param dets: only one class, [N * [4 coordinates score]] :param thresh: thresh for valid detections :return: """ from rcnn.config import config from helper.processing.image_processing import transform_inverse import matplotlib.pyplot as plt inds = np.where(dets[:, -1] >= thresh)[0] if len(inds) == 0: return inds = np.argsort(dets[:, -1])[::-1] inds = inds[:20] class_name = 'obj' fig, ax = plt.subplots(figsize=(12, 12)) im = transform_inverse(im, config.PIXEL_MEANS) ax.imshow(im, aspect='equal') for i in inds: bbox = dets[i, :4] score = dets[i, -1] rect = plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False, edgecolor='red', linewidth=3.5) ax.add_patch(rect) ax.text(bbox[0], bbox[1] - 2, '{:s} {:3f}'.format(class_name, score), bbox=dict(facecolor='blue', alpha=0.5), fontsize=14, color='white') ax.set_title('{} detections with p({} | box) >= {:.1f}'.format(class_name, class_name, thresh), fontsize=14) plt.axis('off') plt.tight_layout() plt.draw() plt.show()