def trueskill_layer(model_path, images_dir, csv_path):
    """
    Compute a trueskill rating for each image of the directory from 100 comparisons predictions.
    Save the results in a csv file.

    :param model_path: path of the trained comparisons model
    :type model_path: str
    :param images_dir: images directory path
    :type images_dir: str
    :param csv_path: path of csv where results are stored
    :type csv_path: str
    """

    # Variable initialization
    print("Loading model ...")
    model = load_model(model_path)
    images_path = glob.glob(images_dir + "/*jpg")
    pictures = []
    images = []

    # Build list of Image objects
    print("Done\nLoading images...")
    pbar = ProgressBar()
    for img_path in pbar(images_path):
        img = Image(img_path, build_array=True)
        img.get_info_from_filename()
        images.append(img)
        pictures.append(img.preprocess_image(224))
    pictures = np.array(pictures)

    pbar2 = ProgressBar()
    print("Done\nComputing trueskill rating for each image ...")
    for i in pbar2(range(len(images))):
        # Create 100 contenders and predict a comparison for each
        contenders = [rd.randint(0, len(images) - 1) for _ in range(100)]
        predictions = model.predict([
            np.array([pictures[i] for _ in range(100)]),
            np.array([pictures[contenders[k]] for k in range(100)])
        ])

        # Update image trueskill rating for each comparison
        for j in range(predictions.shape[0]):
            if predictions[j][0] > predictions[j][1]:
                images[i].trueskill_rating, images[
                    contenders[j]].trueskill_rating = tr.rate_1vs1(
                        images[i].trueskill_rating,
                        images[contenders[j]].trueskill_rating)
            else:
                images[contenders[j]].trueskill_rating, images[
                    i].trueskill_rating = tr.rate_1vs1(
                        images[contenders[j]].trueskill_rating,
                        images[i].trueskill_rating)

    # Save results in csv
    print("Done\nSaving results in csv ...")
    trueskill_csv(images, csv_path)
    print("Done")
Example #2
0
    def complete_images_table(self, table):
        """
        Complete the images table of the database with the relevant values of the directory images.

        :param table: SQL table name
        :type table: str
        """
        # Variable initialization
        cmp_double = 0
        cmp_img = 0

        # Connection to database
        conn, cursor = connection_database(self.db_name, self.host, self.user,
                                           self.password, self.local,
                                           self.ssl_ca)

        # Get images paths
        images = glob.glob(self.img_dir + "/*jpg")
        pbar = progressbar.ProgressBar()
        for image_path in pbar(images):
            image_name = os.path.basename(image_path)
            img = Ci(image_path)

            # Get image information
            lon, lat, k, h, datetime, global_key = img.get_info_from_filename()
            date_sql = mapillary_to_sql_date(datetime)

            columns = ("image_key", "latitude", "longitude", "datetime",
                       "filename")
            values = (global_key, lat, lon, date_sql, image_name)

            try:
                insert_into(cursor, table, columns, values)
                cmp_img += 1
            except mysql.connector.errors.IntegrityError:
                # print("Image already in table", image_name)
                cmp_double += 1
                continue

        # Validate queries
        commit_query(conn)

        # End connection to database
        end_connection(conn, cursor)

        # Display information
        print("Number of images inserted into database: {} ".format(cmp_img))
        print("Number of images already inserted in database : {} ".format(
            cmp_double))
def ranking_layer(model_path, images_dir, csv_path):
    """
    Predict the score of all the images of the directory with the ranking model. Save the results in a csv file.

    :param model_path: path of the trained ranking model
    :type model_path: str
    :param images_dir: images directory path
    :type images_dir: str
    :param csv_path: path of csv where results are stored
    :type csv_path: str
    """

    # Variable initialization
    images_path = glob.glob(images_dir + "/*jpg")
    images = []

    # Load scoring model
    print("Loading model ...")
    model = load_model(model_path)
    
    # Build list of Image objects
    print("Done\nMake score prediction for each image ...")
    pbar = ProgressBar()
    for img_path in pbar(images_path):
        img = Image(img_path, build_array=True)
        img.get_info_from_filename()
        images.append(img)
        img.ranking_score = model.predict(np.array([img.preprocess_image(224)]))[0][0]

    # Save results in csv
    print("Done\nSaving results in csv ...")
    ranking_csv(images, csv_path)
#  Data
DATA_FOLDER_AUG = abspath("Comparisons_npy/augmented_1")
DATA_FOLDER = abspath("Comparisons_npy")
COMP_CSV = join(DATA_FOLDER, "duels_date.csv")

# Results
TRAIN_RESULTS_KFOLD = abspath("Training_results/Kfold")
TRAIN_RESULTS_SIMPLE = abspath("Training_results/Simple")

# ----------------------------------------------------------------------------------------------------------------------
# Run functions
# ----------------------------------------------------------------------------------------------------------------------

# ------------------------------------- Test Image class ---------------------------------------------------------------
image_snow = Image(IMG_SNOW, build_array=True, build_green_segmentation=True, green_seg_model=SEG_GREEN_MODEL,
                   build_snow_segmentation=True, snow_seg_model=SEG_SNOW_MODEL)
print(image_snow)
image_snow.is_blurry()
image_snow.is_overexposed()
image_snow.is_under_green_ratio(0.83)
image_snow.is_wrong_exposed()
image_snow.is_snowy()
print(image_snow)

# ------------------------------------- Test Panorama class ------------------------------------------------------------
pano = Panorama(PANO, build_array=True)
pano.create_4_images(SAVE_DIR)
pano.plot_created_images()

# ------------------------------------- Test Download class ------------------------------------------------------------
d = Download(PHOTO_DIR, LAYER_PATH)