Esempio n. 1
0
def main():

    # %% init
    nDigits = 3
    i_start = 1
    i_end = 2
    N = i_end - i_start

    save = True

    drive = "backup"  #  "UBUNTU 16_0" #
    pwd_root = os.path.join(os.sep, "media", "taeke", drive, "thesis_data",
                            "detect_truss")

    dataset = 'lidl'

    pwd_data = os.path.join(pwd_root, "data", dataset)
    pwd_results = os.path.join(pwd_root, "results", dataset, "06_peduncle")
    make_dirs(pwd_results)

    brightness = 0.9

    for count, i_tomato in enumerate(range(i_start, i_end)):  # 10, 11
        print("Analyzing image %d out of %d" % (i_tomato, N))

        tomato_name = str(i_tomato).zfill(nDigits)
        file_name = tomato_name + ".png"

        img_rgb = load_rgb(file_name, pwd_data, horizontal=True)

        image = ProcessImage(use_truss=True,
                             name=tomato_name,
                             pwd=pwd_results,
                             save=False)

        image.add_image(img_rgb)

        image.color_space()
        image.segment_image()
        image.filter_image()
        image.rotate_cut_img()
        image.detect_tomatoes()
        image.detect_peduncle()
Esempio n. 2
0
def main():
    i_start = 1
    i_end = 85
    save_results = False
    N = i_end - i_start

    # pwd_root = os.path.join(os.sep, 'home', 'taeke', 'Documents', "images")
    pwd_root = os.path.join(os.sep, "media", "taeke", "backup", "thesis_data",
                            "detect_truss")
    pwd_lbl = os.path.join(pwd_root, "data", "lidl")
    pwd_res = os.path.join(pwd_root, "results", "lidl", 'json')

    pwd_final_result = os.path.join(pwd_root, "results", 'final')
    if save_results:
        pwd_store = pwd_final_result
        make_dirs(pwd_store)
    else:
        pwd_store = None

    tomato_error_all = {}
    junction_error_all = {}

    use_mm = True
    dist_thresh_tomato = 15  # [mm] maximum distance for which the predictions may still be labeled as a true positive
    dist_thresh_peduncle = 10  # [mm] maximum distance for which the predictions may still be labeled as a true positive

    for count, i_truss in enumerate(range(i_start, i_end)):
        print("Analyzing image %d out of %d" % (count + 1, N))
        truss_name = str(i_truss).zfill(3)

        file_lbl = os.path.join(pwd_lbl, truss_name + '.json')
        file_inf = os.path.join(pwd_lbl, truss_name + '_info.json')
        file_res = os.path.join(pwd_res, truss_name + '.json')

        # load data
        img_rgb = load_rgb(truss_name + '_rgb.png', pwd_lbl, horizontal=False)

        if not os.path.isfile(file_lbl):
            print('Labels do not exist for image: ' + truss_name +
                  ' skipping this file!')
            continue

        if not os.path.isfile(file_inf):
            print('Info does not exist for image: ' + truss_name +
                  ' skipping this file!')
            continue

        with open(file_lbl, "r") as read_file:
            data_lbl = json.load(read_file)

        tomato_lbl = {'radii': [], 'centers': []}
        peduncle_lbl = {'junctions': [], 'ends': []}
        shapes = data_lbl['shapes']

        for shape in shapes:
            label = shape['label']
            shape_type = shape['shape_type']
            if label == 'tomato':
                if shape_type != 'circle':
                    print("I do not know what to do with ", label,
                          " of shape type ", shape_type)

                else:
                    points = shape['points']
                    center = points[0]
                    radius = euclidean_distance(points[0], points[1])

                    tomato_lbl['centers'].append(center)
                    tomato_lbl['radii'].append(radius)

            elif label == 'junction':
                if shape_type != 'point':
                    print("I do not know what to do with ", label,
                          " of shape type ", shape_type)

                else:
                    point = shape['points'][0]
                    peduncle_lbl['junctions'].append(point)

            elif label == 'end_point' or label == 'end':
                if shape_type != 'point':
                    print("I do not know what to do with ", label,
                          " of shape type ", shape_type)

                point = shape['points'][0]
                peduncle_lbl['ends'].append(point)

            else:
                print "i do not know what to do with ", label

        if use_mm:
            with open(file_inf, "r") as read_file:
                data_inf = json.load(read_file)
            unit = '[mm]'
            px_per_mm = data_inf['px_per_mm']

        else:
            unit = '[px]'
            px_per_mm = 1

        # compute com
        tomato_lbl['com'] = compute_com(tomato_lbl['centers'],
                                        tomato_lbl['radii'])

        if save_results:
            plot_features(img_rgb,
                          tomato=tomato_lbl,
                          pwd=pwd_store,
                          file_name=truss_name + '_tom_lbl')
            plot_features(img_rgb,
                          peduncle=peduncle_lbl,
                          pwd=pwd_store,
                          file_name=truss_name + '_pend_lbl')
            plot_features(img_rgb,
                          tomato=tomato_lbl,
                          peduncle=peduncle_lbl,
                          pwd=pwd_store,
                          file_name=truss_name + '_lbl')

        with open(file_res, "r") as read_file:
            data_results = json.load(read_file)

        img_res = img_rgb.copy()
        tomato_res = data_results['tomato']
        peduncle_res = data_results['peduncle']
        grasp_res = data_results['grasp_location']

        i_true_pos_res, i_true_pos_lbl, i_false_pos, i_false_neg = index_true_positives(
            tomato_lbl['centers'], tomato_res['centers'], dist_thresh_tomato,
            px_per_mm)

        tomato_pred = {}
        tomato_pred['true_pos'] = {}
        tomato_pred['false_pos'] = {}
        tomato_pred['com'] = tomato_res['com']

        tomato_actual = {}
        tomato_actual['true_pos'] = {}
        tomato_actual['false_neg'] = {}
        tomato_actual['com'] = tomato_lbl['com']

        for key in ['centers', 'radii']:
            tomato_pred['true_pos'][key] = np.array(
                tomato_res[key])[i_true_pos_res].tolist()
            tomato_pred['false_pos'][key] = np.array(
                tomato_res[key])[i_false_pos].tolist()

            tomato_actual['true_pos'][key] = np.array(
                tomato_lbl[key])[i_true_pos_lbl].tolist()
            tomato_actual['false_neg'][key] = np.array(
                tomato_lbl[key])[i_false_neg].tolist()

        n_true_pos = len(i_true_pos_res)
        n_false_pos = len(i_false_pos)
        n_labeled_pos = len(tomato_lbl['centers'])
        n_predict_pos = len(tomato_pred['true_pos']['centers'])

        com_error = euclidean_distance(tomato_lbl['com'][0],
                                       tomato_res['com']) / px_per_mm
        centers_error = euclidean_distances(
            tomato_actual['true_pos']['centers'],
            tomato_pred['true_pos']['centers'],
            factor=1 / px_per_mm)
        radii_error = [
            abs(r1 - r2) / px_per_mm
            for r1, r2 in zip(tomato_actual['true_pos']['radii'],
                              tomato_pred['true_pos']['radii'])
        ]

        # compute error
        tomato_error = {
            'radii': radii_error,
            'centers': centers_error,
            'com': com_error,
            'n_true_pos': n_true_pos,
            'n_false_pos': n_false_pos,
            'n_labeled_pos': n_labeled_pos,
            'n_predict_pos': n_predict_pos
        }

        # plot
        if save_results:
            ratio = float(img_res.shape[1]) / float(img_res.shape[0])
            plot_features_result(img_res,
                                 tomato_pred=tomato_pred,
                                 name=truss_name + '_temp')
            plot_error(
                tomato_pred=tomato_pred,  # centers, com,
                tomato_act=tomato_actual,
                error=tomato_error,  # center radii and com
                pwd=pwd_store,
                name=truss_name + '_tom_error',
                use_mm=use_mm)

        # store
        tomato_error_all[truss_name] = tomato_error

        i_true_pos_res, i_true_pos_lbl, i_false_pos, i_false_neg = index_true_positives(
            peduncle_lbl['junctions'], peduncle_res['junctions'],
            dist_thresh_peduncle, px_per_mm)

        junction_pred = {}
        junction_pred['true_pos'] = {}
        junction_pred['false_pos'] = {}

        junction_actual = {}
        junction_actual['true_pos'] = {}
        junction_actual['false_neg'] = {}

        for key in ['centers']:
            junction_pred['true_pos'][key] = np.array(
                peduncle_res['junctions'])[i_true_pos_res].tolist()
            junction_pred['false_pos'][key] = np.array(
                peduncle_res['junctions'])[i_false_pos].tolist()

            junction_actual['true_pos'][key] = np.array(
                peduncle_lbl['junctions'])[i_true_pos_lbl].tolist()
            junction_actual['false_neg'][key] = np.array(
                peduncle_lbl['junctions'])[i_false_neg].tolist()

        n_true_pos = len(i_true_pos_res)
        n_false_pos = len(i_false_pos)
        n_labeled_pos = len(peduncle_lbl['junctions'])
        n_predict_pos = len(peduncle_res['junctions'])

        center_error = euclidean_distances(
            junction_actual['true_pos']['centers'],
            junction_pred['true_pos']['centers'],
            factor=1.0 / px_per_mm)

        junctions_error = {
            'centers': center_error,
            'true_pos': n_true_pos,
            'false_pos': n_false_pos,
            'labeled_pos': n_labeled_pos,
            'predict_pos': n_predict_pos
        }

        # plot
        if save_results:
            plot_features_result(img_res,
                                 peduncle=junction_pred)  # grasp = grasp_ress
            plot_error(
                tomato_pred=junction_pred,  # centers, com,
                tomato_act=junction_actual,
                error=junctions_error,
                pwd=pwd_store,
                name=truss_name + '_pend_error',
                use_mm=use_mm)

        # store
        junction_error_all[truss_name] = junctions_error

    tomato_error_centers = []
    tomato_error_radii = []
    tomato_error_com = []
    n_true_pos = 0
    n_false_pos = 0
    n_labeled_pos = 0
    n_predict_pos = 0

    # not in order be default!
    all_ids = junction_error_all.keys()
    all_ids.sort()
    for id in all_ids:
        tomato_error_centers.extend(tomato_error_all[id]['centers'])
        tomato_error_radii.extend(tomato_error_all[id]['radii'])
        tomato_error_com.append(tomato_error_all[id]['com'])
        n_true_pos += tomato_error_all[id]['n_true_pos']
        n_false_pos += tomato_error_all[id]['n_false_pos']
        n_labeled_pos += tomato_error_all[id]['n_labeled_pos']
        n_predict_pos += tomato_error_all[id]['n_predict_pos']

        if tomato_error_all[id]['n_labeled_pos'] - tomato_error_all[id][
                'n_true_pos'] > 0:
            print id

    error_tomato_center_mean = np.mean(tomato_error_centers)
    error_tomato_center_std = np.std(tomato_error_centers)

    error_tomato_radius_mean = np.mean(tomato_error_radii)
    error_tomato_radius_std = np.std(tomato_error_radii)

    error_com_center_mean = np.mean(tomato_error_com)
    error_com_center_std = np.std(tomato_error_com)

    true_pos_perc = int(round(float(n_true_pos) / float(n_labeled_pos) * 100))
    false_pos_perc = int(round(
        float(n_false_pos) / float(n_predict_pos) * 100))

    print 'Tomato center error: {mean:.2f} {u:s} +- {std:.2f} {u:s} (n = {n:d})'.format(
        mean=error_tomato_center_mean,
        std=error_tomato_center_std,
        n=n_predict_pos,
        u=unit)
    print 'Tomato radius error: {mean:.2f} {u:s} +- {std:.2f} {u:s} (n = {n:d})'.format(
        mean=error_tomato_radius_mean,
        std=error_tomato_radius_std,
        n=n_predict_pos,
        u=unit)
    print 'com error: {mean:.2f} {u:s} +- {std:.2f} {u:s} (n = {n:d})'.format(
        mean=error_com_center_mean,
        std=error_com_center_std,
        n=n_predict_pos,
        u=unit)

    print 'True positive: {true_pos:d} out of {n_tomatoes:d} ({true_pos_perc:d}%)'.format(
        true_pos=n_true_pos,
        n_tomatoes=n_labeled_pos,
        true_pos_perc=true_pos_perc)
    print 'False positive: {false_pos:d} out of {n_tomatoes:d} ({false_pos_perc:d}%)'.format(
        false_pos=n_false_pos,
        n_tomatoes=n_predict_pos,
        false_pos_perc=false_pos_perc)

    cases = ['all', 'center', 'off-center']

    # Junctions https://stackoverflow.com/questions/22307628/python-how-to-extend-the-content-of-a-list-store-in-a-dict
    junction_error_centers = {k: [] for k in cases}
    n_true_pos = dict.fromkeys(cases, 0)
    n_false_pos = dict.fromkeys(cases, 0)
    n_labeled_pos = dict.fromkeys(cases, 0)
    n_predict_pos = dict.fromkeys(cases, 0)

    for id in all_ids:

        id_type = ((int(id) - 1) % 7 + 1)
        if id_type in [1, 2, 3]:
            case = 'center'
        elif id_type in [4, 5, 6, 7]:
            case = 'off-center'
