Skip to content

Commit 9e7d15a

Browse files
committed
save dir
1 parent 84430df commit 9e7d15a

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

OCR.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import atexit
22
import wcocr
33
import os
4+
import sys
45
from flask import Flask, request, jsonify
56
from werkzeug.datastructures.file_storage import FileStorage
67
import uuid
@@ -9,13 +10,20 @@
910
# 创建 Flask 应用
1011
app = Flask(__name__)
1112

12-
# 设置图片保存目录
13-
UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "img")
14-
1513
# 设置允许上传的文件类型
1614
ALLOWED_EXTENSIONS = ("jpg", "jpeg", "png", "bmp", "tif")
1715

1816

17+
def get_save_dir() -> str:
18+
# 图片保存目录
19+
if getattr(sys, "frozen", False):
20+
# 如果是打包后的可执行文件
21+
return os.path.join(os.path.dirname(sys.executable), "img")
22+
else:
23+
# 如果是脚本运行
24+
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "img")
25+
26+
1927
def find_wechat_path():
2028
script_dir = os.path.dirname(os.path.abspath(__file__))
2129
common_paths = os.path.join(script_dir, "path")
@@ -68,11 +76,13 @@ def save_file(file: FileStorage) -> str:
6876
# 生成唯一文件名
6977
new_filename = uuid.uuid4().hex + "." + file.filename.split(".")[-1]
7078

79+
save_dir = get_save_dir()
80+
7181
# 保存图片
72-
if not os.path.exists(UPLOAD_FOLDER):
73-
os.mkdir(UPLOAD_FOLDER)
82+
if not os.path.exists(save_dir):
83+
os.mkdir(save_dir)
7484

75-
file_path = os.path.join(UPLOAD_FOLDER, new_filename)
85+
file_path = os.path.join(save_dir, new_filename)
7686
file.save(file_path)
7787

7888
return file_path
@@ -108,9 +118,11 @@ def upload_image():
108118
# # 设置端口
109119
# app.run(host="0.0.0.0", port=5001)
110120

111-
if __name__ == '__main__':
121+
if __name__ == "__main__":
112122
wechat_ocr_init()
113-
server = pywsgi.WSGIServer(('127.0.0.1', 5000), app)
123+
print("start listen 127.0.0.1:5000 0.0.0.0:5000")
124+
print("example : POST 127.0.0.1:5000/upload_ocr")
125+
server = pywsgi.WSGIServer(("127.0.0.1", 5000), app)
114126
server.serve_forever()
115-
server0 = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
127+
server0 = pywsgi.WSGIServer(("0.0.0.0", 5000), app)
116128
server0.serve_forever()

0 commit comments

Comments
 (0)