Skip to content

Instantly share code, notes, and snippets.

@AlexeyGy
Last active May 29, 2019 13:35
Show Gist options
  • Save AlexeyGy/291e8bbe6412bf589f0a73035658db70 to your computer and use it in GitHub Desktop.
Save AlexeyGy/291e8bbe6412bf589f0a73035658db70 to your computer and use it in GitHub Desktop.
import os
from flask import Flask, flash, request, redirect, url_for, jsonify
import util_mobilnet
import numpy as np
import os, cv2
from flask_cors import CORS
# settings
UPLOAD_FOLDER = '.'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
net = cv2.dnn.readNet('models/no_bn.xml', 'models/no_bn.bin')
# set the target for the computation to the NCS2
net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)
# flask configuration
app = Flask(__name__)
app.secret_key = "super secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# wrapper needed to prevent cross-scripting permission issues
CORS(app)
# file name filter
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
#processing of picture
@app.route('/', methods=['POST'])
def upload_file():
if request.method == 'POST':
image = request.files['file']
threshold = float(request.form.getlist('text')[0]);
nparr = np.fromstring(image.read(), np.uint8)
img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # cv2.IMREAD_COLOR in OpenCV 3.1
res = process(img_np, net, threshold)
return jsonify(res)
def process(image, net, threshold):
image_processed = util_mobilnet.preprocess(image)
net.setInput(image_processed)
outputs = net.forward()
return util_mobilnet.postprocess(image, outputs, threshold)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment