优化接口图片传输方式

This commit is contained in:
2024-09-23 14:45:03 +08:00
parent a2a82df21c
commit c091a82a91
8 changed files with 89 additions and 62 deletions

View File

@@ -1,12 +1,8 @@
import base64
import logging
import tempfile
from collections import defaultdict
import cv2
import numpy as np
import requests
from tenacity import retry, stop_after_attempt, wait_random
from paddle_detection import PADDLE_DET
from paddle_detection.deploy.third_engine.onnx.infer import PredictConfig
@@ -51,26 +47,3 @@ def get_book_areas(image):
for book_area in book_areas:
result.append(image_util.capture(image, book_area["box"]))
return result
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
after=lambda x: logging.warning("获取文档区域失败!"))
def request_book_areas(image):
url = "http://det_api:5000/det/books"
_, encoded_image = cv2.imencode('.jpg', image)
byte_stream = encoded_image.tobytes()
files = {"image": ("image.jpg", byte_stream)}
response = requests.post(url, files=files)
if response.status_code == 200:
img_str_list = response.json()
result = []
for img_str in img_str_list:
img_data = base64.b64decode(img_str)
np_array = np.frombuffer(img_data, np.uint8)
img = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
height, width = img.shape[:2]
if max(height, width) / min(height, width) <= 6.5:
result.append(img) # 过滤异常结果
return result
else:
return []