Created
November 30, 2022 16:51
-
-
Save omerfarukdemir/03dd8967681eca14ed40c114d8bd0b8d to your computer and use it in GitHub Desktop.
Pseudo date/time detector
This file contains hidden or 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
| import cv2 | |
| import dateutil.parser | |
| import PIL | |
| import pytesseract | |
| # brew install ffmpeg | |
| # brew install tesseract | |
| # pip install opencv-contrib-python pytesseract imutils python-dateutil | |
| def is_date(value: str) -> bool: | |
| try: | |
| dateutil.parser.parse(value, fuzzy=True) | |
| return True | |
| except ValueError: | |
| return False | |
| image = cv2.imread('example_image_path') | |
| image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
| data = pytesseract.image_to_data(PIL.Image.fromarray(image), output_type=pytesseract.Output.DICT) | |
| for i, text in enumerate(data['text']): | |
| if (is_date(text)): | |
| (x, y, w, h) = (data['left'][i], data['top'][i], data['width'][i], data['height'][i]) | |
| cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) | |
| cv2.imshow('image', image) | |
| cv2.waitKey(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment