Commit 8c35b739 by 周杭

ocrConsumer功能

1 parent 58500fcf
...@@ -3,8 +3,10 @@ import numpy as np ...@@ -3,8 +3,10 @@ import numpy as np
import base64 import base64
from io import BytesIO from io import BytesIO
import logging import logging
import tf as tf
from keras.models import load_model from keras.models import load_model
from keras.utils import image_utils # from keras.utils import image_utils
from keras import backend as K from keras import backend as K
from PIL import ImageFile from PIL import ImageFile
...@@ -38,9 +40,9 @@ class ImageClassificationService(object): ...@@ -38,9 +40,9 @@ class ImageClassificationService(object):
""" """
# 加载图像 # 加载图像
img = image_utils.load_img(img_path, target_size=self.input_shape) img = tf.keras.utils.load_img(img_path, target_size=self.input_shape)
# 图像预处理 # 图像预处理
x = image_utils.img_to_array(img) / 255.0 # 与训练一致 x = tf.keras.utils.img_to_array(img) / 255.0 # 与训练一致
x = np.expand_dims(x, axis=0) x = np.expand_dims(x, axis=0)
# 对图像进行分类 # 对图像进行分类
......
...@@ -18,6 +18,12 @@ import requests ...@@ -18,6 +18,12 @@ import requests
from paddleocr import PaddleOCR, draw_ocr from paddleocr import PaddleOCR, draw_ocr
from PIL import Image from PIL import Image
from pyzbar.pyzbar import decode from pyzbar.pyzbar import decode
# 获取当前文件所在目录的绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 将根目录添加到 Python 解释器的搜索路径中
root_dir = os.path.join(current_dir, '..') # 假设根目录是当前目录的父目录
sys.path.append(root_dir)
from logger import log from logger import log
environment = sys.argv[1] if len(sys.argv) > 1 else "default" environment = sys.argv[1] if len(sys.argv) > 1 else "default"
...@@ -415,17 +421,17 @@ def handle_process(jsonData): ...@@ -415,17 +421,17 @@ def handle_process(jsonData):
except PermissionError: except PermissionError:
log.info("权限不足,无法删除文件") log.info("权限不足,无法删除文件")
def consume_kafka():
# 消费消息并进行逻辑处理 # 消费消息并进行逻辑处理
try: try:
for msg in consumer: for msg in consumer:
if msg is None: if msg is None:
continue continue
# 处理逻辑 # 处理逻辑
handle_process(msg) handle_process(msg)
except Exception as e: except Exception as e:
log.error("ocr consumer Exception: " + str(e)) log.error("ocr consumer Exception: " + str(e))
finally: finally:
# 关闭消费者 # 关闭消费者
consumer.close() consumer.close()
# 关闭光标对象 # 关闭光标对象
......
...@@ -5,6 +5,7 @@ from flaskr.biz import user_service ...@@ -5,6 +5,7 @@ from flaskr.biz import user_service
from flaskr.biz.identify_service import IdentifyService from flaskr.biz.identify_service import IdentifyService
from flaskr.controller import image_classfication_controller, ocr_controller from flaskr.controller import image_classfication_controller, ocr_controller
import os import os
from kafka_consumer import ocr_consumer
app = create_app() app = create_app()
# 注册蓝图到应用程序中 # 注册蓝图到应用程序中
...@@ -64,4 +65,5 @@ def upload(): ...@@ -64,4 +65,5 @@ def upload():
if __name__ == "__main__": if __name__ == "__main__":
ocr_consumer.consume_kafka()
app.run(host="0.0.0.0", port=5000) app.run(host="0.0.0.0", port=5000)
No preview for this file type
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!