def Den(): execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsDenseNet() prediction.setModelPath(os.path.join(execution_path, "/home/guest/Documents/Test1/ImageAI-master/imageai/Prediction/Weights/DenseNet.h5")) prediction.loadModel() return prediction
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_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 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 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)
import os from imageai.Detection import ObjectDetection from imageai.Prediction import ImagePrediction resNetModel = 'models/DenseNet-BC-121-32.h5' resNetDetectionModel = 'models/resnet50_coco_best_v2.0.1.h5' execution_path = os.getcwd() model_path = os.path.join(execution_path, resNetModel) detection_model_path = os.path.join(execution_path, resNetDetectionModel) # Predictions prediction = ImagePrediction() prediction.setModelTypeAsDenseNet() prediction.setModelPath(model_path) prediction.loadModel() # Detections detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath(detection_model_path) detector.loadModel() def predict_types(img): predictions, probabilities = prediction.predictImage(img.strip(), result_count=5) return list(zip(predictions, probabilities)) def detect_objects(img):
def Make_Features(df): multiple_prediction = ImagePrediction() multiple_prediction.setModelTypeAsDenseNet() multiple_prediction.setModelPath( "../mode/trained_models/DenseNet-BC-121-32.h5") multiple_prediction.loadModel() detector = ObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath("../mode/trained_models/yolo.h5") detector.loadModel() df['DenseNet'] = df['img'].swifter.apply( lambda x: multiple_prediction.predictImage(path_dir + x, result_count=3)) df['Yolo'] = df['img'].swifter.apply( lambda x: detector.detectObjectsFromImage( path_dir + x, output_image_path='./new.jpg', minimum_percentage_probability=20)) df['DenseNet_obj'] = df['DenseNet'].swifter.apply( lambda x: x[0][0] + ' and ' + x[0][1] + ' and ' + x[0][2]) df['Yolo_obj'] = df['Yolo'].swifter.apply( lambda x: ' '.join(word for word in [l['name'] for l in x]), axis=1) df['Img_txt'] = df['DenseNet_obj'] + ' ' + df['Yolo_obj'] df['palette_color'] = df['img'].swifter.apply( lambda x: ColorThief(path_dir + x).get_palette(color_count=5)) df['Bluriness'] = [ cv2.Laplacian(cv2.imread(path_dir + x, 0), cv2.CV_64F).var() for x in df['img'] ] df['Imagedim'] = [ cv2.imread(path_dir + x).flatten().shape[0] for x in df['img'] ] df['Yolo_unique'] = df['Yolo_obj'].swifter.apply(lambda x: len(set(x))) df['Yolo_N_obj'] = df['Yolo_obj'].swifter.apply(lambda x: len(x)) #print(df.describe() ) # First cross variable between text and image : df['sim_txt_img_gen'] = df.swifter.apply( lambda x: nlp(x.text).similarity(nlp(x.DenseNet_obj)), axis=1) df['sim_txt_img_objs'] = df.swifter.apply(lambda x: nlp(x.text).similarity( nlp(' and '.join(word[0] for word in x.Yolo_obj))), axis=1) # extract dominant colors from image df['paletCol_1'] = df['palette_color'].swifter.apply( lambda x: (x[0][0] * 65536 + x[0][1] * 256 + x[0][2])) df['paletCol_2'] = df['palette_color'].swifter.apply( lambda x: (x[1][0] * 65536 + x[1][1] * 256 + x[1][2])) df['paletCol_3'] = df['palette_color'].swifter.apply( lambda x: (x[2][0] * 65536 + x[2][1] * 256 + x[2][2])) df['paletCol_4'] = df['palette_color'].swifter.apply( lambda x: (x[3][0] * 65536 + x[3][1] * 256 + x[3][2])) df['paletCol_5'] = df['palette_color'].swifter.apply( lambda x: (x[4][0] * 65536 + x[4][1] * 256 + x[4][2])) # Get Blurry status # Get shapes df['brightness'] = [ cv2.mean(cv2.cvtColor(cv2.imread(path_dir + x), cv2.COLOR_BGR2HSV))[1] / 255. for x in df['img'] ] df['Saturation'] = [ cv2.mean(cv2.cvtColor(cv2.imread(path_dir + x), cv2.COLOR_BGR2HSV))[0] / 255. for x in df['img'] ] df['ImageValue'] = [ cv2.mean(cv2.cvtColor(cv2.imread(path_dir + x), cv2.COLOR_BGR2HSV))[2] / 255. for x in df['img'] ] df['word_count'] = df['text'].swifter.apply( lambda x: len(str(x).split(" "))) df['char_count'] = df['text'].str.len() df['stp_count'] = df['text'].swifter.apply( lambda x: len([x for x in x.split() if x in stop])) df['spc_count'] = df['text'].swifter.apply( lambda x: len([x for x in list(x) if x in special_char])) df['sentiment_txt'] = df['text'].swifter.apply(lambda x: getsentiment(x)) df['sentiment_img'] = df['DenseNet_obj'].swifter.apply( lambda x: getsentiment(x)) df['prfn_ftr'] = df['text'].swifter.apply(lambda x: nlp(x)._.is_profane) df['Quant'] = df['text'].swifter.apply(lambda x: len([ y for y in nlp(x).ents if str(y.label_) == 'MONEY' or str(y.label_) == 'DATE' or str(y.label_) == 'TIME' or str(y.label_) == 'PERCENT' or str(y.label_) == 'ORDINAL' or str(y.label_) == 'CARDINAL' or str(y.label_) == 'QUANTITY' ])) df['Ent'] = df['text'].swifter.apply(lambda x: len([ y for y in nlp(x).ents if str(y.label_) == 'PERSON' or str(y.label_) == 'NORP' or str( y.label_) == 'ORG' or str(y.label_) == 'LOC' or str(y.label_) == 'GPE' or str(y.label_) == 'WORK_OF_ART' or str(y.label_) == 'EVENT' ])) df['polarity_scores'] = df['text'].swifter.apply( lambda x: sid.polarity_scores(x)) df['neg_txt'] = df['polarity_scores'].swifter.apply(lambda x: x['neg']) df['neu_txt'] = df['polarity_scores'].swifter.apply(lambda x: x['neu']) df['pos_txt'] = df['polarity_scores'].swifter.apply(lambda x: x['pos']) df['com_txt'] = df['polarity_scores'].swifter.apply( lambda x: x['compound']) #df = df.drop(columns=['DenseNet' ,'DenseNet_obj', 'Yolo' , 'Yolo_obj' , 'palette_color', 'polarity_scores']) return df
from imageai.Prediction import ImagePrediction import os data={} f=open("E:/DatasetA/label_list.txt").readlines() for i in f: zj=i.strip('\n').split('\t')[0] la=i.strip('\n').split('\t')[1] data[zj]=la pred=ImagePrediction() pred.setModelTypeAsDenseNet() pred.setModelPath("models/DenseNet-BC-121-32.h5") pred.loadModel() train=open("E:/DatasetA/train.txt").readlines() acc=0 n=0 for index,i in enumerate(train): n+=1 path=i.strip('\n').split('\t')[0] label=i.strip('\n').split('\t')[1] flag=0 predictions, probabilities = pred.predictImage("E:/DatasetA/train/"+path, result_count=1000) for eachPrediction, eachProbability in zip(predictions, probabilities): if eachPrediction in data.values(): flag=1 # print(eachPrediction + " : " + eachProbability) # print("true label",data[label]) if eachPrediction==data[label]: acc+=1 else: pass break
from imageai.Prediction import ImagePrediction import os # 获取当前python文件所在的路径 execution_path = os.getcwd() multiple_prediction = ImagePrediction() multiple_prediction.setModelTypeAsDenseNet() multiple_prediction.setModelPath("/Users/zhusheng/WorkSpace/Tmp/dataset/models/DenseNet-BC-121-32.h5") multiple_prediction.loadModel() all_image_array = [] all_files = os.listdir("/Users/zhusheng/WorkSpace/Tmp/dataset/mycat/") for each_file in all_files: if(each_file.endswith(".jpg") or each_file.endswith(".png")): file_path = os.path.join("/Users/zhusheng/WorkSpace/Tmp/dataset/mycat/", each_file) all_image_array.append(file_path) print(all_image_array) # 开始预测,一次预测一个图片集合 results_array = multiple_prediction.predictMultipleImages(all_image_array, result_count_per_image=5) # 打印预测结果 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("-----------------------")
print('Upload the file and predictions to Box.') exit() TOKEN = 'yEUCETgkQSQIitfOBTBHtQKjkRQZ957i' auth = OAuth2(None, None, access_token=TOKEN) box = Client(auth) me = box.user().get() print('logged in to box as', me.login) folder = box.folder(FOLDER_ID).get() print('current folder', folder) # setup prediction engine engine = ImagePrediction() engine.setModelTypeAsDenseNet() engine.setModelPath('DenseNet-BC-121-32.h5') engine.loadModel() # get .jpg files jpg_files = glob.glob('*.jpg') for image in jpg_files: # run image through prediction engine prediction, probablity = engine.predictImage(image) prediction_data = { 'prediction1': prediction[0] + ' : ' + probablity[0].astype(str), 'prediction2': prediction[1] + ' : ' + probablity[1].astype(str), 'prediction3': prediction[2] + ' : ' + probablity[2].astype(str), } new_file = box.folder(FOLDER_ID).upload(image) print('uploaded file', new_file)