目标检测 获得的boundingbox 绘制并且保存

可能我是读了一个假的研究生,这么简单的弄了3个小时,本来打算用现成的 可是一直保存不了,后面没办法自己写了一个。

其中 VOC_BBOX_LABEL_NAMES是list数组 保存对应的标签名,如果类别多的话可以用循环来写(我写在第二个代码上),颜色全部固定一致的,我第一种写法颜色是不一致的。

        pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs, [
                                                                       sizes])

        image = cv2.imread(
            '/media/chenli/F1/cell_data/BCCD_Dataset/BCCD/' + 'JPEGImages/' + str(id_[0]) + '.jpg')
        bboxs = at.tonumpy(pred_bboxes_[0])
        name = at.tonumpy(pred_labels_[0]).reshape(-1)
        score = at.tonumpy(pred_scores_[0]).reshape(-1)

        for i in range(len(name)):
            xmin = int(round(float(bboxs[i, 1])))
            ymin = int(round(float(bboxs[i, 0])))
            xmax = int(round(float(bboxs[i, 3])))
            ymax = int(round(float(bboxs[i, 2])))
            if name[i] == 0:
                cv2.rectangle(image, (xmin, ymin),
                              (xmax, ymax), (0, 255, 0), 1)
                cv2.putText(image, VOC_BBOX_LABEL_NAMES[0], (xmin, ymin - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 1e-3 * image.shape[0], (0, 255, 0), 1)
                cv2.putText(image, str(score[i])[0:3], (xmin + 30, ymin - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 1e-3 * image.shape[0], (0, 255, 0), 1)
            if name[i] == 1:
                cv2.rectangle(image, (xmin, ymin),
                              (xmax, ymax), (0, 0, 255), 1)
                cv2.putText(image, VOC_BBOX_LABEL_NAMES[1], (xmin, ymin - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 1e-3 * image.shape[0], (0, 0, 255), 1)
                cv2.putText(image, str(score[i])[0:3], (xmin + 30, ymin - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 1e-3 * image.shape[0], (0, 0, 255), 1)
            if name[i] == 2:
                cv2.rectangle(image, (xmin, ymin),
                              (xmax, ymax), (0, 0, 255), 1)
                cv2.putText(image, VOC_BBOX_LABEL_NAMES[2], (xmin, ymin - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 1e-3 * image.shape[0], (0, 0, 255), 1)
                cv2.putText(image, str(score[i])[0:3], (xmin + 30, ymin - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 1e-3 * image.shape[0], (0, 0, 255), 1)
        cv2.imwrite('result/' + str(id_[0]) + '.jpg', image)
    for j in np.unique(name):
        cv2.rectangle(image,(xmin,ymin),(xmax,ymax),(0,0,255),1)
        cv2.putText(image,VOC_BBOX_LABEL_NAMES[j],(xmin,ymin-10),
        cv2.FONT_HERSHEY_SIMPLEX,1e-3*image.shape[0],(0,0,255),1)
        cv2.putText(image,str(score[i])[0:3],(xmin+30,ymin-10),
        cv2.FONT_HERSHEY_SIMPLEX,1e-3*image.shape[0],(0,0,255),1)

 

你可能感兴趣的:(目标检测)