def ShowPreview(self):
     self.MyReqProgressBar.running = False
     self.MyReqProgressBar.quit()
     scene = QtGui.QGraphicsScene()
     scene.addPixmap(QtGui.QPixmap('%s/%s.jpg' % (self.MyPreviewDownload.poster_preview_path,self.MyPreviewDownload.title)))
     self.posterView.setScene(scene)
     w, h = imagesize.get('%s/%s.jpg' % (self.MyRequest.poster_preview_path,self.MyPreviewDownload.title))
     self.posterView.fitInView(QtCore.QRectF(0, 0, w, h), QtCore.Qt.KeepAspectRatio)
 def PosterPreview(self, index):
     title = self.reqtext['results'][index]['title']
     poster_path = self.reqtext['results'][index]['poster_path']
     download('https://image.tmdb.org/t/p/%s/%s' % (self.poster_preview_size,poster_path), '%s/%s.jpg' %(self.poster_preview_path, title))
     scene = QtGui.QGraphicsScene()
     scene.addPixmap(QtGui.QPixmap('%s/%s.jpg' % (self.poster_preview_path,title)))
     self.posterView.setScene(scene)
     w, h = imagesize.get('%s/%s.jpg' % (self.poster_preview_path,title))
     self.posterView.fitInView(QtCore.QRectF(0, 0, w, h), QtCore.Qt.KeepAspectRatio)
Example #3
0
def get_image_size(filename):
    try:
        size = imagesize.get(filename)
        if size[0] == -1:
            size = None

        if Image:  # fallback to PIL
            im = Image.open(filename)
            size = im.size
            try:
                im.fp.close()
            except Exception:
                pass

        return size
    except:
        return None
Example #4
0
def get_image_size(filename):
    # type: (unicode) -> Tuple[int, int]
    try:
        size = imagesize.get(filename)
        if size[0] == -1:
            size = None

        if size is None and Image:  # fallback to PIL
            im = Image.open(filename)
            size = im.size
            try:
                im.fp.close()
            except Exception:
                pass

        return size
    except Exception:
        return None
Example #5
0
 def test_load_jpeg(self):
     width, height = imagesize.get(os.path.join(imagedir, "test.jpg"))
     self.assertEqual(width, 802)
     self.assertEqual(height, 670)
Example #6
0
 def test_littleendian_tiff(self):
     width, height = imagesize.get(
         os.path.join(imagedir, "multipage_tiff_example.tif"))
     self.assertEqual(width, 800)
     self.assertEqual(height, 600)
