-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add blood vessel dataset processing script (#184)
* Add blood vessel dataset processing script * Fix syntax error * Fix syntax error * Fix syntax error * Fix bugs * Fix bugs * Fix bugs * Use safe functions and expand more apis * Use safe functions and expand more apis * Fix hard code and verify dataset integrity
- Loading branch information
Showing
6 changed files
with
544 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import argparse | ||
import os | ||
import os.path as osp | ||
import tempfile | ||
import zipfile | ||
|
||
import mmcv | ||
|
||
CHASE_DB1_LEN = 28 * 3 | ||
TRAINING_LEN = 60 | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser( | ||
description='Convert CHASE_DB1 dataset to mmsegmentation format') | ||
parser.add_argument('dataset_path', help='path of CHASEDB1.zip') | ||
parser.add_argument('--tmp_dir', help='path of the temporary directory') | ||
parser.add_argument('-o', '--out_dir', help='output path') | ||
args = parser.parse_args() | ||
return args | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
dataset_path = args.dataset_path | ||
if args.out_dir is None: | ||
out_dir = osp.join('data', 'CHASE_DB1') | ||
else: | ||
out_dir = args.out_dir | ||
|
||
print('Making directories...') | ||
mmcv.mkdir_or_exist(out_dir) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'images')) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'images', 'training')) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'images', 'validation')) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'annotations')) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'annotations', 'training')) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'annotations', 'validation')) | ||
|
||
with tempfile.TemporaryDirectory(dir=args.tmp_dir) as tmp_dir: | ||
print('Extracting CHASEDB1.zip...') | ||
zip_file = zipfile.ZipFile(dataset_path) | ||
zip_file.extractall(tmp_dir) | ||
|
||
print('Generating training dataset...') | ||
|
||
assert len(os.listdir(tmp_dir)) == CHASE_DB1_LEN, \ | ||
'len(os.listdir(tmp_dir)) != {}'.format(CHASE_DB1_LEN) | ||
|
||
for img_name in sorted(os.listdir(tmp_dir))[:TRAINING_LEN]: | ||
img = mmcv.imread(osp.join(tmp_dir, img_name)) | ||
if osp.splitext(img_name)[1] == '.jpg': | ||
mmcv.imwrite(img, | ||
osp.join(out_dir, 'images', 'training', img_name)) | ||
else: | ||
# The annotation img should be divided by 128, because some of | ||
# the annotation imgs are not standard. We should set a | ||
# threshold to convert the nonstandard annotation imgs. The | ||
# value divided by 128 is equivalent to '1 if value >= 128 | ||
# else 0' | ||
mmcv.imwrite( | ||
img[:, :, 0] // 128, | ||
osp.join(out_dir, 'annotations', 'training', | ||
osp.splitext(img_name)[0] + '.jpg')) | ||
|
||
for img_name in sorted(os.listdir(tmp_dir))[TRAINING_LEN:]: | ||
img = mmcv.imread(osp.join(tmp_dir, img_name)) | ||
if osp.splitext(img_name)[1] == '.jpg': | ||
mmcv.imwrite( | ||
img, osp.join(out_dir, 'images', 'validation', img_name)) | ||
else: | ||
mmcv.imwrite( | ||
img[:, :, 0] // 128, | ||
osp.join(out_dir, 'annotations', 'validation', | ||
osp.splitext(img_name)[0] + '.jpg')) | ||
|
||
print('Removing the temporary files...') | ||
|
||
print('Done!') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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,109 @@ | ||
import argparse | ||
import os | ||
import os.path as osp | ||
import tempfile | ||
import zipfile | ||
|
||
import cv2 | ||
import mmcv | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser( | ||
description='Convert DRIVE dataset to mmsegmentation format') | ||
parser.add_argument( | ||
'training_path', help='the training part of DRIVE dataset') | ||
parser.add_argument( | ||
'testing_path', help='the testing part of DRIVE dataset') | ||
parser.add_argument('--tmp_dir', help='path of the temporary directory') | ||
parser.add_argument('-o', '--out_dir', help='output path') | ||
args = parser.parse_args() | ||
return args | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
training_path = args.training_path | ||
testing_path = args.testing_path | ||
if args.out_dir is None: | ||
out_dir = osp.join('data', 'DRIVE') | ||
else: | ||
out_dir = args.out_dir | ||
|
||
print('Making directories...') | ||
mmcv.mkdir_or_exist(out_dir) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'images')) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'images', 'training')) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'images', 'validation')) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'annotations')) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'annotations', 'training')) | ||
mmcv.mkdir_or_exist(osp.join(out_dir, 'annotations', 'validation')) | ||
|
||
with tempfile.TemporaryDirectory(dir=args.tmp_dir) as tmp_dir: | ||
print('Extracting training.zip...') | ||
zip_file = zipfile.ZipFile(training_path) | ||
zip_file.extractall(tmp_dir) | ||
|
||
print('Generating training dataset...') | ||
now_dir = osp.join(tmp_dir, 'training', 'images') | ||
for img_name in os.listdir(now_dir): | ||
img = mmcv.imread(osp.join(now_dir, img_name)) | ||
mmcv.imwrite( | ||
img, | ||
osp.join(out_dir, 'images', 'training', | ||
osp.splitext(img_name)[0] + '.jpg')) | ||
|
||
now_dir = osp.join(tmp_dir, 'training', '1st_manual') | ||
for img_name in os.listdir(now_dir): | ||
cap = cv2.VideoCapture(osp.join(now_dir, img_name)) | ||
ret, img = cap.read() | ||
mmcv.imwrite( | ||
img[:, :, 0] // 128, | ||
osp.join(out_dir, 'annotations', 'training', | ||
osp.splitext(img_name)[0] + '.jpg')) | ||
|
||
print('Extracting test.zip...') | ||
zip_file = zipfile.ZipFile(testing_path) | ||
zip_file.extractall(tmp_dir) | ||
|
||
print('Generating validation dataset...') | ||
now_dir = osp.join(tmp_dir, 'test', 'images') | ||
for img_name in os.listdir(now_dir): | ||
img = mmcv.imread(osp.join(now_dir, img_name)) | ||
mmcv.imwrite( | ||
img, | ||
osp.join(out_dir, 'images', 'validation', | ||
osp.splitext(img_name)[0] + '.jpg')) | ||
|
||
now_dir = osp.join(tmp_dir, 'test', '1st_manual') | ||
if osp.exists(now_dir): | ||
for img_name in os.listdir(now_dir): | ||
cap = cv2.VideoCapture(osp.join(now_dir, img_name)) | ||
ret, img = cap.read() | ||
# The annotation img should be divided by 128, because some of | ||
# the annotation imgs are not standard. We should set a | ||
# threshold to convert the nonstandard annotation imgs. The | ||
# value divided by 128 is equivalent to '1 if value >= 128 | ||
# else 0' | ||
mmcv.imwrite( | ||
img[:, :, 0] // 128, | ||
osp.join(out_dir, 'annotations', 'validation', | ||
osp.splitext(img_name)[0] + '.jpg')) | ||
|
||
now_dir = osp.join(tmp_dir, 'test', '2nd_manual') | ||
if osp.exists(now_dir): | ||
for img_name in os.listdir(now_dir): | ||
cap = cv2.VideoCapture(osp.join(now_dir, img_name)) | ||
ret, img = cap.read() | ||
mmcv.imwrite( | ||
img[:, :, 0] // 128, | ||
osp.join(out_dir, 'annotations', 'validation', | ||
osp.splitext(img_name)[0] + '.jpg')) | ||
|
||
print('Removing the temporary files...') | ||
|
||
print('Done!') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.