-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
executable file
·72 lines (55 loc) · 1.91 KB
/
Copy pathapi.py
File metadata and controls
executable file
·72 lines (55 loc) · 1.91 KB
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
67
68
69
70
from flask import Flask, Response,jsonify,request,render_template,url_for,flash,redirect
import base64
import pickle
import json
import numpy as np
import cv2
import time
import uuid
import shutil
# import urllibre
import os
from io import BytesIO
app = Flask(__name__)
app.config["CACHE_TYPE"] = "null"
# declare var
def save_image(image):
session_id = str(uuid.uuid1())
dirpath = os.path.join('static/data', session_id)
os.makedirs(dirpath)
inpath = os.path.join(dirpath, 'input.jpg')
cv2.imwrite(inpath, image)
return inpath
@app.route("/", methods=['GET','POST']) # webapi
def upload_file():
if request.method == "GET":
return render_template("index.html")
if request.method == 'POST':
image=request.form["image"]
try:
#________________________process________________________________________
image=image.split(",")[1]
image=base64.b64decode(image)
image= np.frombuffer(image, dtype=np.uint8)
image = cv2.imdecode(image, flags=1)
path = save_image(image) # save original image
#test result is image
res_img = image.copy()
b64_res_img = convert_tob64(res_img)
restext="abc"
return render_template("index.html",orimg=convert_tob64(image),resimg=b64_res_img,result=restext)
except:
print("No image selected")
return render_template("index.html")
def convert_tob64(frame):
_, im_arr = cv2.imencode('.png', frame) # im_arr: image in Numpy one-dim array format.
im_bytes = im_arr.tobytes()
im_b64 = base64.b64encode(im_bytes)
return "data:image/png;base64,"+str(im_b64)[2:-1]
@app.route("/api",methods=['GET','POST'])
def process():
#todo
return
if __name__ == "__main__":
#predict = Process()
app.run(host="0.0.0.0",port=1234,debug=True,threaded=True)