import numpy as np from PIL import Image, ImageDraw from svgpathtools import svg2paths import cv2 import argparse import json import os # 使用示例 parser = argparse.ArgumentParser() parser.add_argument('--dataset', type=str, default='00000') parser.add_argument('--grids', type=int, nargs='+', default=[3,6]) parser.add_argument('--pieces', type=int, nargs='+', default=[8,9,14,15], help='1-based') args = parser.parse_args() svg_file = f'src/{args.dataset}.svg' image_file = f'src/{args.dataset}.png' ori_img = cv2.imread(image_file) ori_img = cv2.resize(ori_img, (600, 400)) # ratio = 600 / max(ori_img.shape[0], ori_img.shape[1]) # ori_img = cv2.resize(ori_img, (int(ori_img.shape[1] * ratio), int(ori_img.shape[0] * ratio))) # vis = np.zeros_like(ori_img[:,:,0], dtype=np.uint8) def get_boundary(svg_file): # 读取 SVG 文件并解析路径 paths, attributes = svg2paths(svg_file) for path in paths: points = [] for segment in path: # print(segment) sample_num = max(ori_img.shape) * 2 for i in range(sample_num): # 采样点 point = segment.point(i / sample_num) points.append((point.real, point.imag)) vis[min(round(point.imag), ori_img.shape[0] - 1), min(round(point.real), ori_img.shape[1] - 1)] = 255 # # cv2.imwrite('mask.png', vis.astype(np.uint8)) get_boundary(svg_file) # ... seg img img = cv2.cvtColor(ori_img, cv2.COLOR_RGB2RGBA) alpha_channel = np.full((img.shape[0], img.shape[1]), 255, dtype=np.uint8) img = np.dstack((img[:, :, :3], alpha_channel)) height = ori_img.shape[0] width = ori_img.shape[1] h_size = height // args.grids[0] w_size = width // args.grids[1] correct = [] tot_mask = np.zeros_like(vis) os.makedirs(f'{args.dataset}', exist_ok=True) for y in range(args.grids[0]): for x in range(args.grids[1]): id = y * args.grids[1] + x + 1 if id in args.pieces: new_mask = np.zeros((vis.shape[0]+2, vis.shape[1]+2), dtype=np.uint8) seed_point = (w_size * x + w_size // 2, h_size * y + h_size // 2) cv2.floodFill(vis, new_mask, seed_point, id, loDiff=(0,)*vis.ndim, upDiff=(0,)*vis.ndim, flags=4) img_mask = np.where(vis == id, 1, 0).astype(np.uint8) kernel = np.ones((3,3), dtype=np.uint8) img_mask = cv2.dilate(img_mask, kernel, iterations=1) rows, cols = np.where(img_mask == 1) piece = {} piece['id'] = args.pieces.index(id) + 1 piece['image'] = f'{args.pieces.index(id) + 1}.png' piece['gt_x'] = int((cols.min() + cols.max()) // 2) piece['gt_y'] = int((rows.min() + rows.max()) // 2) correct.append(piece) piece_img = img.copy() piece_img[:,:, 3] = np.where(img_mask == 0, 0, piece_img[:,:, 3]) piece_img = piece_img[rows.min():rows.max()+1,cols.min():cols.max()+1,:] cv2.imwrite(f'{args.dataset}/{piece["id"]}.png', piece_img) tot_mask |= img_mask img[:,:, 0] = np.where(tot_mask, 0, img[:,:, 0]) img[:,:, 1] = np.where(tot_mask, 0, img[:,:, 1]) img[:,:, 2] = np.where(tot_mask, 0, img[:,:, 2]) img[:,:, 3] = np.where(tot_mask, 0, img[:,:, 3]) # cv2.imwrite('mask.png', vis) cv2.imwrite(f'{args.dataset}/0.png', img) with open(f'{args.dataset}/correct.json', 'w') as file: json.dump(correct, file, indent=4) ''' (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00001 --grids 3 6 --pieces 8 9 14 15 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00002 --grids 3 6 --pieces 7 8 13 14 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00002 --grids 3 6 --pieces 7 8 9 10 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00002 --grids 3 6 --pieces 7 8 13 14 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00003 --grids 3 6 --pieces 7 8 13 14 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00003 --grids 3 6 --pieces 7 8 9 10 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00004 --grids 3 5 --pieces 8 9 13 14 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00005 --grids 3 5 --pieces 1 2 3 8 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00006 --grids 3 5 --pieces 2 4 5 9 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00007 --grids 3 5 --pieces 3 4 8 9 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00008 --grids 4 5 --pieces 5 10 11 17 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00008 --grids 4 6 --pieces 5 10 11 17 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00008 --grids 4 6 --pieces 5 10 11 16 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00008 --grids 4 6 --pieces 5 9 10 11 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00009 --grids 4 6 --pieces 10 16 22 23 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00010 --grids 4 6 --pieces 6 12 16 22 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00011 --grids 4 6 --pieces 14 19 20 22 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00012 --grids 3 4 --pieces 1 5 9 12 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00013 --grids 3 4 --pieces 3 4 7 8 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00013 --grids 3 4 --pieces 3 4 8 12 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00014 --grids 3 4 --pieces 7 8 11 12 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00015 --grids 3 4 --pieces 1 5 6 9 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00015 --grids 3 3 --pieces 1 2 3 4 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00015 --grids 3 4 --pieces 1 5 6 9 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00016 --grids 3 4 --pieces 1 5 6 9 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00016 --grids 3 4 --pieces 1 5 6 12 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00016 --grids 3 4 --pieces 1 5 9 12 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00016 --grids 3 3 --pieces 1 2 4 7 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00016 --grids 3 4 --pieces 1 5 9 12 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00017 --grids 3 3 --pieces 1 2 4 7 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00018 --grids 3 3 --pieces 3 5 6 9 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00018 --grids 3 3 --pieces 3 5 6 9 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00019 --grids 3 3 --pieces 3 5 6 9 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00019 --grids 3 6 --pieces 8 9 15 16 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00019 --grids 3 6 --pieces 8 9 15 16 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00011 --grids 5 6 --pieces 7 8 13 14 19 20 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00011 --grids 4 6 --pieces 1 2 7 8 13 14 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00011 --grids 4 7 --pieces 1 2 7 8 13 14 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00011 --grids 4 7 --pieces 1 2 8 9 15 16 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00011 --grids 4 7 --pieces 10 11 12 17 18 19 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00011 --grids 4 7 --pieces 1 2 8 9 15 16 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00012 --grids 4 7 --pieces 1 2 8 9 15 16 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00012 --grids 4 7 --pieces 10 11 12 17 18 19 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00013 --grids 4 7 --pieces 13 14 20 21 27 28 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00013 --grids 5 7 --pieces 17 18 19 20 26 27 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00013 --grids 4 7 --pieces 13 14 20 21 27 28 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00014 --grids 5 7 --pieces 13 14 20 21 27 28 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00014 --grids 5 7 --pieces 17 18 19 20 26 27 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00014 --grids 5 7 --pieces 17 18 19 20 25 26 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00015 --grids 5 7 --pieces 15 16 17 22 23 24 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00015 --grids 5 7 --pieces 17 18 19 25 26 27 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00016 --grids 5 7 --pieces 17 18 19 24 25 26 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00016 --grids 5 7 --pieces 10 11 12 17 18 19 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00017 --grids 5 7 --pieces 10 11 12 17 18 19 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00017 --grids 5 8 --pieces 14 15 21 22 28 29 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00018 --grids 5 8 --pieces 9 10 22 23 26 27 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00018 --grids 5 8 --pieces 10 11 22 23 27 28 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00019 --grids 5 8 --pieces 9 10 17 18 29 30 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00019 --grids 5 8 --pieces 10 11 18 19 29 30 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00019 --grids 5 8 --pieces 9 10 17 18 29 30 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00020 --grids 6 9 --pieces 14 23 31 32 40 41 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00020 --grids 6 9 --pieces 13 14 22 23 31 32 40 41 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00021 --grids 6 9 --pieces 13 14 22 23 31 32 40 41 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00021 --grids 6 9 --pieces 22 23 24 25 31 32 33 34 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00022 --grids 5 6 --pieces 13 14 15 19 20 21 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00022 --grids 5 6 --pieces 8 9 14 15 20 21 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00022 --grids 5 6 --pieces 13 14 15 19 20 21 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00023 --grids 5 6 --pieces 15 16 21 22 29 30 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00023 --grids 5 6 --pieces 15 16 21 22 27 28 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00024 --grids 5 6 --pieces 9 11 13 15 17 21 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00024 --grids 5 6 --pieces 7 9 11 21 13 25 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00024 --grids 5 6 --pieces 7 9 11 21 23 25 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00024 --grids 5 6 --pieces 7 9 11 19 21 23 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00025 --grids 5 6 --pieces 3 9 10 15 16 17 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00025 --grids 5 6 --pieces 3 9 10 15 16 22 (sam2) liujm@FI-GPU-1:~/code/llm-puzzle/dataset$ python gen.py --dataset 00025 --grids 5 6 --pieces 3 9 10 21 22 27 '''