コード例 #1
0
def NMS(boxes,thresh=0.2,is_Min=False):
       """
       :param boxes: 输入
       :param thresh: 超参,阈值,用于判断是否舍弃数据
       :param is_Min: IoU函数的参数
       :return: NMS后的数据
       """
       #如果网络没有输出建议框,box.shape=(0,)
       if boxes.shape[0]==0:
              return np.array([])
       buffer=[]
       boxes_sorted=boxes[np.argsort(boxes[:,0])[::-1]]#降序
       print(boxes_sorted.shape)
       # print(boxes_sorted)
       # while boxes_sorted.shape[0]>1:
       while boxes_sorted.shape[0] >=1:
           #取第一个框
              a_box=boxes_sorted[0]

           #取剩下的框
              b_boxes=boxes_sorted[1:]
           #第一个框必定是保留的
              buffer.append(a_box)
           #比较IoU,大于阈值的框去掉,用bool索引
              IOU=iou(a_box,b_boxes,is_Min)
              mask=np.where(IOU<thresh)

           #用bool索引得到得array来替代上一次迭代保留的框的array数据
              boxes_sorted=b_boxes[mask]


       # if boxes_sorted.shape[0]>0:
       #        buffer.append(boxes_sorted[0])#boxes_sorted=[[]]的形状

       return np.stack(buffer)
コード例 #2
0
def filter_annotations(img_all_annotations, used_classes):
    """
    过滤掉一些没有用的类别和dontcare区域的annotations
    :param img_all_annotations: 图片的所有标注
    :param used_classes: 需要留下记录的列别
    :return:
    """
    img_filtered_annotations = {}

    # 1、过滤这个图片中标注的我们训练指定不需要的类别,把索引记录下来
    # 方便后面在处理对应的一些坐标时候使用
    relevant_annotation_indices = [
        i for i, x in enumerate(img_all_annotations['type']) if x in used_classes
    ]
    # print("获取过滤之后的数据的下标:", relevant_annotation_indices)
    # 2、获取过滤后的下标对应某个标记物体的其它信息
    for key in img_all_annotations.keys():
        img_filtered_annotations[key] = (
            img_all_annotations[key][relevant_annotation_indices])

    # 3、如果dontcare在我们要获取的类别里面,也进行组合获取,然后过滤相关的bboxes不符合要求的
    if 'dontcare' in used_classes:
        dont_care_indices = [i for i,
                             x in enumerate(img_filtered_annotations['type'])
                             if x == 'dontcare']
        # print("dontcare下标:",dont_care_indices)
        # bounding box的格式[y_min, x_min, y_max, x_max]
        all_boxes = np.stack([img_filtered_annotations['2d_bbox_top'],
                              img_filtered_annotations['2d_bbox_left'],
                              img_filtered_annotations['2d_bbox_bottom'],
                              img_filtered_annotations['2d_bbox_right']],
                             axis=1)

        # 计算bboxesIOU,比如这样的
        # Truck 0.00 0 -1.57 599.41 156.40 629.75 189.25 2.85 2.63 12.34 0.47 1.49 69.44 -1.56
        # DontCare -1 -1 -10 503.89 169.71 590.61 190.13 -1 -1 -1 -1000 -1000 -1000 -10
        # DontCare -1 -1 -10 511.35 174.96 527.81 187.45 -1 -1 -1 -1000 -1000 -1000 -10
        # DontCare -1 -1 -10 532.37 176.35 542.68 185.27 -1 -1 -1 -1000 -1000 -1000 -10
        # DontCare -1 -1 -10 559.62 175.83 575.40 183.15 -1 -1 -1 -1000 -1000 -1000 -10
        ious = iou(boxes1=all_boxes,
                   boxes2=all_boxes[dont_care_indices])

        # 删除所有 bounding boxes 与 dontcare region 重叠的区域
        if ious.size > 0:
            # 找出下标
            boxes_to_remove = np.amax(ious, axis=1) > 0.0
            for key in img_all_annotations.keys():
                img_filtered_annotations[key] = (
                    img_filtered_annotations[key][np.logical_not(boxes_to_remove)])

    return img_filtered_annotations