def main():
    i_start = 1  # tomato file to load
    i_end = 85
    N = i_end - i_start

    extension = ".png"
    dataset = "lidl"  # "failures" #

    labels = ['tomato', 'peduncle']

    pwd_current = os.path.dirname(__file__)
    drive = "backup"  # "UBUNTU 16_0"  #
    pwd_root = os.path.join(os.sep, "media", "taeke", drive, "thesis_data",
                            "detect_truss")
    pwd_data = os.path.join(pwd_root, "data", dataset)
    pwd_results = os.path.join(pwd_root, "labels", dataset)

    make_dirs(pwd_results)
    process_image = ProcessImage()
    epsilon_factor = 0.0005  # contour simplification factor, lower is simpler!

    for count, i_tomato in enumerate(range(i_start, i_end)):
        print("Analyzing image ID %d (%d/%d)" % (i_tomato, count + 1, N))

        tomato_ID = str(i_tomato).zfill(3)
        tomato_name = tomato_ID
        file_name = tomato_name + "_rgb" + extension

        img_rgb = load_rgb(file_name, pwd_data, horizontal=True)
        process_image.add_image(img_rgb, name=tomato_name)
        process_image.color_space()
        process_image.segment_image()
        process_image.filter_image()

        tomato_label = 'TOMATO'
        stem_label = 'STEM'
        background_label = 'BACKGROUND'

        segments = {}
        segments[
            tomato_label] = process_image.tomato.data  # first segment is red
        segments[
            stem_label] = process_image.peduncle.data  # second segment is green

        width = img_rgb.shape[1]
        height = img_rgb.shape[0]
        label_me_dict = initialize_label_me_dict(tomato_name + extension,
                                                 width=width,
                                                 height=height)

        for label in segments:
            contours, _ = cv2.findContours(segments[label], cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_NONE)[-2:]

            for contour in contours:
                epsilon = epsilon_factor * cv2.arcLength(contour, True)
                polygon = cv2.approxPolyDP(contour, epsilon, True)
                polygon = polygon[:, 0].tolist()

                # Polygon must have more than two points
                if len(polygon) > 2:
                    label_me_dict["shapes"].append(
                        get_label_me_shape(polygon, label))

        with open(os.path.join(pwd_results, tomato_ID + '.json'), 'w') as fp:
            json.dump(label_me_dict, fp)

        count = count + 1
        print("completed image %d out of %d" % (count, N))
