Example #1
0
def main():
    """Main function for the task 4"""
    feature_extraction_model = get_input_feature_extractor_model()
    dimension_reduction_model = get_input_dimensionality_reduction_model()
    k_value = get_input_k()
    label = get_input_image_label()
    folder = get_input_folder()
    image_name = get_input_image(folder)
    m_value = get_input_m()

    if dimension_reduction_model != "NMF":
        dist_func = "euclidean"
    elif feature_extraction_model in ["CM", "LBP"]:
        dist_func = "nvsc1"
    else:
        dist_func = "euclidean"
        # dist_func = "cosine"
        # dist_func = "chebyshev"
        # dist_func = "manhattan"
        # dist_func = "chi_square"
        # dist_func = "euclidean"

    print(global_constants.LINE_SEPARATOR)
    print("User Inputs summary")
    print(global_constants.LINE_SEPARATOR)
    print("Feature Extraction Model: {}\nDimensionality Reduction Model: {}\nk-value: {}\nLabel: {}\nFolder: {}\n"
          "Image: {}\nm-value: {}".format(feature_extraction_model, dimension_reduction_model, k_value, label, folder,
                                          image_name, m_value))
    print(global_constants.LINE_SEPARATOR)

    # Saves the returned model
    filename = "{0}_{1}_{2}_{3}".format(feature_extraction_model, dimension_reduction_model, label.replace(" ", ''),
                                        str(k_value))
    model = model_interact.load_model(filename=filename)
    if not model:
        print("Please run Task 3 for {}, {}, {} and {}".format(feature_extraction_model, dimension_reduction_model,
                                                               label, k_value))
        sys.exit(1)

    # Compute the reduced dimensions for the new query image and find m similar images
    dim_reduction = DimensionReduction(feature_extraction_model, dimension_reduction_model, k_value, label)
    result = dim_reduction.find_m_similar_images(model, m_value, folder, image_name, dist_func)
    print(global_constants.LINE_SEPARATOR)
    print("Similar Images")
    print(global_constants.LINE_SEPARATOR)
    for rec in result:
        print(rec)
    print(global_constants.LINE_SEPARATOR)

    title = {
        "Feature Extraction": feature_extraction_model,
        "Dimension Reduction": dimension_reduction_model,
        "k": k_value,
        "Label": label,
        "Distance": dist_func
             }
    show_images(os.path.abspath(os.path.join(folder, image_name)), result, title)
Example #2
0
def run_task4(feature_extraction_model, dimension_reduction_model, folder,
              image_name, dist_func, label, k_value, m_value):
    """Main function for the Task4"""

    # Saves the returned model
    filename = "{0}_{1}_{2}_{3}".format(feature_extraction_model,
                                        dimension_reduction_model,
                                        label.replace(" ", ''), str(k_value))
    model = model_interact.load_model(filename=filename)

    # Compute the reduced dimensions for the new query image and find m similar images
    dim_reduction = DimensionReduction(feature_extraction_model,
                                       dimension_reduction_model, k_value,
                                       label)
    obj_feature = dim_reduction.get_object_feature_matrix()
    result = dim_reduction.find_m_similar_images(model, m_value, folder,
                                                 image_name, dist_func)
    return result