def process_captcha(self, image):
"""
TODO: DOC
"""
cv2_img = cv2.cvtColor(numpy.array(image), cv2.COLOR_BGR2GRAY)
# Find the threshold of the image so that we can identify contours.
ret, thresh = cv2.threshold(
cv2_img,
127,
255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C
)
# Find the contours of the image
_, contours, hierarchy = cv2.findContours(
thresh,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE
)
# Find the largest contour in the image with 4 points. This is the
# rectangle that is required to crop to for the captcha.
largest_contour = None
for contour in contours:
if (len(cv2.approxPolyDP(contour, 0.1*cv2.arcLength(contour, True), True)) == 4) and (2500 < cv2.contourArea(contour) < 4000):
if isinstance(largest_contour, type(None)):
largest_contour = contour
continue
if cv2.contourArea(contour) > cv2.contourArea(largest_contour):
largest_contour = contour
# If we don't have a matching contour, don't try to crop and such
if isinstance(largest_contour, type(None)):
return None
# If we do have a matching contour, build the rectangle
crop_x, crop_y, crop_width, crop_height = cv2.boundingRect(
largest_contour
)
# Crop down to the contour rectangle
image = image.crop(
(
crop_x,
crop_y,
crop_x + crop_width,
crop_y + crop_height
)
)
return image
评论列表
文章目录