添加获取图片的超时和重试,防止一直获取而导致程序卡住

This commit is contained in:
2024-07-20 14:54:59 +08:00
parent 2e7e238c77
commit a71e64c24f
2 changed files with 13 additions and 5 deletions

View File

@@ -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数组