def result(): #Gets base64 encoded image from request body and saves it in a temporary file imagedata = base64.b64decode(str(request.json['image'])) with open(photo, 'wb') as f: f.write(imagedata) #Calls Tensorflow and sets its main settings execution_path = os.getcwd() prediction = ImagePrediction() #Sets model type (change it if using a different type dataset) prediction.setModelTypeAsResNet() #Sets the correct dataset (defined above) prediction.setModelPath(os.path.join(execution_path, dataset)) prediction.loadModel() #Recognizes image, based on how many results we requested predictions, probabilities = prediction.predictImage(os.path.join( execution_path, photo), result_count=results) #Prints all the chosen results (for debug purpose) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability) #Removes underscores from result if present (result must be read out loud, so the underscore must be removed) announcement = predictions[0].replace("_", " ") #Deletes the prediction image from system os.remove('tmp.jpeg') #Responds to the received request with a JSON return jsonify(announcement)
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 predict_img(filename): tensorflow.compat.v1.logging.set_verbosity( tensorflow.compat.v1.logging.ERROR) target = os.path.join( APP_ROOT, 'temp/' + filename) #location of image present in temp directory prediction = ImagePrediction() prediction.setModelTypeAsResNet() tensorflow.compat.v1.logging.set_verbosity( tensorflow.compat.v1.logging.ERROR) prediction.setModelPath( os.path.join(static_loc, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(target, result_count=1) d = {} #dictionary that will save results for eachPrediction, eachProbability in zip(predictions, probabilities): d[eachPrediction] = eachProbability #prediction output #print(eachPrediction , " : " , eachProbability) os.remove(target) #delete temporary file return d
def handle(self): print('New connection from', self.client_address) while True: data = self.request.recv(1024) print('recv: ', data.decode()) self.request.send('ok'.encode()) size = int(data.decode()) print("size", size) img = '' while len(img) < size: data_new = self.request.recv(1024 * 500).decode() img += str(data_new) f = open("/geni/imgSave.png", "wb") f.write(base64.decodestring(img.encode())) f.close() # model prediction prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( '/geni/resnet50_weights_tf_dim_ordering_tf_kernels.h5') prediction.loadModel() predict, prob = prediction.predictImage("/geni/imgSave.png", result_count=3) for i in range(len(predict)): print(str(predict[i]) + ' : ' + str(prob[i])) # return first prediction res = str(predict[0]) + str(prob(0)) self.request.send(res.encode()) print('Result sent.')
def predictionModelLoad(model_path, mod="normal"): multiple_prediction = ImagePrediction() multiple_prediction.setModelTypeAsResNet() multiple_prediction.setModelPath(model_path) multiple_prediction.loadModel(prediction_speed=mod) return multiple_prediction
def food_dect(predict_image, model_position): list_predict_results = [] list_predict_confi = [] prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(model_position) prediction.loadModel() predictions, probabilities = prediction.predictImage(predict_image, result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): #print(eachPrediction," : ", eachProbability) if eachProbability > 3: if_fod, name_fod = name_rectify(eachPrediction) if if_fod == True: list_predict_results = list_predict_results + name_fod for f in name_fod: list_predict_confi.append(eachProbability) if not list_predict_results: list_predict_results.append("can not recognize item") #ist_predict_results = list(dict.fromkeys(list_predict_results)) return_list_name = [] #return_list_pro=[] for i in range(len(list_predict_results)): if list_predict_results[i] in return_list_name: j = return_list_name.index(list_predict_results[i]) #return_list_pro[j] = return_list_pro[j]+list_predict_confi[i] else: return_list_name.append(list_predict_results[i]) #return_list_pro.append(list_predict_confi[i]) return return_list_name #, return_list_pro
def load_models(): # load models keras.backend.clear_session() # SqueezeNet for image recognition # higher speed and intermediate accuracy predictor_sqz = ImagePrediction() predictor_sqz.setModelTypeAsSqueezeNet() predictor_sqz.setModelPath( os.path.join( MEDIA_ROOT, 'models/squeezenet_weights_tf_dim_ordering_tf_kernels.h5')) predictor_sqz.loadModel() # ResNet for image recognition # high speed and high accuracy predictor_res = ImagePrediction() predictor_res.setModelTypeAsResNet() predictor_res.setModelPath( os.path.join(MEDIA_ROOT, 'models/resnet50_weights_tf_dim_ordering_tf_kernels.h5')) predictor_res.loadModel() # RetinaNet for object detection detector_res = ObjectDetection() detector_res.setModelTypeAsRetinaNet() detector_res.setModelPath( os.path.join(MEDIA_ROOT, 'models/resnet50_coco_best_v2.0.1.h5')) detector_res.loadModel() return predictor_sqz, predictor_res, detector_res
def Res(): execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(os.path.join(execution_path, "/home/guest/Documents/Test1/ImageAI-master/imageai/Prediction/Weights/ResNet.h5")) prediction.loadModel() return prediction
def main(path_model, path_img): prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(path_model) prediction.loadModel() predictions, probabilities = prediction.predictImage(path_img, result_count=10) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
def deteccion_de_persona(imagen): execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( execution_path + "/resnet50_weights_tf_dim_ordering_tf_kernels.h5") prediction.loadModel() predictions, percentage_probabilities = prediction.predictImage(imagen, result_count=5) for index in range(len(predictions)): print(predictions[index] , " : " , percentage_probabilities[index])
def image2text(imagefile): execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() # choose between Resnet, SqeezeNet, InceptionV3, DenseNet Model prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) #weight should be compatible with the model prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join(execution_path, imagefile), result_count=5 ) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : " , eachProbability) return predictions
def imagePrediction(self, pathInputData): self.type = imghdr.what(pathInputData) if self.type == "jpeg": self.type = "jpg" if self.type not in ["jpg", "png"]: return None tempData = pathInputData[0:-4] pathData = tempData + ".jpg" zhuanhuanPic = tempData + "zh." + self.type # 目标图片大小 dst_w = 400 dst_h = 0 # #保存的图片质量 save_q = 40 # 等比例压缩 try: img = image.open(pathInputData) bg = image.new("RGB", img.size, (255, 255, 255)) bg.paste(img, img) bg.save(pathInputData) except: pass self.resizeImg(ori_img=pathData, dst_img=zhuanhuanPic, dst_w=dst_w, dst_h=dst_h, save_q=save_q) prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( os.path.join( self.getExecutionPath(), os.path.join( self.getExecutionPath(), "tools/imageAI/resnet50_weights_tf_dim_ordering_tf_kernels.h5" ))) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join( self.getExecutionPath(), zhuanhuanPic), result_count=5) result = [] for eachPrediction, eachProbability in zip(predictions, probabilities): result.append(eachPrediction + " : " + eachProbability) return json.dumps(result)
def load_model(): global g_1 global prediction global sess sess = tf.Session() set_session(sess) prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() g_1 = tf.get_default_graph()
def main(): # GooeyParser used in place of ArgumentParser in order to specify widgets parser = GooeyParser(description="Image Recognition Search for Wikipedia") # facilitates file selection for later image processing parser.add_argument("Filename", help="Select the the image file to process", widget="FileChooser") arg = parser.parse_args() print("Your image is processing...") # ImageAI library setup, sets specifications for image prediction and processing execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(execution_path + "/resnet50_weights_tf_dim_ordering_tf_kernels.h5") prediction.loadModel() # call of predictImage function, returns object predictions and probabilities predictions, percentage_probabilities = prediction.predictImage( arg.Filename, result_count=1) # output testing of prediction and probabilities, used for debugging # for index in range(len(predictions)): # print(predictions[index], " : ", percentage_probabilities[index]) # if object probability is greater than 20%, proceed with output if percentage_probabilities[0] > 20: # removes underscores in string with spaces for cleaner output prediction_object = predictions[0].replace('_', ' ') print( "Success!\nA " + prediction_object + " has been detected.\nPlease see your default web browser to view the Wikipedia page.\n" ) # calls fetch_webpage in order to load correct Wikipedia page fetch_webpage(predictions[0]) # if object probability is less than 20%, print below and do NOT load webpage else: print( "Sorry!\nWe couldn't find an object in your image with high probability.\n" ) return
class ObjectDetector: def __init__(self): self.detector = ObjectDetection() self.detector.setModelTypeAsRetinaNet() self.detector.setModelPath("./models/resnet50_coco_best_v2.0.1.h5") self.detector.loadModel() self.prediction = ImagePrediction() self.prediction.setModelTypeAsResNet() self.prediction.setModelPath( "./models/resnet50_weights_tf_dim_ordering_tf_kernels.h5") self.prediction.loadModel() def detect(self, filename, dimensions): print("Filename: {}".format(filename)) detections = self.detector.detectObjectsFromImage( input_image=filename, input_type="file", output_image_path="./tmp/out.jpg") items = [] for detection in detections: obj = {} obj["type"] = "object" obj["name"] = detection["name"] obj["probability"] = detection["percentage_probability"] x1, y1, x2, y2 = detection["box_points"] obj["box"] = {} obj["box"]["left"] = int(x1) obj["box"]["top"] = int(y1) obj["box"]["bottom"] = dimensions["height"] - ( dimensions["height"] - int(y2)) obj["box"]["right"] = dimensions["width"] - (dimensions["width"] - int(x2)) items.append(obj) predictions, probabilities = self.prediction.predictImage( filename, result_count=10) preds = [] for idx, val in enumerate(predictions): obj = {} obj["type"] = "tag" obj["name"] = val obj["probability"] = probabilities[idx] items.append(obj) return items
def predict_objects_resnet(filename): prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( os.path.join(os.getcwd(), "models/resnet50_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() print("----------ResNet--------------") predictions, probabilities = prediction.predictImage(filename, result_count=20) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
def __init__(self, client): self.client = client # Load model path = os.path.abspath(os.path.join( __file__, "../../..")) + "\\resnet50_weights_tf_dim_ordering_tf_kernels.h5" print(path) prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(path) prediction.loadModel() self.model = prediction
def prediction(): predire = [] execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("resnet50_weights_tf_dim_ordering_tf_kernels.h5") prediction.loadModel() predictions, percentage_probabilities = prediction.predictImage( "lion.jpg", result_count=5) for index in range(len(predictions)): a = predictions[index], " : ", percentage_probabilities[index] predire.append(a) print(a) return predire
def test_recognition_model_resnet(): predictor = ImagePrediction() predictor.setModelTypeAsResNet() predictor.setModelPath( os.path.join(main_folder, "data-models", "resnet50_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 object_recognition(picture_path, result_count=10): cwd = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( os.path.join(cwd, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage( picture_path, result_count=result_count) #for eachPrediction, eachProbability in zip(predictions, probabilities): #print(eachPrediction, " : ", eachProbability, "%") return predictions, probabilities
def process(self, params): # execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( "../aimodel/resnet50_weights_tf_dim_ordering_tf_kernels.h5") prediction.loadModel() predictions, percentage_probabilities = prediction.predictImage( "../images/sample.jpeg", result_count=1) # for index in range(len(predictions)): # print(predictions[index], " : ", percentage_probabilities[index]) return {"object": predictions[0], "probability": percentage_probabilities[0]}
def object_recognition(): execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join( execution_path, "rabbit.jpg"), result_count=10) print("It is a", predictions[0]) engine = pyttsx3.init() engine.say(predictions[0]) engine.runAndWait()
def imapre(file_n): execution_path=file_n[0] prediction= ImagePrediction() # prediction.setModelTypeAsSqueezeNet() # prediction.setModelPath(os.path.join(execution_path, "squeezenet_weights_tf_dim_ordering_tf_kernels.h5")) global inc prediction.setModelTypeAsResNet() prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join(execution_path,file_n[1]), result_count=5 ) for eachPrediction, eachProbability in zip(predictions, probabilities): output.append("{} : {}".format(eachPrediction , eachProbability)) print(output) drop_in_list_box(output,inc) inc += 1
def recognize(file): prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(project_path + "restnet.h5") prediction.loadModel() predictions, percentage_probabilities = prediction.predictImage( file, result_count=5) result = {} for index in range(len(predictions)): result[predictions[index]] = percentage_probabilities[index] os.remove(file) return json.dumps(result)
class ImAITagExtractorBoltPython(storm.BasicBolt): def initialize(self, conf, context): self._conf = conf self._context = context self.path = os.path.dirname(__file__) self.image_predictions = ImagePrediction() self.image_predictions.setModelTypeAsResNet() self.image_predictions.setModelPath( os.path.join(self.path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) self.image_predictions.loadModel() def process(self, tuple): screenName = tuple.values[0] statusID = tuple.values[1] text = tuple.values[2] hashtags = tuple.values[3] media_urls = tuple.values[4] image_path = self.path + "/image.jpeg" media = [] for url in media_urls: r.urlretrieve(url, image_path) results_array = self.image_predictions.predictImage( image_path, result_count=10) tags = self.consolidate_prediction_results(results_array) res_str = {"link": url, "tags_ai": tags} media.append(str(res_str)) storm.emit([str(screenName), statusID, str(text), hashtags, media]) def consolidate_prediction_results(self, results): predictions, percentage_probabilities = results list_of_tags = [] for i, prediction in enumerate(predictions): tag = { "name": prediction, "confidence": percentage_probabilities[i] } list_of_tags.append(tag) return list_of_tags
def image_predict(image): prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( "/home/kds3000/django_image_ai/resnet50_weights_tf_dim_ordering_tf_kernels.h5" ) prediction.loadModel() predictions, probabilities = prediction.predictImage(image, result_count=5) output_dict = {} translator = Translator(to_lang="ru") for eachPrediction, eachProbability in zip(predictions, probabilities): converted_prob = convert_probability(eachProbability) translated_name = translator.translate(eachPrediction.replace( '_', ' ')) output_dict[translated_name] = converted_prob return output_dict
def my_prediction(img_path, prob): result = {} execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( os.path.join(execution_path, "./data/resnet50.h5")) # I rename a model to simple name prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join( execution_path, img_path), result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): if (eachProbability >= prob): result[eachPrediction] = eachProbability # print(eachPrediction , " : " , eachProbability) return result
def test_recognition_model_resnet(): predictor = ImagePrediction() predictor.setModelTypeAsResNet() predictor.setModelPath( os.path.join(main_folder, "data-models", "resnet50_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 demo01(): # https://github.com/OlafenwaMoses/ImageAI/blob/master/imageai/Prediction/README.md from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() # https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_weights_tf_dim_ordering_tf_kernels.h5 prediction.setModelPath( r"E:\bigdata\ai\imageai\resnet50_weights_tf_dim_ordering_tf_kernels.h5" ) #(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join( execution_path, "1.png"), result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
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() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "1.jpg"), result_count=5 ) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction + " : " + eachProbability)