imgutil.py 文件源码

python
阅读 41 收藏 0 点赞 0 评论 0

项目:face-classifier-cnn 作者: nknytk 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号