-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
86 lines (72 loc) · 2.41 KB
/
server.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from werkzeug.utils import secure_filename
from flask import Flask, render_template, jsonify, request, make_response, send_from_directory, abort
# from flask_cors import CORS
import server_text
import time
import json
import os
import base64
import cv2
import numpy as np
app = Flask(__name__)
#CORS(app, supports_credentials=True)
UPLOAD_FOLDER = 'upload'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
basedir = os.path.abspath(os.path.dirname(__file__))
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'JPG', 'PNG', 'gif', 'GIF'])
model = server_text.text_helper(mode = 'test')
video = None
cnt = 0
FLAG = False
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/upload')
def upload_test():
return render_template('up.html')
# json
@app.route('/up_photo', methods=['POST', 'GET'], strict_slashes=False)
def upload():
global cnt, FLAG, video
if(not os.path.exists('../server/')):
os.makedirs('../server/')
img = request.values['photo']
s = request.values['s']
img = img.replace('data:image/png;base64,', '')
img = base64.b64decode(img)
if(FLAG == False and s == 'ok'):
FLAG = True
cnt = 0
video = cv2.VideoWriter('../server/output.mp4',cv2.VideoWriter_fourcc(*'XVID'), 20.0, (720,480))
if img and video and s == 'ok' and FLAG:
with open('../server/p_' + str(cnt) + '.png', 'wb') as fdecode:
fdecode.write(img)
tmp = cv2.imread('../server/p_' + str(cnt) + '.png')
video.write(tmp)
cnt += 1
fdecode.close()
rst = make_response(jsonify({"success": 0, "flag": 0, "msg": "you are success"}))
rst.headers['Access-Control-Allow-Origin'] = '*'
return rst, 201
else:
rst_flag = 2
print("ok")
msg = ''
if(video != None):
rst_flag = 1
video.release()
video = None
FLAG = False
cnt = 0
word = model.get_text('../server/output.mp4')
for w in word:
msg += w
print(msg)
rst = make_response(jsonify({"success": 0, "flag": rst_flag, "msg": msg}))
rst.headers['Access-Control-Allow-Origin'] = '*'
return rst, 201
# show photo
@app.route('/show', methods=['GET', 'POST'])
def show_txt():
pass
if __name__ == '__main__':
app.run(debug=True, host = '0.0.0.0', port = 5000)