From a71e64c24fd9e618d1a574d9d66552eb7b151f1a Mon Sep 17 00:00:00 2001 From: liuyebo <1515783401@qq.com> Date: Sat, 20 Jul 2024 14:54:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E7=9A=84=E8=B6=85=E6=97=B6=E5=92=8C=E9=87=8D=E8=AF=95?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E4=B8=80=E7=9B=B4=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=80=8C=E5=AF=BC=E8=87=B4=E7=A8=8B=E5=BA=8F=E5=8D=A1=E4=BD=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ucloud/ucloud.py | 6 +++--- util/image_util.py | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ucloud/ucloud.py b/ucloud/ucloud.py index a62c940..1475dad 100644 --- a/ucloud/ucloud.py +++ b/ucloud/ucloud.py @@ -15,7 +15,7 @@ def get_private_url(key, bucket=BUCKET): _, resp = UFILE_HANDLER.head_file(bucket, key) if resp.status_code == -1: logging.warning(f"uCloud连接失败!即将重试...") - sleep(1) + sleep(3) continue if resp.status_code != 200: logging.warning(f"uCloud中未找到({key})! status: {resp.status_code} error: {resp.error}") @@ -35,7 +35,7 @@ def copy_file(source_bucket, source_key, target_bucket, target_key): ret, resp = UFILE_HANDLER.copy(target_bucket, target_key, source_bucket, source_key) if resp.status_code == -1: logging.warning(f"uCloud连接失败!即将重试...") - sleep(1) + sleep(3) continue if resp.status_code != 200: logging.warning( @@ -50,7 +50,7 @@ def upload_file(key, file_path, bucket=BUCKET): ret, resp = UFILE_HANDLER.putfile(bucket, key, file_path, header=None) if resp.status_code == -1: logging.warning(f"uCloud连接失败!即将重试...") - sleep(1) + sleep(3) continue if resp.status_code != 200: logging.warning(f"上传({key})失败! status: {resp.status_code} error: {resp.error}") diff --git a/util/image_util.py b/util/image_util.py index 45f13bc..02e93c2 100644 --- a/util/image_util.py +++ b/util/image_util.py @@ -1,6 +1,7 @@ import logging import math import urllib.request +from time import sleep import cv2 import numpy @@ -14,8 +15,15 @@ def read(image_path): :return: NumPy数组形式的图片 """ if image_path.startswith("http"): - # 发送HTTP请求并获取图像数据 - resp = urllib.request.urlopen(image_path) + 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) # 将数据读取为字节流 image_data = resp.read() # 将字节流转换为NumPy数组