OCR
OCR
'''
Function to detect the card square
input: image
output: image with detected card
'''
# Copy image
im_copy = im.copy()
im_copy = cv2.cvtColor(im_copy, cv2.COLOR_BGR2GRAY)
# Downscale image
# scale_percent = 50 # percent of original size
# width = int(im_copy.shape[1] * scale_percent / 100)
# height = int(im_copy.shape[0] * scale_percent / 100)
# dim = (width, height)
# # resize image
# im_copy = cv2.resize(im_copy, dim, interpolation=cv2.INTER_AREA)
orgs = im_copy.copy()
# Find contours
contours, _ = cv2.findContours(
edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS)
contours = filter(lambda cont: cv2.arcLength(cont, False) > 10000, contours)
contours = filter(lambda cont: cv2.contourArea(cont) > 20000, contours)
# that's basically it
cv2.drawContours(orgs, rects, -1, (0, 255, 0), 1)
# show only contours
new = get_new(im_copy)
cv2.drawContours(new, rects, -1, (0, 255, 0), 1)
cv2.GaussianBlur(new, (9, 9), 0, new)
new = cv2.Canny(new, 0, CANNY, apertureSize=3)
# cv2.imshow('result', orgs)
# cv2.waitKey(0)
# cv2.imshow('result', dilated)
# cv2.waitKey(0)
# cv2.imshow('result', edges)
# cv2.waitKey(0)
# cv2.imshow('result', new)
# cv2.waitKey(0)
# Save image
cv2.imwrite('card.jpg', new)
return im_copy