Пример #1
0
def db2patches(output_path, train_label=None, sample_limit=None):
    # All human annotations in DB converted to a training set
    n_angles = 8
    angles = np.linspace(0, 360, num=n_angles, endpoint=False)
    extract_size = (256, 256)  # wdt, hgt
    annotated_samples = db.get_human_annotated_samples(train_label=train_label)
    n_annotated_samples = len(annotated_samples)
    if sample_limit is not None and n_annotated_samples > sample_limit:
        print 'Reducing from %d to sample limit %d...' % (n_annotated_samples,
                                                          sample_limit)
        annotated_samples = np.random.choice(annotated_samples,
                                             sample_limit,
                                             replace=False)
    print 'Extracting patches from %d images to %s...' % (
        len(annotated_samples), output_path)
    n = 0
    for s in tqdm(annotated_samples):
        img_filename = os.path.join(config.get_server_image_path(),
                                    s['filename'])
        img_name = s['filename'].replace('.', '_')
        for annotation in db.get_human_annotations(s['_id']):
            allpos = [dbpos2extpos(p) for p in annotation['positions']]
            n += extract_positions(img_filename, allpos, output_path, img_name,
                                   angles, extract_size, s['_id'])
    print '%d patches extracted.' % n
Пример #2
0
def annotate(sid):
    # Query info from DB
    sample_id = ObjectId(sid)
    sample_entry = db.get_sample_by_id(sample_id)
    dataset_id = sample_entry['dataset_id']
    name = sample_entry['name']
    readonly = db.is_readonly_dataset_id(dataset_id)
    sample_index, sample_count, prev_sample_id, next_sample_id = db.get_sample_index(
        dataset_id, sample_id)
    # Unknown entry?
    if sample_entry is None:
        return error_redirect('Unknown entry: "%s".' % str(sample_id))
    # Determine data
    image_filename = 'images/' + sample_entry['filename']
    info_string = ''
    # Differential? Then load machine annotations, unless there are human annotations already
    annotations = db.get_human_annotations(sample_id)
    is_differential = request.args.get('differential')
    if is_differential and not annotations:
        annotations = db.get_machine_annotations(sample_id)
    if len(annotations):
        annotations_json = annotations[0]['positions']
        # Machine annotations are in a different format
        if len(annotations_json) and isinstance(annotations_json[0], list):
            annotations_json = [{
                'x': a[0],
                'y': a[1]
            } for a in annotations_json]
    else:
        annotations_json = []
    return render_template("annotate.html",
                           id=sid,
                           image_filename=image_filename,
                           info_string=info_string,
                           error=pop_last_error(),
                           height=sample_entry['size'][1],
                           width=sample_entry['size'][0],
                           annotations=annotations_json,
                           margin=96,
                           dataset_id=str(dataset_id),
                           sample_index=sample_index,
                           sample_count=sample_count,
                           prev_id=str(prev_sample_id),
                           next_id=str(next_sample_id),
                           is_differential=is_differential,
                           readonly=readonly,
                           name=name)
Пример #3
0
def export_positions_generator(samples, yield_header=True, human=False):
    if yield_header:
        yield ','.join(
            ('sample_name', 'sample_filename', 'center_x_px', 'center_y_px',
             'center_x_relative', 'center_y_relatrive')) + '\n'
    for s in samples:
        sample_name = s.get('name')
        sample_filename = s.get('filename')
        if human:
            annotations = db.get_human_annotations(s['_id'])
        else:
            annotations = db.get_machine_annotations(s['_id'])
        if not annotations:
            continue
        size = s['size']
        for pos in annotations[0]['positions']:
            if human:
                pos = (pos['x'], pos['y'])
            rel_pos = ['%.3f' % (float(p) / s) for p, s in zip(pos, size)]
            yield ','.join(
                map(str, (sample_name, sample_filename, pos[0], pos[1],
                          rel_pos[0], rel_pos[1]))) + '\n'
Пример #4
0
def show_info(sid):
    # Query info from DB
    sample_id = ObjectId(sid)
    sample_entry = db.get_sample_by_id(sample_id)
    sample_index, sample_count, prev_sample_id, next_sample_id = db.get_sample_index(sample_entry['dataset_id'],
                                                                                     sample_id)
    # Unknown entry?
    if sample_entry is None:
        return error_redirect('Unknown entry: "%s".' % str(sample_id))
    # Allow fixing of machine annotation? Only if not human-annotated and there is a machine annotation with stomata
    can_annotate_diff = (not sample_entry.get('annotated')) and (sample_entry.get('machine_position_count'))
    # Determine data
    refresh = False
    filename = sample_entry['filename']
    name = sample_entry['name']
    dataset_id = sample_entry['dataset_id']
    readonly = db.is_readonly_dataset_id(dataset_id)
    info_table = []
    for info_name, info_key in info_table_entries:
        info_value = sample_entry.get(info_key)
        if info_value is not None:
            info_table.append((info_name, info_value))
    annotations = []
    if sample_entry['error']:
        info_string = Markup('Error: <pre>' + sample_entry['error_string'] + '</pre>')
    elif not sample_entry['processed']:
        info_string = 'Processing...'
        refresh = True
    else:
        info_string = 'Processed.'
    annotation_data = db.get_machine_annotations(sample_id) + db.get_human_annotations(sample_id)
    has_image_output = False
    for ad in annotation_data:
        model_id = ad.get('model_id')
        an = {'info_string': ''}
        if model_id is not None:
            model_data = db.get_model_by_id(model_id)
            if model_data is None:
                an['info_string'] += '??? Unknown model ???'
                an['title'] = 'Unknown'
            else:
                an['title'] = model_data['name']
                an['info_string'] += 'Margin: %d' % (ad.get('margin') or model_data['margin'])
            an['image_filename'] = 'heatmaps/' + ad['heatmap_image_filename']
            has_image_output = True
        else:
            an['title'] = 'By user %s' % ad.get('user_name')
        if 'scale' in ad:
            an['info_string'] += ' - Scale: %.1f' % ad['scale']
        positions = ad['positions']
        if positions is not None:
            an['info_string'] += ' - %d stomata' % len(positions)
        annotations += [an]
    annotations = list(reversed(annotations))
    if not has_image_output:
        annotations += [{'image_filename': 'images/' + filename, 'title': 'Input image', 'info_string': ''}]
    return render_template("info.html", id=sid, name=name, dataset_id=str(dataset_id), info_string=info_string,
                           annotations=annotations, error=pop_last_error(), refresh=refresh,
                           sample_index=sample_index, sample_count=sample_count, prev_id=str(prev_sample_id),
                           next_id=str(next_sample_id), can_annotate_diff=can_annotate_diff, readonly=readonly,
                           info_table=info_table)