Skip to content

Commit

Permalink
additional params and internal documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandip117 committed Oct 3, 2023
1 parent 4f858db commit e72ab0f
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions image_textRemove.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@
import numpy as np
from chris_plugin import chris_plugin, PathMapper

__version__ = '0.2.0'
__version__ = '0.2.1'

DISPLAY_TITLE = r"""
ChRIS Plugin to remove texts from images
_ _ _ _ ______
| | (_) | | | | | ___ \
_ __ | |______ _ _ __ ___ __ _ __ _ ___ | |_ _____ _| |_| |_/ /___ _ __ ___ _____ _____
| '_ \| |______| | '_ ` _ \ / _` |/ _` |/ _ \| __/ _ \ \/ / __| // _ \ '_ ` _ \ / _ \ \ / / _ \
| |_) | | | | | | | | | (_| | (_| | __/| || __/> <| |_| |\ \ __/ | | | | | (_) \ V / __/
| .__/|_| |_|_| |_| |_|\__,_|\__, |\___| \__\___/_/\_\\__\_| \_\___|_| |_| |_|\___/ \_/ \___|
| | __/ | ______
|_| |___/ |______|
"""


parser = ArgumentParser(description='A ChRIS plugin to remove text from images',
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-V', '--version', action='version',
version=f'%(prog)s {__version__}')
parser.add_argument('-p', '--pattern', default='**/*.txt', type=str,
help='input file filter glob')
parser.add_argument('-r', '--removeAll', default=False,action="store_true",
parser.add_argument('-f', '--fileFilter', default='png', type=str,
help='input file filter(only the extension)')
parser.add_argument('-o', '--outputType', default='png', type=str,
help='output file type(only the extension)')
parser.add_argument('-r', '--removeAll', default=False, action="store_true",
help='Remove all texts from image using text recognition model')


Expand All @@ -32,10 +40,10 @@
@chris_plugin(
parser=parser,
title='My ChRIS plugin',
category='', # ref. https://chrisstore.co/plugins
min_memory_limit='100Mi', # supported units: Mi, Gi
min_cpu_limit='1000m', # millicores, e.g. "1000m" = 1 CPU core
min_gpu_limit=0 # set min_gpu_limit=1 to enable GPU
category='', # ref. https://chrisstore.co/plugins
min_memory_limit='100Mi', # supported units: Mi, Gi
min_cpu_limit='1000m', # millicores, e.g. "1000m" = 1 CPU core
min_gpu_limit=0 # set min_gpu_limit=1 to enable GPU
)
def main(options: Namespace, inputdir: Path, outputdir: Path):
"""
Expand All @@ -57,25 +65,28 @@ def main(options: Namespace, inputdir: Path, outputdir: Path):
#
# Refer to the documentation for more options, examples, and advanced uses e.g.
# adding a progress bar and parallelism.
mapper = PathMapper.file_mapper(inputdir, outputdir, glob=options.pattern)
mapper = PathMapper.file_mapper(inputdir, outputdir, glob=f"**/*.{options.fileFilter}")
for input_file, output_file in mapper:
# The code block below is a small and easy example of how to use a ``PathMapper``.
# It is recommended that you put your functionality in a helper function, so that
# it is more legible and can be unit tested.

final_image = inpaint_text(str(input_file), options.removeAll)
img_rgb = cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB)
print(f"Saving output file as {output_file}")
cv2.imwrite(str(output_file), img_rgb)

output_file = str(output_file).replace(options.fileFilter, options.outputType)
print(f"Saving output file as ----->{output_file}<-----\n\n")
cv2.imwrite(output_file, img_rgb)


def midpoint(x1, y1, x2, y2):
x_mid = int((x1 + x2) / 2)
y_mid = int((y1 + y2) / 2)
return x_mid, y_mid


def inpaint_text(img_path, remove_all):
# Currently we have hardcoded the box coordinates for
# first name, last name, MRN and DOB
box_list = [('fname', [[75.661415, 12.579701],
[159.15764, 15.109892],
[158.6009, 33.481747],
Expand All @@ -93,7 +104,7 @@ def inpaint_text(img_path, remove_all):
[499.21875, 32.171875],
[401.59375, 32.171875]])]
# read image
print(f"Reading input file from {img_path}")
print(f"Reading input file from ---->{img_path}<----")
img = cv2.imread(img_path)

print("Removing fname, lname, MRN, DoB")
Expand All @@ -106,19 +117,19 @@ def inpaint_text(img_path, remove_all):

mask = np.zeros(img.shape[:2], dtype="uint8")
for box in box_list:
x0, y0 = box[1][0]
x1, y1 = box[1][1]
x2, y2 = box[1][2]
x3, y3 = box[1][3]
x0, y0 = box[1][0]
x1, y1 = box[1][1]
x2, y2 = box[1][2]
x3, y3 = box[1][3]

x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)
x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)

thickness = int(math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2))
thickness = int(math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2))

cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255,
thickness)
img = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)
cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255,
thickness)
img = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)

return img

Expand Down

0 comments on commit e72ab0f

Please sign in to comment.