def predict(image_path): """Use Xception to label image""" img = image.load_img(image_path, target_size=image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = model.predict(x) plt.imshow(img) print('Predicted:', decode_predictions(predictions, top=1)[0]) return decode_predictions(predictions, top=1)[0]
def upload_file(): data = {"success": False} if request.method == 'POST': print(request) if request.files.get('file'): # read the file file = request.files['file'] # read the filename filename = file.filename # create a path to the uploads folder filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) print("HELP!") file.save(filepath) # Load the saved image using Keras and resize it to the Xception # format of 299x299 pixels image_size = (299, 299) im = keras.preprocessing.image.load_img(filepath, target_size=image_size, grayscale=False) # preprocess the image and prepare it for classification image = prepare_image(im) global graph with graph.as_default(): preds = model.predict(image) results = decode_predictions(preds) data["predictions"] = [] print(decode_predictions(preds, top=3)[0]) # loop over the results and add them to the list of # returned predictions for (imagenetID, label, prob) in results[0]: r = {"label": label, "probability": float(prob)} data["predictions"].append(r) # indicate that the request was a success data["success"] = True print(data) # {'success': True, 'predictions': [{'label': 'redbone', 'probability': 0.6793243885040283}, {'label': 'golden_retriever', 'probability': 0.016980241984128952}, {'label': 'black-and-tan_coonhound', 'probability': 0.015220019035041332}, {'label': 'basset', 'probability': 0.012955794110894203}, {'label': 'Chesapeake_Bay_retriever', 'probability': 0.012856719084084034}]} # [('n02090379', 'redbone', 0.6793244), ('n02099601', 'golden_retriever', 0.016980242), ('n02089078', 'black-and-tan_coonhound', 0.015220019)] predictions = [{ "breed": data["predictions"][0]['label'], "id": 1 }, { "breed": data["predictions"][1]['label'], "id": 2 }] return render_template("results.html", data=predictions) return render_template("index.html")
def predict(image_path): """Use Xception to label image""" global graph with graph.as_default(): image_size = (299, 299) img = image.load_img(image_path, target_size=image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = model.predict(x) print('Predicted:', decode_predictions(predictions, top=1)[0]) return decode_predictions(predictions, top=1)[0]
def upload_file(): data = {"success": False} if request.method == 'POST': # print(request.method) print(request) print('posted') if request.files.get('file'): # read the file file = request.files['file'] # read the filename filename = file.filename # create a path to the uploads folder filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) print('HELP!') file.save(filepath) # Load the saved image using Keras and resize it to the Xception # format of 299x299 pixels image_size = (299, 299) im = keras.preprocessing.image.load_img(filepath, target_size=image_size, grayscale=False) # preprocess the image and prepare it for classification image = prepare_image(im) global graph with graph.as_default(): preds = model.predict(image) results = decode_predictions(preds) data["predictions"] = [] print(decode_predictions(preds, top=3)[0]) # loop over the results and add them to the list of # returned predictions for (imagenetID, label, prob) in results[0]: r = {"label": label, "probability": float(prob)} data["predictions"].append(r) print(data) # indicate that the request was a success data["success"] = True predictions = [ {"breed": data["predictions"][0]['label'], "id": 1}, {"breed": data["predictions"][1]['label'], "id": 2} ] return render_template("results.html", data=predictions) return render_template("index.html")
def upload_file(): data = {"success": False} if request.method == 'POST': if request.files.get('file'): # read the image in PIL format file = request.files['file'] image = file.read() image = Image.open(io.BytesIO(image)) # preprocess the image and prepare it for classification image = prepare_image(image) global graph with graph.as_default(): preds = model.predict(image) results = decode_predictions(preds) data["predictions"] = [] # loop over the results and add them to the list of # returned predictions for (imagenetID, label, prob) in results[0]: r = {"label": label, "probability": float(prob)} data["predictions"].append(r) # indicate that the request was a success data["success"] = True return jsonify(data) return '''
def xception_process(image_path, k=5, threshold=None): """ Extracts returns the k top predictions. :param image_path: :param k: :return: """ img = image.load_img(image_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = xception_model.predict(x) preds = decode_predictions(preds, top=k)[0] if threshold is not None: preds = [ dict(desc=p[1], prob=float(p[2])) for p in preds if p[2] >= threshold ] else: preds = [dict(desc=p[1], prob=float(p[2])) for p in preds] # decode the results into a list of tuples (class, description, probability) # (one such list for each sample in the batch) return preds
def predict(self, images, top=None): if top is None: top = self.num_classes images = preprocess_input(images) predictions = self.model.predict(images) labels = decode_predictions(predictions, top=top) return labels
def predict(): # initialize the data dictionary that will be returned from the # view data = {"success": False} # ensure an image was properly uploaded to our endpoint if flask.request.method == "POST": if flask.request.files.get("image"): # read the image in PIL format image = flask.request.files["image"].read() image = Image.open(io.BytesIO(image)) # preprocess the image and prepare it for classification image = prepare_image(image, target=(224, 224)) # classify the input image and then initialize the list # of predictions to return to the client preds = model.predict(image) results = decode_predictions(preds) data["predictions"] = [] # loop over the results and add them to the list of # returned predictions for (imagenetID, label, prob) in results[0]: r = {"label": label, "probability": float(prob)} data["predictions"].append(r) # indicate that the request was a success data["success"] = True # return the data dictionary as a JSON response return flask.jsonify(data)
def upload_file(): data = {"success": False} if request.method == 'POST': if request.files.get('file'): file = request.files['file'] filename = file.filename filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) # Load the saved image using Keras. # Resize it to the Xception format of 299x299 pixels. image_size = (299, 299) im = keras.preprocessing.image.load_img(filepath, target_size=image_size, grayscale=False) # Preprocess the image and prepare it for classification. image = prepare_image(im) global graph with graph.as_default(): preds = model.predict(image) results = decode_predictions(preds) data["predictions"] = [] for (imagenetID, label, prob) in results[0]: r = {"label": label, "probability": float(prob)} data["predictions"].append(r) # indicate that the request was a success data["success"] = True return jsonify(data) return '''
def upload_file(): data = {"success": False} if request.method == 'POST': if request.files.get('file'): #read input file file = request.files['file'] #read filename filename = file.filename #create os path to uploads directory filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) #load the saved image and resize to the Xception 299x299 pixels image_size = (299, 299) im = keras.preprocessing.image.load_img(filepath, target_size=image_size, grayscale=False) #preprocess the image for classification image = prepare_image(im) global graph with graph.as_default(): preds = model.predict(image) res = decode_predictions(preds) #print the res print(res) data["predictions"] = [] #loop over the results and add to returned predictions for (imagenetID, label, prob) in res[0]: r = {"label": label, "probability": float(prob)} data["predictions"].append(r) #store boolean for process success data["success"] = True return jsonify(data) return '''
def callback(image_msg): #First convert the image to OpenCV image cv_image = bridge.imgmsg_to_cv2(image_msg, desired_encoding="passthrough") cv_image = cv2.resize(cv_image, target_size) # resize image #Segment image ################################################## #""" original = cv_image.copy() gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (3, 3), 0) canny = cv2.Canny(blurred, 120, 255, 1) kernel = np.ones((15,15),np.uint8) dilate = cv2.dilate(canny, kernel, iterations=1) # Find contours cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if len(cnts) == 2 else cnts[1] #catch multiple objects in image objects=[] for c in cnts: x,y,w,h = cv2.boundingRect(c) cv2.rectangle(original, (x, y), (x + w, y + h), (36,255,12), 2) #here ROI stores last object only ROI = original[y:y+h, x:x+w] objects.append(ROI) cv2.imshow('Original', cv_image) cv2.imshow('Contours', original) cv2.imshow('extracted', objects[0]) #""" ############################################################ #np_image = np.asarray(cv_image) np_image = np.asarray(objects[0]) # read as np array np_image = np.expand_dims(np_image, axis=0) # Add another dimension for tensorflow np_image = np_image.astype(float) # preprocess needs float64 and img is uint8 np_image = preprocess_input(np_image) # Regularize the data global graph # This is a workaround for asynchronous execution with graph.as_default(): preds = model.predict(np_image) # Classify the image pred_string = decode_predictions(preds, top=20)[0] # Decode top 1 predictions if pred_string[0][2] >0.1: msg_string.data = pred_string[0][1] pub.publish(msg_string) print(pred_string[0][1]) print(pred_string[0][2]) print('--------------------') else: print('No Object detected') cv2.waitKey(2000)
def predict(image): model = Xception() pred = model.predict(image) decoded_predictions = decode_predictions(pred, top=10) response = 'Xception predictions: ' + str(decoded_predictions[0][0:5]) print(response) np.argmax(pred[0]) return response
def upload_file(): display = "" imagefile = "" data = {"success": False} if request.method == 'POST': if 'file' not in request.files: # flash('No file part') return redirect(request.url) file = request.files['file'] # if user does not select file, browser also # submit a empty part without filename if file.filename == '': return redirect(request.url) if file and allowed_file(file.filename): # create a path to the uploads folder filename = secure_filename(file.filename) filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) # Load the saved image using Keras and resize it to the Xception # format of 299x299 pixels image_size = (299, 299) im = keras.preprocessing.image.load_img(filepath, target_size=image_size, grayscale=False) # preprocess the image and prepare it for classification image = prepare_image(im) # imagefile = "Uplaods/"+filename.replace('\\', '/') imagefile= filename global graph with graph.as_default(): preds = model.predict(image) results = decode_predictions(preds) data["predictions"] = [] # loop over the results and add them to the list of # returned predictions for (imagenetID, label, prob) in results[0]: r = {"label": label, "probability": float(prob)} data["predictions"].append(r) # indicate that the request was a success data["success"] = True display = data["predictions"][0]["label"] return render_template("index.html",display=display,imagefile=imagefile)
def main(args): image_file = os.path.abspath(args.image_file) model = Xception(include_top=True, weights='imagenet') img = image.load_img(image_file, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) preds = model.predict(preprocess_input(x)) results = decode_predictions(preds, top=5)[0] for result in results: print(result)
def upload_file(): data = {"success": False} if request.method == 'POST': if request.files.get('img_file'): # read the file file = request.files['img_file'] target = request.form.get("lang_options") # read the filename filename = file.filename # create a path to the uploads folder filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) now = datetime.datetime.now() cd = cloudinary.uploader.upload(file, folder="rudsfinal") filepath = cd['url'] image_size = (299, 299) im = '' with urlopen(filepath) as img: im = keras.preprocessing.image.load_img(img, target_size=image_size, grayscale=False) # preprocess the image and prepare it for classification image = prepare_image(im) global graph with graph.as_default(): preds = model.predict(image) results = decode_predictions(preds) # print the results print(results) data["predictions"] = [] # loop over the results and add them to the list of # returned predictions for (imagenetID, label, prob) in results[0]: translation = translate_client.translate( label, target_language=target) r = { "label": label, "translation": translation["translatedText"] } data["predictions"].append(r) # indicate that the request was a success data["success"] = True return jsonify(data)
def predict(url): img = url_to_image(url) # image array # here to add some prep steps #img = image.load_img(url, target_size=(224, 224)) #x = image.img_to_array(img) img = np.expand_dims(img, axis=0) #img = preprocess_input(img) model = get_nnet() preds = model.predict(img) img = Image.fromarray(np.squeeze(img, axis=(0, )), 'RGB') return jsonify( {'predictions': str(decode_predictions(preds, top=1)[0][0][1])})
def upload_file(): data = {"success": False} if request.method == 'POST': # if request.files.get('file'): if request.files.get('file'): # read the file file = request.files['file'] # read the filename filename = file.filename # create a path to the uploads folder filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) # Load the saved image using Keras and resize it to the Xception # format of 299x299 pixels image_size = (299, 299) im = keras.preprocessing.image.load_img(filepath, target_size=image_size, grayscale=False) # preprocess the image and prepare it for classification image = prepare_image(im) global graph with graph.as_default(): preds = model.predict(image) print(preds) results = decode_predictions(preds) print(results) prediction = [] # loop over the results and add them to the list of # returned predictions for (imagenetID, label, prob) in results[0]: prediction.append(label) page = [] if prediction[0] == "broccoli": page.append("broccoli_low.html") elif prediction[0] == "cucumber": page.append("cucumber_low.html") else: page.append("butternut_squash_low.html") return render_template(page) return render_template("upload_img.html")
def Xpredict(image_path): """Use Xception to label image""" K.clear_session() load_Xmodel() img = image.load_img(image_path, target_size=Xception_image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) Xmodel._make_predict_function() global graph with graph.as_default(): predictions = Xmodel.predict(x) print(predictions) plt.imshow(img) return jsonify('Predicted:', decode_predictions(predictions, top=3)[0])
def predict(img_path, pred_threshold): img = image.load_img(img_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) # decode the results into a list of tuples (class, description, probability) # (one such list for each sample in the batch) # print('Predicted:', decode_predictions(preds, top=3)[0]) decoded_preds = decode_predictions(preds, top=3)[0] for pred in decoded_preds: class_, desc, prob = pred if prob > pred_threshold: print("|".join([img_path, desc, str(prob)]))
def upload_file(): data = {"success": False} if request.method == 'POST': if request.files.get('file'): # read the file file = request.files['file'] # read the filename filename = file.filename # create a path to the uploads folder filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) # Load the saved image using Keras and resize it to the Xception format of 299x299 pixels image_size = (299, 299) img = keras.preprocessing.image.load_img( filepath, target_size=image_size, grayscale=False ) # preprocess the image and prepare it for classification image = prepare_image(img) global graph with graph.as_default(): predictions = model.predict(image) results = decode_predictions(predictions) print(results) data['predictions'] = [] # loop over the results and add them to the list of # returned predictions for (imagenetID, label, prob) in results[0]: predictions_result = { "label": label, "probability": float(prob) } data['predictions'].append(predictions_result) # indicate that the request was a success data["success"] = True return jsonify(data) return '''
def Vpredict(image_path): """Use VGG19 to label image""" K.clear_session() load_Vmodel() img = image.load_img(image_path, target_size=VGG_image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) Vmodel._make_predict_function() with graph.as_default(): predictions = Vmodel.predict(x) print(predictions) plt.imshow(img) global prediction prediction = str(('Predicted:', decode_predictions(predictions, top=3)[0])) return prediction
def predict(image1): image = image1 size=(299,299) #for xception it expects 299x299 size image = ImageOps.fit(image, size) # 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 xception 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 upload(): if request.method == 'POST': # Get the file from post request f = request.files['file'] # Save the file to ./uploads basepath = os.path.dirname(__file__) file_path = os.path.join(basepath, 'uploads', secure_filename(f.filename)) f.save(file_path) # Make prediction preds = model_predict(file_path, model) pred_class = decode_predictions(preds, top=5) # ImageNet Decode result = str(pred_class[0][0][1]) # Convert to string return 'The model Xception predicts this image as : ' + result return None
def classify_image(url, top): model = Xception( weights='/home/xception_weights_tf_dim_ordering_tf_kernels.h5') img_raw = requests.get(url, stream=True).raw img = Image.open(img_raw) img = img.resize(size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) preds_top = decode_predictions(preds, top=top)[0] backend.clear_session() return {i[1]: str(i[2]) for i in preds_top}
def get_msg(msg_list, img_path): img = image.load_img(img_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) keywords = [pred[1] for pred in decode_predictions(preds, top=3)[0]] for k in keywords: if k not in image_mapping: continue msg_indexes = image_mapping[k] if len(msg_indexes) > 0: print('Predicted image as {' + ', '.join(keywords) + '}. Picked ' + k) i = random.choice(msg_indexes) if i < len(msg_list): return msg_list[i]
def prepare_img(picture_path): global model global graph data = {} output_size=(299,299) response = requests.get(picture_path) img = Image.open(BytesIO(response.content)) prepared_img = img_to_array(img) prepared_img = np.expand_dims(prepared_img, axis=0) prepared_img = preprocess_input(prepared_img) with graph.as_default(): preds = model.predict(prepared_img, verbose=1) results = decode_predictions(preds) data['predictions'] = [] for (imagenetID, label, prob) in results[0]: r = {"label": label, "probability": round(100*float(prob),2)} data['predictions'].append(r) return data, picture_path
def image_callback(channel_, method, properties, body): header = {"__TypeId__": "answer"} user_dict = json.loads(body.decode()) print(user_dict) down_url = user_dict.get("downUrl") img = requests.get(down_url).content img = BytesIO(img) img = image.load_img(img, target_size=(224, 224)) img_arr = np.expand_dims(image.img_to_array(img), axis=0) x = preprocess_input(img_arr) preds = model.predict(x) answer = decode_predictions(preds, top=3)[0] to_user = dict() to_user['answer'] = "{}".format(answer) to_user['toUser'] = user_dict.get("toUser") print(to_user) properties_1 = pika.BasicProperties(content_type="json", delivery_mode=2, headers=header, priority=0) channel_.basic_publish(exchange=setting.exchange_name, routing_key='answer.abc', body=json.dumps(to_user), properties=properties_1)
def postPicture(request): if not (request.method == 'POST'): return badMethodJson try: try: data = json.loads(request.body) except: return JsonResponse( { 'msg': 'No post data in request', 'status': 400 }, status=400) img2 = Image.open(BytesIO(base64.b64decode(data['data']))) print('image opened') if not 'fast' in data: img2 = img2.resize((299, 299)) print('resized') model = Xception(weights='imagenet') print('Xception run successfully') else: img2 = img2.resize((224, 224)) print('resized') model = MobileNetV2(weights='imagenet') print('MobileNet run successfully') x = image.img_to_array(img2) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) outcome = decode_predictions(preds, top=1)[0][0] if outcome[2] < 0.35: outcome = "nothing recognised" else: outcome = outcome[1] return JsonResponse({'outcome': str(outcome)}) except Exception as err: print(err) return JsonResponse({'err': 'Error occured. Please try again'}, status='400') finally: subprocess.run('heroku restart --app ufluent', shell=True)
def upload_file(): data = {"success": False} if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: return redirect(request.url) file = request.files['file'] # if user does not select file, browser also # submit a empty part without filename if file.filename == '': return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) filepath = os.path.join(app.config['UPLOAD_FOLDER']) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) # Preprocess image for model prediction # This step handles scaling and normalization for Xception global graph with graph.as_default(): img = image.load_img(os.path.join(app.config['UPLOAD_FOLDER'], file.filename), target_size=image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = model.predict(x) results = decode_predictions(predictions, top=3) # print(results) data["predictions"] = [] # loop over the results and add them to the list of # returned predictions for (xceptionID, label, prob) in results[0]: r = {"label": label, "probability": float(prob)} data["predictions"].append(r) # indicate that the request was a success data["success"] = True return jsonify(data) # return redirect(url_for('uploaded_file', # filename=filename)) return '''
def upload(): if request.method == 'POST': # Get the file from post request f = request.files['file'] # Save the file to ./uploads basepath = os.path.dirname(__file__) file_path = os.path.join( basepath, 'uploads', secure_filename(f.filename)) f.save(file_path) # Make prediction preds = model_predict(file_path, model) # Process your result for human # pred_class = preds.argmax(axis=-1) # Simple argmax pred_class = decode_predictions(preds, top=5) # ImageNet Decode result = str(pred_class[0][0][1]) # Convert to string return result return None