Ejemplo n.º 1
0
 def crop_once(anno, write_images):
     image_id = anno['image_id']
     if write_images:
         image = cv2.imread(
             os.path.join(settings.TEST_IMAGE_DIR, anno['file_name']))
         assert image.shape == imshape
     cropped_list = []
     for level_id, (cropratio,
                    cropoverlap) in enumerate(settings.TEST_CROP_LEVELS):
         cropshape = (int(round(settings.TEST_IMAGE_SIZE // cropratio)),
                      int(round(settings.TEST_IMAGE_SIZE // cropratio)))
         for o in darknet_tools.get_crop_bboxes(imshape, cropshape,
                                                (cropoverlap, cropoverlap)):
             xlo = o['xlo']
             xhi = xlo + cropshape[1]
             ylo = o['ylo']
             yhi = ylo + cropshape[0]
             basename = '{}_{}_{}'.format(image_id, level_id, o['name'])
             cropped_file_name = os.path.join(settings.TEST_CROPPED_DIR,
                                              '{}.jpg'.format(basename))
             cropped_list.append(cropped_file_name)
             if write_images:
                 cropped = image[ylo:yhi, xlo:xhi]
                 cv2.imwrite(cropped_file_name, cropped)
     return cropped_list
Ejemplo n.º 2
0
def read(file_paths):
    all = defaultdict(list)
    imshape = (2048, 2048, 3)
    removal = (0., 0., 0.)
    size_ranges = ((float('-inf'), float('inf')), (32., float('inf')),
                   (64., float('inf')))
    levelmap = dict()
    for level_id, (cropratio,
                   cropoverlap) in enumerate(settings.TEST_CROP_LEVELS):
        cropshape = (settings.TEST_IMAGE_SIZE // cropratio,
                     settings.TEST_IMAGE_SIZE // cropratio)
        for o in darknet_tools.get_crop_bboxes(imshape, cropshape,
                                               (cropoverlap, cropoverlap)):
            levelmap[level_id, o['name']] = (o['xlo'], o['ylo'], cropshape[1],
                                             cropshape[0])

    def bounded_bbox(bbox):
        x, y, w, h = bbox
        x1, y1 = x + w, y + h
        x0, y0 = max(0, x), max(0, y)
        x1, y1 = min(imshape[1], x1), min(imshape[0], y1)
        return (x0, y0, x1 - x0, y1 - y0)

    def read_one(result_file_path):
        with open(result_file_path) as f:
            lines = f.read().splitlines()
        one = []
        for line in lines:
            file_path, cate_id, x, y, w, h, prob = line.split()
            image_id, level_id, crop_name = os.path.splitext(
                os.path.basename(file_path))[0].split('_', 2)
            level_id = int(level_id)
            cx, cy, cw, ch = levelmap[level_id, crop_name]
            cate_id = settings.NUM_CHAR_CATES if proposal_output else int(
                cate_id)
            x, y, w, h, prob = float(x), float(y), float(w), float(h), float(
                prob)
            longsize = max(w, h)
            size_range = size_ranges[level_id]
            if longsize < size_range[0] or size_range[1] <= longsize:
                continue
            rm = removal[level_id]
            if (cx != 0 and x < rm) or (cy != 0 and y < rm) or (
                    cx + cw != imshape[1]
                    and x + w + rm >= cw) or (cy + ch != imshape[0]
                                              and y + h + rm >= ch):
                continue
            real_bbox = bounded_bbox((x + cx, y + cy, w, h))
            if real_bbox[2] > 0 and real_bbox[3] > 0:
                all[image_id].append({
                    'image_id': image_id,
                    'cate_id': cate_id,
                    'prob': prob,
                    'bbox': real_bbox
                })

    for file_path in file_paths:
        read_one(file_path)
    return all
Ejemplo n.º 3
0
 def construct_levelmap(imshape):
     levelmap = {}
     for level_id, (cropratio,
                    cropoverlap) in enumerate(settings.TEST_CROP_LEVELS):
         cropshape = (settings.TEST_IMAGE_SIZE // cropratio,
                      settings.TEST_IMAGE_SIZE // cropratio)
         for o in darknet_tools.get_crop_bboxes(imshape, cropshape,
                                                (cropoverlap, cropoverlap)):
             levelmap[level_id, o['name']] = (o['xlo'], o['ylo'],
                                              cropshape[1], cropshape[0])
     return levelmap
Ejemplo n.º 4
0
 def crop_once(line, write_images):
     anno = json.loads(line.strip())
     image_id = anno['image_id']
     all = []
     for char in anno_tools.each_char(anno):
         if not char['is_chinese']:
             continue
         cate_id = text2cate[char['text']]
         if cate_id >= settings.NUM_CHAR_CATES:
             cate_id = settings.NUM_CHAR_CATES
         all.append((char['adjusted_bbox'], cate_id))
     if write_images:
         image = cv2.imread(
             os.path.join(settings.TRAINVAL_IMAGE_DIR, anno['file_name']))
         assert image.shape == imshape
         for o in anno['ignore']:
             poly = (np.array(o['polygon'])).astype(np.int32)
             cv2.fillConvexPoly(image, poly, (128, 128, 128))
     cropped_list = list()
     for o in darknet_tools.get_crop_bboxes(imshape, cropshape,
                                            cropoverlap):
         xlo = o['xlo']
         xhi = xlo + cropshape[1]
         ylo = o['ylo']
         yhi = ylo + cropshape[0]
         labels = []
         for bbox, cate_id in all:
             x, y, w, h = bbox
             if x > xhi or x + w < xlo or y > yhi or y + h < ylo:
                 continue
             bbox = ((x + w / 2 - xlo) / cropshape[1],
                     (y + h / 2 - ylo) / cropshape[0], w / cropshape[1],
                     h / cropshape[0])
             if 0.5 < in_image_ratio(bbox):
                 labels.append((bbox, cate_id))
         if 0 < len(labels):
             basename = '{}_{}'.format(image_id, o['name'])
             cropped_file_name = os.path.join(settings.TRAINVAL_CROPPED_DIR,
                                              '{}.jpg'.format(basename))
             cropped_xml_name = os.path.join(settings.TRAINVAL_CROPPED_DIR,
                                             '{}.xml'.format(basename))
             cropped_list.append('{} {}'.format(cropped_file_name,
                                                cropped_xml_name))
             if write_images:
                 cropped = image[ylo:yhi, xlo:xhi]
                 cv2.imwrite(cropped_file_name, cropped)
                 with open(
                         os.path.join(settings.TRAINVAL_CROPPED_DIR,
                                      '{}.xml'.format(basename)), 'w') as f:
                     write_xml(labels, cropshape, f)
     return cropped_list
Ejemplo n.º 5
0
 def crop_once(anno, write_images):
     image_id = anno['image_id']
     print(image_id)
     #if write_images:
     image = cv2.imread(
         os.path.join(settings.TEST_IMAGE_DIR, anno['file_name']))
     imshape = image.shape  # height,width,channel
     #            assert image.shape == imshape
     cropped_list = []
     #TEST_CROP_LEVELS=((1,32),(0.5,96),(.25,96)
     #TEST_IMAGE_SIZE=(128,128), so cropped image is 128*128 and 256*256 and 512*512,
     for level_id, (cropratio,
                    cropoverlap) in enumerate(settings.TEST_CROP_LEVELS):
         print(cropratio, cropoverlap)
         cropshape = (int(round(settings.TEST_IMAGE_SIZE // cropratio)),
                      int(round(settings.TEST_IMAGE_SIZE // cropratio)))
         for o in darknet_tools.get_crop_bboxes(imshape, cropshape,
                                                (cropoverlap, cropoverlap)):
             xlo = o['xlo']
             xhi = xlo + cropshape[1]
             ylo = o['ylo']
             yhi = ylo + cropshape[0]
             basename = '{}_{}_{}'.format(image_id, level_id, o['name'])
             cropped_file_name = os.path.join(settings.TEST_CROPPED_DIR,
                                              '{}.jpg'.format(basename))
             cropped_list.append(cropped_file_name)
             if write_images:
                 if xhi > imshape[1]:
                     image = cv2.resize(image, (xhi, imshape[0]),
                                        cv2.INTER_CUBIC)
                     imshape = image.shape
                 if yhi > imshape[0]:
                     image = cv2.resize(image, (imshape[1], yhi),
                                        cv2.INTER_CUBIC)
                     imshape = image.shape
                 cropped = image[ylo:yhi, xlo:xhi]
                 cv2.imwrite(cropped_file_name, cropped)
     return cropped_list
Ejemplo n.º 6
0
 def crop_once(imgPath, testName_list):
     image = cv2.imread(imgPath)
     imshape = image.shape
     fileName = os.path.basename(imgPath)
     image_id = os.path.splitext(fileName)[0]
     image_type = os.path.splitext(fileName)[1]
     cropped_list = []
     for level_id, (cropratio,
                    cropoverlap) in enumerate(settings.TEST_CROP_LEVELS):
         cropshape = (int(round(settings.TEST_IMAGE_SIZE // cropratio)),
                      int(round(settings.TEST_IMAGE_SIZE // cropratio)))
         for o in darknet_tools.get_crop_bboxes(imshape, cropshape,
                                                (cropoverlap, cropoverlap)):
             xlo = o['xlo']
             xhi = xlo + cropshape[1]
             ylo = o['ylo']
             yhi = ylo + cropshape[0]
             basename = '{}_{}_{}'.format(image_id, level_id, o['name'])
             cropped_file_name = os.path.join(settings.TEST_CROPPED_DIR,
                                              basename + image_type)
             cropped_list.append(cropped_file_name)
             cropped = image[ylo:yhi, xlo:xhi]
             cv2.imwrite(cropped_file_name, cropped)
     testName_list += cropped_list