From d6e2af9bd54def51326f3f5e79aca45febdb16b7 Mon Sep 17 00:00:00 2001 From: brian95827 <54231251+brian95827@users.noreply.github.com> Date: Sun, 25 Feb 2024 06:15:59 +0800 Subject: [PATCH 01/10] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5c72d6a..3b1dd96 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # segment_anything_gui +**也可以直接将seg5.py文件复制到segment_anything项目下使用** + 这是一个类似PS的抠图工具,支持cpu和英伟达gpu。推荐opencv-python版本4.5.5.64(4.x大概都能跑) 使用方法 From acbc117342b08ef77d9046ca6d4be34da9fc8262 Mon Sep 17 00:00:00 2001 From: brian95827 <54231251+brian95827@users.noreply.github.com> Date: Sun, 25 Feb 2024 06:16:15 +0800 Subject: [PATCH 02/10] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3b1dd96..5d50096 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # segment_anything_gui -**也可以直接将seg5.py文件复制到segment_anything项目下使用** + +## **也可以直接将seg5.py文件复制到segment_anything项目下使用** 这是一个类似PS的抠图工具,支持cpu和英伟达gpu。推荐opencv-python版本4.5.5.64(4.x大概都能跑) From ec897e381aec82233edff26e9f9b7a79a495219a Mon Sep 17 00:00:00 2001 From: brian95827 <54231251+brian95827@users.noreply.github.com> Date: Sun, 25 Feb 2024 06:16:50 +0800 Subject: [PATCH 03/10] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d50096..6e5d3fb 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,12 @@ # segment_anything_gui - +--- ## **也可以直接将seg5.py文件复制到segment_anything项目下使用** +--- + +## 使用方法 + 这是一个类似PS的抠图工具,支持cpu和英伟达gpu。推荐opencv-python版本4.5.5.64(4.x大概都能跑) 使用方法 From 7d7407a2425f0de540867279f2f0c2f88bb0e0d4 Mon Sep 17 00:00:00 2001 From: brian95827 <54231251+brian95827@users.noreply.github.com> Date: Mon, 26 Feb 2024 09:34:33 +0800 Subject: [PATCH 04/10] Update README.md --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6e5d3fb..997c38c 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,6 @@ # segment_anything_gui ---- -## **也可以直接将seg5.py文件复制到segment_anything项目下使用** ---- ## 使用方法 @@ -43,6 +40,11 @@ https://github.com/facebookresearch/segment-anything 程序将在output文件夹中生成抠好的图片,新切割出来图片的文件名会自增。 +--- +## **也可以直接将seg5.py文件复制到segment_anything项目下使用** + +--- + Segment-Anything-GUI From ee41a39faece8bddfb5daf02f157fa6fdd0e5292 Mon Sep 17 00:00:00 2001 From: brian Date: Tue, 27 Feb 2024 03:16:45 +0800 Subject: [PATCH 05/10] =?UTF-8?q?1.=20=E6=96=B0=E5=A2=9E=E7=BC=A9=E6=94=BE?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=202.=20=E6=96=B0=E5=A2=9E=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=203.=20=E4=BF=AE=E5=A4=8D=E5=B7=B2=E8=A2=AB?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=83=A8=E5=88=86=E8=83=BD=E8=A2=ABstable=20?= =?UTF-8?q?diffusion=E6=A3=80=E6=B5=8B=E7=9A=84=E9=97=AE=E9=A2=98=204.=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=9A=90=E8=97=8F=E6=8F=90=E7=A4=BA=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seg5.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/seg5.py b/seg5.py index 09524d6..3d07a7e 100644 --- a/seg5.py +++ b/seg5.py @@ -3,19 +3,28 @@ import numpy as np from segment_anything import sam_model_registry, SamPredictor -input_dir = 'input' -output_dir = 'output' -crop_mode=True#是否裁剪到最小范围 +input_dir = r'G:\xiaowu-pic\133_new' +output_dir = r'G:\xiaowu-pic\133_new_segment' +skip_dir = r'G:\xiaowu-pic\133_done' +crop_mode=False#是否裁剪到最小范围 #alpha_channel是否保留透明通道 print('最好是每加一个点就按w键predict一次') os.makedirs(output_dir, exist_ok=True) image_files = [f for f in os.listdir(input_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg','.JPG','.JPEG','.PNG'))] +skip_files = [] +if os.path.exists(skip_dir) and os.path.isdir(skip_dir): + skip_files = [f for f in os.listdir(skip_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.JPG', '.JPEG', '.PNG'))] + skip_files = [f[:f.rfind('_')] for f in skip_files] + image_files = [f for f in image_files if f[:f.rfind('.')] not in skip_files] -sam = sam_model_registry["vit_b"](checkpoint="sam_vit_b_01ec64.pth") +sam = sam_model_registry["vit_h"](checkpoint="./checkpoint/sam_vit_h_4b8939.pth") _ = sam.to(device="cuda")#注释掉这一行,会用cpu运行,速度会慢很多 predictor = SamPredictor(sam) def mouse_click(event, x, y, flags, param): global input_point, input_label, input_stop + + x = round(x / zoom_rate) + y = round(y / zoom_rate) if not input_stop: if event == cv2.EVENT_LBUTTONDOWN : input_point.append([x, y]) @@ -29,12 +38,12 @@ def mouse_click(event, x, y, flags, param): def apply_mask(image, mask, alpha_channel=True): - if alpha_channel: + image = np.where(mask[..., None] == 1, image, 0) + + if alpha_channel and image.shape[-1] == 3: alpha = np.zeros_like(image[..., 0]) alpha[mask == 1] = 255 image = cv2.merge((image[..., 0], image[..., 1], image[..., 2], alpha)) - else: - image = np.where(mask[..., None] == 1, image, 0) return image def apply_color_mask(image, mask, color, color_dark = 0.5): @@ -44,6 +53,8 @@ def apply_color_mask(image, mask, color, color_dark = 0.5): def get_next_filename(base_path, filename): name, ext = os.path.splitext(filename) + if name in skip_files: + return None for i in range(1, 101): new_name = f"{name}_{i}{ext}" if not os.path.exists(os.path.join(base_path, new_name)): @@ -78,6 +89,8 @@ def save_masked_image(image, mask, output_dir, filename, crop_mode_): input_point = [] input_label = [] input_stop=False +zoom_rate = 1 +show_info = True while True: filename = image_files[current_index] image_orign = cv2.imread(os.path.join(input_dir, filename)) @@ -89,8 +102,18 @@ def save_masked_image(image, mask, output_dir, filename, crop_mode_): #print(input_point) input_stop=False image_display = image_orign.copy() - display_info = f'{filename} | Press s to save | Press w to predict | Press d to next image | Press a to previous image | Press space to clear | Press q to remove last point ' - cv2.putText(image_display, display_info, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2, cv2.LINE_AA) + + display_info = f'{filename} | Press s to save | Press w to predict | Press d to next image ' + display_info1 = f'| Press a to previous image | Press space to clear | Press q to remove last point ' + display_info2 = f'| Press e to hide suggestion info' + if show_info: + cv2.putText(image_display, display_info, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2, + cv2.LINE_AA) + cv2.putText(image_display, display_info1, (10, 30 + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2, + cv2.LINE_AA) + cv2.putText(image_display, display_info2, (10, 30 + 40 + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), + 2, cv2.LINE_AA) + for point, label in zip(input_point, input_label): color = (0, 255, 0) if label == 1 else (0, 0, 255) cv2.circle(image_display, tuple(point), 5, color, -1) @@ -98,6 +121,7 @@ def save_masked_image(image, mask, output_dir, filename, crop_mode_): color = tuple(np.random.randint(0, 256, 3).tolist()) selected_image = apply_color_mask(image_display,selected_mask, color) + image_display = cv2.resize(image_display, None, fx=zoom_rate, fy=zoom_rate, interpolation=cv2.INTER_LINEAR) cv2.imshow("image", image_display) key = cv2.waitKey(1) @@ -128,9 +152,19 @@ def save_masked_image(image, mask, output_dir, filename, crop_mode_): image_select = image_orign.copy() selected_mask=masks[mask_idx] selected_image = apply_color_mask(image_select,selected_mask, color) - mask_info = f'Total: {num_masks} | Current: {mask_idx} | Score: {scores[mask_idx]:.2f} | Press w to confirm | Press d to next mask | Press a to previous mask | Press q to remove last point | Press s to save' - cv2.putText(selected_image, mask_info, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2, cv2.LINE_AA) + mask_info = f'Total: {num_masks} | Current: {mask_idx} | Score: {scores[mask_idx]:.2f} | Press w to confirm ' + mask_info1 = f'| Press d to next mask | Press a to previous mask | Press q to remove last point ' + mask_info2 = f'| Press s to save | Press e to hide suggestion info' + if show_info: + cv2.putText(selected_image, mask_info, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), + 2, cv2.LINE_AA) + cv2.putText(selected_image, mask_info1, (10, 30 + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, + (0, 255, 255), 2, cv2.LINE_AA) + cv2.putText(selected_image, mask_info2, (10, 30 + 40 + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, + (0, 255, 255), 2, cv2.LINE_AA) + + selected_image = cv2.resize(selected_image, None, fx=zoom_rate, fy=zoom_rate, interpolation=cv2.INTER_LINEAR) cv2.imshow("image", selected_image) key=cv2.waitKey(10) @@ -157,6 +191,14 @@ def save_masked_image(image, mask, output_dir, filename, crop_mode_): selected_mask = None logit_input= None break + elif key == ord('+'): + zoom_rate += 0.05 + elif key == ord('-'): + if zoom_rate > 0.05: + zoom_rate -= 0.05 + elif key == ord('e'): + show_info = not show_info + logit_input=logits[mask_idx, :, :] print('max score:',np.argmax(scores),' select:',mask_idx) @@ -177,6 +219,13 @@ def save_masked_image(image, mask, output_dir, filename, crop_mode_): input_label.pop(-1) elif key == ord('s') and selected_mask is not None : save_masked_image(image_crop, selected_mask, output_dir, filename, crop_mode_=crop_mode) + elif key == ord('+'): + zoom_rate += 0.05 + elif key == ord('-'): + if zoom_rate > 0.05: + zoom_rate -= 0.05 + elif key == ord('e'): + show_info = not show_info if key == 27: break From 12aba23868b454e8783bd413045fd13e9aaf95e7 Mon Sep 17 00:00:00 2001 From: brian Date: Tue, 27 Feb 2024 12:27:11 +0800 Subject: [PATCH 06/10] =?UTF-8?q?1.=20=E6=96=B0=E5=A2=9E=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E7=99=BD=E8=83=8C=E6=99=AF=E7=9A=84=E5=8A=9F=E8=83=BD=E9=80=89?= =?UTF-8?q?=E9=A1=B9=202.=20=E6=A0=B9=E6=8D=AE=E7=BC=96=E8=BE=91=E5=99=A8?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E8=B0=83=E6=95=B4=E4=BA=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seg5.py | 189 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 105 insertions(+), 84 deletions(-) diff --git a/seg5.py b/seg5.py index 3d07a7e..b90d7aa 100644 --- a/seg5.py +++ b/seg5.py @@ -3,123 +3,147 @@ import numpy as np from segment_anything import sam_model_registry, SamPredictor -input_dir = r'G:\xiaowu-pic\133_new' +input_dir = r'G:\xiaowu-pic\133_select_new' output_dir = r'G:\xiaowu-pic\133_new_segment' skip_dir = r'G:\xiaowu-pic\133_done' -crop_mode=False#是否裁剪到最小范围 -#alpha_channel是否保留透明通道 +crop_mode = False # 是否裁剪到最小范围 +# alpha_channel = True # alpha_channel是否保留透明通道 +save_background_img = False # 是否同步生成白背景图片 + print('最好是每加一个点就按w键predict一次') os.makedirs(output_dir, exist_ok=True) -image_files = [f for f in os.listdir(input_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg','.JPG','.JPEG','.PNG'))] +image_files = [f for f in os.listdir(input_dir) if + f.lower().endswith(('.png', '.jpg', '.jpeg', '.JPG', '.JPEG', '.PNG'))] skip_files = [] if os.path.exists(skip_dir) and os.path.isdir(skip_dir): - skip_files = [f for f in os.listdir(skip_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.JPG', '.JPEG', '.PNG'))] + skip_files = [f for f in os.listdir(skip_dir) if + f.lower().endswith(('.png', '.jpg', '.jpeg', '.JPG', '.JPEG', '.PNG'))] skip_files = [f[:f.rfind('_')] for f in skip_files] image_files = [f for f in image_files if f[:f.rfind('.')] not in skip_files] sam = sam_model_registry["vit_h"](checkpoint="./checkpoint/sam_vit_h_4b8939.pth") -_ = sam.to(device="cuda")#注释掉这一行,会用cpu运行,速度会慢很多 +_ = sam.to(device="cuda") # 注释掉这一行,会用cpu运行,速度会慢很多 predictor = SamPredictor(sam) + + def mouse_click(event, x, y, flags, param): global input_point, input_label, input_stop x = round(x / zoom_rate) y = round(y / zoom_rate) if not input_stop: - if event == cv2.EVENT_LBUTTONDOWN : + if event == cv2.EVENT_LBUTTONDOWN: input_point.append([x, y]) input_label.append(1) - elif event == cv2.EVENT_RBUTTONDOWN : + elif event == cv2.EVENT_RBUTTONDOWN: input_point.append([x, y]) input_label.append(0) else: - if event == cv2.EVENT_LBUTTONDOWN or event == cv2.EVENT_RBUTTONDOWN : + if event == cv2.EVENT_LBUTTONDOWN or event == cv2.EVENT_RBUTTONDOWN: print('此时不能添加点,按w退出mask选择模式') def apply_mask(image, mask, alpha_channel=True): image = np.where(mask[..., None] == 1, image, 0) + images = [image] + if save_background_img: + image_white = np.where(mask[..., None] == 1, image, 255) + images.append(image_white) - if alpha_channel and image.shape[-1] == 3: - alpha = np.zeros_like(image[..., 0]) - alpha[mask == 1] = 255 - image = cv2.merge((image[..., 0], image[..., 1], image[..., 2], alpha)) - return image + for i, im in enumerate(images): + if alpha_channel and im.shape[-1] == 3: + alpha = np.zeros_like(im[..., 0]) + alpha[mask == 1] = 255 + images[i] = cv2.merge((im[..., 0], im[..., 1], im[..., 2], alpha)) + return images -def apply_color_mask(image, mask, color, color_dark = 0.5): + +def apply_color_mask(image, mask, color, color_dark=0.5): for c in range(3): image[:, :, c] = np.where(mask == 1, image[:, :, c] * (1 - color_dark) + color_dark * color[c], image[:, :, c]) return image + def get_next_filename(base_path, filename): name, ext = os.path.splitext(filename) - if name in skip_files: - return None + for i in range(1, 101): new_name = f"{name}_{i}{ext}" - if not os.path.exists(os.path.join(base_path, new_name)): - return new_name + names = [new_name] + if save_background_img: + new_name_white = f"{name}_{i}w{ext}" + names.append(new_name_white) + if not os.path.exists(os.path.join(base_path, names[0])) and (len(names) == 1 or not os.path.exists( + os.path.join(base_path, names[1]))): + return names return None + def save_masked_image(image, mask, output_dir, filename, crop_mode_): if crop_mode_: y, x = np.where(mask) y_min, y_max, x_min, x_max = y.min(), y.max(), x.min(), x.max() - cropped_mask = mask[y_min:y_max+1, x_min:x_max+1] - cropped_image = image[y_min:y_max+1, x_min:x_max+1] - masked_image = apply_mask(cropped_image, cropped_mask) + cropped_mask = mask[y_min:y_max + 1, x_min:x_max + 1] + cropped_image = image[y_min:y_max + 1, x_min:x_max + 1] + masked_images = apply_mask(cropped_image, cropped_mask) else: - masked_image = apply_mask(image, mask) - filename = filename[:filename.rfind('.')]+'.png' - new_filename = get_next_filename(output_dir, filename) - - if new_filename: - if masked_image.shape[-1] == 4: - cv2.imwrite(os.path.join(output_dir, new_filename), masked_image, [cv2.IMWRITE_PNG_COMPRESSION, 9]) - else: - cv2.imwrite(os.path.join(output_dir, new_filename), masked_image) - print(f"Saved as {new_filename}") + masked_images = apply_mask(image, mask) + + filename = filename[:filename.rfind('.')] + '.png' + new_filenames = get_next_filename(output_dir, filename) + + if new_filenames and masked_images: + for new_filename, masked_image in zip(new_filenames, masked_images): + if masked_image.shape[-1] == 4: + cv2.imwrite(os.path.join(output_dir, new_filename), masked_image, [cv2.IMWRITE_PNG_COMPRESSION, 9]) + else: + cv2.imwrite(os.path.join(output_dir, new_filename), masked_image) + print(f"Saved as {new_filename}") else: print("Could not save the image. Too many variations exist.") + +def show_infos(info, show, image_to_show): + if not show: + return + + for i, inf in enumerate(info): + cv2.putText(image_to_show, inf, (10, 30 + 40 * i), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2, cv2.LINE_AA) + + current_index = 0 cv2.namedWindow("image") cv2.setMouseCallback("image", mouse_click) input_point = [] input_label = [] -input_stop=False +input_stop = False zoom_rate = 1 show_info = True while True: filename = image_files[current_index] - image_orign = cv2.imread(os.path.join(input_dir, filename)) + image_orign = cv2.imread(os.path.join(input_dir, filename), cv2.IMREAD_UNCHANGED) image_crop = image_orign.copy() image = cv2.cvtColor(image_orign.copy(), cv2.COLOR_BGR2RGB) selected_mask = None - logit_input= None + logit_input = None while True: - #print(input_point) - input_stop=False + # print(input_point) + input_stop = False image_display = image_orign.copy() - display_info = f'{filename} | Press s to save | Press w to predict | Press d to next image ' - display_info1 = f'| Press a to previous image | Press space to clear | Press q to remove last point ' - display_info2 = f'| Press e to hide suggestion info' - if show_info: - cv2.putText(image_display, display_info, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2, - cv2.LINE_AA) - cv2.putText(image_display, display_info1, (10, 30 + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2, - cv2.LINE_AA) - cv2.putText(image_display, display_info2, (10, 30 + 40 + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), - 2, cv2.LINE_AA) + display_info = f'{filename} | Press s to save | Press w to predict' + display_info1 = f'| Press a to previous image | Press d to next image | Press space to clear' + display_info2 = f'| Press q to remove last point | Press +,- to zoom in or out | Press e to hide suggestion info' + infos = [display_info, display_info1, display_info2] + show_infos(infos, show_info, image_display) for point, label in zip(input_point, input_label): color = (0, 255, 0) if label == 1 else (0, 0, 255) cv2.circle(image_display, tuple(point), 5, color, -1) - if selected_mask is not None : + if selected_mask is not None: color = tuple(np.random.randint(0, 256, 3).tolist()) - selected_image = apply_color_mask(image_display,selected_mask, color) + selected_image = apply_color_mask(image_display, selected_mask, color) image_display = cv2.resize(image_display, None, fx=zoom_rate, fy=zoom_rate, interpolation=cv2.INTER_LINEAR) cv2.imshow("image", image_display) @@ -129,67 +153,64 @@ def save_masked_image(image, mask, output_dir, filename, crop_mode_): input_point = [] input_label = [] selected_mask = None - logit_input= None + logit_input = None elif key == ord("w"): - input_stop=True + input_stop = True if len(input_point) > 0 and len(input_label) > 0: - + predictor.set_image(image) input_point_np = np.array(input_point) input_label_np = np.array(input_label) - masks, scores, logits= predictor.predict( + masks, scores, logits = predictor.predict( point_coords=input_point_np, point_labels=input_label_np, mask_input=logit_input[None, :, :] if logit_input is not None else None, multimask_output=True, ) - mask_idx=0 + mask_idx = 0 num_masks = len(masks) - while(1): + while (1): color = tuple(np.random.randint(0, 256, 3).tolist()) image_select = image_orign.copy() - selected_mask=masks[mask_idx] - selected_image = apply_color_mask(image_select,selected_mask, color) + selected_mask = masks[mask_idx] + selected_image = apply_color_mask(image_select, selected_mask, color) - mask_info = f'Total: {num_masks} | Current: {mask_idx} | Score: {scores[mask_idx]:.2f} | Press w to confirm ' + mask_info = f'Total: {num_masks} | Current: {mask_idx} | Score: {scores[mask_idx]:.2f} | Press w ' \ + f'to confirm' mask_info1 = f'| Press d to next mask | Press a to previous mask | Press q to remove last point ' - mask_info2 = f'| Press s to save | Press e to hide suggestion info' - if show_info: - cv2.putText(selected_image, mask_info, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), - 2, cv2.LINE_AA) - cv2.putText(selected_image, mask_info1, (10, 30 + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, - (0, 255, 255), 2, cv2.LINE_AA) - cv2.putText(selected_image, mask_info2, (10, 30 + 40 + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, - (0, 255, 255), 2, cv2.LINE_AA) - - selected_image = cv2.resize(selected_image, None, fx=zoom_rate, fy=zoom_rate, interpolation=cv2.INTER_LINEAR) + mask_info2 = f'| Press s to save | Press +,- to zoom in or out | Press e to hide suggestion info' + infos = [mask_info, mask_info1, mask_info2] + show_infos(infos, show_info, selected_image) + + selected_image = cv2.resize(selected_image, None, fx=zoom_rate, fy=zoom_rate, + interpolation=cv2.INTER_LINEAR) cv2.imshow("image", selected_image) - key=cv2.waitKey(10) - if key == ord('q') and len(input_point)>0: + key = cv2.waitKey(10) + if key == ord('q') and len(input_point) > 0: input_point.pop(-1) input_label.pop(-1) elif key == ord('s'): save_masked_image(image_crop, selected_mask, output_dir, filename, crop_mode_=crop_mode) - elif key == ord('a') : - if mask_idx>0: - mask_idx-=1 + elif key == ord('a'): + if mask_idx > 0: + mask_idx -= 1 else: - mask_idx=num_masks-1 - elif key == ord('d') : - if mask_idx0: + elif key == ord('q') and len(input_point) > 0: input_point.pop(-1) input_label.pop(-1) - elif key == ord('s') and selected_mask is not None : + elif key == ord('s') and selected_mask is not None: save_masked_image(image_crop, selected_mask, output_dir, filename, crop_mode_=crop_mode) elif key == ord('+'): zoom_rate += 0.05 From 941de266b81080fef93b57d5dbc48011335e93ec Mon Sep 17 00:00:00 2001 From: brian95827 <54231251+brian95827@users.noreply.github.com> Date: Tue, 27 Feb 2024 12:40:32 +0800 Subject: [PATCH 07/10] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 997c38c..3b4ca99 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ https://github.com/facebookresearch/segment-anything 按下s键保存抠图结果(如果有生成过Mask的话)。 +按下 `+`或`-`放大或缩小窗口 + 3.Mask选取模式: @@ -32,6 +34,8 @@ https://github.com/facebookresearch/segment-anything 按下w键返回选点模式,下次模型将会在此mask基础上进行预测 +按下 `+`或`-`放大或缩小窗口 + 4.返回选点模式,迭代优化选点 把不需要的地方右键点一下,需要但mask没覆盖的地方左键点一下,几个点就行,别太多了 @@ -40,6 +44,7 @@ https://github.com/facebookresearch/segment-anything 程序将在output文件夹中生成抠好的图片,新切割出来图片的文件名会自增。 + --- ## **也可以直接将seg5.py文件复制到segment_anything项目下使用** From 6afa4b1c31fd0069ee303047140769e3867838b3 Mon Sep 17 00:00:00 2001 From: brian95827 <54231251+brian95827@users.noreply.github.com> Date: Tue, 27 Feb 2024 12:43:06 +0800 Subject: [PATCH 08/10] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3b4ca99..aae6cbe 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ https://github.com/facebookresearch/segment-anything --- ## **也可以直接将seg5.py文件复制到segment_anything项目下使用** - +## **You can also directly copy the seg5.py file to the segment_anything project for use** --- @@ -74,6 +74,8 @@ Press the q key to delete the last selected point. Press the s key to save the segmentation result (if a mask has been generated). +Press + or - to zoom in or out of the window + Mask selection mode: Press the w key to use the model for prediction and enter the mask selection mode. @@ -84,6 +86,8 @@ Press the s key to save the segmentation result. Press the w key to return to point selection mode. The model will predict based on this mask the next time. +Press + or - to zoom in or out of the window + Return to point selection mode to iteratively optimize selected points: Right-click on the areas you don't need and left-click on the areas you need but are not covered by the mask. Just a few points are enough; don't use too many. From 3a7f49f20335d0f460bc822b684398a6ee05cd56 Mon Sep 17 00:00:00 2001 From: brian95827 <54231251+brian95827@users.noreply.github.com> Date: Wed, 28 Feb 2024 07:02:45 +0800 Subject: [PATCH 09/10] Update README.md --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index aae6cbe..6575560 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,16 @@ 使用方法 +0. 打开程序文件,修改配置 +``` + input_dir = r'G:\xiaowu-pic\133_select_new' + output_dir = r'G:\xiaowu-pic\133_new_segment' + skip_dir = r'G:\xiaowu-pic\133_done' # 需要跳动的文件(可以放原文件,也可以放程序输出的文件) + crop_mode = False # 是否裁剪到最小范围 + # alpha_channel = True # alpha_channel是否保留透明通道 + save_background_img = True # 是否同步生成白背景图片 +``` + 1.将待抠图的图片放到input文件夹中,然后启动程序。 https://github.com/facebookresearch/segment-anything @@ -17,25 +27,30 @@ https://github.com/facebookresearch/segment-anything 在图像上左键单击选择前景点(绿色),右键单击选择背景点(红色)。 -按下a或d键切换到上一张或下一张图片。按下空格键清除所有选点和mask。按下q键删除最后一个选点。 +按下`a`或d``键切换到上一张或下一张图片。按下空格键清除所有选点和mask。按下q键删除最后一个选点。 -按下s键保存抠图结果(如果有生成过Mask的话)。 +按下`s`键保存抠图结果(如果有生成过Mask的话)。 按下 `+`或`-`放大或缩小窗口 +按下`e`键隐藏提示信息 + 3.Mask选取模式: -按下w键使用模型进行预测,进入Mask选取模式。 +按下`w`键使用模型进行预测,进入Mask选取模式。 -在Mask选取模式下,可以按下a和d键切换不同的Mask。 +在Mask选取模式下,可以按下`a`和`d`键切换不同的Mask。 -按下s键保存抠图结果。 +按下`s`键保存抠图结果。 -按下w键返回选点模式,下次模型将会在此mask基础上进行预测 +按下`w`键返回选点模式,下次模型将会在此mask基础上进行预测 按下 `+`或`-`放大或缩小窗口 +按下`e`键隐藏提示信息 + + 4.返回选点模式,迭代优化选点 把不需要的地方右键点一下,需要但mask没覆盖的地方左键点一下,几个点就行,别太多了 @@ -58,6 +73,16 @@ This is a Photoshop-like image segmentation and extraction tool, supporting both How to Use +Open the program file and modify the configuration +``` + input_dir = r'G:\xiaowu-pic\133_select_new' + output_dir = r'G:\xiaowu-pic\133_new_segment' + skip_dir = r'G:\xiaowu-pic\133_done' # Files that need to be jumped (you can put the original file or the file output by the program) + crop_mode = False # Whether to crop to the minimum range + # alpha_channel = True # Whether alpha_channel retains the transparent channel + save_background_img = True # Whether to generate white background images simultaneously +``` + Place the images to be segmented into the input folder, then run the program. Point selection mode (one by one, multiple points at once may have average results): @@ -66,27 +91,31 @@ Left-click on the image to select foreground points (green). Right-click to select background points (red). -Press the a or d key to switch to the previous or next image. +Press the `a` or `d` key to switch to the previous or next image. Press the spacebar to clear all selected points and masks. -Press the q key to delete the last selected point. +Press the `q` key to delete the last selected point. + +Press the `s` key to save the segmentation result (if a mask has been generated). -Press the s key to save the segmentation result (if a mask has been generated). +Press `+` or `-` to zoom in or out of the window -Press + or - to zoom in or out of the window +Press the `e` key to hide the prompt message Mask selection mode: -Press the w key to use the model for prediction and enter the mask selection mode. +Press the `w` key to use the model for prediction and enter the mask selection mode. In the mask selection mode, you can press the a and d keys to switch between different masks. -Press the s key to save the segmentation result. +Press the `s` key to save the segmentation result. + +Press the `w` key to return to point selection mode. The model will predict based on this mask the next time. -Press the w key to return to point selection mode. The model will predict based on this mask the next time. +Press `+` or `-` to zoom in or out of the window -Press + or - to zoom in or out of the window +Press the `e` key to hide the prompt message Return to point selection mode to iteratively optimize selected points: From 4c8d1d4844504d9f86c7f4f281d24e4e02d79e69 Mon Sep 17 00:00:00 2001 From: brian95827 <54231251+brian95827@users.noreply.github.com> Date: Wed, 28 Feb 2024 07:03:20 +0800 Subject: [PATCH 10/10] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6575560..3335440 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ 使用方法 -0. 打开程序文件,修改配置 +0.打开程序文件,修改配置 + ``` input_dir = r'G:\xiaowu-pic\133_select_new' output_dir = r'G:\xiaowu-pic\133_new_segment'