Exemplo n.º 1
0
def main():
    max_width()
    config, regions, mmc, sdk_url = sidebar()
    my_file = st.file_uploader('Pick an image',
                               type=['jpg', 'png', 'jpeg', 'gif', 'bmp'])
    if not my_file:
        return
    image = Image.open(my_file).convert('RGB')
    res = recognition(my_file, regions, sdk_url, mmc, config)
    res = copy.deepcopy(res)
    vehicles = []
    for result in res['results']:
        if 'vehicle' in result:
            details = []
            model_make = result.get('model_make')
            if model_make:
                details.append(
                    '{make} {model} {score:.2f}'.format(**model_make[0]))
            color = result.get('color')
            if color:
                details.append('{color} {score:.2f}'.format(**color[0]))
            result['vehicle']['details'] = ', '.join(details)
            vehicles.append(result['vehicle'])
    overlay = st.checkbox('Show Overlay', value=True)
    if overlay:
        draw_bb(
            image, vehicles, None, lambda vehicle: '%s (%s) [%s]' %
            (vehicle['type'], vehicle['score'], vehicle['details']))
        draw_bb(
            image, res['results'], None, lambda result: '%s [%s] (%s, %s)' %
            (result['plate'].upper(), result['region']['code'], result[
                'score'], result['dscore']))
    st.image(image, width=image.width)
    res
def process_image(path, args, i):
    config = dict(threshold_d=args.detection_threshold,
                  threshold_o=args.ocr_threshold,
                  mode='redaction')

    # Predictions
    source_im = Image.open(path)
    if source_im.mode != 'RGB':
        source_im = source_im.convert('RGB')
    images = [((0, 0), source_im)]  # Entire image
    # Top left and top right crops
    if args.split_image:
        y = 0
        win_size = .55
        width, height = source_im.width * win_size, source_im.height * win_size
        for x in [0, int((1 - win_size) * source_im.width)]:
            images.append(((x, y), source_im.crop(
                (x, y, x + width, y + height))))

    # Inference
    results = []
    for (x, y), im in images:
        im_bytes = io.BytesIO()
        im.save(im_bytes, 'JPEG', quality=95)
        im_bytes.seek(0)
        im_results = recognition_api(im_bytes,
                                     args.regions,
                                     args.api_key,
                                     args.sdk_url,
                                     config=config)
        results.append(dict(prediction=im_results, x=x, y=y))
    results = post_processing(merge_results(results))
    results['filename'] = Path(path).name

    # Set bounding box padding
    for item in results['results']:
        # Decrease padding size for large bounding boxes
        b = item['box']
        width, height = b['xmax'] - b['xmin'], b['ymax'] - b['ymin']
        padding_x = int(
            max(0, width * (.3 * math.exp(-10 * width / source_im.width))))
        padding_y = int(
            max(0, height * (.3 * math.exp(-10 * height / source_im.height))))
        b['xmin'] = b['xmin'] - padding_x
        b['ymin'] = b['ymin'] - padding_y
        b['xmax'] = b['xmax'] + padding_x
        b['ymax'] = b['ymax'] + padding_y

    if args.show_boxes or args.save_blurred:
        im = blur(source_im, 5, results)
        if args.show_boxes:
            im.show()
        if args.save_blurred:
            filename = Path(path)
            im.save(filename.parent / ('%s_blurred%s' %
                                       (filename.stem, filename.suffix)))
    if 0:
        draw_bb(source_im, results['results']).show()
    return results
Exemplo n.º 3
0
def process_image(path, args):
    options = CAMERA_OPTIONS[args.camera]
    config = dict(threshold_d=.2, threshold_o=.3, mode='redaction')

    # Pre process image
    source_im = Image.open(path)
    rotated = source_im.rotate(options['rotation'], expand=True)
    top = rotated.height * options['crop_top']
    bottom = rotated.height * options['crop_bottom']
    api_im = rotated.crop((0, top, rotated.width, bottom))

    # Predictions
    images = [((0, 0), api_im)]  # Entire image
    # Top left and top right crops
    y = 0
    win_size = .55
    width, height = api_im.width * win_size, api_im.height * win_size
    for x in [0, (1 - win_size) * api_im.width]:
        images.append(((x, y), api_im.crop((x, y, x + width, y + height))))

    # Inference
    results = []
    for (x, y), im in images:
        im_bytes = io.BytesIO()
        im.save(im_bytes, 'JPEG', quality=95)
        im_bytes.seek(0)
        im_results = recognition_api(im_bytes,
                                     args.regions,
                                     args.api_key,
                                     args.sdk_url,
                                     config=config)
        results.append(dict(prediction=im_results, x=x, y=y))
    results = merge_results(results)

    # Update bounding boxes
    for lp in results['results']:
        box = lp['box']
        box['ymin'] += top
        box['ymax'] += top
        # Rotate
        lp['box'] = rotate_bb(box, options['rotation'], rotated.size)

    if 0:
        draw_bb(source_im, results['results']).show()
    return results
Exemplo n.º 4
0
def process_image(path, args, i):
    config = dict(threshold_d=args.detection_threshold,
                  threshold_o=args.ocr_threshold,
                  mode='redaction')

    # Predictions
    source_im = Image.open(path)
    images = [((0, 0), source_im)]  # Entire image
    # Top left and top right crops
    if args.split_image:
        y = 0
        win_size = .55
        width, height = source_im.width * win_size, source_im.height * win_size
        for x in [0, int((1 - win_size) * source_im.width)]:
            images.append(((x, y), source_im.crop(
                (x, y, x + width, y + height))))

    # Inference
    results = []
    for (x, y), im in images:
        im_bytes = io.BytesIO()
        if im.mode == 'RGBA':
            im = im.convert('RGB')
        im.save(im_bytes, 'JPEG', quality=95)
        im_bytes.seek(0)
        im_results = recognition_api(im_bytes,
                                     args.regions,
                                     args.api_key,
                                     args.sdk_url,
                                     config=config)
        results.append(dict(prediction=im_results, x=x, y=y))
    results = merge_results(results)

    if args.show_boxes:
        blur(source_im, 5, results).show()
    if 0:
        draw_bb(source_im, results['results']).show()
    return results