Spaces:
Sleeping
Sleeping
Update seg_script.py
Browse files- seg_script.py +20 -7
seg_script.py
CHANGED
@@ -39,16 +39,13 @@ def process_image(image_path, output_dir):
|
|
39 |
drawed = img.copy()
|
40 |
im_h, im_w = img.shape[:2]
|
41 |
|
|
|
|
|
|
|
42 |
# 如果没有检测到对象,直接返回原图
|
43 |
if instances.bboxes is None:
|
44 |
return
|
45 |
|
46 |
-
# 保存绘制后的图像(只保存一次)
|
47 |
-
base_name = Path(image_path).stem
|
48 |
-
output_path = Path(output_dir)
|
49 |
-
output_path.mkdir(parents=True, exist_ok=True)
|
50 |
-
Image.fromarray(drawed).save(output_path / f"{base_name}_drawed.png")
|
51 |
-
|
52 |
# 处理每个实例
|
53 |
for ii, (xywh, mask) in enumerate(zip(instances.bboxes, instances.masks)):
|
54 |
color = get_color(ii)
|
@@ -69,6 +66,9 @@ def process_image(image_path, output_dir):
|
|
69 |
|
70 |
drawed = drawed.astype(np.uint8)
|
71 |
|
|
|
|
|
|
|
72 |
# 裁剪图像
|
73 |
x1, y1, x2, y2 = int(xywh[0]), int(xywh[1]), int(xywh[0] + xywh[2]), int(xywh[1] + xywh[3])
|
74 |
cropped_img = img[y1:y2, x1:x2]
|
@@ -78,10 +78,23 @@ def process_image(image_path, output_dir):
|
|
78 |
alpha_channel = (cropped_mask * 255).astype(np.uint8)
|
79 |
rgba_image = np.dstack((cropped_img, alpha_channel))
|
80 |
|
81 |
-
#
|
|
|
|
|
|
|
|
|
|
|
82 |
Image.fromarray(cropped_img).save(output_path / f"{base_name}_cropped_{ii}.png")
|
|
|
|
|
83 |
Image.fromarray(rgba_image, 'RGBA').save(output_path / f"{base_name}_segmented_{ii}.png")
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
def main():
|
86 |
parser = argparse.ArgumentParser(description="Anime Instance Segmentation")
|
87 |
parser.add_argument("input_path", type=str, help="Path to the input image or folder")
|
|
|
39 |
drawed = img.copy()
|
40 |
im_h, im_w = img.shape[:2]
|
41 |
|
42 |
+
# 创建黑色背景的 mask 图像
|
43 |
+
mask_image = np.zeros((im_h, im_w), dtype=np.uint8)
|
44 |
+
|
45 |
# 如果没有检测到对象,直接返回原图
|
46 |
if instances.bboxes is None:
|
47 |
return
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
# 处理每个实例
|
50 |
for ii, (xywh, mask) in enumerate(zip(instances.bboxes, instances.masks)):
|
51 |
color = get_color(ii)
|
|
|
66 |
|
67 |
drawed = drawed.astype(np.uint8)
|
68 |
|
69 |
+
# 将当前实例的 mask 绘制到黑色背景上(白色表示 mask)
|
70 |
+
mask_image[mask > 0] = 255
|
71 |
+
|
72 |
# 裁剪图像
|
73 |
x1, y1, x2, y2 = int(xywh[0]), int(xywh[1]), int(xywh[0] + xywh[2]), int(xywh[1] + xywh[3])
|
74 |
cropped_img = img[y1:y2, x1:x2]
|
|
|
78 |
alpha_channel = (cropped_mask * 255).astype(np.uint8)
|
79 |
rgba_image = np.dstack((cropped_img, alpha_channel))
|
80 |
|
81 |
+
# 保存裁剪后的图像和分割后的图像
|
82 |
+
base_name = Path(image_path).stem
|
83 |
+
output_path = Path(output_dir)
|
84 |
+
output_path.mkdir(parents=True, exist_ok=True)
|
85 |
+
|
86 |
+
# 保存裁剪后的图像
|
87 |
Image.fromarray(cropped_img).save(output_path / f"{base_name}_cropped_{ii}.png")
|
88 |
+
|
89 |
+
# 保存分割后的图像(RGBA)
|
90 |
Image.fromarray(rgba_image, 'RGBA').save(output_path / f"{base_name}_segmented_{ii}.png")
|
91 |
|
92 |
+
# 保存绘制后的图像
|
93 |
+
Image.fromarray(drawed).save(output_path / f"{base_name}_drawed.png")
|
94 |
+
|
95 |
+
# 保存 mask 图像
|
96 |
+
Image.fromarray(mask_image).save(output_path / f"{base_name}_mask.png")
|
97 |
+
|
98 |
def main():
|
99 |
parser = argparse.ArgumentParser(description="Anime Instance Segmentation")
|
100 |
parser.add_argument("input_path", type=str, help="Path to the input image or folder")
|