class PredictionDetector(ObstacleDetector): obstacleTypes = [ "horizontal_bar", "parallel_bars", "pole", "swing", "crane" ] CUT_OFF = 80.0 def __init__(self): self.prediction = ImagePrediction() self.prediction.setModelTypeAsInceptionV3() self.prediction.setModelPath(model_paths.inceptionPath) self.prediction.loadModel() def __isObstacle__(self, prediction, percent): return prediction in self.obstacleTypes and percent > self.CUT_OFF def classify(self, img): predictions, probability_percent = self.prediction.predictImage( input_type="array", image_input=img, result_count=2) for index in range(len(predictions)): prediction = predictions[index] percent = probability_percent[index] if self.__isObstacle__(prediction, percent): return prediction return None
def run(self): print("预测线程启动") global PredictionResult global PredictionModelPath global PredictionResult global PredictionSpeed prediction = ImagePrediction() PredictionResult.set('') if PredictionModel.get() == 'SqueezeNet': print('预测模型选中:SqueezeNet') prediction.setModelTypeAsSqueezeNet() elif PredictionModel.get() == 'ResNet50': print('预测模型选中:ResNet50') prediction.setModelTypeAsResNet() elif PredictionModel.get() == 'InceptionV3': print('预测模型选中:InceptionV3') prediction.setModelTypeAsInceptionV3() elif PredictionModel.get() == 'DenseNet121': print('预测模型选中:DenseNet121') prediction.setModelTypeAsDenseNet() PredictionModelPath = prediction_model() print('模型路径:' + PredictionModelPath) prediction.setModelPath(PredictionModelPath) speedindex = SpeedSelector.get() print('识别速度' + PredictionSpeed[speedindex - 1]) bk.clear_session() prediction.loadModel(prediction_speed=PredictionSpeed[speedindex - 1]) predictions, probabilities = prediction.predictImage( imagePath, result_count=CountSelector.get()) for eachPrediction, eachProbability in zip(predictions, probabilities): PredictionResult.set(PredictionResult.get() + "\n" + str(eachPrediction) + zh_cn(str(eachPrediction)) + " : " + str(eachProbability)) print("预测线程结束")
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_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() predictions, probabilities = predictor.predictImage( image_input=os.path.join(main_folder, main_folder, "data-images", "1.jpg")) assert isinstance(predictions, list) assert isinstance(probabilities, list) assert isinstance(predictions[0], str) assert isinstance(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 objdetection_handler(message): print('Object detection task received: ', message['data']) data = json.loads(message['data']) print(data) if (data["type"] != "todo"): return execution_path = os.getcwd() minioClient = Minio('minio:9000', access_key='minio', secret_key='minio123', secure=False) img_path = os.path.join(execution_path, "tmp", data['object_id']) minioClient.fget_object('photostack', data['object_id'], img_path, request_headers=None) prediction = ImagePrediction() prediction.setModelTypeAsInceptionV3() prediction.setModelPath( os.path.join(execution_path, "inception_v3_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(img_path, result_count=3) pred_list = list() for eachPrediction, eachProbability in zip(predictions, probabilities): print("Predicted: ", eachProbability, ":", eachPrediction) if eachProbability > 15: pred_list.append(eachPrediction) result = { 'type': "done", "photo_id": data['photo_id'], "object_id": data['object_id'], "objects": pred_list } json_string = json.dumps(result) r = redis.Redis(host='redis', port=6379) r.publish('objdetection', json_string) os.remove(img_path)
from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsInceptionV3() prediction.setModelPath( os.path.join( execution_path, './models/inception_v3_weights_tf_dim_ordering_tf_kernels.h5')) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join( execution_path, "wp4589844-inosuke-hashibira-wallpapers.jpg"), result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " :: ", eachProbability)
from imageai.Prediction import ImagePrediction #import Image AI libray import os path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsInceptionV3( ) # Set model type as Inception (becuase I use the Inception model) prediction.setModelPath( os.path.join(path, "inception_v3_weights_tf_dim_ordering_tf_kernels.h5") ) # Put the Inception model's path prediction.loadModel() predictions, probabilities = prediction.predictImage( os.path.join(path, "file.jpg"), result_count=5) # Here input the file name for eachPrediction, eachProbability in zip(predictions, probabilities): print( f"According to the program, there is a {eachProbability}% chance that this is an {eachPrediction}" ) # The program prints out the results
) 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("-----------------------") results_array_idx = results_array_idx + 1 except Exception as e: print("\n\n\ResNet didn't work.") print(e) print("Next model..") elif 'inception' in model_abs_path.lower(): try: multiple_prediction.setModelTypeAsInceptionV3() 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 Inception 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)):