代码基于以上文章修改,原始文章只支持rectangle 标注类型,这里增加polygon的切割。
原始代码没有对base64图片进行处理,这里增加处理
基于我个人的需求,需100%匹配才切图。arc的没做处理,因为我一开始就避免使用arc
json 需和jpg在同级目录且同名。 目前labelme的数据就是这样子放的。
from queue import Full
import cv2
import json
import os
import base64
from PIL import Image
import io
def tobase64(img):
# OpenCV 读取的图像是 BGR 格式,而 PIL 以及大多数显示工具使用 RGB 格式
# 因此需要转换颜色通道
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 将 NumPy 数组转换成 PIL 图像对象
pil_image = Image.fromarray(img)
# 将 PIL 图像对象转换为 bytes
byte_arr = io.BytesIO()
pil_image.save(byte_arr, format='JPEG')
byte_arr = byte_arr.getvalue()
# 对 bytes 进行 base64 编码
base64_image = base64.b64encode(byte_arr).decode('utf-8')
return base64_image
# 读取大图的JSON文件
def splitImgandJson(IPath_json, IPath_img, OPath_json, OPath_img):
events = os.listdir(IPath_json)
events = [x for x in events if x.endswith('.json')]
#循环处理每一张图像和标注
for onevent in events:
name = onevent.split('.json')[0]
img_name = name + '.jpg'
with open(os.path.join(IPath_json, onevent), 'r') as json_file:
large_json_data = json.load(json_file)
# 读取大图的JPEG图像
large_image = cv2.imread(os.path.join(IPath_img, img_name))
# 定义每个小图的大小
crop_width, crop_height = 500, 500 # 可以根据需要调整大小
# 循环裁剪图像并生成新的JSON文件
L_H = large_image.shape[0]
L_W = large_image.shape[1]
for i in range(0, L_H, crop_height):
for j in range(0, L_W, crop_width):
# 裁剪图像
small_image = large_image[i:i + crop_height, j:j + crop_width]
# 创建新的JSON数据,基于大图的JSON数据进行调整
small_json_data = large_json_data.copy()
# 遍历大图标注看是否存在在小图中
new_target = [] # 替换'shapes'
for items in large_json_data[('shapes')]:
# 提取边界框坐标
x1, y1 = items["points"][0]
x2, y2 = items["points"][1]
# 判断目标框在小图中的位置
# 1、目标框全在小图中
new_item = items.copy()
mc = [p for p in items['points'] if (p[0] > j and p[0] < j+crop_width) and (p[1] >i and p[1] <i+crop_height)]
if len(mc) == len(items['points']):
new_item['points'] = []
for p in items['points']:
new_item['points'].append([p[0]-j,p[1]-i])
new_target.append(new_item)
if new_target != []:
small_json_data['shapes'] = new_target
small_json_data["imageWidth"] = crop_width
small_json_data["imageHeight"] = crop_height
small_json_data['imageData'] = tobase64(small_image)
# 保存小图的JSON文件
small_json_filename = f'{name}_{i}_{j}.json'
with open(os.path.join(OPath_json, small_json_filename), 'w') as small_json_file:
json.dump(small_json_data, small_json_file, indent=4)
# 保存小图的JPEG图像
small_image_filename = f'{name}_{i}_{j}.jpg'
cv2.imwrite(os.path.join(OPath_img, small_image_filename), small_image)
# 完成后,你会得到多个小图像和对应的JSON文件
if __name__ == '__main__':
IPath_json = 'E:\pycode\ghbz\markbz'
IPath_img = 'E:\pycode\ghbz\markbz'
OPath_json = 'E:\pycode\ghbz\img_s500'
OPath_img = 'E:\pycode\ghbz\img_s500'
if not os.path.exists(OPath_json):
os.mkdir(OPath_json)
if not os.path.exists(OPath_img):
os.mkdir(OPath_img)
splitImgandJson(IPath_json,IPath_img,OPath_json,OPath_img)