与tryexpect的奋斗

def read_image_text(img_path,json_path):

"""Keep reading image until succeed.
This can avoid IOError incurred by heavy IO process."""
got_img_text  = False

if not osp.exists(img_path) and osp.exists(json_path):
    raise IOError("path does not exist")
while not got_img_text:
    try:
        img = Image.open(img_path).convert('RGB')
        jsonfile = json.load(open(json_path, encoding='utf-8'))
        text_recs = []
        for j in [jsonfile]:
            for r in j['shapes']:
                pts = r['points']
                br, bl, tl, tr = _order_points(pts)
                rect = [tl[0], tl[1], tr[0], tr[1], bl[0], bl[1], br[0], br[1]]
                rect = [int(i) for i in rect]
                text_recs.append(rect)
        got_img_text = True
    except :
        pass
return img,text_recs

通过上面,可以使得读取文件和生成数值遇到问题后,直接直接跳过全部的数值。对于训练时候,读取数据如果遇到数据有问题有帮助。
同时注意这里的try。。。。except。。。。。pass的用法,可以说是非常经典,值得学习。

def fortry(x):
try:
for i in range(x):
i += 1
except:
pass

D = fortry(5)
print(D)

这里面是一个值得学习的案例,无论输入什么数值,这个循环总是输出一个空的数据集,所以pass里面这样的循环是不可以写的,因为整个循环都会被pass。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值