# -*- coding:utf-8 -*- from flask import Flask, render_template, request, redirect, url_for, make_response, jsonify,Markup from werkzeug.utils import secure_filename from tools import get_text,get_image from datetime import timedelta import os from dev_image import run_cut import json ALLOWED_EXTENSIONS = set(['png', 'jpg', 'JPG', 'PNG', 'bmp']) def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS app = Flask(__name__) # 设置静态文件缓存过期时间 app.send_file_max_age_default = timedelta(seconds=1) # @app.route('/upload', methods=['POST', 'GET']) @app.route('/', methods=['POST', 'GET']) # 添加路由 def upload(): if request.method == 'POST': print(request.files) f = request.files['file'] if not (f and allowed_file(f.filename)): return jsonify({"error": 1001, "msg": "请检查上传的图片类型,仅限于png、PNG、jpg、JPG、bmp"}) # user_input = request.form.get("name") basepath = os.path.dirname(__file__) # 当前文件所在路径 upload_path = os.path.join('./image_dir', secure_filename(f.filename)) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径 # upload_path = os.path.join(basepath, 'static/images','test.jpg') #注意:没有的文件夹一定要先创建,不然会提示没有该路径 print(upload_path) f.save(upload_path) # 使用Opencv转换一下图片格式和名称 # img = cv2.imread(upload_path) # cv2.imwrite(os.path.join(basepath, 'static/images', 'test.jpg'), img) # maple = requests.post('http://127.0.0.1:6666/static_pic',json={'path':upload_path}).text # print(maple) run_cut(upload_path,tabel=False) text = get_text() images = get_image() print(text) return render_template('show.html', w=500, contents=text, images=images ) return render_template('upload.html') @app.route('/online', methods=['POST', 'GET']) # 添加路由 def upload_online(): if request.method == 'POST': f = request.files['file'] if not (f and allowed_file(f.filename)): return jsonify({"error": 1001, "msg": "请检查上传的图片类型,仅限于png、PNG、jpg、JPG、bmp"}) # user_input = request.form.get("name") basepath = os.path.dirname(__file__) # 当前文件所在路径 upload_path = os.path.join('./image_dir', secure_filename(f.filename)) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径 # upload_path = os.path.join(basepath, 'static/images','test.jpg') #注意:没有的文件夹一定要先创建,不然会提示没有该路径 print(upload_path) f.save(upload_path) # 使用Opencv转换一下图片格式和名称 # img = cv2.imread(upload_path) # cv2.imwrite(os.path.join(basepath, 'static/images', 'test.jpg'), img) # maple = requests.post('http://127.0.0.1:6666/static_pic',json={'path':upload_path}).text # print(maple) run_cut(upload_path,tabel=False) text = get_text(online=False) images = get_image() return render_template('show.html', w=500, contents=text, images=images ) return render_template('upload.html') if __name__ == '__main__': app.run('0.0.0.0',port=12535)