Skip to content

Commit

Permalink
Add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
jonchang committed May 7, 2024
1 parent 071a8fa commit 3ec97f6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions OCR/image-alignment/deskew.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import cv2 as cv


def order_points(inn):
def order_points(inn: np.ndarray) -> np.ndarray:
"Reorder points such that the points go in order from top left, top right, bottom right, and bottom left."
inn = inn.reshape(4, 2)
out = np.zeros([4, 2]).astype(np.float32)
Expand All @@ -25,7 +25,7 @@ def order_points(inn):
return out


def dewarp(img):
def dewarp(img: np.ndarray) -> np.ndarray:
# compute contours for an image and find the biggest one by area
cnts, hier = cv.findContours(img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)[-2:]
biggest_contour = functools.reduce(
Expand All @@ -37,13 +37,13 @@ def dewarp(img):
approx = cv.approxPolyDP(biggest_contour, 0.01 * peri, True)

h, w = img.shape
dest = np.float32([[0, 0], [w, 0], [w, h], [0, h]])
dest = np.array([[0, 0], [w, 0], [w, h], [0, h]], dtype=np.float32)

M = cv.getPerspectiveTransform(order_points(approx), dest)
return cv.warpPerspective(img, M, (w, h))


def parse_args():
def parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
Expand Down
2 changes: 1 addition & 1 deletion OCR/image-alignment/perspective-transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def parse_args():
return p.parse_args()


def make_tform(distortion_scale: float):
def make_tform(distortion_scale: float) -> object:
return transforms.Compose(
[
transforms.RandomPerspective(distortion_scale=distortion_scale, p=1),
Expand Down

0 comments on commit 3ec97f6

Please sign in to comment.