コード例 #1
0
def run_one_scn(demo_scn, demo_dir, opt):
    Detector = detector_factory[opt.task]
    detector = Detector(opt)

    basename = os.path.basename(demo_scn)
    basename = basename.replace('.scn', '')
    basename = basename.replace('.svs', '')
    # basename = basename.replace(' ', '-')
    working_dir = os.path.join(demo_dir, basename)

    xml_file = os.path.join(working_dir, '%s.xml' % (basename))
    if os.path.exists(xml_file):
        return

    patch_2d_dir, simg_big, simg = scn_to_patchs(demo_scn, working_dir, opt)

    if os.path.isdir(patch_2d_dir):
        image_names = []
        ls = os.listdir(patch_2d_dir)
        for file_name in sorted(ls):
            ext = file_name[file_name.rfind('.') + 1:].lower()
            if ext in image_ext:
                image_names.append(os.path.join(patch_2d_dir, file_name))
    else:
        image_names = [patch_2d_dir]

    detect_all = None
    count = 1
    for (image_name) in image_names:
        ret = detector.run(image_name)
        results = ret['results']
        res_strs = os.path.basename(image_name).replace('.png',
                                                        '').split('-x-')
        lv_str = res_strs[0]
        patch_start_x = np.int(res_strs[3])
        patch_start_y = np.int(res_strs[4])

        if opt.filter_boarder:
            output_h = opt.input_h  # hard coded
            output_w = opt.input_w  # hard coded
            for j in range(1, opt.num_classes + 1):
                for i in range(len(results[j])):
                    cp = [0, 0]
                    cp[0] = results[j][i][0]
                    cp[1] = results[j][i][1]
                    cr = results[j][i][2]
                    if cp[0] - cr < 0 or cp[0] + cr > output_w:
                        results[j][i][3] = 0
                        continue
                    if cp[1] - cr < 0 or cp[1] + cr > output_h:
                        results[j][i][3] = 0
                        continue

        for j in range(1, opt.num_classes + 1):
            for circle in results[j]:
                if circle[3] > opt.vis_thresh:
                    circle_out = circle[:]
                    circle_out[0] = circle[0] + patch_start_x
                    circle_out[1] = circle[1] + patch_start_y
                    if detect_all is None:
                        detect_all = [circle]
                    else:
                        detect_all = np.append(detect_all, [circle], axis=0)

        time_str = ''
        for stat in time_stats:
            time_str = time_str + '{} {:.3f}s |'.format(stat, ret[stat])
        print(' %d/%d %s' % (count, len(image_names), time_str))
        count = count + 1

    num_classes = 1
    scales = 1
    max_per_image = 2000
    run_nms = True
    results2 = merge_outputs(num_classes, max_per_image, run_nms, detect_all)
    detect_all = results2[1]

    # detections = []
    # det_clss = {}
    # det_clss[1] = detect_all
    # detections.append(det_clss)
    # detect_all = merge_outputs(opt, detections)

    if not simg_big is None:
        debugger = Debugger(dataset=opt.dataset,
                            ipynb=(opt.debug == 3),
                            theme=opt.debugger_theme)
        debugger.add_img(simg_big, img_id='')
        debugger.save_all_imgs(working_dir,
                               prefix='%s' % (basename))  # save original image
        json_file = os.path.join(working_dir, '%s.json' % (basename))
        debugger.save_detect_all_to_json(simg_big, detect_all, json_file, opt,
                                         simg)

        for circle in detect_all:
            debugger.add_coco_circle(circle[:3],
                                     circle[-1],
                                     circle[3],
                                     img_id='')
        debugger.save_all_imgs(working_dir, prefix='%s_overlay' %
                               (basename))  # save original overlay

    # # make open slide file
    # with open("/media/huoy1/48EAE4F7EAE4E264/Projects/detection/test_demo/Case 01-3_manual_good.xml") as fd:
    #     doc = xmltodict.parse(fd.read())

    try:
        start_x = np.int(simg.properties['openslide.bounds-x'])
        start_y = np.int(simg.properties['openslide.bounds-y'])
        width_x = np.int(simg.properties['openslide.bounds-width'])
        height_y = np.int(simg.properties['openslide.bounds-height'])
    except:
        start_x = 0
        start_y = 0
        width_x = np.int(simg.properties['aperio.OriginalWidth'])
        height_y = np.int(simg.properties['aperio.OriginalHeight'])
    down_rate = simg.level_downsamples[opt.lv]

    detect_json = []
    doc_out = {}
    doc_out['Annotations'] = {}
    doc_out['Annotations']['@MicronsPerPixel'] = simg.properties[
        'openslide.mpp-x']
    doc_out['Annotations']['@Level'] = opt.lv
    doc_out['Annotations']['@DownRate'] = down_rate
    doc_out['Annotations']['@start_x'] = start_x
    doc_out['Annotations']['@start_y'] = start_y
    doc_out['Annotations']['@width_x'] = width_x
    doc_out['Annotations']['@height_y'] = height_y
    if 'leica.device-model' in simg.properties:
        doc_out['Annotations']['@Device'] = 'leica.device-model'
    else:
        doc_out['Annotations']['@Device'] = 'aperio.Filename'
    doc_out['Annotations']['Annotation'] = {}
    doc_out['Annotations']['Annotation']['@Id'] = '1'
    doc_out['Annotations']['Annotation']['@Name'] = ''
    doc_out['Annotations']['Annotation']['@ReadOnly'] = '0'
    doc_out['Annotations']['Annotation']['@LineColorReadOnly'] = '0'
    doc_out['Annotations']['Annotation']['@Incremental'] = '0'
    doc_out['Annotations']['Annotation']['@Type'] = '4'
    doc_out['Annotations']['Annotation']['@LineColor'] = '65280'
    doc_out['Annotations']['Annotation']['@Visible'] = '1'
    doc_out['Annotations']['Annotation']['@Selected'] = '1'
    doc_out['Annotations']['Annotation']['@MarkupImagePath'] = ''
    doc_out['Annotations']['Annotation']['@MacroName'] = ''
    doc_out['Annotations']['Annotation']['Attributes'] = {}
    doc_out['Annotations']['Annotation']['Attributes']['Attribute'] = {}
    doc_out['Annotations']['Annotation']['Attributes']['Attribute'][
        '@Name'] = 'glomerulus'
    doc_out['Annotations']['Annotation']['Attributes']['Attribute'][
        '@Id'] = '0'
    doc_out['Annotations']['Annotation']['Attributes']['Attribute'][
        '@Value'] = ''
    doc_out['Annotations']['Annotation']['Plots'] = None
    doc_out['Annotations']['Annotation']['Regions'] = {}
    doc_out['Annotations']['Annotation']['Regions'][
        'RegionAttributeHeaders'] = {}
    doc_out['Annotations']['Annotation']['Regions']['AttributeHeader'] = []
    doc_out['Annotations']['Annotation']['Regions']['Region'] = []

    for di in range(len(detect_all)):
        detect_one = detect_all[di]
        detect_dict = {}
        detect_dict['@Id'] = str(di + 1)
        detect_dict['@Type'] = '2'
        detect_dict['@Zoom'] = '0.5'
        detect_dict['@ImageLocation'] = ''
        detect_dict['@ImageFocus'] = '-1'
        detect_dict['@Length'] = '2909.1'
        detect_dict['@Area'] = '673460.1'
        detect_dict['@LengthMicrons'] = '727.3'
        detect_dict['@AreaMicrons'] = '42091.3'
        detect_dict['@Text'] = ('%.3f' % detect_one[3])
        detect_dict['@NegativeROA'] = '0'
        detect_dict['@InputRegionId'] = '0'
        detect_dict['@Analyze'] = '0'
        detect_dict['@DisplayId'] = str(di + 1)
        detect_dict['Attributes'] = None
        detect_dict['Vertices'] = '0'
        detect_dict['Vertices'] = {}
        detect_dict['Vertices']['Vertex'] = []

        if 'leica.device-model' in simg.properties:  #leica
            coord1 = {}
            coord1['@X'] = str(height_y -
                               (detect_one[1] - detect_one[2]) * down_rate)
            coord1['@Y'] = str((detect_one[0] - detect_one[2]) * down_rate)
            coord1['@Z'] = '0'
            coord2 = {}
            coord2['@X'] = str(
                height_y - (detect_one[1] + detect_one[2]) * down_rate)  # 左右
            coord2['@Y'] = str(
                (detect_one[0] + detect_one[2]) * down_rate)  # 上下
            coord2['@Z'] = '0'
            detect_dict['Vertices']['Vertex'].append(coord1)
            detect_dict['Vertices']['Vertex'].append(coord2)
        elif 'aperio.Filename' in simg.properties:
            coord1 = {}
            coord1['@X'] = str((detect_one[0] - detect_one[2]) * down_rate)
            coord1['@Y'] = str((detect_one[1] - detect_one[2]) * down_rate)
            coord1['@Z'] = '0'
            coord2 = {}
            coord2['@X'] = str(
                (detect_one[0] + detect_one[2]) * down_rate)  # 左右
            coord2['@Y'] = str(
                (detect_one[1] + detect_one[2]) * down_rate)  # 上下
            coord2['@Z'] = '0'
            detect_dict['Vertices']['Vertex'].append(coord1)
            detect_dict['Vertices']['Vertex'].append(coord2)
        doc_out['Annotations']['Annotation']['Regions']['Region'].append(
            detect_dict)

    out = xmltodict.unparse(doc_out, pretty=True)
    with open(xml_file, 'wb') as file:
        file.write(out.encode('utf-8'))

    os.system('rm -r "%s"' % (os.path.join(working_dir, '2d_patch')))

    return