Example #7
0
def init(mdlParams_):
    mdlParams = {}
    local_path = '/isic2019/'
    # local_path = '\isic2019\\'
    mdlParams['saveDir'] = mdlParams_['pathBase'] + '/'
    # Data is loaded from here
    mdlParams['dataDir'] = mdlParams_['pathBase'] + local_path

    ### Model Selection ###
    mdlParams['model_type'] = 'Resnet101'
    mdlParams['dataset_names'] = ['official']  #,'sevenpoint_rez3_ll']
    mdlParams['file_ending'] = '.jpg'
    mdlParams['exclude_inds'] = False
    mdlParams['same_sized_crops'] = False
    mdlParams['multiCropEval'] = 9
    mdlParams['var_im_size'] = False
    mdlParams['orderedCrop'] = False
    mdlParams['voting_scheme'] = 'average'
    mdlParams['classification'] = True
    mdlParams['balance_classes'] = 9
    mdlParams['extra_fac'] = 1.0
    mdlParams['numClasses'] = 9
    mdlParams['no_c9_eval'] = True
    mdlParams['numOut'] = mdlParams['numClasses']
    mdlParams['numCV'] = 1
    mdlParams['trans_norm_first'] = True
    # Deterministic cropping
    mdlParams['deterministic_eval'] = True
    mdlParams['numCropPositions'] = 1
    num_scales = 4
    all_scales = [1.0, 0.5, 0.75, 0.25, 0.9, 0.6, 0.4]
    mdlParams['cropScales'] = all_scales[:num_scales]
    mdlParams['cropFlipping'] = 4
    mdlParams['multiCropEval'] = mdlParams['numCropPositions'] * len(
        mdlParams['cropScales']) * mdlParams['cropFlipping']
    mdlParams['offset_crop'] = 0.2
    # Scale up for b1-b7
    mdlParams['input_size'] = [224, 224, 3]

    ### Training Parameters ###
    # Batch size
    mdlParams['batchSize'] = 20  #*len(mdlParams['numGPUs'])
    # Initial learning rate
    mdlParams['learning_rate'] = 0.000015  #*len(mdlParams['numGPUs'])
    # Lower learning rate after no improvement over 100 epochs
    mdlParams['lowerLRAfter'] = 25
    # If there is no validation set, start lowering the LR after X steps
    mdlParams['lowerLRat'] = 50
    # Divide learning rate by this value
    mdlParams['LRstep'] = 5
    # Maximum number of training iterations
    mdlParams['training_steps'] = 150  #250
    # Display error every X steps
    mdlParams['display_step'] = 10
    # Scale?
    mdlParams['scale_targets'] = False
    # Peak at test error during training? (generally, dont do this!)
    mdlParams['peak_at_testerr'] = False
    # Print trainerr
    mdlParams['print_trainerr'] = False
    # Subtract trainset mean?
    mdlParams['subtract_set_mean'] = False
    mdlParams['setMean'] = np.array([0.0, 0.0, 0.0])
    mdlParams['setStd'] = np.array([1.0, 1.0, 1.0])

    # Data AUG
    #mdlParams['full_color_distort'] = True
    mdlParams['autoaugment'] = True
    mdlParams['flip_lr_ud'] = False
    mdlParams['full_rot'] = 0
    mdlParams['scale'] = (0.8, 1.2)
    mdlParams['shear'] = 10
    mdlParams['cutout'] = 0

    ### Data ###
    mdlParams['preload'] = False
    # Labels first
    # Targets, as dictionary, indexed by im file name
    mdlParams['labels_dict'] = {}
    path1 = mdlParams['dataDir'] + '/labels/'
    # All sets
    allSets = glob(path1 + '*/')
    # Go through all sets
    for i in range(len(allSets)):
        # Check if want to include this dataset
        foundSet = False
        for j in range(len(mdlParams['dataset_names'])):
            if mdlParams['dataset_names'][j] in allSets[i]:
                foundSet = True
        if not foundSet:
            continue
        # Find csv file
        files = sorted(glob(allSets[i] + '*'))
        for j in range(len(files)):
            if 'csv' in files[j]:
                break
        # Load csv file
        with open(files[j], newline='') as csvfile:
            labels_str = csv.reader(csvfile, delimiter=',', quotechar='|')
            for row in labels_str:
                if 'image' == row[0]:
                    continue
                #if 'ISIC' in row[0] and '_downsampled' in row[0]:
                #    print(row[0])
                if row[0] + '_downsampled' in mdlParams['labels_dict']:
                    print("removed", row[0] + '_downsampled')
                    continue
                if mdlParams['numClasses'] == 7:
                    mdlParams['labels_dict'][row[0]] = np.array([
                        int(float(row[1])),
                        int(float(row[2])),
                        int(float(row[3])),
                        int(float(row[4])),
                        int(float(row[5])),
                        int(float(row[6])),
                        int(float(row[7]))
                    ])
                elif mdlParams['numClasses'] == 8:
                    if len(row) < 9 or row[8] == '':
                        class_8 = 0
                    else:
                        class_8 = int(float(row[8]))
                    mdlParams['labels_dict'][row[0]] = np.array([
                        int(float(row[1])),
                        int(float(row[2])),
                        int(float(row[3])),
                        int(float(row[4])),
                        int(float(row[5])),
                        int(float(row[6])),
                        int(float(row[7])), class_8
                    ])
                elif mdlParams['numClasses'] == 9:
                    if len(row) < 9 or row[8] == '':
                        class_8 = 0
                    else:
                        class_8 = int(float(row[8]))
                    if len(row) < 10 or row[9] == '':
                        class_9 = 0
                    else:
                        class_9 = int(float(row[9]))
                    mdlParams['labels_dict'][row[0]] = np.array([
                        int(float(row[1])),
                        int(float(row[2])),
                        int(float(row[3])),
                        int(float(row[4])),
                        int(float(row[5])),
                        int(float(row[6])),
                        int(float(row[7])), class_8, class_9
                    ])
    # Save all im paths here
    mdlParams['im_paths'] = []
    mdlParams['labels_list'] = []
    # Define the sets
    path1 = mdlParams['dataDir'] + '/images/'
    # All sets
    allSets = sorted(glob(path1 + '*/'))
    # Ids which name the folders
    # Make official first dataset
    for i in range(len(allSets)):
        if mdlParams['dataset_names'][0] in allSets[i]:
            temp = allSets[i]
            allSets.remove(allSets[i])
            allSets.insert(0, temp)
    print(allSets)
    # Set of keys, for marking old HAM10000
    mdlParams['key_list'] = []
    if mdlParams['exclude_inds']:
        with open(mdlParams['saveDir'] + 'indices_exclude.pkl', 'rb') as f:
            indices_exclude = pickle.load(f)
        exclude_list = []
    for i in range(len(allSets)):
        # All files in that set
        files = sorted(glob(allSets[i] + '*'))
        # Check if there is something in there, if not, discard
        if len(files) == 0:
            continue
        # Check if want to include this dataset
        foundSet = False
        for j in range(len(mdlParams['dataset_names'])):
            if mdlParams['dataset_names'][j] in allSets[i]:
                foundSet = True
        if not foundSet:
            continue
        for j in range(len(files)):
            if '.jpg' in files[j] or '.jpeg' in files[j] or '.JPG' in files[
                    j] or '.JPEG' in files[j] or '.png' in files[
                        j] or '.PNG' in files[j]:
                # Add according label, find it first
                found_already = False
                for key in mdlParams['labels_dict']:
                    if key + mdlParams['file_ending'] in files[j]:
                        if found_already:
                            print("Found already:", key, files[j])
                        mdlParams['key_list'].append(key)
                        mdlParams['labels_list'].append(
                            mdlParams['labels_dict'][key])
                        found_already = True
                if found_already:
                    mdlParams['im_paths'].append(files[j])
                    if mdlParams['exclude_inds']:
                        for key in indices_exclude:
                            if key in files[j]:
                                exclude_list.append(indices_exclude[key])
    # Convert label list to array
    mdlParams['labels_array'] = np.array(mdlParams['labels_list'])
    print(np.mean(mdlParams['labels_array'], axis=0))
    # Create indices list with HAM10000 only
    mdlParams['HAM10000_inds'] = []
    HAM_START = 24306
    HAM_END = 34320
    for j in range(len(mdlParams['key_list'])):
        try:
            curr_id = [
                int(s) for s in re.findall(r'\d+', mdlParams['key_list'][j])
            ][-1]
        except:
            continue
        if curr_id >= HAM_START and curr_id <= HAM_END:
            mdlParams['HAM10000_inds'].append(j)
    mdlParams['HAM10000_inds'] = np.array(mdlParams['HAM10000_inds'])
    print("Len ham", len(mdlParams['HAM10000_inds']))
    # Perhaps preload images
    if mdlParams['preload']:
        mdlParams['images_array'] = np.zeros([
            len(mdlParams['im_paths']), mdlParams['input_size_load'][0],
            mdlParams['input_size_load'][1], mdlParams['input_size_load'][2]
        ],
                                             dtype=np.uint8)
        for i in range(len(mdlParams['im_paths'])):
            x = scipy.ndimage.imread(mdlParams['im_paths'][i])
            #x = x.astype(np.float32)
            # Scale to 0-1
            #min_x = np.min(x)
            #max_x = np.max(x)
            #x = (x-min_x)/(max_x-min_x)
            mdlParams['images_array'][i, :, :, :] = x
            if i % 1000 == 0:
                print(i + 1, "images loaded...")
    if mdlParams['subtract_set_mean']:
        mdlParams['images_means'] = np.zeros([len(mdlParams['im_paths']), 3])
        for i in range(len(mdlParams['im_paths'])):
            x = scipy.ndimage.imread(mdlParams['im_paths'][i])
            x = x.astype(np.float32)
            # Scale to 0-1
            min_x = np.min(x)
            max_x = np.max(x)
            x = (x - min_x) / (max_x - min_x)
            mdlParams['images_means'][i, :] = np.mean(x, (0, 1))
            if i % 1000 == 0:
                print(i + 1, "images processed for mean...")

    ### Define Indices ###
    # Just divide into 5 equally large sets
    with open(mdlParams['saveDir'] + 'indices_isic2019.pkl', 'rb') as f:
        indices = pickle.load(f)
    mdlParams['trainIndCV'] = indices['trainIndCV']
    mdlParams['valIndCV'] = indices['valIndCV']
    if mdlParams['exclude_inds']:
        exclude_list = np.array(exclude_list)
        all_inds = np.arange(len(mdlParams['im_paths']))
        exclude_inds = all_inds[exclude_list.astype(bool)]
        for i in range(len(mdlParams['trainIndCV'])):
            mdlParams['trainIndCV'] = np.setdiff1d(mdlParams['trainIndCV'],
                                                   exclude_inds)
        for i in range(len(mdlParams['valIndCV'])):
            mdlParams['valIndCV'] = np.setdiff1d(mdlParams['valIndCV'],
                                                 exclude_inds)
    # Consider case with more than one set
    if len(mdlParams['dataset_names']) > 1:
        restInds = np.array(
            np.arange(25331, mdlParams['labels_array'].shape[0]))
        for i in range(mdlParams['numCV']):
            mdlParams['trainIndCV'] = np.concatenate(
                (mdlParams['trainIndCV'], restInds))
    print("Train")
    # for i in range(len(mdlParams['trainIndCV'])):
    #     print(mdlParams['trainIndCV'][i].shape)
    # print("Val")
    # for i in range(len(mdlParams['valIndCV'])):
    #     print(mdlParams['valIndCV'][i].shape)

    # Use this for ordered multi crops
    if mdlParams['orderedCrop']:
        # Crop positions, always choose multiCropEval to be 4, 9, 16, 25, etc.
        mdlParams['cropPositions'] = np.zeros(
            [len(mdlParams['im_paths']), mdlParams['multiCropEval'], 2],
            dtype=np.int64)
        #mdlParams['imSizes'] = np.zeros([len(mdlParams['im_paths']),mdlParams['multiCropEval'],2],dtype=np.int64)
        for u in range(len(mdlParams['im_paths'])):
            height, width = imagesize.get(mdlParams['im_paths'][u])
            if width < mdlParams['input_size'][0]:
                height = int(
                    mdlParams['input_size'][0] / float(width)) * height
                width = mdlParams['input_size'][0]
            if height < mdlParams['input_size'][0]:
                width = int(mdlParams['input_size'][0] / float(height)) * width
                height = mdlParams['input_size'][0]
            ind = 0
            for i in range(np.int32(np.sqrt(mdlParams['multiCropEval']))):
                for j in range(np.int32(np.sqrt(mdlParams['multiCropEval']))):
                    mdlParams['cropPositions'][
                        u, ind, 0] = mdlParams['input_size'][0] / 2 + i * (
                            (width - mdlParams['input_size'][1]) /
                            (np.sqrt(mdlParams['multiCropEval']) - 1))
                    mdlParams['cropPositions'][
                        u, ind, 1] = mdlParams['input_size'][1] / 2 + j * (
                            (height - mdlParams['input_size'][0]) /
                            (np.sqrt(mdlParams['multiCropEval']) - 1))
                    #mdlParams['imSizes'][u,ind,0] = curr_im_size[0]

                    ind += 1
        # Sanity checks
        #print("Positions",mdlParams['cropPositions'])
        # Test image sizes
        height = mdlParams['input_size'][0]
        width = mdlParams['input_size'][1]
        for u in range(len(mdlParams['im_paths'])):
            height_test, width_test = imagesize.get(mdlParams['im_paths'][u])
            if width_test < mdlParams['input_size'][0]:
                height_test = int(mdlParams['input_size'][0] /
                                  float(width_test)) * height_test
                width_test = mdlParams['input_size'][0]
            if height_test < mdlParams['input_size'][0]:
                width_test = int(mdlParams['input_size'][0] /
                                 float(height_test)) * width_test
                height_test = mdlParams['input_size'][0]
            test_im = np.zeros([width_test, height_test])
            for i in range(mdlParams['multiCropEval']):
                im_crop = test_im[
                    np.int32(mdlParams['cropPositions'][u, i, 0] - height /
                             2):np.int32(mdlParams['cropPositions'][u, i, 0] -
                                         height / 2) + height,
                    np.int32(mdlParams['cropPositions'][u, i, 1] - width /
                             2):np.int32(mdlParams['cropPositions'][u, i, 1] -
                                         width / 2) + width]
                if im_crop.shape[0] != mdlParams['input_size'][0]:
                    print("Wrong shape", im_crop.shape[0],
                          mdlParams['im_paths'][u])
                if im_crop.shape[1] != mdlParams['input_size'][1]:
                    print("Wrong shape", im_crop.shape[1],
                          mdlParams['im_paths'][u])
    return mdlParams
