Esempio n. 1
0
def choose_best(new_database):

	#### New database should be checked on all the algorithms and the best case efficiency should be taken for furthur uses

		
	#print efficiency_PCA

	efficiency_PCA=PCA_main.main_pca(new_database)
	efficiency_LPP=0
	efficiency_DCT=0
	efficiency_histL1=histogram_main_L1.histmain(new_database)
	efficiency_histL2=histogram_main_L2.histmain(new_database)
	
	efficiency=[efficiency_PCA,efficiency_histL1,efficiency_histL2,efficiency_DCT,efficiency_LPP,]
	max_efficiency=max(efficiency)
	max_efficiency_index=efficiency.index(max_efficiency)
	
	#print "Printing PCA and HBS efficiency"
	#print efficiency_PCA
	#print efficiency_hist

	
	print "printing max efficiency "
	print max_efficiency
	
	"""if(max_efficiency_index==0) :
		print "PCA"
	if(max_efficiency_index==1) :
		print "DCT"
	if(max_efficiency_index==2) :
		print "LPP" 
"""
	return max_efficiency_index
Esempio n. 2
0
def decide_algo(input_str):

    # print input_str

    wrapper_test_image_names = lslR.get_files(input_str)

    # Uncomment following 2-lines to print all the image names

    # print "image names"
    # print wrapper_test_image_names

    wrapper_test_image_names.sort()  # Sorting the image names
    no_of_images = len(wrapper_test_image_names)  # Getting the length of number of images
    randomly_selected_image = random.random() * no_of_images  # getting the random value in between 0 and no_of_images
    randomly_selected_image = int(round(randomly_selected_image))  # converting to integer coz index should be a integer

    # Uncomment following 2 - lines to print the index randomly selected image

    # print "Index of randomly selected image = %d" %(randomly_selected_image)

    # print "number of images"
    # print len(wrapper_test_image_names)

    # wrapper_test_image=Image.open(wrapper_test_image_names[randomly_selected_image])

    # test_image_stat=ImageStat.Stat(wrapper_test_image) # To convert to Imagestat object whcih gives the stat properties of the image

    ######## We will be doing the decision based on the 16-metrices.
    ###### Only when the test image (image set) satisfies all the 16 metrices it means that the given test database is actually one of the trained dataset
    #### If test image fails to meet all the 16-metric criteria, then the wrapper depending on how close the new data set is decides which algorithm to be chosen
    ## Over time it actually appends the values and maintains the updated metrices of the trained dataset of the new data set

    ####### We actually test only one image of the entire given test data set ( using the old taught which says one rice grain is often enough to say whether the rice is boiled #### or not, similar we regressively test only one image of the dataset ( given by the user ) ) and choose the algorithm.

    ####### Image.Stat properties

    flag = (
        0
    )  # Initializing the flag, flag=0 means database not identified, assuming db not identified in the beginning, will be set once db is identified

    metric = getmetrics.return_metrics(
        wrapper_test_image_names[randomly_selected_image]
    )  # Calling the return_metrics of getmetrics which returns 16 metrics as list

    # 			break   # once if database is identified then we can come out of loop  """

    ##### We need to get the names of the databases which are previously trained, Rewriting this part with more efficient way

    fp_for_trained_db = open(
        "mapping_dataset_algo", "r"
    )  # Opening trained_databases in "r" mode to read the list of trained db's
    fp_for_trained_db.seek(
        0
    )  # Not necessary,  but still on safer hand its given so fp_for_trained_db points to beginning of the file
    trained_datasets = pickle.load(fp_for_trained_db)  # loading the trained data lists from pickle to trained_datasets

    for i in range(len(trained_datasets)):  # Have to be checked on all the previously trained datasets
        face_names = lslR.get_files(trained_datasets[i][0])  # Getting the absolute path names of the database
        face_names.sort()  # Sorting the absolute path names
        no_of_images_in_train = len(face_names)
        if no_of_images_in_train < randomly_selected_image:
            continue
        trained_metric = getmetrics.return_metrics(
            face_names[randomly_selected_image]
        )  # Calling the return_metrics of getmetrics which returns 16 metrics as list

        if metric.__eq__(trained_metric):  # Comparing if all the 16 metrics of the image is matching

            print "Data base identified"
            print "Identified database is"
            print trained_datasets[i][0]  # printing the identified database name
            flag = 1  # Setting the flag if database is identified
            identified_algo_name = trained_datasets[i][1]
            if identified_algo_name == "PCA":
                PCA_main.main_pca(trained_datasets[i][0])
            if identified_algo_name == "HBSL1":
                histogram_main_L1.histmain(trained_datasets[i][0])
            if identified_algo_name == "HBSL2":
                histogram_main_L2.histmain(trained_datasets[i][0])
            if identified_algo_name == "DCT":
                print "DCT is to be called"
            if identified_algo_name == "LPP":
                print "LPP is to be called"

            break  # once if database is identified then we can come out of loop

    if flag == 0:  # means database not identified
        ##### Need to extract the trained dataset path
        trained_data_path = trained_datasets[0][0]  # Taking any database path to extract trained database path
        rindex_ossep = trained_data_path.rindex(os.sep)  # Getting the path of the trained databases directory
        trained_data_path = trained_data_path[0:rindex_ossep]  # Getting the path of the trained databases directory
        src_path = input_str  # Getting source directory of new database
        dest_path = trained_data_path + os.sep  # Creating destination directory for taking back up of new database
        dest_path = dest_path + get_db_name(
            input_str
        )  # Creating destination directory for taking back up of new database

        print "Data base not identifed"
        print "Adding database to our trained database sets"
        shutil.copytree(
            src_path, dest_path
        )  # creating a copy of the entire database, dynamically updating new database to trained set
        # add_database.add_db(dest_path) # Adding the new database (which is presently copied to dest_path) to the previously trained list.
        print "Database added"
        print dest_path
        index_best_algo_chosen = compare_with_all.choose_best_algo(dest_path, trained_datasets)
        best_algo_chosen = algorithms[index_best_algo_chosen]

        print "Algorithm chosen is " + best_algo_chosen + " bacause it has the more efficiency then other algorithms on this database"

        new_list_to_append = []
        new_list_to_append.append(dest_path)
        new_list_to_append.append(best_algo_chosen)

        trained_datasets.append(new_list_to_append)

        fp_to_update_trained_db = open(
            "mapping_dataset_algo", "w+"
        )  # Opening trained_databases in "r" mode to read the list of trained db's
        fp_to_update_trained_db.seek(
            0
        )  # Not necessary,  but still on safer hand its given so fp_for_trained_db points to beginning of the file
        pickle.dump(trained_datasets, fp_to_update_trained_db)  # updating the mapping_dataset file
        fp_to_update_trained_db.close()
        if best_algo_chosen == "PCA":
            PCA_main.main_pca(dest_path)
        if best_algo_chosen == "HBSL1":
            histogram_main_L1.histmain(dest_path)
        if best_algo_chosen == "HBSL2":
            histogram_main_L2.histmain(dest_path)
        if best_algo_chosen == "DCT":
            print "DCT is to be called"
        if best_algo_chosen == "LPP":
            print "LPP is to be called"