コード例 #2
0
ファイル: circledet.py プロジェクト: hrlblab/CircleNet
    def debug(self, batch, output, iter_id):
        opt = self.opt
        reg = output['reg'] if opt.reg_offset else None
        # print(output)
        dets = circledet_decode(output['hm'],
                                output['cl'],
                                reg=reg,
                                cat_spec_wh=opt.cat_spec_wh,
                                K=opt.K)

        # print(dets)
        if opt.filter_boarder:
            output_h = self.opt.default_resolution[
                0] // self.opt.down_ratio  #hard coded
            output_w = self.opt.default_resolution[
                1] // self.opt.down_ratio  #hard coded
            for i in range(dets.shape[1]):
                cp = [0, 0]
                cp[0] = dets[0, i, 0]
                cp[1] = dets[0, i, 1]
                cr = dets[0, i, 2]
                if cp[0] - cr < 0 or cp[0] + cr > output_w:
                    dets[0, i, 3] = 0
                    continue
                if cp[1] - cr < 0 or cp[1] + cr > output_h:
                    dets[0, i, 3] = 0
                    continue

        dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2])
        dets[:, :, :3] *= opt.down_ratio
        dets_gt = batch['meta']['gt_det'].numpy().reshape(1, -1, dets.shape[2])
        dets_gt[:, :, :3] *= opt.down_ratio
        for i in range(1):
            debugger = Debugger(dataset=opt.dataset,
                                ipynb=(opt.debug == 3),
                                theme=opt.debugger_theme)
            img = batch['input'][i].detach().cpu().numpy().transpose(1, 2, 0)
            img = np.clip(((img * opt.std + opt.mean) * 255.), 0,
                          255).astype(np.uint8)
            pred = debugger.gen_colormap(
                output['hm'][i].detach().cpu().numpy())
            gt = debugger.gen_colormap(batch['hm'][i].detach().cpu().numpy())
            debugger.add_blend_img(img, pred, 'pred_hm')
            debugger.add_blend_img(img, gt, 'gt_hm')
            debugger.add_img(img, img_id='out_pred')

            for k in range(len(dets[i])):
                # print('risk = %f' % dets[i, k, 3])
                if dets[i, k, 3] > opt.center_thresh:
                    debugger.add_coco_circle(dets[i, k, :3],
                                             dets[i, k, -1],
                                             dets[i, k, 3],
                                             img_id='out_pred')

            debugger.add_img(img, img_id='out_gt')
            for k in range(len(dets_gt[i])):
                if dets_gt[i, k, 3] > opt.center_thresh:
                    debugger.add_coco_circle(dets_gt[i, k, :3],
                                             dets_gt[i, k, -1],
                                             dets_gt[i, k, 3],
                                             img_id='out_gt')

            if opt.debug == 4:
                debugger.save_all_imgs(opt.debug_dir,
                                       prefix='{}'.format(iter_id))
            else:
                debugger.show_all_imgs(pause=True)