优化错误日志及重试机制
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import logging
|
||||
import math
|
||||
import urllib.request
|
||||
from time import sleep
|
||||
|
||||
import cv2
|
||||
import numpy
|
||||
from paddleclas import PaddleClas
|
||||
from tenacity import retry, stop_after_attempt, wait_random
|
||||
|
||||
|
||||
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
|
||||
retry_error_callback=lambda e: logging.warning("获取图片失败", exc_info=e))
|
||||
def read(image_path):
|
||||
"""
|
||||
从网络或本地读取图片
|
||||
@@ -15,15 +17,8 @@ def read(image_path):
|
||||
:return: NumPy数组形式的图片
|
||||
"""
|
||||
if image_path.startswith("http"):
|
||||
resp = None
|
||||
for i in range(3):
|
||||
# 发送HTTP请求并获取图像数据
|
||||
try:
|
||||
resp = urllib.request.urlopen(image_path, timeout=60)
|
||||
break
|
||||
except Exception as e:
|
||||
logging.warning("获取图片失败", exc_info=e)
|
||||
sleep(3)
|
||||
# 发送HTTP请求并获取图像数据
|
||||
resp = urllib.request.urlopen(image_path, timeout=60)
|
||||
# 将数据读取为字节流
|
||||
image_data = resp.read()
|
||||
# 将字节流转换为NumPy数组
|
||||
|
||||
Reference in New Issue
Block a user