Esempio n. 4
0
    extension = ".png"
    dataset = "lidl"  # "failures" #
    save = True

    pwd_current = os.path.dirname(__file__)
    drive = "backup"  # "UBUNTU 16_0"  #
    pwd_root = os.path.join(os.sep, "media", "taeke", drive, "thesis_data",
                            "detect_truss")
    pwd_data = os.path.join(pwd_root, "data", dataset)
    pwd_results = os.path.join(pwd_root, "results", dataset)

    make_dirs(pwd_results)
    process_image = ProcessImage(use_truss=True, pwd=pwd_results, save=save)
    radii = [None]  #  [1.5]  # 0.5, 1.0, 1.5, 2.0, 3.0

    for radius in radii:
        for count, i_tomato in enumerate(range(i_start, i_end)):
            print("Analyzing image ID %d (%d/%d)" % (i_tomato, count + 1, N))

            tomato_ID = str(i_tomato).zfill(3)
            tomato_name = tomato_ID
            file_name = tomato_name + extension

            img_rgb = load_rgb(file_name, pwd_data, horizontal=True)
            process_image.add_image(img_rgb, name=tomato_name)
            process_image.color_space(compute_a=True)
            process_image.segment_image(radius=radius)
            process_image.filter_image(folder_name=str(radius))
            count = count + 1
            print("completed image %d out of %d" % (count, N))
