def test_recognition_model_squeezenet(): try: keras.backend.clear_session() except: None predictor = ImagePrediction() predictor.setModelTypeAsSqueezeNet() predictor.setModelPath( os.path.join(main_folder, "data-models", "squeezenet_weights_tf_dim_ordering_tf_kernels.h5")) predictor.loadModel() images_to_image_array() result_array = predictor.predictMultipleImages( sent_images_array=all_images_array) assert isinstance(result_array, list) for result in result_array: assert "predictions" in result assert "percentage_probabilities" in result assert isinstance(result["predictions"], list) assert isinstance(result["percentage_probabilities"], list) assert isinstance(result["predictions"][0], str) assert isinstance(result["percentage_probabilities"][0], float)
def multiple_predict(threshold): execution_path = os.getcwd() multiple_prediction = ImagePrediction() multiple_prediction.setModelTypeAsInceptionV3() multiple_prediction.setModelPath( os.path.join(execution_path, "inception_v3_weights_tf_dim_ordering_tf_kernels.h5")) multiple_prediction.loadModel() all_images_array = [] all_files = os.listdir(execution_path + "/adian") for each_file in all_files: if (each_file.endswith(".jpg") or each_file.endswith(".png")): all_images_array.append("adian/" + each_file) results_array = multiple_prediction.predictMultipleImages( all_images_array, result_count_per_image=1) objects = [] for each_result in results_array: predictions, percentage_probabilities = each_result[ "predictions"], each_result["percentage_probabilities"] for index in range(len(predictions)): print(predictions[index], " : ", percentage_probabilities[index]) prob = percentage_probabilities[index] if prob > threshold: objects.append(predictions[index]) #print("-----------------------") # Remove duplicates #objects = list(dict.fromkeys(objects)) objects = list(set(objects)) supported_list = [ 'banana', 'orange', 'mixing_bowl', 'soup_bowl', 'washbowl', 'washbasin', 'handbasin', 'lavabo', 'wash-hand basin', 'custard apple', 'pomegranate', 'beer_bottle', 'pop_bottle', 'soda_bottle', 'water_bottle', 'wine_bottle', 'sundial', 'bathtub', 'mortar', 'orange' ] objects = [i for i in objects if i in supported_list] objects = [ i if i not in ['pomegranate', 'custard apple', 'sundial'] else 'apple' for i in objects ] objects = [i if i not in ['lemon'] else 'orange' for i in objects] objects = [ i if i not in ['pop_bottle', 'soda_bottle', 'wine_bottle'] else 'water_bottle' for i in objects ] objects = [ i if i not in [ 'soup_bowl', 'washbowl', 'washbasin', 'handbasin', 'bathtub', 'mortar' ] else 'mixing_bowl' for i in objects ] objects = list(set(objects)) return objects
def test_recognition_model_densenet(): predictor = ImagePrediction() predictor.setModelTypeAsDenseNet() predictor.setModelPath( os.path.join(main_folder, "data-models", "DenseNet-BC-121-32.h5")) predictor.loadModel() images_to_image_array() result_array = predictor.predictMultipleImages( sent_images_array=all_images_array) assert isinstance(result_array, list) for result in result_array: assert "predictions" in result assert "percentage_probabilities" in result assert isinstance(result["predictions"], list) assert isinstance(result["percentage_probabilities"], list) assert isinstance(result["predictions"][0], str) assert isinstance(result["percentage_probabilities"][0], float)
def test_recognition_model_inceptionv3(): predictor = ImagePrediction() predictor.setModelTypeAsInceptionV3() predictor.setModelPath( os.path.join(main_folder, "data-models", "inception_v3_weights_tf_dim_ordering_tf_kernels.h5")) predictor.loadModel() images_to_image_array() result_array = predictor.predictMultipleImages( sent_images_array=all_images_array) assert isinstance(result_array, list) for result in result_array: assert "predictions" in result assert "percentage_probabilities" in result assert isinstance(result["predictions"], list) assert isinstance(result["percentage_probabilities"], list) assert isinstance(result["predictions"][0], str) assert isinstance(result["percentage_probabilities"][0], float)
def main(path_model, path_dir): multiple_prediction = ImagePrediction() multiple_prediction.setModelTypeAsResNet() multiple_prediction.setModelPath(path_model) multiple_prediction.loadModel() all_images_array = [] all_files = os.listdir(path_dir) for each_file in all_files: if each_file.endswith(".jpg" or each_file.endswith(".png")): all_images_array.append(each_file) results_array = multiple_prediction.predictMultipleImages( all_images_array, result_count_per_image=5) for each_result in results_array: for idx in range(len(each_result["predictions"])): print(each_result["predictions"][idx], " : ", each_result["percentage_probabilities"][idx]) print("-----------------------")
from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() multiple_prediction = ImagePrediction() multiple_prediction.setModelTypeAsResNet() multiple_prediction.setModelPath( os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) multiple_prediction.loadModel() all_images_array = [] all_files = os.listdir(execution_path) for each_file in all_files: if (each_file.endswith(".jpg") or each_file.endswith(".png")): all_images_array.append(each_file) results_array = multiple_prediction.predictMultipleImages( all_images_array, result_count_per_image=4) for each_result in results_array: predictions, percentage_probabilities = each_result[ "predictions"], each_result["percentage_probabilities"] for index in range(len(predictions)): print(predictions[index], " : ", percentage_probabilities[index]) print("-----------------------")
#Multiple image prediction # from imageai.Prediction import ImagePrediction import os working_path = os.getcwd() multiple_prediction = ImagePrediction() multiple_prediction.setModelTypeResNet() multiple_prediction.setModelPath(os.path.join(working_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) multiple_prediction.loadModel(prediction_speed="fast") all_images = [] all_files = os.listdir(working_path) for file in all_files: if(file.endswith(".jpg") or file.endswith(".png")): all_images.append(file) final_array = multiple_prediction.predictMultipleImages(all_images, result_count_per_image = 5) #Display the output for each_result in final_array: pred , percent = each_result["pred"],each_result["percent"] for i in range(len(pred)): print(pred[i]," : ", percent[i])
#Initializxe a text file to write to def write_to_txt(line): with open('results.txt', mode='a') as output: output.write(line) for model_abs_path in models_abs_paths: multiple_prediction = ImagePrediction() if 'squeezenet' in model_abs_path.lower(): try: multiple_prediction.setModelTypeAsSqueezeNet() multiple_prediction.setModelPath(model_abs_path) multiple_prediction.loadModel() results_array = multiple_prediction.predictMultipleImages( image_files, result_count_per_image=result_count) # print(f"results_array: {results_array}") results_array_idx = 0 for each_result in results_array: predictions, percentage_probabilities = each_result[ "predictions"], each_result["percentage_probabilities"] line = ( f"\n\nThe SqueezeNet model's top {result_count} predictions with probability for {image_files[results_array_idx]}: " ) print(line, end='') write_to_txt(line) for index in range(len(predictions)): line = f"\n{predictions[index]}: {percentage_probabilities[index]}" print(line, end='') write_to_txt(line) print("-----------------------")
output_image_path=output_path) for eachItem in detection: print(eachItem["name"], " : ", eachItem["percentage_probability"]) current_dir = os.getcwd() image_predictions = ImagePrediction() image_predictions.setModelTypeAsResNet() image_predictions.setModelPath( os.path.join(current_dir, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) image_predictions.loadModel() images_array = list( filter(lambda x: x.endswith(".jpg") or x.endswith(".png"), os.listdir(current_dir))) results_array = image_predictions.predictMultipleImages( images_array, result_count_per_image=10) def consolidate_prediction_results(results): list_of_prediction = [] for each_result in results: predictions, percentage_probabilities = each_result[ "predictions"], each_result["percentage_probabilities"] list_of_prediction.append(predictions) for index in range(len(predictions)): print(predictions[index], " : ", percentage_probabilities[index]) print("______________________________") print('set_of_prediction:', set([item for items in list_of_prediction for item in items]))