# Contour is previously obtained card value shape
# np is Python standard NumPy module which provides various mathematical functions
peri = cv2.arcLength(contour,True)
approx = cv2.approxPolyDP(contour,0.01*peri,True)
pts = np.float32(approx)
# Warp card into 200x300 flattened image using perspective transform
Temp_rect = np.zeros((4,2), dtype = "float32")
s = np.sum(pts, axis = 2)
tl = pts[np.argmin(s)]
br = pts[np.argmax(s)]
diff = np.diff(pts, axis = -1)
tr = pts[np.argmin(diff)]
bl = pts[np.argmax(diff)]
# Need to create an array listing points in order of
# [top left, top right, bottom right, bottom left]
# before doing the perspective transform
temp_rect[0] = tl
temp_rect[1] = tr
temp_rect[2] = br
temp_rect[3] = bl
maxWidth = 200
maxHeight = 300
# Create destination array, calculate perspective transform matrix,
# and warp card image
dst = np.array([[0,0],[maxWidth-1,0],[maxWidth-1,maxHeight-1],[0, maxHeight-1]], np.float32)
M = cv2.getPerspectiveTransform(temp_rect,dst)
warp = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
warp = cv2.cvtColor(warp,cv2.COLOR_BGR2GRAY)