Example #8
0
def bbox_annotations_to_coco(images_folder, bbox_annotation_file, class_names_file):

    try:
        p1 = Popen(['cat', '{}'.format(bbox_annotation_file)], stdout=PIPE)
        p2 = Popen(['wc', '-l'], stdin=p1.stdout, stdout=PIPE)
        out, _ = p2.communicate()
        row_count = int(out)
    except Exception:
        row_count = None

    coco_spec = {
        'categories': [],
        'images': [],
        'annotations': []
    }

    class_idx_to_name, class_code_to_idx = parse_class_names(class_names_file)
    for i, name in class_idx_to_name.items():
        coco_spec['categories'].append({'id': i, 'name': name})

    with open(bbox_annotation_file, 'r') as f:
        reader = csv.reader(f, delimiter=',')
        header = next(reader)
        header_to_idx = {header[idx]: idx for idx in range(len(header))}

        seen_images = set()
        image_id = 0
        for i, row in tqdm(enumerate(reader), total=row_count - 1):
            image_name = get_column(row, header_to_idx, 'ImageID')
            if image_name not in seen_images:
                image_id += 1
                image_file_name = '{}.jpg'.format(image_name)

                try:
                    width, height = imagesize.get("{}/{}".format(images_folder, image_file_name))
                except FileNotFoundError:
                    print("file {}/{} not found, skipping!".format(images_folder, image_name))
                    image_id -= 1
                    continue

                coco_spec['images'].append(
                    {
                        'id': image_id,
                        'height': height,
                        'width': width,
                        'file_name': image_file_name
                    }
                )

            bbox = get_bbox(row, header_to_idx, width=width, height=height)
            bbox_list = bbox.to_list(mode='xywh')
            x, y, w, h = bbox_list

            # placeholder for segmentation
            segmentation = [
                [
                    x, y,  # Top left coordinates.
                    x, y + h,  # Bottom left coordinates.
                    x + w, y + h,  # Bottom right coordinates.
                    x + w, y  # Top right coordinates.
                ]
            ]

            label_code = get_column(row, header_to_idx, 'LabelName')
            is_crowd = int(get_column(row, header_to_idx, 'IsGroupOf'))
            is_crowd = is_crowd if is_crowd >= 0 else 0

            coco_spec['annotations'].append(
                {
                    'id': i,
                    'image_id': image_id,
                    'bbox': bbox_list,
                    'segmentation': segmentation,
                    'area': bbox.area(),
                    'iscrowd': is_crowd,
                    'category_id': class_code_to_idx[label_code]
                }
            )

    return coco_spec
# set correct path
path = join("uploads/galleries/", image_path)
print('path: ' + path)

# extract image files
print('Collecting files...')
files = [f for f in listdir(path) if isfile(join(path, f))]
files = [f for f in files if f[f.rfind('.') + 1:] in extensions]

# rename image files
print('Renaming files...')
new_files = []
for f in files:
    if f[f.rfind('-') + 1:f.rfind('.')] != 'thumbnail':
        newf = f[:f.rfind('-')] + "-%sx%s" % imagesize.get(join(
            path, f)) + f[f.rfind('.'):]
        rename(join(path, f), join(path, newf))
    else:
        newf = f
    new_files.append(newf)

files = new_files

# helper objects to store gallery data
new_gallery = {}
thumbs = {}

# group gallery data
print('Grouping files...')
for f in files:
    filename = f[:f.rfind('-')]
Example #10
0
    def amazon_result(self, file_path, variant, method, image_keyword,
                      result_key, value_key, block_key, result):
        '''Returns the result from calling the service on the 'file_path'.
        The result is returned as an TRResult named tuple.
        '''

        # Delay loading the API packages until needed because they take time to
        # load.  Doing this speeds up overall application start time.
        import boto3
        import botocore

        if not result:
            # If any exceptions occur, let them be passed to caller.
            (image, error) = self._image_from_file(file_path)
            if error:
                return TRResult(path=file_path,
                                data={},
                                boxes=[],
                                text='',
                                error=error)
            try:
                if __debug__:
                    log(f'setting up Amazon client function "{variant}"')
                creds = self._credentials
                session = boto3.session.Session()
                client = session.client(
                    variant,
                    region_name=creds['region_name'],
                    aws_access_key_id=creds['aws_access_key_id'],
                    aws_secret_access_key=creds['aws_secret_access_key'])
                if __debug__: log('calling Amazon API function')
                result = getattr(client, method)(**{
                    image_keyword: {
                        'Bytes': image
                    }
                })
                if __debug__: log(f'received {len(result[result_key])} blocks')
            except botocore.exceptions.EndpointConnectionError as ex:
                raise AuthFailure(
                    f'Problem with credentials file -- {str(ex)}')
            except KeyboardInterrupt as ex:
                raise
            except KeyError as ex:
                msg = f'Amazon credentials file is missing {",".join(ex.args)}'
                raise AuthFailure(msg)
            except Exception as ex:
                if getattr(ex, 'response', False) and 'Error' in ex.response:
                    error = ex.response['Error']
                    code = error['Code']
                    text = error['Message']
                    path = relative(file_path)
                    if code in [
                            'UnsupportedDocumentException',
                            'BadDocumentException'
                    ]:
                        msg = f'Amazon {variant} reports bad or corrupted image in {path}'
                        raise CorruptedContent(msg)
                    elif code in [
                            'InvalidSignatureException',
                            'UnrecognizedClientException'
                    ]:
                        raise AuthFailure(
                            f'Problem with credentials file -- {text}')
                # Fallback if we can't get details.
                if __debug__: log(f'Amazon returned exception {str(ex)}')
                msg = f'Amazon {variant} failure for {path} -- {error["Message"]}'
                raise ServiceFailure(msg)

        raise_for_interrupts()
        full_text = ''
        boxes = []
        width, height = imagesize.get(file_path)
        if __debug__: log(f'parsing Amazon result for {relative(file_path)}')
        for block in result[result_key]:
            if value_key not in block:
                continue
            kind = block[value_key].lower()
            if kind in ['word', 'line']:
                text = block[block_key]
                corners = corner_list(block['Geometry']['Polygon'], width,
                                      height)
                if corners:
                    boxes.append(
                        Box(kind=kind,
                            bb=corners,
                            text=text,
                            score=block['Confidence'] / 100))
                else:
                    # Something's wrong with the vertex list. Skip & continue.
                    if __debug__: log(f'bad bb for {text}: {bb}')
            if kind == "line":
                if 'Text' in block:
                    full_text += block['Text'] + '\n'
                elif 'DetectedText' in block:
                    full_text += block['DetectedText'] + '\n'
        return TRResult(path=file_path,
                        data=result,
                        boxes=boxes,
                        text=full_text,
                        error=None)