コード例 #3
0
        confidence = prediction['confidence']
        tl = (prediction['topleft']['x'], prediction['topleft']['y'])
        br = (prediction['bottomright']['x'], prediction['bottomright']['y'])
        if label == 'RBC' and confidence < .5:
            continue
        if label == 'WBC' and confidence < .25:
            continue
        if label == 'Platelets' and confidence < .25:
            continue

        # clearing up spurious platelets
        if label == 'Platelets':
            if record != []:
                tree = spatial.cKDTree(record)
                index = tree.query(tl)[1]
                iou_value = iou(tl + br, tl_[index] + br_[index])
                iou_.append(iou_value)
            if iou_value > 0.1:
                continue
            record.append(tl)
            tl_.append(tl)
            br_.append(br)

        # image = cv2.rectangle(image, tl, br,color, 2)
        center_x = int((tl[0] + br[0]) / 2)
        center_y = int((tl[1] + br[1]) / 2)
        center = (center_x, center_y)
        # color = tuple(255 * np.random.rand(3))
        if label == 'RBC':
            color = (255, 0, 0)
            rbc = rbc + 1
コード例 #4
0
import torch
import numpy as np
from IoU import iou
from NMS import NMS
from draw_rectangle import draw_rect
a = np.array([90, 10, 120, 40])
b = np.array([[20, 70, 60, 120], [148, 132, 238, 282]])
iu = iou(a, b)
# print(iu)
#
a = np.array([533., 41., 622., 175.])
b = np.array([[490, 67, 563, 251.]])
IOU = iou(a, b)
# print(IOU)
path = "test_img.jpg"
boxes = np.array([[0.97, 133, 80, 225, 265], [0.89, 157, 69, 261, 238],
                  [0.85, 148, 132, 238, 282], [0.70, 105, 112, 195, 303],
                  [0.69, 88, 50, 187, 193], [0.70, 316, 209, 378, 312],
                  [0.50, 298, 173, 348, 340], [0.90, 490, 67, 563, 251],
                  [0.70, 446, 46, 526, 181], [0.79, 533, 41, 622, 175],
                  [0.85, 429, 87, 619, 216]])
# box=NMS(boxes,thresh=0.1)
# box=[0.97,133,80,225,265]
# print(len([boxes[0]]))

# draw_rect(path,boxes)
# draw_rect(path,box)
# a=np.array(5)
# b=np.array([1,2,3,4])
# c=np.minimum(a,b)
# print(c)
def blood_cell_count(file_name):
    rbc = 0
    wbc = 0
    platelets = 0

    cell = []
    cls = []
    conf = []

    record = []
    tl_ = []
    br_ = []
    iou_ = []
    iou_value = 0

    tic = time.time()
    image = cv2.imread('Images/' + file_name)
    output = tfnet.return_predict(image)

    for prediction in output:
        label = prediction['label']
        confidence = prediction['confidence']
        tl = (prediction['topleft']['x'], prediction['topleft']['y'])
        br = (prediction['bottomright']['x'], prediction['bottomright']['y'])
        if label == 'RBC' and confidence < .5:
            continue
        if label == 'WBC' and confidence < .25:
            continue
        if label == 'Platelets' and confidence < .25:
            continue

        # clearing up overlapped same platelets
        if label == 'Platelets':
            if record != []:
                tree = spatial.cKDTree(record)
                index = tree.query(tl)[1]
                iou_value = iou(tl + br, tl_[index] + br_[index])
                iou_.append(iou_value)
            if iou_value > 0.1:
                continue
            record.append(tl)
            tl_.append(tl)
            br_.append(br)

        # image = cv2.rectangle(image, tl, br,color, 2)
        center_x = int((tl[0] + br[0]) / 2)
        center_y = int((tl[1] + br[1]) / 2)
        center = (center_x, center_y)
        # color = tuple(255 * np.random.rand(3))
        if label == 'RBC':
            color = (255, 0, 0)
            rbc = rbc + 1
        if label == 'WBC':
            color = (0, 255, 0)
            wbc = wbc + 1
        if label == 'Platelets':
            color = (0, 0, 255)
            platelets = platelets + 1
        radius = int((br[0] - tl[0]) / 2)
        image = cv2.circle(image, center, radius, color, 2)
        font = cv2.FONT_HERSHEY_COMPLEX
        image = cv2.putText(image, label, (center_x - 15, center_y + 5), font,
                            .5, color, 1)
        cell.append([tl[0], tl[1], br[0], br[1]])
        if label == 'RBC': cls.append(0)
        if label == 'WBC': cls.append(1)
        if label == 'Platelets': cls.append(2)
        conf.append(confidence)
    toc = time.time()
    pred_bb.append(cell)
    pred_cls.append(cls)
    pred_conf.append(conf)
    avg_time = (toc - tic) * 1000
    print('{0:.5}'.format(avg_time), 'ms')

    cv2.imwrite('Output/' + file_name, image)
    cv2.imshow(
        'Total RBC: ' + str(rbc) + ', WBC: ' + str(wbc) + ', Platelets: ' +
        str(platelets), image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()