根据json,批量从图片中提取ROI
import cv2
import numpy as np
import json
import os
import glob
seq_dir = os.path.expanduser('./')
img_files=glob.glob(seq_dir + '*.json')
category_types = ["r"]
for img_file in img_files:
imgn=str(img_file[2:5])+".jpg"
img = cv2.imread(str(imgn))
with open(img_file, "r") as f:
label = json.load(f)
shapes = label["shapes"]
i = 0
for shape in shapes:
category = shape["label"]
points = shape["points"]
x0=int(points[0][0])
y0=int(points[0][1])
x1=int(points[1][0])
y1=int(points[1][1])
i=i+1
img1=img[y0:y1,x0:x1]
cv2.imwrite("mask/"+str(img_file[2:5])+'_'+str(i)+".jpg", img1)