def init(mdlParams_):
    mdlParams = {}
    local_path = '/isic2019/'
    mdlParams['saveDir'] = mdlParams_['pathBase'] + '/'
    # Data is loaded from here
    mdlParams['dataDir'] = mdlParams_['pathBase'] + local_path

    ### Model Selection ###
    mdlParams['model_type'] = 'efficientnet-b3'
    mdlParams['dataset_names'] = ['official']  # ,'sevenpoint_rez3_ll']
    mdlParams['file_ending'] = '.jpg'
    mdlParams['exclude_inds'] = False
    mdlParams['same_sized_crops'] = False
    mdlParams['multiCropEval'] = 9
    mdlParams['var_im_size'] = False
    mdlParams['orderedCrop'] = False
    mdlParams['voting_scheme'] = 'average'
    mdlParams['classification'] = True
    mdlParams['balance_classes'] = 9
    mdlParams['extra_fac'] = 1.0
    mdlParams['numClasses'] = 8
    mdlParams['no_c9_eval'] = True
    mdlParams['numOut'] = mdlParams['numClasses']
    mdlParams['numCV'] = 1
    mdlParams['trans_norm_first'] = True
    # Scale up for b1-b7
    mdlParams['input_size'] = [300, 300, 3]

    ### Training Parameters ###
    # Batch size
    mdlParams['batchSize'] = 16  # *len(mdlParams['numGPUs'])
    # Initial learning rate
    mdlParams['learning_rate'] = 0.000015  # *len(mdlParams['numGPUs'])
    # Lower learning rate after no improvement over 100 epochs
    mdlParams['lowerLRAfter'] = 25
    # If there is no validation set, start lowering the LR after X steps
    mdlParams['lowerLRat'] = 50
    # Divide learning rate by this value
    mdlParams['LRstep'] = 5
    # Maximum number of training iterations
    mdlParams['training_steps'] = 60  # 250
    # Display error every X steps
    mdlParams['display_step'] = 10
    # Scale?
    mdlParams['scale_targets'] = False
    # Peak at test error during training? (generally, dont do this!)
    mdlParams['peak_at_testerr'] = False
    # Print trainerr
    mdlParams['print_trainerr'] = False
    # Subtract trainset mean?
    mdlParams['subtract_set_mean'] = False
    mdlParams['setMean'] = np.array([0.0, 0.0, 0.0])
    mdlParams['setStd'] = np.array([1.0, 1.0, 1.0])

    # Data AUG
    # mdlParams['full_color_distort'] = True
    mdlParams['autoaugment'] = False
    mdlParams['flip_lr_ud'] = True
    mdlParams['full_rot'] = 180
    mdlParams['scale'] = (0.8, 1.2)
    mdlParams['shear'] = 10
    mdlParams['cutout'] = 16
    ### Data ###
    mdlParams['preload'] = False
    # Labels first
    # Targets, as dictionary, indexed by im file name
    mdlParams['labels_dict'] = {}
    path1 = mdlParams['dataDir'] + '/AISC_ISIC_full_labels/'
    # path1 = mdlParams['dataDir'] + '\labels\\'
    # All sets
    allSets = glob(path1 + '*/')
    # allSets = glob(path1 + '*\\')
    # Go through all sets
    for i in range(len(allSets)):
        # Check if want to include this dataset
        foundSet = False
        for j in range(len(mdlParams['dataset_names'])):
            if mdlParams['dataset_names'][j] in allSets[i]:
                foundSet = True
        if not foundSet:
            continue
        # Find csv file
        files = sorted(glob(allSets[i] + '*'))
        for j in range(len(files)):
            if 'csv' in files[j]:
                break
        # Load csv file
        with open(files[j], newline='') as csvfile:
            labels_str = csv.reader(csvfile, delimiter=',', quotechar='|')
            for row in labels_str:
                if 'image' == row[0]:
                    continue
                # if 'ISIC' in row[0] and '_downsampled' in row[0]:
                #    print(row[0])
                if row[0] + '_downsampled' in mdlParams['labels_dict']:
                    print("removed", row[0] + '_downsampled')
                    continue
                if mdlParams['numClasses'] == 7:
                    mdlParams['labels_dict'][row[0]] = np.array([
                        int(float(row[1])),
                        int(float(row[2])),
                        int(float(row[3])),
                        int(float(row[4])),
                        int(float(row[5])),
                        int(float(row[6])),
                        int(float(row[7]))
                    ])
                elif mdlParams['numClasses'] == 8:
                    if len(row) < 9 or row[8] == '':
                        class_8 = 0
                    else:
                        class_8 = int(float(row[8]))
                    mdlParams['labels_dict'][row[0]] = np.array([
                        int(float(row[1])),
                        int(float(row[2])),
                        int(float(row[3])),
                        int(float(row[4])),
                        int(float(row[5])),
                        int(float(row[6])),
                        int(float(row[7])), class_8
                    ])
                elif mdlParams['numClasses'] == 9:
                    if len(row) < 9 or row[8] == '':
                        class_8 = 0
                    else:
                        class_8 = int(float(row[8]))
                    if len(row) < 10 or row[9] == '':
                        class_9 = 0
                    else:
                        class_9 = int(float(row[9]))
                    mdlParams['labels_dict'][row[0]] = np.array([
                        int(float(row[1])),
                        int(float(row[2])),
                        int(float(row[3])),
                        int(float(row[4])),
                        int(float(row[5])),
                        int(float(row[6])),
                        int(float(row[7])), class_8, class_9
                    ])
    # Save all im paths here
    mdlParams['im_paths'] = []
    mdlParams['labels_list'] = []
    # Define the sets
    path1 = mdlParams['dataDir'] + '/AISC_ISIC_full/official/'
    # All sets
    allSets = sorted(glob(path1 + '*/'))
    # Ids which name the folders
    # Make official first dataset
    for i in range(len(allSets)):
        if mdlParams['dataset_names'][0] in allSets[i]:
            temp = allSets[i]
            allSets.remove(allSets[i])
            allSets.insert(0, temp)
    print(allSets)
    # Set of keys, for marking old HAM10000
    mdlParams['key_list'] = []
    if mdlParams['exclude_inds']:
        with open(mdlParams['saveDir'] + 'indices_exclude.pkl', 'rb') as f:
            indices_exclude = pickle.load(f)
        exclude_list = []
    for i in range(len(allSets)):
        # All files in that set
        files = sorted(glob(allSets[i] + '*'))
        # Check if there is something in there, if not, discard
        if len(files) == 0:
            continue
        # Check if want to include this dataset
        foundSet = False
        for j in range(len(mdlParams['dataset_names'])):
            if mdlParams['dataset_names'][j] in allSets[i]:
                foundSet = True
        if not foundSet:
            continue
        for key in mdlParams['labels_dict']:
            for j in range(len(files)):
                name = files[j].split('/')[-1]
                name = name.split('.')[0]
                if key == name:
                    mdlParams['labels_list'].append(
                        mdlParams['labels_dict'][key])
                    mdlParams['im_paths'].append(files[j])
                    break

    # Convert label list to array
    mdlParams['labels_array'] = np.array(mdlParams['labels_list'])

    ### Define Indices ###
    with open(mdlParams['saveDir'] + 'indices_aisc_isic_2019.pkl', 'rb') as f:
        indices = pickle.load(f)

    mdlParams['trainIndCV'] = indices['trainIndCV']
    mdlParams['valIndCV'] = indices['valIndCV']
    val_labels = mdlParams['labels_array'][mdlParams['valIndCV']]
    print("Class distribution in b3 rr configuration")
    print(np.sum(val_labels, axis=0))

    print("ValIndCV:")
    print(mdlParams['valIndCV'])

    print("Train")
    # Use this for ordered multi crops
    if mdlParams['orderedCrop']:
        # Crop positions, always choose multiCropEval to be 4, 9, 16, 25, etc.
        mdlParams['cropPositions'] = np.zeros(
            [len(mdlParams['im_paths']), mdlParams['multiCropEval'], 2],
            dtype=np.int64)
        # mdlParams['imSizes'] = np.zeros([len(mdlParams['im_paths']),mdlParams['multiCropEval'],2],dtype=np.int64)
        for u in range(len(mdlParams['im_paths'])):
            height, width = imagesize.get(mdlParams['im_paths'][u])
            if width < mdlParams['input_size'][0]:
                height = int(
                    mdlParams['input_size'][0] / float(width)) * height
                width = mdlParams['input_size'][0]
            if height < mdlParams['input_size'][0]:
                width = int(mdlParams['input_size'][0] / float(height)) * width
                height = mdlParams['input_size'][0]
            ind = 0
            for i in range(np.int32(np.sqrt(mdlParams['multiCropEval']))):
                for j in range(np.int32(np.sqrt(mdlParams['multiCropEval']))):
                    mdlParams['cropPositions'][
                        u, ind, 0] = mdlParams['input_size'][0] / 2 + i * (
                            (width - mdlParams['input_size'][1]) /
                            (np.sqrt(mdlParams['multiCropEval']) - 1))
                    mdlParams['cropPositions'][
                        u, ind, 1] = mdlParams['input_size'][1] / 2 + j * (
                            (height - mdlParams['input_size'][0]) /
                            (np.sqrt(mdlParams['multiCropEval']) - 1))
                    # mdlParams['imSizes'][u,ind,0] = curr_im_size[0]

                    ind += 1
        # Sanity checks
        # print("Positions",mdlParams['cropPositions'])
        # Test image sizes
        height = mdlParams['input_size'][0]
        width = mdlParams['input_size'][1]
        for u in range(len(mdlParams['im_paths'])):
            height_test, width_test = imagesize.get(mdlParams['im_paths'][u])
            if width_test < mdlParams['input_size'][0]:
                height_test = int(mdlParams['input_size'][0] /
                                  float(width_test)) * height_test
                width_test = mdlParams['input_size'][0]
            if height_test < mdlParams['input_size'][0]:
                width_test = int(mdlParams['input_size'][0] /
                                 float(height_test)) * width_test
                height_test = mdlParams['input_size'][0]
            test_im = np.zeros([width_test, height_test])
            for i in range(mdlParams['multiCropEval']):
                im_crop = test_im[
                    np.int32(mdlParams['cropPositions'][u, i, 0] - height /
                             2):np.int32(mdlParams['cropPositions'][u, i, 0] -
                                         height / 2) + height,
                    np.int32(mdlParams['cropPositions'][u, i, 1] - width /
                             2):np.int32(mdlParams['cropPositions'][u, i, 1] -
                                         width / 2) + width]
                if im_crop.shape[0] != mdlParams['input_size'][0]:
                    print("Wrong shape", im_crop.shape[0],
                          mdlParams['im_paths'][u])
                if im_crop.shape[1] != mdlParams['input_size'][1]:
                    print("Wrong shape", im_crop.shape[1],
                          mdlParams['im_paths'][u])
    return mdlParams
