def classification(request): if request.method == 'POST' and request.FILES['myfile']: my_file = request.FILES['myfile'] fs = FileSystemStorage() filename = fs.save(my_file.name, my_file) img_file = fs.url(filename) # Img is a PIL image of size 224*224 img_file = settings.BASE_DIR + '/' + img_file img = image.load_img(img_file, target_size=(224, 224)) # Converting img to a numpy array --> `img_as_a_numpy_array` is a float32 Numpy array of shape (224, 224, 3) img_as_a_numpy_array = image.img_to_array(img) # Transforming Array into Batch of size (1,244,244,3) -> by adding extra dim img_as_a_numpy_array = np.expand_dims(img_as_a_numpy_array, axis=0) # Image Pre_processing img_as_a_numpy_array = preprocess_input( img_as_a_numpy_array) # Channel-Wise color normalization # Applying Model (Pre_trained Model) model = VGG16(weights='imagenet', include_top=True) # Making Predictions on passed image (img_as_a_numpy_array) predictions = model.predict(img_as_a_numpy_array) print('Predicted:', decode_predictions(predictions, top=3)[0]) prediction = decode_predictions(predictions, top=1)[0][0][1] # Returning Final output to the frond end return render(request, 'App_1_CV_Images/classification.html', { 'original_img': img_file, 'prediction': prediction }) return render(request, 'App_1_CV_Images/classification.html')
def classification(): menu = { 'home': False, 'rgrs': False, 'stmt': False, 'clsf': True, 'clst': False, 'user': False } if request.method == "GET": return render_template('classification.html', menu=menu) else: try: f = request.files['image'] filename = os.path.join( app.root_path, 'static/images/Uploads/') + secure_filename( f.filename) f.save(filename) img = np.array(Image.open(filename).resize((224, 224))) yhat = vgg.predict(img.reshape(-1, 224, 224, 3)) label_key = np.argmax(yhat) label = decode_predictions(yhat) label = label[0][0] except: return render_template('classification.html', menu=menu) return render_template('cla_result.html', menu=menu, filename=secure_filename(f.filename), name=label[1], pct='%.2f' % (label[2] * 100))
def result(): if request.method == "POST": # ファイルの存在と形式を確認 if "file" not in request.files: print("File doesn't exist!") return redirect(url_for("index")) file = request.files["file"] if not allowed_file(file.filename): print(file.filename + ": File not allowed!") return redirect(url_for("index")) # ファイルの保存 if os.path.isdir(UPLOAD_FOLDER): shutil.rmtree(UPLOAD_FOLDER) os.mkdir(UPLOAD_FOLDER) filename = secure_filename(file.filename) # ファイル名を安全なものに filepath = os.path.join(UPLOAD_FOLDER, filename) file.save(filepath) # 予測 img = image.load_img(filepath, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) preds = model.predict(preprocess_input(x)) results = decode_predictions(preds, top=10)[0] # for result in results: print(results) return render_template("result.html", result=results, filepath=filepath) else: return redirect(url_for("index"))
def image(): if request.method == 'GET': return render_template('advanced/image.html', menu=menu, weather=get_weather()) else: f_img = request.files['image'] file_img = os.path.join(current_app.root_path, 'static/upload/') + f_img.filename f_img.save(file_img) current_app.logger.debug(f"{f_img.filename}, {file_img}") img = np.array(Image.open(file_img).resize((224, 224))) ''' img = cv2.imread(file_img, -1) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (224, 224)) ''' yhat = resnet.predict(img.reshape(-1, 224, 224, 3)) label = decode_predictions(yhat) label = label[0][0] mtime = int(os.stat(file_img).st_mtime) return render_template('advanced/image_res.html', menu=menu, weather=get_weather(), name=label[1], prob=np.round(label[2] * 100, 2), filename=f_img.filename, mtime=mtime)
def predict(filename, featuresize): img = image.load_img(filename, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) preds = model.predict(preprocess_input(x)) results = decode_predictions(preds, top=featuresize)[0] return results
def home(): data = request.files['fileToUpload'] b = BytesIO(data.read()) im = Image.open(b).convert('RGB') img = im.resize((224, 224), Image.ANTIALIAS) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) ##process the image and decode the predictions into a vector model = ResNet50() preds = model.predict(x) listPreds = decode_predictions(preds, top=4)[0] predictionVector = np.array( (listPreds[0][2], listPreds[1][2], listPreds[2][2], listPreds[3][2])) returnString = np.array( (listPreds[0][1], listPreds[1][1], listPreds[2][1])) print(returnString) print(predictionVector) X = pca.transform(np.array([predictionVector]).reshape(1, -1)) finalPredict = ourModel.predict(X) toReturn = "" if finalPredict > 0.5: toReturn = "pure," + str(returnString[0]) else: toReturn = "mixed," + str(returnString) return json.dumps(str(toReturn))
def getpredictions(filename): imgpath = 'Uploaded_temp/' + filename img = load_img(imgpath, target_size=(224, 224)) img = img_to_array(img) img = img.reshape((1, img.shape[0], img.shape[1], img.shape[2])) img = preprocess_input(img) predictions = model.predict(img) prediction_result = decode_predictions(predictions, top=1) return prediction_result[0][0]
def visualise_heatmaps(): """ This function uses the VGG16 network and processes an images stored in the project folder of 2 African Elephants. The intent of the code is to process the image and then determine the gradient of the African Elephant class w.r.t the last convolutional layer of the network. I couldn't get the overlay section working as many of the libraries used in the book have since been outdated. :return: None """ model = VGG16(weights='imagenet') img_path = 'C:\\Users\\owatkins\\OneDrive - Analog Devices, Inc\\Documents\\Project Folder\\Tutorials and ' \ 'Courses\\Deep Learning with Python\\African-Elephant-With-Baby.jpg' # Load the image and plot it img = image.load_img(img_path, target_size=(224, 224)) plt.imshow(img) plt.show() # Convert the image to a numpy array and reshape x_arr = image.img_to_array(img) x = np.expand_dims(x_arr, axis=0) x = preprocess_input(x) # Perform the forward pass using the VGG16 network. Print the top 3 predictions and their probabilities, as well # as the index of the most prominent output. preds = model.predict(x) print('Predictions: ', decode_predictions(preds, top=3)[0]) idx_of_pred = np.argmax(preds[0]) print('Index of the max prediction: ', idx_of_pred) # African Elephant Entry in the prediction vector, and the output feature map of the last convolutional layer african_elephant_output = model.output[:, 386] last_conv_layer = model.get_layer('block5_conv3') # Gradient of the African Elephant class w.r.t. the output feature map. Each of the (512, ) entries from # pooled_grads is the mean intensity of the gradient over a specific feature map-channel grads = K.gradients(african_elephant_output, last_conv_layer.output)[0] pooled_grads = K.mean(grads, axis=(0, 1, 2)) # Function to let you access the values of the quantities just defined: pooled_grads and the output feature map # of block5_conv3 given the input image iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]]) pooled_grads_value, conv_layer_output_value = iterate([x]) # Multiply each channel in the feature map byt "how important" that channel is w.r.t. the elephant class for i in range(512): conv_layer_output_value[:, :, i] *= pooled_grads_value[i] # Compute the channel-wise mean of the resulting heatmap of class activation and plot. heatmap = np.mean(conv_layer_output_value, axis=-1) heatmap = np.maximum(heatmap, 0) heatmap /= np.max(heatmap) plt.matshow(heatmap) plt.show()
def labels_and_probabilities(self, image): """ Returns labels and self.net's corresponding estimated probabilities. :param image: image for which probabilities should be estimated. :return: list of labels and list of corresponding probabilities. """ predictions = [(label, probability) for _, label, probability in decode_predictions(self.net.predict(image), top=5)[0]] labels = [a for a, _ in predictions] probabilities = [b for _, b in predictions] return labels, probabilities
async def category(self, ctx): # 関数名=コマンド名 '''送られた画像の分類''' for attach in ctx.message.attachments: if attach.url.endswith(("png", "jpg", "jpeg")): data = await attach.read() image = cv2.imdecode(np.array(data), 1) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.resize(image, dsize=(32, 32)) image = np.expand_dims(image / 255.0, axis=0) result = model.predict(image) result = decode_predictions(result, top=5)[0] await ctx.send(f'{result}')
def predict(item_no): with session.as_default(): with session.graph.as_default(): res = {} image = load_img('data/'+item_no+'.jpg', target_size=(224, 224)) image = img_to_array(image) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) y = model.predict(image) label = decode_predictions(y)[0][:3] res['top3'] = [(i[1], round(i[2]*100, 2)) for i in label] return res
def predict(file): img = image.load_img(file, target_size=(224, 224)) img = image.img_to_array(img) img = img.reshape((1, img.shape[0], img.shape[1], img.shape[2])) img = preprocess_input(img) yhat = model.predict(img) label = decode_predictions(yhat) label = label[0][0] result = '%s' % (label[1]) if result == "hotdog": print(result + "!") else: print("not hotdog!")
def array2predict(self, x): # 3次元テンソル(rows, cols, channels) を # 4次元テンソル (samples, rows, cols, channels) に変換 # 入力画像は1枚なのでsamples=1でよい x = np.expand_dims(x, axis=0) # Top-5のクラスを予測する # VGG16の1000クラスはdecode_predictions()で文字列に変換される preds = self.model.predict(preprocess_input(x)) results = decode_predictions(preds, top=5)[0] return results
def vgg16_standard_classifier(image): """ Classifies images based on the 1000 outputs VGG16 was designed to classify. """ # load the model model = VGG16() print(model.summary()) # predict the probability across all output classes yhat = model.predict(image) # convert the probabilities to class labels label = decode_predictions(yhat) return label[0]
def classify(): original = Image.open(image_data) original = original.resize((224, 224), Image.ANTIALIAS) numpy_image = img_to_array(original) image_batch = np.expand_dims(numpy_image, axis=0) processed_image = vgg16.preprocess_input(image_batch.copy()) predictions = vgg_model.predict(processed_image) label = decode_predictions(predictions) table = tk.Label( frame, text="Top image class predictions and confidences").pack() for i in range(0, len(label[0])): result = tk.Label(frame, text=str(label[0][i][1]).upper() + ': ' + str(round(float(label[0][i][2]) * 100, 3)) + '%').pack()
def predict(): if request.method == 'POST': img = base64_to_pil(request.json) preds = model_predict(img, model) pred_proba = "{:.3f}".format(np.amax(preds)) # Max probability pred_class = decode_predictions(preds, top=1) # ImageNet Decode result = str(pred_class[0][0][1]) # Convert to string result = result.replace('_', ' ').capitalize() return jsonify(result=result, probability=pred_proba) return None
def get_image_description(image_path): """ uses VGG16 neural-network to get image description. the description will be used to generate more suited comments. :param image_path: media item to get description of :return: image description (string) """ im_array = img_to_array( load_img(image_path, color_mode="rgb", target_size=(224, 224))) shape = (1, ) + im_array.shape image_data = preprocess_input(im_array.reshape(shape)) prediction = VGG16().predict(image_data) labels = decode_predictions(prediction) labels = [(label[1], label[2]) for label in labels[0]] print("description confident=", labels[0][1]) return (labels[0][0]).replace("_", " ")
def predict(): #get the image from the post request and open it with pillow picture = request.form.get("content").split(",")[1] decoded = base64.b64decode(picture) image = Image.open(io.BytesIO(decoded)).convert("RGB") # Make prediction preds = model_predict(image, model) # Process your result for human # pred_class = preds.argmax(axis=-1) # Simple argmax pred_class = decode_predictions(preds, top=1) # ImageNet Decode result = str(pred_class[0][0][1]) # Convert to string confidence = str(pred_class[0][0][2]) return '{} {}'.format(result, confidence)
def predict(image1): model = VGG16() image = load_img(image1, target_size=(224, 224)) # convert the image pixels to a numpy array image = img_to_array(image) # reshape data for the model image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # prepare the image for the VGG model image = preprocess_input(image) # predict the probability across all output classes yhat = model.predict(image) # convert the probabilities to class labels label = decode_predictions(yhat) # retrieve the most likely result, e.g. highest probability label = label[0][0] return label
def predict(image1): model = VGG16() image = load_img(image1, target_size=(224, 224)) # pixels -> numpy array image = img_to_array(image) # reshape data image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # prepare for VGG model image = preprocess_input(image) # predict yhat = model.predict(image) # convert label = decode_predictions(yhat) # retrieve result (hotdog or not hotdog) label = label[0][0] return label identify(label)
def do_inference(mdl, img): """ Perform inference on input image, return label with highest score Parameters ---------- mdl: keras.model vgg16 img: image array of shape (1,224,224) Returns ------- label: string """ labels = decode_predictions(mdl.predict(img)) result = [] for i in range(5): result.append(labels[0][i][1]) return result
def generatePredictions(outputArray, path, first, localLen, localModel): i = 0 #for each image path for img_path in path: #load in the image img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) #process the image and decode the predictions into a vector preds = localModel.predict(x) listPreds = decode_predictions(preds, top=4)[0] predictionVector = np.array((listPreds[0][2],listPreds[1][2],listPreds[2][2],listPreds[3][2])) #insert into training matrix, first half is pure second half is mixed if not first: outputArray[i+int(localLen/2)] = predictionVector else: outputArray[i] = predictionVector i+=1 if i == int(localLen/2): return outputArray
def home(): #receive the file data = request.files['fileToUpload'] #convert to buffer stream b = BytesIO(data.read()) #convert rgba to rgb im = Image.open(b).convert('RGB') #resize the image img = im.resize((224, 224), Image.ANTIALIAS) #prepreocess the input (to np array) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) listPreds = decode_predictions(preds, top=4)[0] predictionVector = np.array((listPreds[0][2],listPreds[1][2],listPreds[2][2],listPreds[3][2])) returnString = np.array((listPreds[0][1],listPreds[1][1],listPreds[2][1])) print(returnString) print(predictionVector) X = pca.transform(np.array([predictionVector]).reshape(1, -1)) finalPredict = ourModel.predict(X) url0 = "https://ltrinity.w3.uvm.edu/cs148/animalsoundquiz/photos/" + str(returnString[0]) + ".jpg" url1 = "https://ltrinity.w3.uvm.edu/cs148/animalsoundquiz/photos/" + str(returnString[1]) + ".jpg" string0 = str(returnString[0]).replace('_',' ') string1 = str(returnString[1]).replace('_',' ') rendered = render_template('phpTemplatePure.html', \ title = "My Generated Page", \ breeds = [{"url":url0, "name": string0}]) if finalPredict < 0.5: rendered = render_template('phpTemplateMixed.html', \ title = "My Generated Page", \ breeds = [{"url":url0, "name": string0},{"url":url1,"name": string1}]) return(rendered)
def predict(): if request.method == 'POST': # Get the image from post request img = base64_to_pil(request.json) # Save the image to ./uploads # img.save("./uploads/image.png") # Make prediction preds = model_predict(img, model) # Process your result for human pred_proba = "{:.3f}".format(np.amax(preds)) # Max probability pred_class = decode_predictions(preds, top=1) # ImageNet Decode result = str(pred_class[0][0][1]) # Convert to string result = result.replace('_', ' ').capitalize() # Serialize the result, you can add additional fields return jsonify(result=result, probability=pred_proba) return None
def predictPic(self, url): # load and resize image resp = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}) im = Image.open(BytesIO(resp.content)) im = im.resize((224, 224)) if im.mode != 'RGB': im = im.convert('RGB') #Convert Image to Numpy Array x = image.img_to_array(im) x = np.expand_dims(x, axis=0) print(x.shape) #Place image into outer-array layer (Required by model) #Pre-process x = preprocess_input(x) preds = self.model.predict(x) predictions = pd.DataFrame(decode_predictions(preds, top=3)[0], columns=['col1', 'category', 'probability']).iloc[:, 1:] #print('predicted class:', predictions.loc[0,'category']) return predictions.loc[0, 'category']
def predict(): message = request.get_json(force=True) encoded = message['image'] decoded = base64.b64decode(encoded) image = Image.open(io.BytesIO(decoded)) if image.mode != "RGB": image = image.convert("RGB") image = image.resize((224, 224)) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = preprocess_input(image) yhat = model.predict(image) labels = decode_predictions(yhat) prd1 = labels[0][0] prd2 = labels[0][1] prd3 = labels[0][2] prd4 = labels[0][3] prd5 = labels[0][4] response = { 'prediction': { 'prd1': prd1[1], 'prd1_v': prd1[2] * 100, 'prd2': prd2[1], 'prd2_v': prd2[2] * 100, 'prd3': prd3[1], 'prd3_v': prd3[2] * 100, 'prd4': prd4[1], 'prd4_v': prd4[2] * 100, 'prd5': prd5[1], 'prd5_v': prd5[2] * 100, } } return jsonify(response)
def detect(Image): from tensorflow.keras.preprocessing.image import load_img from tensorflow.keras.preprocessing.image import img_to_array from tensorflow.keras.applications.vgg16 import preprocess_input from tensorflow.keras.applications.vgg16 import decode_predictions model = SingleTone.getInstance() # load an image from file image = load_img(Image, target_size=(224, 224)) # convert the image pixels to a numpy array image = img_to_array(image) # reshape data for the model image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # prepare the image for the VGG model image = preprocess_input(image) # predict the probability across all output classes yhat = model.model.predict(image) # convert the probabilities to class labels label = decode_predictions(yhat) # retrieve the most likely result, e.g. highest probability label = label[0][0] # print the classification result = '%s (%.2f%%)' % (label[1], label[2]*100) # display(Image, result) return result
arr_face5 = preprocess_input(arr_face5) arr_face6 = preprocess_input(arr_face6) arr_input = np.stack( [arr_face1, arr_face2, arr_face3, arr_face4, arr_face5, arr_face6]) print(arr_input.shape) # (4, 224, 224, 3) # 2. 모델 구성 model = VGG16() results = model.predict(arr_input) print(results) print('results.shape : ', results.shape) # results.shape : (4, 1000) # 이미지 결과 확인 from tensorflow.keras.applications.vgg16 import decode_predictions results = decode_predictions(results, top=3) print("===========================================") print("dog1 : ", results[0]) print("===========================================") print("cat1 : ", results[1]) print("===========================================") print("lion1 : ", results[2]) print("===========================================") print("suit1 : ", results[3]) print("===========================================") # =========================================== # dog1 : [('n02111889', 'Samoyed', 0.6032323), ('n02114548', 'white_wolf', 0.32809937), ('n02104029', 'kuvasz', 0.01873776)] # =========================================== # cat1 : [('n02124075', 'Egyptian_cat', 0.59878814), ('n02123045', 'tabby', 0.22296095), ('n02123159', 'tiger_cat', 0.12437381)] # =========================================== # lion1 : [('n03291819', 'envelope', 0.2933515), ('n04548280', 'wall_clock', 0.09355649), ('n02708093', 'analog_clock', 0.050705183)]
# Image Classification import numpy as np from tensorflow.keras.preprocessing import image from tensorflow.keras.applications import vgg16, resnet50, inception_v3 # Load Model model = vgg16.VGG16(weights='imagenet') # Load test data img_path = 'images/cat.jpg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = vgg16.preprocess_input(x) # Model Predict preds = model.predict(x) # Print Prediction # decode the results into a list of tuples (class, description, probability) dec_preds = vgg16.decode_predictions(preds, top=3)[0] print('Predicted:', dec_preds) for item in dec_preds: print(item[1], item[2])
# Load an image in a PIL format original = load_img(filename, target_size=(224, 224)) numpy_image = img_to_array(original) # We add the extra dimension to the axis 0 image_batch = np.expand_dims(numpy_image, axis=0) print('image batch size', image_batch.shape) plt.imshow(np.uint8(image_batch[0])) ### Now we can convert the image for the model and try and predict it # preparing image processed_img = vgg16.preprocess_input(image_batch.copy()) # get the predicted proba predictions = vgg_model.predict(processed_img) # decode and print prediction def get_label(predicted_labels): prob = 0 res = '' for count, prediction in enumerate(predicted_labels[0]): if prediction[2] > prob: prob = prediction[2] res = prediction[1] return res label = vgg16.decode_predictions(predictions) print(f"this is a {get_label(label)}")