抠图,labelme打标之后,提取图片中的目标

import os
import json
import cv2
# from numpy import imag
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np


path_ori = r"E:\dabiao\DES_ori"
path_dest = r"E:\dabiao\DES_EXTRACT"

files = os.listdir(path_ori)

for file in files:
    if file.endswith(".json"):
        f = open(path_ori + '\\' +file)
        data = json.load(f)
        img = Image.open(path_ori + '\\' + file.split('.')[0] + '.JPG')
        for i in range(len(data["shapes"])):
            points =  data["shapes"][i]["points"]
            
            points_01 = round(points[0][0])
            points_02 = round(points[0][1])
            points_03 = round(points[1][0])
            points_04 = round(points[1][1])

            box = [points_01,points_02,points_03,points_04]
            roi = img.crop(box)
            cv2.imwrite(path_dest + '\\' + file.split('.')[0] + '-' + str(i) + '.jpg', np.array(roi))

你可能感兴趣的:(计算机视觉,python,深度学习)