Example #12
0
 def check_image(image_path):
     im_shape = imagesize.get(image_path)
     if im_shape[0] != self.__target_image_shape[0] or im_shape[
             1] != self.__target_image_shape[1]:
         return False
     return True
Example #13
0
    x = (box[0] + box[1])/2.0 - 1
    y = (box[2] + box[3])/2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

for infile in infiles:
    if not os.path.exists(indir + 'labels/' + infile):
        os.makedirs(indir + 'labels/' + infile)
    list_file = open(infile + '_train.txt', 'w')
    f = open(indir + 'Annotations/' + infile + '.txt', 'r')
    i = 0
    for line in f:
        i += 1
        if i < 3: continue
        items = line.split()
        imgfile = '%s/JPEGImages/%s/%s' % (indir, infile, items[0])
        list_file.write('%s\n'% imgfile)
        w, h = imagesize.get(imgfile)
        out_file = open('%s/labels/%s/%s.txt' % (indir, infile, items[0][0:-4].replace('/', '_')), 'a')
        b = (float(items[2]), float(items[4]), float(items[3]), float(items[5]))
        bb = convert((w, h), b)
        out_file.write(items[1] + " " + " ".join([str(a) for a in bb]) + '\n')
        out_file.close()
    list_file.close()

Example #14
0
import imagesize
import glob

from tqdm import tqdm

train_images = glob.glob('../../data/jpeg/train/*.jpg')
test_images = glob.glob('../../data/jpeg/test/*.jpg')

train_sizes = []
for imfile in tqdm(train_images, total=len(train_images)):
    train_sizes += [imagesize.get(imfile)]

test_sizes = []
for imfile in tqdm(test_images, total=len(test_images)):
    test_sizes += [imagesize.get(imfile)]
Example #15
0
def load_sem_seg(gt_root, image_root, gt_ext="png", image_ext="jpg"):
    """
    Load semantic segmentation datasets. All files under "gt_root" with "gt_ext" extension are
    treated as ground truth annotations and all files under "image_root" with "image_ext" extension
    as input images. Ground truth and input images are matched using file paths relative to
    "gt_root" and "image_root" respectively without taking into account file extensions.
    This works for COCO as well as some other datasets.

    Args:
        gt_root (str): full path to ground truth semantic segmentation files. Semantic segmentation
            annotations are stored as images with integer values in pixels that represent
            corresponding semantic labels.
        image_root (str): the directory where the input images are.
        gt_ext (str): file extension for ground truth annotations.
        image_ext (str): file extension for input images.

    Returns:
        list[dict]:
            a list of dicts in detectron2 standard format without instance-level
            annotation.

    Notes:
        1. This function does not read the image and ground truth files.
           The results do not have the "image" and "sem_seg" fields.
    """

    # We match input images with ground truth based on their relative filepaths (without file
    # extensions) starting from 'image_root' and 'gt_root' respectively.
    def file2id(folder_path, file_path):
        # extract relative path starting from `folder_path`
        image_id = os.path.normpath(os.path.relpath(file_path, start=folder_path))
        # remove file extension
        image_id = os.path.splitext(image_id)[0]
        return image_id

    input_files = sorted(
        (os.path.join(image_root, f) for f in PathManager.ls(image_root) if f.endswith(image_ext)),
        key=lambda file_path: file2id(image_root, file_path),
    )
    gt_files = sorted(
        (os.path.join(gt_root, f) for f in PathManager.ls(gt_root) if f.endswith(gt_ext)),
        key=lambda file_path: file2id(gt_root, file_path),
    )

    assert len(gt_files) > 0, "No annotations found in {}.".format(gt_root)

    # Use the intersection, so that val2017_100 annotations can run smoothly with val2017 images
    if len(input_files) != len(gt_files):
        logger.warn(
            "Directory {} and {} has {} and {} files, respectively.".format(
                image_root, gt_root, len(input_files), len(gt_files)
            )
        )
        input_basenames = [os.path.basename(f)[: -len(image_ext)] for f in input_files]
        gt_basenames = [os.path.basename(f)[: -len(gt_ext)] for f in gt_files]
        intersect = list(set(input_basenames) & set(gt_basenames))
        # sort, otherwise each worker may obtain a list[dict] in different order
        intersect = sorted(intersect)
        logger.warn("Will use their intersection of {} files.".format(len(intersect)))
        input_files = [os.path.join(image_root, f + image_ext) for f in intersect]
        gt_files = [os.path.join(gt_root, f + gt_ext) for f in intersect]

    logger.info(
        "Loaded {} images with semantic segmentation from {}".format(len(input_files), image_root)
    )

    dataset_dicts = []
    for (img_path, gt_path) in zip(input_files, gt_files):
        local_path = PathManager.get_local_path(gt_path)
        w, h = imagesize.get(local_path)
        record = {}
        record["file_name"] = img_path
        record["sem_seg_file_name"] = gt_path
        record["height"] = h
        record["width"] = w
        dataset_dicts.append(record)

    return dataset_dicts
Example #16
0
def aspectratio(img_file, cl, colors_dict):
    width, height = imagesize.get(img_file)
    ar = width / height  # w/h
    color = colors_dict[cl]
    return (ar, color)
    images = os.listdir(args.image_dir)
    image_ids = [os.path.splitext(x)[0] for x in images]

