优化文档检测为接口
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
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
|
||||
@@ -47,3 +51,23 @@ 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/detect_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)
|
||||
result.append(cv2.imdecode(np_array, cv2.IMREAD_COLOR))
|
||||
return result
|
||||
else:
|
||||
return []
|
||||
|
||||
Reference in New Issue
Block a user