Esempio n. 5
0
def main():
    i_start = 1
    i_end = 2
    N = i_end - i_start

    save = False
    drive = "backup"  # "UBUNTU 16_0"  #

    pwd_root = os.path.join(os.sep, "media" ,"taeke", "backup", "thesis_data", "detect_truss")

    dataset = "lidl"  # "failures" #
    pwd_data = os.path.join(pwd_root, "data", dataset)
    pwd_results = os.path.join(pwd_root, "results", dataset)
    pwd_json = os.path.join(pwd_results, 'json')

    make_dirs(pwd_results)
    make_dirs(pwd_json)

    process_image = ProcessImage(use_truss=True,
                                 pwd=pwd_results,
                                 save=save)

    for count, i_tomato in enumerate(range(i_start, i_end)):
        print("Analyzing image ID %d (%d/%d)" % (i_tomato, count + 1, N))

        tomato_name = str(i_tomato).zfill(3)
        file_name = tomato_name + "_rgb" + ".png"

        rgb_data = load_rgb(file_name, pwd=pwd_data, horizontal=True)
        px_per_mm = load_px_per_mm(pwd_data, tomato_name)
        process_image.add_image(rgb_data, px_per_mm=px_per_mm, name=tomato_name)

        success = process_image.process_image()
        process_image.get_truss_visualization(local=True, save=True)
        process_image.get_truss_visualization(local=False, save=True)

        json_data = process_image.get_object_features()

        pwd_json_file = os.path.join(pwd_json, tomato_name + '.json')
        with open(pwd_json_file, "w") as write_file:
            json.dump(json_data, write_file)

    if True:  # save is not True:
        plot_timer(Timer.timers['main'].copy(), threshold=0.02, pwd=pwd_results, name='main', title='Processing time',
                   startangle=-20)

    total_key = "process image"
    time_tot_mean = np.mean(Timer.timers[total_key]) / 1000
    time_tot_std = np.std(Timer.timers[total_key]) / 1000

    time_ms = Timer.timers[total_key]
    time_s = [x / 1000 for x in time_ms]

    time_min = min(time_s)
    time_max = max(time_s)

    print 'Processing time: {mean:.2f}s +- {std:.2f}s (n = {n:d})'.format(mean=time_tot_mean, std=time_tot_std, n=N)
    print 'Processing time lies between {time_min:.2f}s and {time_max:.2f}s (n = {n:d})'.format(time_min=time_min,
                                                                                                time_max=time_max, n=N)

    width = 0.5
    fig, ax = plt.subplots()

    ax.p1 = plt.bar(np.arange(i_start, i_end), time_s, width)

    plt.ylabel('time [s]')
    plt.xlabel('image ID')
    plt.title('Processing time per image')
    plt.rcParams["savefig.format"] = 'pdf'

    fig.show()
    fig.savefig(os.path.join(pwd_results, 'time_bar'), dpi=300)  # , bbox_inches='tight', pad_inches=0)