# prepare per instance information
img_pred_map = defaultdict(list)
for pred in tqdm(predictions, desc='Converting predictions '):
    image_id = pred['image_id']
    cat = pred['category_id']
    
    # Extract height and width
    image_size = image_size_dict.get(image_id, None)
    if image_size is not None:
        image_width, image_height = image_size
    else:
        filename = os.path.join(args.image_dir, image_id + '.jpg')
        image_width, image_height = imagesize.get(filename)
        image_size_dict[image_id] = image_width, image_height

    xmin = pred['bbox'][0] / image_width
    ymin = pred['bbox'][1] / image_height
    xmax = xmin + pred['bbox'][2] / image_width
    ymax = ymin + pred['bbox'][3] / image_height
    
    conf = pred['score']
    
    img_pred_map[image_id].append(f"{cat} {conf:.4f} {xmin:.4f} {ymin:.4f} {xmax:.4f} {ymax:.4f}")
    
    
# collect into per image strings
converted_predictions = [['ImageId', 'PredictionString']]
for image_id in image_ids:
    def __call__(self, *args, **kwargs):
        dataset = TextOnlyCocoAnnotation()

        begin, end = (100, 328 + 1) if self.is_train else (1, 233 + 1)
        gt_format = 'gt_{}.txt' if self.is_train else 'gt_img_{}.txt'
        img_format = '{}.jpg' if self.is_train else 'img_{}.jpg'
        char_gt_format = '{}_GT.txt'

        for i in tqdm(range(begin, end)):
            image_path = os.path.join(self.images_folder, img_format.format(i))
            image_size = imagesize.get(image_path)
            annotation_path = os.path.join(self.annotations_folder,
                                           gt_format.format(i))

            with open(annotation_path, encoding='utf-8-sig') as read_file:
                if self.characters_annotations_folder:
                    char_annotation_path = os.path.join(
                        self.characters_annotations_folder,
                        char_gt_format.format(i))
                    with open(char_annotation_path) as f:
                        content = f.readlines()
                        content = ''.join(content)
                        content = content.split('\n\n')
                        characters = [
                            line.split('\n') for line in content
                            if not line.strip().startswith('#')
                        ]
                for j, line in enumerate(
                    [line.strip() for line in read_file.readlines()]):
                    obj = self.parse_line(line)

                    if self.characters_annotations_folder:

                        obj['attributes']['chars'] = []
                        for chars in characters[j]:
                            if not chars:
                                continue
                            chars = chars.split(' ')
                            bbox = [int(x) for x in chars[5:9]]
                            bbox[2] -= bbox[0]
                            bbox[3] -= bbox[1]
                            char = ' '.join(chars[9:])
                            if len(char) == 3 and char[0] == char[-1] == '"':
                                char = char[1]
                            if char == ' ':
                                continue
                            assert len(char) == 1, f'char = "{char}"'
                            obj['attributes']['chars'].append({
                                'bbox':
                                bbox,
                                'segmentation':
                                box2poly(bbox),
                                'char':
                                char
                            })
                        united_chars = ''.join(
                            [x['char'] for x in obj['attributes']['chars']])
                        if united_chars != obj['attributes']['transcription']:
                            logging.warning(
                                f"Transcription of {obj['attributes']['transcription']} in "
                                f"{annotation_path} "
                                f"has been changed to {united_chars}."
                                f"It is known error in original annotation.")
                            obj['attributes']['transcription'] = united_chars

                    dataset.add_bbox(image_path, image_size, obj)

        return dataset
Example #19
0
        "div", class_="Tags Tags_type_simple Tags_view_buttons")
    if search_page_dim_div:
        links = search_page_dim_div.find_all("a")
        dimensions = links[0].getText().split("×")
        pixels = int(dimensions[0]) * int(dimensions[1])
        return (img_search_url, pixels, dimensions)
    return (0, 0, 0)


IMAGE_PATH = "./../../import/images"
file_names = listdir(IMAGE_PATH)

for file_name in tqdm(file_names):
    io_buf = None
    img_path = f'{IMAGE_PATH}/{file_name}'
    width, height = imagesize.get(img_path)

    if width * height > RESIZE_THRESHOLD:
        mime_type = magic.from_buffer(open(img_path, "rb").read(2048),
                                      mime=True)
        if mime_type == "image/jpeg":
            ext = ".jpg"
        if mime_type == "image/png":
            ext = ".png"
        img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
        img = resize_img_to_threshold(img, height, width)
        is_success, buffer = cv2.imencode(ext, img)
        if is_success:
            io_buf = io.BytesIO(buffer)

    try:
Example #20
0
            if 'left.json' not in annoname:
                continue

            annopath = opj(FAT, 'mixed', dirname, annoname)
            fullpath = annopath.replace('.json', '.jpg')
            voc_bbox = fullpath.replace('.jpg', '.txt')
            voc_annos = []
            fat_lists.append(fullpath.split('ycb/')[-1] + '\n')

            if os.path.exists(voc_bbox):
                continue

            with open(annopath) as f:
                data = json.load(f)

            W, H = imagesize.get(fullpath)
            for obj in data['objects']:
                y1, x1 = obj['bounding_box']['top_left']
                y2, x2 = obj['bounding_box']['bottom_right']
                if x1 > x2 or y1 > y2 or x1 < 0 or x2 > W or y1 < 0 or y2 > H:
                    continue
                xc = (x1 + x2) / 2
                yc = (y1 + y2) / 2
                w = x2 - x1
                h = y2 - y1
                label = NAMES.index(obj['class'][:-4])
                voc_annos.append('%d %f %f %f %f\n' %
                                 (label, xc / W, yc / H, w / W, h / H))

            with open(voc_bbox, 'w') as f:
                f.writelines(voc_annos)
