import
logging
import
cv2
import
numpy as np
def
ProcessCaptcha(bg_path:
str
, front_path:
str
):
result
=
[]
bg
=
cv2.imread(bg_path)
front
=
cv2.imread(front_path, cv2.IMREAD_UNCHANGED)
white_front
=
np.ones_like(front)
*
255
alpha_channel
=
front[:, :,
3
]
white_front[:, :,
0
:
3
]
=
cv2.bitwise_and(
white_front[:, :,
0
:
3
],
white_front[:, :,
0
:
3
],
mask
=
cv2.bitwise_not(alpha_channel)
)
rectangle_list
=
[[
9
,
9
,
75
,
75
], [
109
,
9
,
175
,
75
], [
209
,
9
,
275
,
75
]]
for
rectangle
in
rectangle_list:
cropped_image
=
white_front[rectangle[
1
]:rectangle[
3
], rectangle[
0
]:rectangle[
2
]]
gray_bg
=
cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY)
_, strong_contrast_bg
=
cv2.threshold(gray_bg,
250
,
255
, cv2.THRESH_BINARY)
strong_contrast_front
=
cv2.bitwise_not(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2GRAY))
res
=
cv2.matchTemplate(
strong_contrast_bg,
strong_contrast_front,
cv2.TM_CCOEFF_NORMED
)
x, y
=
cv2.minMaxLoc(res)[
3
]
result.append((x
+
20
, y
+
20
))
logging.info(f
"Done process: "
+
str
(result).replace(
'\n'
,
' '
))
return
result