forked from QihanZhao/enlighten-anything
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor:
test.py
is to be replaced by test_test.py
, where result…
… is bad
- Loading branch information
HanX
committed
Jun 10, 2023
1 parent
3697d0e
commit 8fbc388
Showing
156 changed files
with
3,016 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
EXP | ||
# Ignore logs and data files | ||
EXP/ | ||
data/ | ||
segment_anything/sam_vit_h_4b8939.pth | ||
|
||
# Ignore compiled binaries and cache | ||
.ipynb_checkpoints | ||
__pycache__ | ||
|
||
# Ignore editor files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# This script batch segments pictures in <source> directory using SAM(Segment Anything). | ||
# And save the semantic pictures in <source_semantic> for colored versions, | ||
# and <source_semanticB> for binary versions | ||
|
||
# Author: Qihan Zhao | ||
|
||
# Prerequisite: | ||
# 1. download this script in directory `SAM` | ||
# 2. download SAM weights in directory `SAM` | ||
# 3. add <source> file in directory `SAM` | ||
|
||
import numpy as np | ||
import torch | ||
import matplotlib.pyplot as plt | ||
import cv2 | ||
|
||
from segment_anything import SamAutomaticMaskGenerator, sam_model_registry | ||
|
||
import argparse | ||
parser = argparse.ArgumentParser("enlighten-anything") | ||
parser.add_argument('--source_dir', type=str, default='data/LOL/test15/low', help='directory of data to be segmented') | ||
args = parser.parse_args() | ||
|
||
|
||
|
||
|
||
sam_checkpoint = "./segment_anything/sam_vit_h_4b8939.pth" | ||
model_type = "vit_h" | ||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint) | ||
sam.to(device=device) | ||
mask_generator = SamAutomaticMaskGenerator(sam) | ||
|
||
|
||
import os | ||
sourcedir = args.source_dir | ||
for i, filename in enumerate(os.listdir(sourcedir)): | ||
if filename.endswith('.jpg') or filename.endswith('.png'): | ||
print(f'{i}th pic: {filename}') | ||
# read image | ||
img_path = os.path.join(sourcedir, filename) | ||
image = cv2.imread(img_path) | ||
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | ||
|
||
# segment | ||
masks = mask_generator.generate(image) | ||
|
||
# save semantic | ||
img = np.ones((masks[0]['segmentation'].shape[0], masks[0]['segmentation'].shape[1], 4)) | ||
img[:,:,3] = 0 | ||
|
||
# Binary | ||
os.makedirs(f'{sourcedir}_semanticB', exist_ok=True) | ||
for i, mask in enumerate(masks): | ||
save_path = os.path.join(f'{sourcedir}_semanticB', f'{os.path.splitext(filename)[0]}_semanticB_{i}.png') | ||
cv2.imwrite(save_path, np.uint8(mask["segmentation"]) * 255) | ||
|
||
#Color | ||
os.makedirs(f'{sourcedir}_semantic', exist_ok=True) | ||
save_path = os.path.join(f'{sourcedir}_semantic', f'{os.path.splitext(filename)[0]}_semantic.png') | ||
print(save_path) | ||
for i, mask in enumerate(masks): | ||
mask_bool = mask['segmentation'] | ||
color_mask = np.concatenate([np.random.random(3), [0.35]]) | ||
img[mask_bool] = color_mask | ||
cv2.imwrite(save_path, np.uint8(img * 255)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
|
||
# This source code is licensed under the license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from .build_sam import ( | ||
build_sam, | ||
build_sam_vit_h, | ||
build_sam_vit_l, | ||
build_sam_vit_b, | ||
sam_model_registry, | ||
) | ||
from .predictor import SamPredictor | ||
from .automatic_mask_generator import SamAutomaticMaskGenerator |
Oops, something went wrong.