Example #21
0
def json_to_viz(parsed_json, debug=False):
    # graph dimensions (adjust these values)
    is_top_down = True  # grow graph downward
    g_left = 0  # left boundary of page
    # g_bot = 0                 # bottom boundary of page
    g_horizontal_gap = 1
    g_vertical_gap = 1

    g_txt_width = 5
    g_txt_height = 0.5
    g_txt_border = 0.125  # padding around each side

    g_img_width = 4
    g_img_height = 3

    # g_inner_txt_boundary_color = 'invis'  # required for html
    g_inner_txt_boundary_color = 'gray'
    g_outer_txt_boundary_color = 'dimgray'
    g_txt_font_color = 'midnightblue'

    g_bb_color = 'limegreen'

    g_edge_to_bb_color = 'limegreen'
    g_edge_to_step_color = 'darkorange'

    # resulting attributes
    g_item_height = g_vertical_gap + max(g_txt_height + 2 * g_txt_border,
                                         g_img_height)

    g_txt_center_x = g_left + g_img_width + g_horizontal_gap + g_txt_border + g_txt_width / 2
    g_img_center_x = g_left + g_img_width / 2

    g_outer_txt_width = g_txt_width + 2 * g_txt_border
    g_outer_txt_height = g_txt_height + 2 * g_txt_border

    # create graph
    g = Digraph('text_core', engine='neato')

    # set graph and node attributes
    g.graph_attr['splines'] = 'curved'

    g.node_attr['shape'] = 'rect'
    g.node_attr['fixedsize'] = 'true'
    g.node_attr['fontsize'] = '12'
    g.node_attr['fontcolor'] = str(g_txt_font_color)
    g.node_attr['imagescale'] = 'true'
    orientation_top = ':n' if is_top_down else ':s'
    orientation_bot = ':s' if is_top_down else ':n'

    step_count = len(parsed_json)

    # draw nodes for images and texts
    for i in range(step_count):
        # item_id is used for vertical positioning
        item_id = (step_count - 1 - i) if is_top_down else i

        # formulate nohtml for text node
        text = "<f0>" + insert_newlines(parsed_json[i]['pred'])
        entity_count = len(parsed_json[i]['entities'])
        for j in range(entity_count):
            text += "|<f" + str(j + 1) + ">"
            text += insert_newlines(parsed_json[i]['entities'][j])

        # # formulate html for text node
        # text = '''<<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="1"><TR><TD PORT="f0">''' + insert_newlines(parsed_json[i]['pred']) + '''</TD>'''
        # entity_count = len(parsed_json[i]['entities'])
        # for j in range(entity_count):
        #     text += '''<TD BGCOLOR="#EEFFDD" PORT="f''' + str(j+1) + '''">''' + insert_newlines(parsed_json[i]['entities'][j]) + '''</TD>'''
        # text += '''</TR></TABLE>>'''

        # draw text node
        # g.node("text_"+str(i),text,pos=str(g_txt_center_x)+","+str(i*g_item_height)+"!",width=str(g_txt_width),height=str(g_txt_height),color=str(g_inner_txt_boundary_color))

        g.node("text_shell_" + str(i),
               '',
               pos=str(g_txt_center_x) + "," + str(item_id * g_item_height) +
               "!",
               width=str(g_outer_txt_width),
               height=str(g_outer_txt_height),
               color=str(g_outer_txt_boundary_color))
        g.node("text_" + str(i),
               nohtml(text),
               shape='Mrecord',
               pos=str(g_txt_center_x) + "," + str(item_id * g_item_height) +
               "!",
               width=str(g_txt_width),
               height=str(g_txt_height),
               color=str(g_inner_txt_boundary_color))  # required with nohtml

        # draw image node
        g.node("img_" + str(i),
               pos=str(g_img_center_x) + "," + str(item_id * g_item_height) +
               "!",
               width=str(g_img_width),
               height=str(g_img_height),
               label='',
               image=parsed_json[i]['img'],
               color='invis')

        # draw text-image pairing edge (needed for connected graph to manage overlapping)
        g.edge("text_" + str(i), "img_" + str(i), color='invis')

    # draw edges from entity to action step
    for i in range(step_count):
        entity_count = len(parsed_json[i]['ea'])
        for j in range(entity_count):
            action_id_ref = parsed_json[i]['ea'][j]
            if (action_id_ref != -1):
                g.edge("text_" + str(i) + ":f" + str(j + 1) + orientation_top,
                       "text_shell_" + str(action_id_ref) + orientation_bot,
                       color=str(g_edge_to_step_color))

    # draw bounding boxes
    for i in range(step_count):
        # item_id is used for vertical positioning
        item_id = (step_count - 1 - i) if is_top_down else i

        bb_count = len(parsed_json[i]['bboxes'])
        width, height = imagesize.get(parsed_json[i]['img'])
        w_ratio = g_img_width / width
        h_ratio = g_img_height / height
        min_ratio = min(w_ratio, h_ratio)

        is_width_bounded = (w_ratio < h_ratio)  # bounded due to width
        w = h_ratio * width if not is_width_bounded else g_img_width
        h = w_ratio * height if is_width_bounded else g_img_height

        for j in range(bb_count):
            bb = parsed_json[i]['bboxes'][j]
            g.node("bb_" + str(i) + "_" + str(j),
                   label='',
                   pos=get_scaled_center(g_img_center_x,
                                         item_id * g_item_height, w, h,
                                         min_ratio, bb['left'], bb['bot'],
                                         bb['right'], bb['top']),
                   width=str((bb['right'] - bb['left']) * min_ratio),
                   height=str((bb['top'] - bb['bot']) * min_ratio),
                   color=str(g_bb_color))

    # draw edges from entity to bounding boxes
    for i in range(step_count):
        # item_id is used for vertical positioning
        item_id = (step_count - 1 - i) if is_top_down else i

        edge_orientation = orientation_top if (
            i < step_count / 2) else orientation_bot
        entity_count = len(parsed_json[i]['eb'])
        for j in range(entity_count):
            bb_id_ref = parsed_json[i]['eb'][j]
            if (bb_id_ref != -1):
                g.edge("text_" + str(i) + ":f" + str(j + 1) + edge_orientation,
                       "bb_" + str(i) + "_" + str(bb_id_ref),
                       color=str(g_edge_to_bb_color))
                # g.edge("text_"+str(i)+":f"+str(j+1)+":n","bb_"+str(i)+"_"+str(bb_id_ref),color=str(g_edge_to_bb_color),n='1',pos="e,152.13,411.67 91.566,463.4 108.12,449.26 127.94,432.34 144.37,418.3")

    if debug:
        print(g.source)

    g.render('visualizer-output/graph')
Example #22
0
        labels = [results[-1][i] for i in range(n)]
        return bboxes, labels


# annotation_id = 0
# offset = 0

