from imageai.Prediction import ImagePrediction import os import pandas as pd import numpy as np from PIL import Image from gensim.models import Word2Vec from sklearn.cluster import KMeans import matplotlib.pyplot as plt 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() TEST_PATH = '/home/guest/Documents/Aikomi' def tag(TEST_PATH, prediction): pred_array = np.empty((0, 6), dtype=object) for img in os.listdir(TEST_PATH): if img.endswith('.jpg'): image = Image.open(os.path.join(TEST_PATH, img)) image = image.convert("RGB") image = np.array(image, dtype=np.uint8) predictions, probabilities = prediction.predictImage( os.path.join(TEST_PATH, img), result_count=5) temprow = np.zeros((1, pred_array.shape[1]), dtype=object)
from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() 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, "car.jpg"), result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
#https://medium.com/@guymodscientist/image-prediction-with-10-lines-of-code-3266f4039c7a from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(execution_path + r"\resnet50_weights_tf_dim_ordering_tf_kernels.h5") prediction.loadModel() predictions, percentage_probabilities = prediction.predictImage( r"C:\Users\prasa\Python Programs\p_env\sample.jpg", result_count=5) for index in range(len(predictions)): print(predictions[index], " : ", percentage_probabilities[index])
from imageai.Prediction import ImagePrediction from imageai.Prediction.Custom import ModelTraining # obtains the path to the folder that contains your python file !!!! execution_path = os.getcwd() SOURCE_PATH = "https://github.com/OlafenwaMoses/IdenProf/releases/download/v1.0/idenprof-jpg.zip" FILE_DIR = os.path.join(execution_path, "idenprof-jpg.zip") # model_trainer = ModelTraining() # model_trainer.setModelTypeAsResNet() # model_trainer.setDataDirectory("idenprof") # model_trainer.trainModel(num_objects=10, num_experiments=1, batch_size=32) prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( os.path.join(execution_path, "idenprof/models/model_ex-001_acc-0.100000.h5")) prediction.loadModel() all_images_array = [] all_files = os.listdir(execution_path + "test") for each_file in all_files: if each_file.endswith(".jpg") or each_file.endswith(".png"): all_images_array.append(each_file) results_array = prediction.predictMultipleImages(all_images_array, result_count_per_image=5)
class Util: def __init__(self, image_folder): super().__init__() self.img1 = Util.to_grayscale( imread(os.path.join(self.execution_path, 'bad_image.jpg')).astype(float)) self.execution_path = '/home/spike/Projects/object_detection/' #os.getcwd() self.image_folder = image_folder self.temp_path = os.path.join(self.execution_path, 'tmp') self.detector = ObjectDetection() self.detector.setModelTypeAsRetinaNet() self.detector.setModelPath( os.path.join(self.execution_path, "resnet50_coco_best_v2.0.1.h5")) self.detector.loadModel() self.custom_objects = self.detector.CustomObjects(person=True, backpack=True, umbrella=True, handbag=True, tie=True, suitcase=True) self.prediction = ImagePrediction() self.prediction.setModelTypeAsResNet() self.prediction.setModelPath( os.path.join(self.execution_path, 'resnet50_weights_tf_dim_ordering_tf_kernels.h5')) self.prediction.loadModel() @staticmethod def to_grayscale(arr): "If arr is a color image (3D array), convert it to grayscale (2D array)." if len(arr.shape) == 3: return average(arr, -1) # average over the last axis (color channels) else: return arr @staticmethod def normalize(arr): rng = arr.max() - arr.min() if rng == 0: rng = 1 amin = arr.min() return (arr - amin) * 255 / rng @staticmethod def compare_images(img1, img2): # normalize to compensate for exposure difference, this may be unnecessary # consider disabling it img1 = Util.normalize(img1) img2 = Util.normalize(img2) # calculate the difference and its norms diff = img1 - img2 # elementwise for scipy arrays m_norm = sum(abs(diff)) # Manhattan norm z_norm = norm(diff.ravel(), 0) # Zero norm return (m_norm, z_norm) def is_image_bad(self, image_to_compare): img2 = Util.to_grayscale( imread(os.path.join(self.image_folder, image_to_compare)).astype(float)) if self.img1.shape != img2.shape: return False # compare n_m, n_0 = Util.compare_images(self.img1, img2) if n_m < 1: return True else: return False
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=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("-----------------------")
def __init__(self, Threshold=20, modelName="DenseNet", CustomModelName=None, CustomModelJsonFilePath=None): global Model_dir_Path, Web_app_dir Model_dir_Path = os.path.dirname(os.path.realpath(__file__)) Web_app_dir = os.path.dirname(os.path.realpath(__file__ + "../../..")) self.Threshold = Threshold print("Here ....3\n") if CustomModelName is None: print("Here ....4\n") self.prediction = ImagePrediction() else: self.prediction = CustomImagePrediction() if modelName in "ResNet": print("Here ....5\n") self.prediction.setModelTypeAsResNet() if CustomModelName is None: self.prediction.setModelPath( Model_dir_Path + "/Models/resnet50_weights_tf_dim_ordering_tf_kernels.h5") else: self.prediction.setModelPath(Model_dir_Path + "/Models/" + CustomModelName) self.prediction.setJsonPath(Model_dir_Path + "/Models/" + CustomModelJsonFilePath) elif modelName in "SqueezeNet": print("Here ....5\n") self.prediction.setModelTypeAsSqueezeNet() if CustomModelName is None: self.prediction.setModelPath( Model_dir_Path + "/Models/squeezenet_weights_tf_dim_ordering_tf_kernels.h5") else: self.prediction.setModelPath(Model_dir_Path + "/Models/" + CustomModelName) self.prediction.setJsonPath(Model_dir_Path + "/Models/" + CustomModelJsonFilePath) elif modelName in "InceptionV3": print("Here ....6\n") self.prediction.setModelTypeAsInceptionV3() if CustomModelName is None: self.prediction.setModelPath( Model_dir_Path + "/Models/inception_v3_weights_tf_dim_ordering_tf_kernels.h5" ) else: self.prediction.setModelPath(Model_dir_Path + "/Models/" + CustomModelName) self.prediction.setJsonPath(Model_dir_Path + "/Models/" + CustomModelJsonFilePath) elif modelName in "DenseNet": print("Here ....7\n") self.prediction.setModelTypeAsDenseNet() if CustomModelName is None: print("Here ....7.3\n") print("value of Model Dir is" + Model_dir_Path + "/Models/DenseNet-BC-121-32.h5" + "\n") self.prediction.setModelPath(Model_dir_Path + "/Models/DenseNet-BC-121-32.h5") else: print("Here ....8\n") self.prediction.setModelPath(Model_dir_Path + "/Models/" + CustomModelName) self.prediction.setJsonPath(Model_dir_Path + "/Models/" + CustomModelJsonFilePath) self.prediction.loadModel()
from tkinter import filedialog from tkinter import * from PIL import ImageTk, Image import time root = Tk() root.filename = filedialog.askopenfilename(initialdir="/", title="Select file", filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*"))) from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( "C:\\Users\\praneeth\\AppData\\Local\\Programs\\Python\\Python36\\resnet50_weights_tf_dim_ordering_tf_kernels.h5" ) prediction.loadModel() x = "" logo = ImageTk.PhotoImage( Image.open(root.filename).resize((250, 250), Image.ANTIALIAS)) w = Label(root, image=logo) w.pack() predictions, percentage_probabilities = prediction.predictImage(root.filename, result_count=5) for index in range(len(predictions)): x += predictions[index] + " : " + str( percentage_probabilities[index]) + "\n" w = Label(root, text=x) w.pack()
# The test image print('Choose an input Jpg file') root = tk.Tk() root.withdraw() infile = filedialog.askopenfilename() print('You selected %s' % (infile)) from PIL import Image #infile = '1.jpg' img = Image.open(infile) width, height = img.size print(' %d x %d ' % (width, height)) # Eventually have a case statement to choose algorithm: prediction = ImagePrediction() which_algo = simpledialog.askstring( "Select", "Pick an algorithm\n1 = SqueezeNet\n2 = Inception\n3 = ResNet\n4 = DenseNet", parent=root) fp = switcher[int(which_algo)]() prediction.loadModel() # Scan over all possible fragmentations for percentage in range(30, 101, 5): chopsize = int(percentage / 100.0 * height) print('Chopsize = %d ' % (chopsize))
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
if execution_path: execution_path += str('/') im_name = str(sys.argv[1]) nn_name = 'resnet50_coco_best_v2.0.1.h5' pre_name = 'resnet50_weights_tf_dim_ordering_tf_kernels.h5' threshold = 50 detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath(os.path.join(execution_path, nn_name)) detector.loadModel() detections = detector.detectObjectsFromImage( input_image=im_name, output_image_path=os.path.join(execution_path, "output.jpg"), minimum_percentage_probability=threshold) predictor = ImagePrediction() predictor.setModelTypeAsResNet() predictor.setModelPath(os.path.join(execution_path, pre_name)) predictor.loadModel() predictions, probabilities = predictor.predictImage(image_input=im_name) for eachObject in detections: #print(eachObject["name"] , " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"] ) print(eachObject["name"]) for eachPrediction, eachProbability in zip(predictions, probabilities): if eachProbability > threshold: print(eachPrediction)
#import socket library import socket import pickle import image import os import psutil import platform import time from datetime import datetime import threading from imageai.Prediction import ImagePrediction prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("resnet50_weights_tf_dim_ordering_tf_kernels.h5") prediction.loadModel() # Now we can create socket object s = socket.socket() # Lets choose one port and start listening on that port port = 40000 print("\n Server is listing on port :", port, "\n") host = '0.0.0.0' # Now we need to bind to the above port at server side s.bind(('', port)) # Now we will put server into listenig mode s.listen(10)
from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() 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(os.path.join( execution_path, "images/test1.jpeg"), result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
from imageai.Prediction import ImagePrediction import os import threading execution_paht = os.getcwd() thread_prediction = ImagePrediction() thread_prediction.setModelTypeAsResNet() thread_prediction.setModelPath("/Users/zhusheng/WorkSpace/Tmp/dataset/models/resnet50_weights_tf_dim_ordering_tf_kernels.h5") 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) class PredictionThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): thread_prediction.loadModel() # 一张一张的预测 for eachPicture in all_image_array: print(eachPicture) predictions, probabilities = thread_prediction.predictImage(eachPicture, result_count=1)
resCount = 5 try: model = int(sys.argv[3]) if (model < 4 and model > -1): userModel = model else: userModel = 0 except: #Variable to select the appropriate model and model type userModel = 0 #Printing to console the model we are going to use print("Detecting using {} ...".format(models[userModel])) prediction = ImagePrediction() if userModel == 0: prediction.setModelTypeAsSqueezeNet() elif userModel == 1: prediction.setModelTypeAsResNet() elif userModel == 2: prediction.setModelTypeAsInceptionV3() else: prediction.setModelTypeAsDenseNet() prediction.setModelPath( os.path.join(execution_path, modelsDir + models[userModel])) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join(
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Sep 12 17:22:49 2019 @author: guest """ from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsSqueezeNet() prediction.setModelPath(os.path.join(execution_path, "SqueezeNet.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join( execution_path, "10.jpg"), result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
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("预测线程结束")
f = open(path+'/imagiai/images/'+'downloadlist.csv', "r") reader = csv.reader(f) download_data = [ e for e in reader ] print(download_data) f.close() # video_id 取得 df = pd.read_table(path+'/JPvideos.csv', delimiter=",") df_thumbnail_link = pd.DataFrame({'video_id': df['video_id'], 'thumbnail_link': df['thumbnail_link']}) df_thumbnail_link['thumbnail_link'] = df_thumbnail_link['thumbnail_link'].str.replace('default.jpg', 'maxresdefault.jpg') df_thumbnail_link.head(3) # 画像認識 from imageai.Prediction import ImagePrediction prediction = ImagePrediction() prediction.setModelTypeAsDenseNet() prediction.setModelPath(os.path.join(execution_path, "DenseNet-BC-121-32.h5")) prediction.loadModel() def predict_img(video_id): predictions, probabilities = prediction.predictImage(path+'/imagiai/images/' + video_id +'.jpg', result_count=3) predict_list.append([[video_id], [predictions]]) # 処理 download_list = [] for video_name in download_data[0]: download_list.append(video_name) predict_list = [] for download in download_list:
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") ) # Download the model via this link https://github.com/OlafenwaMoses/ImageAI/releases/tag/1.0 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=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("-----------------------")
# -*- coding: utf-8 -*- """ Created on Thu Sep 12 17:22:49 2019 @author: guest """ from imageai.Prediction import ImagePrediction import os import pandas as pd import numpy as np from PIL import Image execution_path = os.getcwd() pred_array = np.empty((0, 6), dtype=object) prediction = ImagePrediction() prediction.setModelTypeAsSqueezeNet() prediction.setModelPath( os.path.join( execution_path, "/home/guest/Documents/Test1/ImageAI-master/imageai/Prediction/Weights/SqueezeNet.h5" )) prediction.loadModel() TEST_PATH = '/home/guest/Documents/Aikomi' for img in os.listdir(TEST_PATH): if img.endswith('.jpg'): image = Image.open(os.path.join(TEST_PATH, img)) image = image.convert("RGB") image = np.array(image, dtype=np.uint8) # It should probably be img instead of "5.jpg"
def load_models(model_file): prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(model_file) prediction.loadModel() return prediction
from imageai.Prediction import ImagePrediction prediction_obj = ImagePrediction() # prediction class prediction_obj.setModelTypeAsResNet() prediction_obj.setModelPath( "models/resnet50_weights_tf_dim_ordering_tf_kernels.h5") prediction_obj.loadModel() prediction_objs, percentage_probabilities = prediction_obj.predictImage( "inputs/test.jpeg", result_count=3) # result count gives the number of items we wish to spot in the image. Most common case would be the top 3 items range is 1-1000 for index in range(len(prediction_objs)): print(prediction_objs[index], " : ", percentage_probabilities[index])
from imageai.Prediction import ImagePrediction import os import threading execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) picturesfolder = os.environ["USERPROFILE"] + "\Pictures\"" allfiles = os.listdir(picturesfolder) class PredictionThread(threading.Thread): def init(self): threading.Thread.init(self) def run(self): prediction.loadModel() for eachPicture in allfiles: if eachPicture.endswith(".png") or eachPicture.endswith(".jpg"): predictions, percentage_probabilities = prediction.predictImage( picturesfolder + eachPicture, result_count=1) for prediction, percentage_probability in zip(predictions, probabilities): print(prediction, " : ", percentage_probability)
from imageai import Prediction from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() # print(execution_path) prediction = ImagePrediction() prediction.setModelTypeAsMobileNetV2() # decide what model we're going to use prediction.setModelPath(os.path.join(execution_path, "mobilenet_v2.h5")) prediction.loadModel() predictions, probabilities = prediction.classifyImage(os.path.join( execution_path, "house.jpg"), result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
from imageai.Prediction import ImagePrediction from os import getcwd, path execution_path = getcwd() #test prediction = ImagePrediction() prediction.setModelTypeAsDenseNet() prediction.setModelPath(path.join(execution_path, "DenseNet-BC-121-32.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(path.join( execution_path, "giraffe.jpg"), result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() #gets current working directory inputimage = input("enter name of image you want to predict :") count = input("enter the number of predictions you want :") prediction = ImagePrediction() prediction.setModelTypeAsSqueezeNet() #you can use different models prediction.setModelPath( os.path.join(execution_path, "squeezenet_weights_tf_dim_ordering_tf_kernels.h5") ) #instead of this input the filename of your model prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join( execution_path, inputimage), count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( os.path.join(execution_path, "models/resnet50_weights_tf_dim_ordering_tf_kernels.h5") ) #"DenseNet-BC-121-32.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join( execution_path, "images/B005tqTBqrY.jpg"), result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
from imageai.Prediction import ImagePrediction import os execution_path = os.getcwd() prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath(execution_path + r"\resnet50_weights_tf_dim_ordering_tf_kernels.h5") prediction.loadModel() def predict(img): try: predictions, percentage_probabilities = prediction.predictImage( execution_path + r"\{}".format(img), result_count=5) except: predictions, percentage_probabilities = prediction.predictImage( "{}".format(img), result_count=5) for index in range(len(predictions)): print(predictions[index], " : ", percentage_probabilities[index]) # predict("sample.jfif")
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
detector = ObjectDetection() #выполн. задачи обнаружения объекта, используя предварительно обученную модель «RetinaNet» detector.setModelTypeAsRetinaNet() # указываю путь к загруженному файлу модели detector.setModelPath(os.path.join(exec_path, "resnet50_coco_best_v2.0.1.h5")) #функция загружает модель из пути, указанного в вызове функции выше, в экземпляр обнаружения объекта detector.loadModel() #функция, которая выполняет задачу обнаружения объекта после загрузки модели list = detector.detectObjectsFromImage( input_image=os.path.join(exec_path, "1.jpg"), output_image_path=os.path.join(exec_path, "new_objects.jpg"), minimum_percentage_probability=60, display_percentage_probability=True, display_object_name=True, #извлекает и сохраняет каждый обьект обнаруженный на изображении extract_detected_objects=False) #Классы прогнозирования prediction = ImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath( os.path.join(exec_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5")) prediction.loadModel() predictions, probabilities = prediction.predictImage(os.path.join( exec_path, "Картинка.jpg"), result_count=5) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
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)