-
Notifications
You must be signed in to change notification settings - Fork 31
/
segment_structures_in_document.py
45 lines (41 loc) · 1.58 KB
/
segment_structures_in_document.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
* This Software is under the MIT License
* Refer to LICENSE or https://opensource.org/licenses/MIT for more information
* Written by ©Kohulan Rajan 2020
"""
import sys
import os
from decimer_segmentation import segment_chemical_structures_from_file
from decimer_segmentation import save_images, get_bnw_image, get_square_image
def main():
"""
This script segments chemical structures in a document, saves the original
segmented images as well as a binarized image and a an undistorted square
image
"""
if len(sys.argv) != 2:
print("Usage of this function: convert.py input_path")
if len(sys.argv) == 2:
# Extract chemical structure depictions and save them
raw_segments = segment_chemical_structures_from_file(sys.argv[1])
segment_dir = os.path.join(f"{sys.argv[1]}_output", "segments")
save_images(
raw_segments, segment_dir, f"{os.path.split(sys.argv[1])[1][:-4]}_orig"
)
# Get binarized segment images
binarized_segments = [get_bnw_image(segment) for segment in raw_segments]
save_images(
binarized_segments, segment_dir, f"{os.path.split(sys.argv[1])[1][:-4]}_bnw"
)
# Get segments in size 299*299 and save them
normalized_segments = [
get_square_image(segment, 299) for segment in raw_segments
]
save_images(
normalized_segments,
segment_dir,
f"{os.path.split(sys.argv[1])[1][:-4]}_norm",
)
print(f"Segments saved at {segment_dir}.")
if __name__ == "__main__":
main()