diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b19bf42 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +node_modules +dist +build +out +*.log* +package-lock.json diff --git a/code/main.py b/code/main.py index 934baaf..ee32129 100644 --- a/code/main.py +++ b/code/main.py @@ -7,6 +7,7 @@ import numpy as np import cv2 from paddleocr import PaddleOCR import re +import logging # import time app = Flask(__name__) @@ -17,24 +18,45 @@ CORS(app) # 允许所有来源的请求 # CORS(app) # 加载模型 current_dir = os.path.dirname(os.path.abspath(__file__)) +logging.basicConfig(filename=current_dir+'app.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') model = YOLO(os.path.join(current_dir, "models/best.pt")) print("模型加载成功") -ocrSimple = [PaddleOCR( +# ocrSimple = [PaddleOCR( +# use_gpu=False, +# use_angle_cls=True, +# det_model_dir=os.path.join(current_dir, "ocr/simple/ch_PP-OCRv4_det_infer"), +# rec_model_dir=os.path.join(current_dir, "ocr/simple/ch_PP-OCRv4_rec_infer"), +# use_tensorrt=True,#静态图模式可能会提高性能 +# # det_model_dir=os.path.join(current_dir, "ocr/complex/ch_PP-OCRv4_det_server_infer"), +# # rec_model_dir=os.path.join(current_dir, "ocr/complex/ch_PP-OCRv4_rec_server_infer"), +# ) for _ in range(4)] +# ocrComplex =[PaddleOCR( +# use_gpu=False, +# use_angle_cls=True, +# # det_model_dir=os.path.join(current_dir, "ocr/simple/ch_PP-OCRv4_det_infer"), +# # rec_model_dir=os.path.join(current_dir, "ocr/simple/ch_PP-OCRv4_rec_infer"), +# det_model_dir=os.path.join(current_dir, "ocr/complex/ch_PP-OCRv4_det_server_infer"), +# rec_model_dir=os.path.join(current_dir, "ocr/complex/ch_PP-OCRv4_rec_server_infer"), +# use_tensorrt=True,#静态图模式可能会提高性能 +# ) for _ in range(4)] +ocrSimple = PaddleOCR( use_gpu=False, use_angle_cls=True, det_model_dir=os.path.join(current_dir, "ocr/simple/ch_PP-OCRv4_det_infer"), rec_model_dir=os.path.join(current_dir, "ocr/simple/ch_PP-OCRv4_rec_infer"), + use_tensorrt=True,#静态图模式可能会提高性能 # det_model_dir=os.path.join(current_dir, "ocr/complex/ch_PP-OCRv4_det_server_infer"), # rec_model_dir=os.path.join(current_dir, "ocr/complex/ch_PP-OCRv4_rec_server_infer"), - ) for _ in range(4)] -ocrComplex =[PaddleOCR( + ) +ocrComplex =PaddleOCR( use_gpu=False, use_angle_cls=True, # det_model_dir=os.path.join(current_dir, "ocr/simple/ch_PP-OCRv4_det_infer"), # rec_model_dir=os.path.join(current_dir, "ocr/simple/ch_PP-OCRv4_rec_infer"), det_model_dir=os.path.join(current_dir, "ocr/complex/ch_PP-OCRv4_det_server_infer"), rec_model_dir=os.path.join(current_dir, "ocr/complex/ch_PP-OCRv4_rec_server_infer"), - ) for _ in range(4)] + use_tensorrt=True,#静态图模式可能会提高性能 + ) # 开始识别 @app.route("/startOcr", methods=["post"]) def startOcr(): @@ -44,8 +66,10 @@ def startOcr(): global model global ocr # 这里省略了实际的base64数据 - base64_str =request.json.get("base64Str") - ocrType =request.json.get("ocrType") + base64_str = request.json.get("base64Str") + ocrType = request.json.get("ocrType") + pngName = request.json.get("pngName") + threshold = 127 # 解码base64字符串 _, img_data = base64_str.split(",") img_data = base64.b64decode(img_data) @@ -91,7 +115,7 @@ def startOcr(): 'target_2': [], 'target_3': [] } - returnObj={"resultsObj":{},"message":''} + returnObj={"resultsObj":{},"message":'',pngName:pngName} resultAll={} # 检查类别数量 if (len(target_0) == 2 and len(target_1) == 1 and (len(target_2) == 1 or len(target_2) == 2) and len(target_3) == 2): @@ -102,7 +126,11 @@ def startOcr(): for target, name in zip([left_point, right_point], ['address', 'name']): target_img = img[target[1]:target[3], target[0]:target[2]] # cv2.imwrite(f'{name}.jpg', target_img) - result = ocr[0].ocr(target_img) + # 灰值化处理但是会识别不到 + gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY) + _, binary = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) + # result = ocr[0].ocr(binary) + result = ocr.ocr(binary) out = '' if not result or not any(result): out = '未识别到文字' @@ -115,7 +143,10 @@ def startOcr(): for target in target_1: target_img = img[target[1]-5:target[3]+5, target[0]-5:target[2]+5] # cv2.imwrite(f'当前有功.jpg', target_img) - result = ocr[1].ocr(target_img, det=False) + gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY) + _, binary = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) + # result = ocrSimple[1].ocr(binary, det=False) + result = ocr.ocr(binary, det=False) for lines in result: for line in lines: out = line[0] @@ -132,7 +163,10 @@ def startOcr(): top_target = target_2[0] target_img = img[top_target[1]:top_target[3], top_target[0]:top_target[2]] # cv2.imwrite(f'电表资产号.jpg', target_img) - result = ocr[2].ocr(target_img) + gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY) + _, binary = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) + # result = ocrSimple[2].ocr(binary) + result = ocr.ocr(binary) longest_line = "" max_length = 0 for lines in result: @@ -154,7 +188,10 @@ def startOcr(): target_img = cv2.transpose(target_img) target_img = cv2.flip(target_img, flipCode=1) # cv2.imwrite(f'{name}.jpg', target_img) - result = ocr[3].ocr(target_img) + gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY) + _, binary = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) + # result = ocrSimple[3].ocr(binary) + result = ocr.ocr(binary) out = '' for lines in result: for line in lines: diff --git a/code/main.spec b/code/main.spec new file mode 100644 index 0000000..fae13fd --- /dev/null +++ b/code/main.spec @@ -0,0 +1,63 @@ +# -*- mode: python ; coding: utf-8 -*- +from PyInstaller.utils.hooks import collect_all + +datas = [] +binaries = [] +hiddenimports = [] +tmp_ret = collect_all('paddleocr') +datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2] +tmp_ret = collect_all('pyclipper') +datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2] +tmp_ret = collect_all('imghdr') +datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2] +tmp_ret = collect_all('skimage') +datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2] +tmp_ret = collect_all('imgaug') +datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2] +tmp_ret = collect_all('scipy.io') +datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2] +tmp_ret = collect_all('lmdb') +datas += tmp_ret[0]; binaries += tmp_ret[1]; hiddenimports += tmp_ret[2] + + +a = Analysis( + ['main.py'], + pathex=[], + binaries=binaries, + datas=datas, + hiddenimports=hiddenimports, + hookspath=[], + hooksconfig={}, + runtime_hooks=[], + excludes=[], + noarchive=False, + optimize=0, +) +pyz = PYZ(a.pure) + +exe = EXE( + pyz, + a.scripts, + [], + exclude_binaries=True, + name='main', + debug=False, + bootloader_ignore_signals=False, + strip=False, + upx=True, + console=True, + disable_windowed_traceback=False, + argv_emulation=False, + target_arch=None, + codesign_identity=None, + entitlements_file=None, +) +coll = COLLECT( + exe, + a.binaries, + a.datas, + strip=False, + upx=True, + upx_exclude=[], + name='main', +) diff --git a/code/ocrCurrent.jpg b/code/ocrCurrent.jpg index 2574d34..9ea7990 100644 Binary files a/code/ocrCurrent.jpg and b/code/ocrCurrent.jpg differ diff --git a/code/readme.md b/code/readme.md new file mode 100644 index 0000000..05abffd --- /dev/null +++ b/code/readme.md @@ -0,0 +1,9 @@ +# 打包 +pyinstaller.exe -D .\main.py --collect-all paddleocr --collect-all pyclipper --collect-all imghdr --collect-all skimage --collect-all imgaug --collect-all scipy.io --collect-all lmdb +# 关于模型路径问题 +你自己的模型文件 统一放置于 ocr 文件夹内,ocr文件夹放置在 _internal目录下 的同级目录下 +paddle\libs全部文件复制到_internal\paddle\libs +ultralytics 复制到_internal下 +models 复制到_internal下 +# 地址 +https://github.com/PaddlePaddle/PaddleOCR/discussions/11342 diff --git a/code/第二批电表样本.zip b/code/第二批电表样本.zip new file mode 100644 index 0000000..f14adee Binary files /dev/null and b/code/第二批电表样本.zip differ diff --git a/code/第二批电表样本/IMG_20240813_105828.jpg b/code/第二批电表样本/IMG_20240813_105828.jpg new file mode 100644 index 0000000..b919d78 Binary files /dev/null and b/code/第二批电表样本/IMG_20240813_105828.jpg differ diff --git a/code/第二批电表样本/IMG_20240813_113420.jpg b/code/第二批电表样本/IMG_20240813_113420.jpg new file mode 100644 index 0000000..0549d0b Binary files /dev/null and b/code/第二批电表样本/IMG_20240813_113420.jpg differ diff --git a/code/第二批电表样本/IMG_20240813_113515.jpg b/code/第二批电表样本/IMG_20240813_113515.jpg new file mode 100644 index 0000000..a013e42 Binary files /dev/null and b/code/第二批电表样本/IMG_20240813_113515.jpg differ diff --git a/front/src/renderer/src/App.vue b/front/src/renderer/src/App.vue index e54f70e..12049be 100644 --- a/front/src/renderer/src/App.vue +++ b/front/src/renderer/src/App.vue @@ -19,7 +19,7 @@ export default { data() { return { _: _, - isVip: false, + isVip: true, } }, methods: { @@ -28,7 +28,7 @@ export default { }, }, async mounted() { - let fiveDay = dayjs('2024-09-27T00:00:00').valueOf() + let fiveDay = dayjs('2024-10-27T00:00:00').valueOf() if (!this.isVip) { if (dayjs().valueOf() > fiveDay) { this.isVip = false diff --git a/front/src/renderer/src/views/user.vue b/front/src/renderer/src/views/user.vue index 5d84716..4bd439e 100644 --- a/front/src/renderer/src/views/user.vue +++ b/front/src/renderer/src/views/user.vue @@ -68,6 +68,13 @@ +