import cv2
import numpy as np

def recognize(image):
    board1 = cv2.imread("project/board1.png")
    board2 = cv2.imread("project/board2.png")
    _, thrash, t_min_loc1, t_max_loc = cv2.minMaxLoc(
            cv2.matchTemplate(image, board1, cv2.TM_SQDIFF_NORMED)
        )
    _, thrash, t_min_loc2, t_max_loc = cv2.minMaxLoc(
            cv2.matchTemplate(image, board2, cv2.TM_SQDIFF_NORMED)
        )

    image = image[t_min_loc1[1] + 20:t_min_loc2[1], t_min_loc1[0] + 20:t_min_loc2[0]]

    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    hsv = cv2.GaussianBlur(hsv, (9, 9), 0)
    _, thresh = cv2.threshold(hsv[:, :, 1], 27, 255, cv2.THRESH_BINARY)
    cnts, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    polys = []
    for cont in cnts:
        eps = 0.044 * cv2.arcLength(cont, True)
        rect = cv2.minAreaRect(cont)
        box = cv2.boxPoints(rect)
        box = np.intp(box)
        approx = cv2.approxPolyDP(box, eps, True)
        approx = approx[:, 0, :].tolist()
        polys.append(approx)

    return polys, (t_min_loc2[0] + 20, t_min_loc2[1] + 20)
