-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv_web_app.py
executable file
·66 lines (50 loc) · 1.67 KB
/
cv_web_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from flask import Flask,render_template,request
import numpy as np
import cv2
import base64
import sys
import json
# Define App
app = Flask(__name__,template_folder="templates")
# The home page is routed to index.html inside
@app.route('/')
def index():
return render_template('index.html')
# Load Digit Recogniztion model
net = cv2.dnn.readNetFromONNX('model.onnx')
# Implements softmax function
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
return np.exp(x) / np.sum(np.exp(x), axis=0)
# Handles uploaded image
@app.route('/upload',methods=["POST"])
def upload():
# Get uploaded form
d = request.form
# Extract the data field
data = d.get('data')
# The first part of the string simply indicates
# what kind of file it is. So we extract only the data part.
data = data.split(',')[1]
# Get base64 decoded
data = base64.decodebytes(data.encode())
# Convert to numpy array
nparr = np.frombuffer(data, np.uint8)
# Read image
img = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
cv2.imwrite("/tmp/test.jpg", img)
# Create a 4D blob from image
blob = cv2.dnn.blobFromImage(img, 1/255, (28, 28))
# Run a model
net.setInput(blob)
out = net.forward()
# Get a class with a highest score
out = softmax(out.flatten())
classId = np.argmax(out)
confidence = out[classId]
# Print results on the server side
print("classId: {} confidence: {}".format(classId, confidence), file=sys.stdout)
# Return result as a json object
return json.dumps({'success':True, 'class': int(classId), 'confidence': float(confidence)}), 200, {'ContentType':'application/json'}
if __name__ == '__main__':
app.run(debug = True)