annotation_id = 73257
offset = 33402
images = []
annotations = []
for i in range(d['name'].shape[0]):
    print(i)
    file_name = read_name(d['name'][i][:][0])
    image_id = offset + int(file_name[:-4])
    width, height = imagesize.get(root / file_name)
    images.append({
        'file_name': file_name,
        'height': height,
        'width': width,
        'id': image_id,
    })

    bboxes, labels = read_bboxes(d['bbox'][i][:][0])
    nb = len(labels)
    for j in range(nb):
        bbox = bboxes[j]
        annotation_id += 1
        annotations.append({
            'iscrowd': 0,
            'area': bbox[2] * bbox[3],
Example #23
0
 def _test_from_file(self, filename: str, width: int, height: int):
     w, h = imagesize.get(os.path.join(image_dir, filename))
     self.assertEqual(w, width)
     self.assertEqual(h, height)
Example #24
0
def scaling_factor_v2(img_path):
    x, y = imagesize.get(img_path)
    return x / IMAGE_X, y / IMAGE_Y
Example #25
0
def get_image_size(fname):
    width, height = imagesize.get(fname)
    return width, height
Example #26
0
                   "bbox": [1, 1, 1, 1],
                   "category_id": 1,
                   "id": 1}

    polygon_files = []
    if args.image_dir:
        polygon_files = glob.glob(os.path.join(args.image_dir, "*.xy"))
    for i, polygon_file in enumerate(polygon_files):
        image_file = os.path.splitext(polygon_file)[0]
        if not os.path.isfile(image_file):
            print("not image file", image_file)
            continue

        segmentation = [
            [float(coord.strip()) for coord in open(polygon_file).readline().split(" ")[1:] if coord and coord.strip()]]
        width, height = imagesize.get(image_file)
        rles = mask.frPyObjects(segmentation, height, width)
        rle = mask.merge(rles)
        bbox = mask.toBbox(rle)
        area = mask.area(rle)

        image_fn = os.path.basename(image_file)

        if image_fn not in image_id_map:
            image_id_map[image_fn] = len(image_id_map) + 1

        coco_output["images"].append({
            "license": 1,
            "url": "",
            "file_name": image_fn,
            "height": height,
Example #27
0
    def generate_labels_helen(cls, run_config):
        CACHE_FILENAME = os.path.join(run_config.data.data_dir,
                                      'helen_labels_cache.csv')
        annotation_fnames = glob.glob(
            os.path.join(cls.HELEN_PATH, 'annotation', '*.txt'))
        images = glob.glob(os.path.join(cls.HELEN_PATH, 'helen_?', '*.jpg'))
        if os.path.exists(CACHE_FILENAME):
            warnings.warn("USING face_hash cached labels")
            df = pd.read_csv(CACHE_FILENAME)
        else:
            #Sorted:
            pass
            annotations_dict = {}
            annotations = []
            #contains  <image_name_without_extension:full_path_to_image>
            images_dict = {
                os.path.split(filename)[1].split('.')[0]: filename
                for filename in images
            }
            for fname_annotation in annotation_fnames:
                annot_df = pd.read_csv(fname_annotation)
                key = annot_df.columns[0]
                img_size = imagesize.get(images_dict[key])
                SCALE_SIZE = cls.MAX_SCALE / img_size[
                    0], cls.MAX_SCALE / img_size[1]
                x, y = annot_df.index.values, annot_df[
                    annot_df.columns[0]].values
                x *= SCALE_SIZE[0]
                y *= SCALE_SIZE[1]
                coords = np.empty(len(x) * 2)
                for i in range(len(x)):
                    coords[2 * i] = x[i]
                    coords[2 * i + 1] = y[i]
                coords_repr = ":".join("{:.2f}".format(coord)
                                       for coord in coords)
                annotations.append({
                    'id': key,
                    'filename': images_dict[key],
                    'class': coords_repr,
                })
            df = pd.DataFrame(annotations)
            df.to_csv(CACHE_FILENAME, index=False)

        #
        nb_items = len(df)
        train_ratio = 1.0 - (run_config.split.validation_ratio +
                             run_config.split.test_ratio)
        nb_train = int(nb_items * train_ratio)
        nb_validation = int(nb_items * run_config.split.validation_ratio)
        nb_test = nb_items - (nb_train + nb_validation)
        #pylint: disable=E0632
        train_indexes, validation_indexes, test_indexes = np.split(
            df.index, [nb_train, nb_train + nb_validation])
        train_df = df.loc[train_indexes]
        validation_df = df.loc[validation_indexes]
        test_df = df.loc[test_indexes]

        train_df.to_csv(run_config.data.train_file,
                        index=False,
                        float_format="%.3f")
        validation_df.to_csv(run_config.data.validation_file,
                             index=False,
                             float_format="%.3f")
        if run_config.data.test_file:
            test_df.to_csv(run_config.data.test_file,
                           index=False,
                           float_format="%.3f")
Example #28
0
 def test_bigendian_tiff(self):
     width, height = imagesize.get(os.path.join(imagedir, "test.tiff"))
     self.assertEqual(width, 802)
     self.assertEqual(height, 670)
    #accquire image file list
    imgList = sorted(glob.glob(image_file_path))

    #print(imgList)

    # make the folder to store the results
    parent_path = os.path.abspath(os.path.join(file_path, os.pardir))
    mkpath = parent_path + '/' + str('analysis_result')
    mkdir(mkpath)
    save_path_result = mkpath + '/'

    n_samples = len(imgList)

    if n_samples > 0:

        width, height = imagesize.get(imgList[0])
        #print(width, height)

    else:

        print("Empty image folder, abort!")
        sys.exit(0)

    ##################################################################
    #Loading image dask array
    #sample = imageio.imread(imgList[0])

    lazy_arrays = [dask.delayed(load_image)(fn) for fn in imgList]

    lazy_arrays = [
        da.from_delayed(x, shape=(height, width), dtype=np.float64)
Example #30
0
    ### load testing data
    all_test_imgs = os.listdir(args.test_folder)
    tmp_json_dict = {"annotation":[],
                     "images":[],
                     "categories":[{'id': 0, 'supercategory': 'Button', 'name': 'Button'}, {'id': 1, 'supercategory': 'CheckBox', 'name': 'CheckBox'}, {'id': 2, 'supercategory': 'Chronometer', 'name': 'Chronometer'}, {'id': 3, 'supercategory': 'EditText', 'name': 'EditText'}, {'id': 4, 'supercategory': 'ImageButton', 'name': 'ImageButton'}, {'id': 5, 'supercategory': 'ImageView', 'name': 'ImageView'}, {'id': 6, 'supercategory': 'ProgressBar', 'name': 'ProgressBar'}, {'id': 7, 'supercategory': 'RadioButton', 'name': 'RadioButton'}, {'id': 8, 'supercategory': 'RatingBar', 'name': 'RatingBar'}, {'id': 9, 'supercategory': 'SeekBar', 'name': 'SeekBar'}, {'id': 10, 'supercategory': 'Spinner', 'name': 'Spinner'}, {'id': 11, 'supercategory': 'Switch', 'name': 'Switch'}, {'id': 12, 'supercategory': 'ToggleButton', 'name': 'ToggleButton'}, {'id': 13, 'supercategory': 'VideoView', 'name': 'VideoView'}]
                    }


    curr_id = 0
    imgname2id = {}
    for imgpath in all_test_imgs:
        if imgpath.split(".")[-1].lower() in ["jpg", "png", "jpeg"]:
            if "detected" in imgpath:
                continue
            full_path = os.path.join(args.test_folder, imgpath)
            w, h = imagesize.get(full_path)
            curr_item = {"id":curr_id,
                         "file_name":full_path,
                         "height":h, 
                         "width":w}
            imgname2id[full_path] = curr_id
            tmp_json_dict["images"].append(curr_item)
            curr_id += 1

    with open(os.path.join(args.test_folder, "instances_tmp.json"), "w") as f:
        json.dump(tmp_json_dict, f)

    print("loading all datasets...")
    dataset = system_configs.dataset
    print("split: {}".format(split))
    testing_db = RICO_DEMO(configs["db"], split, args.test_folder)
Example #31
0
import imagesize
import sys
import glob
from os import listdir
from os.path import isfile, join
from pathlib import Path

maxheight = 0
maxwidth = 0

if len(sys.argv) != 2:
    print("Usage: python checkmaxsize.py path_extension")
    exit()

pictures = sorted(Path(Path.cwd() / sys.argv[1]).glob('*'))

for f in pictures:
    width, height = imagesize.get(f)
    print("w: " + str(width) + " h: " + str(height))
    if width > maxwidth:
        maxwidth = width
    if height > maxheight:
        maxheight = width
print("maxw: " + str(maxwidth) + " maxh: " + str(maxheight))
Example #32
0
def bench_purepython():
    imagesize.get(imagepath)
           mdlParams['cropFlipping'])
     mdlParams['multiCropEval'] = mdlParams['numCropPositions'] * len(
         mdlParams['cropScales']) * mdlParams['cropFlipping']
     mdlParams['offset_crop'] = 0.2
 elif 'order' in crop_strategy:
     mdlParams['orderedCrop'] = True
     mdlParams['var_im_size'] = True
     if mdlParams.get('var_im_size', True):
         # Crop positions, always choose multiCropEval to be 4, 9, 16, 25, etc.
         mdlParams['cropPositions'] = np.zeros([
             len(mdlParams['im_paths']), mdlParams['multiCropEval'], 2
         ],
                                               dtype=np.int64)
         # mdlParams['imSizes'] = np.zeros([len(mdlParams['im_paths']),mdlParams['multiCropEval'],2],dtype=np.int64)
         for u in range(len(mdlParams['im_paths'])):
             height, width = imagesize.get(mdlParams['im_paths'][u])
             if width < mdlParams['input_size'][0]:
                 height = int(
                     mdlParams['input_size'][0] / float(width)) * height
                 width = mdlParams['input_size'][0]
             if height < mdlParams['input_size'][0]:
                 width = int(
                     mdlParams['input_size'][0] / float(height)) * width
                 height = mdlParams['input_size'][0]
             if mdlParams.get('resize_large_ones') is not None:
                 if width == mdlParams[
                         'large_size'] and height == mdlParams[
                             'large_size']:
                     width, height = (mdlParams['resize_large_ones'],
                                      mdlParams['resize_large_ones'])
             ind = 0
Example #34
0
 def test_load_png(self):
     width, height = imagesize.get(os.path.join(imagedir, "test.png"))
     self.assertEqual(width, 802)
     self.assertEqual(height, 670)
Example #35
0
def get_image_size(filepath: Union[str, Path]) -> Tuple[int, int]:
    """
    Returns image (width, height)
    """
    logger.warning("get_image_size is deprecated, use get_img_size instead")
    return imagesize.get(filepath)
Example #36
0
# merge with global image gallery locat
path = join("./src/photos/", image_path)

# extract image files
print('Collecting files...')
files = [f for f in listdir(path) if isfile(join(path, f))]
files = [f for f in files if f[f.rfind('.')+1:].lower() in extensions ]

# rename image files
print('Renaming files...')
new_files = []
for f in files:
    if f[f.rfind('-')+1:f.rfind('.')] != 'thumbnail':
        # Something breaks in this step
        newf = f[:f.rfind('-')] + "-%sx%s" % imagesize.get(join(path, f)) + f[f.rfind('.'):]

        rename(join(path, f),join(path, newf))
    else:
        newf = f
    new_files.append(newf)

files = new_files

# helper objects to store gallery data
new_gallery = {}
thumbs = {}

# group gallery data
print('Grouping files...')
for f in files:
Example #37
0
def get_img_size(filepath: Union[str, Path]) -> ImgSize:
    """
    Returns image (width, height)
    """
    return ImgSize(*imagesize.get(filepath))