Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for negative signs and optimization of exclusions #76

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions receipt_parser/importer.py
Expand Up @@ -65,7 +65,7 @@ def find_images(folder):
pass


def rotate_image(input_file, output_file, angle=90):
def rotate_image(input_file, output_file, angle):
"""
:param input_file: str
Path to image to rotate
Expand All @@ -76,14 +76,22 @@ def rotate_image(input_file, output_file, angle=90):
:return: void
Rotates image and saves result
"""

if (angle == 0):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move those conditions out for separation of concerns:
the function should just rotate the image unconditionally; the logic whether to rotate the image should be outside.
Furthermore I have mixed feelings about reverting the sign of the rotation. What was the use-case for that?

So basically

if angle != 0:
  rotate_image(input_file, output_file, angle)

What do you think? 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @monolidth said, this should rotate the image in the correct possition. Maybe it's not trivial.

return None
elif (angle == -90):
angle = 90
elif (angle == 90):
angle = -90

print(ORANGE + '\t~: ' + RESET + 'Rotate image' + RESET)
with WandImage(filename=input_file) as img:
with img.clone() as rotated:
rotated.rotate(angle)
rotated.save(filename=output_file)


def sharpen_image(input_file, output_file):
def sharpen_image(input_file, output_file, angle):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see that you added angle to pass it to rotate_image, but I think we should rather move rotate_image out of sharpen_image and remove the angle parameter here. It's easier to test this way and again ensures separation of concerns.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this with last commit, thanks for your input! 👍

"""
:param input_file: str
Path to image to prettify
Expand All @@ -93,7 +101,7 @@ def sharpen_image(input_file, output_file):
Prettifies image and saves result
"""

rotate_image(input_file, output_file) # rotate
rotate_image(input_file, output_file, angle) # rotate
print(ORANGE + '\t~: ' + RESET + 'Increase image contrast and sharp image' + RESET)

with WandImage(filename=output_file) as img:
Expand Down Expand Up @@ -167,15 +175,20 @@ def remove_noise(img):


def deskew(image):
output = []
coords = np.column_stack(np.where(image > 0))
angle = cv2.minAreaRect(coords)[-1]

print(ORANGE + '\t~: ' + RESET + 'Get rotation angle:' + str(angle) + RESET)

# (h, w) = image.shape[:2]
# center = (w // 2, h // 2)
# M = cv2.getRotationMatrix2D(center, angle, 1.0)
# rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return image

output = [image, int(angle)]

return output


def main():
Expand Down Expand Up @@ -212,9 +225,9 @@ def main():
img = grayscale_image(img)
img = remove_noise(img)
img = deskew(img)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of img[0] and img[1] you can also destructure return parameters like so:

[output, angle] = deskew(img)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this with last commit, thanks for your input! 👍

cv2.imwrite(tmp_path, img)
cv2.imwrite(tmp_path, img[0])

sharpen_image(tmp_path, tmp_path)
sharpen_image(tmp_path, tmp_path, img[1])
run_tesseract(tmp_path, out_path, config.language)

i = i + 1
Expand Down
9 changes: 7 additions & 2 deletions receipt_parser/receipt.py
Expand Up @@ -108,6 +108,11 @@ def parse_items(self):
match = re.search(self.config.item_format, line)
if hasattr(match, 'group'):
article_name = match.group(1)

if (match.group(2) == "-"):
article_sum = "-" + match.group(3).replace(",", ".")
else:
article_sum = match.group(3).replace(",", ".")
else:
continue

Expand All @@ -116,10 +121,10 @@ def parse_items(self):
ignored_words.append(word)

for word in ignored_words:
parse_stop = fnmatch.fnmatch(article_name, f"{word}*")
parse_stop = fnmatch.fnmatch(article_name, f"*{word}*")
if parse_stop: return items

items.append(item(match.group(1), match.group(3).replace(",", ".")))
items.append(item(article_name, article_sum))

return items

Expand Down