json 批量读取图片然后进行识别

该代码示例展示了如何利用OpenCV读取图片和视频流,通过预先定义的标注数据进行目标区域提取,然后对目标区域应用图像变换。它还涉及到了批量图片处理和RTSP视频流的抓帧,以及使用OCR技术进行数字识别。

要点:

  • 快速验证算法


一 单张图片

frame = cv2.imread('../save_img/0517_img/0-0515-310912.jpg')

# img_show('frame', frame)
mask_path = 'ocr_masks/31 进厂水流量计.json'

data = json.load(open(mask_path, encoding='utf-8'))
# 获取屏幕范围点
shapes = data['shapes']
target_label = '1'
for shape in shapes:
    label = shape['label']
    if label == target_label:
        points = [[float(round(x[0])), float(round(x[1]))] for x in shape['points']]
        points = np.float32(sort_points(points))

    elif label == '2':
        aim_points = [[float(round(x[0])), float(round(x[1]))] for x in shape['points']]
        aim_points = np.float32(sort_points(aim_points))

aim_img = extract_and_transform_image(frame, aim_points)
img_show('aim_img', aim_img)

二 批量查看

# 算法批量识别
path = "../save_img/0517_img/"  # 修改为你自己的图片路径

for i, filename in enumerate(os.listdir(path)):
    if filename.split('.')[-1] != 'jpg':
        continue
    print('查看文件名:', filename)
    img_name = '../save_img/0517_img/' + filename
    frame = cv2.imread(img_name)

    frame_to_push = cv2.resize(frame, (int(frame.shape[1]*0.5), int(frame.shape[0]*0.5)), interpolation=cv2.INTER_AREA)
    img_show('frame_to_push', frame_to_push)

    k = int(filename.split('-')[0])
    mask_path = mask_list[k]

    data = json.load(open(mask_path, encoding='utf-8'))
    # 获取屏幕范围点
    shapes = data['shapes']
    target_label = '1'
    for shape in shapes:
        label = shape['label']
        if label == target_label:
            points = [[float(round(x[0])), float(round(x[1]))] for x in shape['points']]
            points = np.float32(sort_points(points))

        elif label == '2':
            aim_points = [[float(round(x[0])), float(round(x[1]))] for x in shape['points']]
            aim_points = np.float32(sort_points(aim_points))

    aim_img = extract_and_transform_image(frame, aim_points)
    img_show('aim_img', aim_img)
    ocr_result = digital_recognize(aim_img)
    print('识别结果:', ocr_result)

三 查看视频流

camera_list = [
    # 取水泵房仪表间球机
    'rtsp://10.0.58.253:9090/dss/monitor/param?cameraid=1003517%248&substream=1',
    # 送水泵房出水水质间球机
    'rtsp://10.0.58.253:9090/dss/monitor/param?cameraid=1003517%2418&substream=1',
    # 滤池水质间球机
    'rtsp://10.0.58.253:9090/dss/monitor/param?cameraid=1003517%2410&substream=1',
    # 滤池廊道浊度仪1号
    'rtsp://10.0.58.253:9090/dss/monitor/param?cameraid=1003517%244&substream=1',
    # 滤池廊道浊度仪2号
    'rtsp://10.0.58.253:9090/dss/monitor/param?cameraid=1003517%246&substream=1'
]

rtsp_url = camera_list[2]
cap = cv2.VideoCapture(rtsp_url)

save_dir = '../00get_new_img/'

while True:
    ret, frame = cap.read()
    if ret:

        cv2.imshow('frame', frame)
        key = cv2.waitKey(1)
        if key == ord('s'):
            print('img_shape:', frame.shape)
            img_name = f'{save_dir}0510_1# {time.time()}.jpg'
            cv2.imwrite(img_name, frame)
            print(f'Saved image to {img_name}')
        elif key == ord('q'):
            break
    else:
        print('Failed to read frame')
        break

cap.release()
cv2.destroyAllWindows()

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值