def load_img(file_path):
try:
if os.path.exists(file_path):
return cv2.imread(file_path)
elif file_path.startswith('http'):
with urlopen(file_path) as fp:
img_bin = numpy.fromstring(fp.read(), dtype=numpy.uint8)
mime = fp.getheader('Content-Type', '')
print(mime)
if MIME_JPG_PTN.match(mime):
return cv2.imdecode(img_bin, cv2.IMREAD_UNCHANGED)
elif MIME_PNG_PTN.match(mime):
return cv2.imdecode(img_bin, cv2.IMREAD_UNCHANDED)
else:
sys.stderr.write('Unacceptable mime type {}.\n'.format(mime))
else:
sys.stderr.write('{} is not found.\n'.format(file_path))
except Exception as e:
sys.stderr.write('Failed to load {} by {}\n'.format(file_path, e))
return None
评论列表
文章目录