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 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 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 classify_img(self, img): # input: Image object to classify # output: classification results in dictionary # where key is object as a string # and value is confidence level scaled 0-100 if not isinstance(img, Image.Image): return None prediction = ImagePrediction() prediction.setModelTypeAsSqueezeNet() prediction.setModelPath( 'src/squeezenet_weights_tf_dim_ordering_tf_kernels.h5') prediction.loadModel() predictions, probabilities = [ elem[::-1] for elem in prediction.predictImage(img, input_type='array') ] results = {} for idx, prediction in enumerate(predictions): related_words = self.get_related_words(prediction) results.update( {word: probabilities[idx] for word in related_words}) return results
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 runAI(name, root, base): """ Prints and writes to file the results of an A.I. engine's predictions and prediction probabilities of an image's content. Args: name (str): File name root (str): Superior path to file; the folder in which the file is stored base (str): File name without extension """ if ai["enable"]: prediction = ImagePrediction() try: exec(f"prediction.setModelTypeAs{ai['modelType']}()") except: raise "Unknown A.I. model type." prediction.setModelPath(f"./models/{ai['model']}") prediction.loadModel() predictions, probabilities = prediction.predictImage( f"{root}/{name}".replace("/", "/"), result_count=ai["results"]) # Calculate predictions with their corresponding probabilities print("\nPredictions:") for eachPrediction, eachProbability in zip(predictions, probabilities): print(" " * 4 + f"{eachPrediction}: {round(eachProbability, 1)}%") open(f"{root}/{base}/predictions.csv", "a+").write(f"{eachPrediction};{eachProbability}%\n")
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 detect_img(): # return nothing if no file if 'file' not in request.files: return jsonify("") file = request.files['file'] if file.filename == '': flash('No selected file') return jsonify("") if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(execution_path, filename)) logging.debug(os.path.join(execution_path , filename)); prediction = ImagePrediction() prediction.setModelTypeAsSqueezeNet() prediction.setModelPath(os.path.join(execution_path, "squeezenet_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join(execution_path, filename), result_count=5 ) results = []; for eachPrediction, eachProbability in zip(predictions, probabilities): results.append((eachPrediction, eachProbability)) os.remove(os.path.join(execution_path, filename)) return jsonify(results)
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 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 predict_objects_densnet(filename): prediction = ImagePrediction() prediction.setModelTypeAsDenseNet() prediction.setModelPath( os.path.join(os.getcwd(), "models/DenseNet-BC-121-32.h5")) prediction.loadModel() print("----------DenseNet--------------") predictions, probabilities = prediction.predictImage(filename, result_count=20) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
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() 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 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 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
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
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 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 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 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 test_recognition_model_resnet_array_input(): predictor = ImagePrediction() predictor.setModelTypeAsResNet() predictor.setModelPath( os.path.join(main_folder, "data-models", "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) predictor.loadModel() image_array = cv2.imread( os.path.join(main_folder, main_folder, "data-images", "1.jpg")) predictions, probabilities = predictor.predictImage( image_input=image_array, input_type="array") assert isinstance(predictions, list) assert isinstance(probabilities, list) assert isinstance(predictions[0], str) assert isinstance(probabilities[0], float)
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)
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 test_recognition_model_squeezenet(): print("TOP FOLDER: ", main_folder) predictor = ImagePrediction() predictor.setModelTypeAsSqueezeNet() predictor.setModelPath( os.path.join(main_folder, "data-models", "squeezenet_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)
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 func_model(self): prediction = ImagePrediction() prediction.setModelTypeAsDenseNet() prediction.setModelPath(self.model_path) prediction.loadModel() time_init = time.time() predictions, probabilities = prediction.predictImage(self.image_path, result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): self.data_dict[eachPrediction] = eachProbability print(eachPrediction + " : " + str(eachProbability)) time2 = time.time() print('this is:' + max(self.data_dict, key=self.data_dict.get)) print('time:' + str(time2 - time_init) + 's')
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
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)