#encoding:utf-8
#!/usr/bin/env python
from werkzeug.utils import secure_filename
from flask import Flask, render_template, jsonify, request, make_response, send_from_directory, abort, redirect
import time
import os, shutil
from strUtil import Pic_str
from app.flask_app import app, db
import base64
from ocr import getText
import cv2
from config import STATICDIR, TMPFILEDIR
from app.models import ImageData
# db.create_all()
# app = Flask(__name__)
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'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload')
def upload_test():
return render_template('index.html')
# 上传文件
@app.route('/up_photo', methods=['POST'], strict_slashes=False)
def api_upload():
file_dir = os.path.join(STATICDIR, app.config['UPLOAD_FOLDER'])
if not os.path.exists(file_dir):
os.makedirs(file_dir)
f = request.files['photo']
if f and allowed_file(f.filename):
start_time = time.time()
fname = secure_filename(f.filename)
# fname_, ext = fname.rsplit('.', 1)
new_filename = Pic_str().create_uuid() + '.jpg'
save_path = os.path.join(file_dir, new_filename)
f.save(save_path)
ocr = getText(save_path)
data = {
"filename":fname,
"filepath":"upload/"+new_filename,
"ocr":ocr,
'duration':round(time.time() - start_time, 2)
}
print(data)
new = ImageData(filename=fname,filepath="result/"+new_filename,content=str(data))
db.session.add(new)
db.session.commit()
return jsonify({"success": 0, "msg": "上传成功", "data":data})
else:
return jsonify({"error": 1001, "msg": "上传失败"})
if __name__ == '__main__':
app.run(debug=True)