# Path: bot/common.py
# def mask_image(lower_mask, upper_mask, img, apply_mask=False):
#     """" Masks an image according to the upper and lower bounds
#     Parameters
#     ----------
#     lower_mask : ndarray
#         lower mask to apply to image, length must match image channels
#     upper_mask : ndarray
#         upper mask to apply to image, length must match image channels
#     img :  ndarray
#         image to apply mask to
#     apply_mask : bool
#         returns the masked image instead of the mask itself
#     """
#     shape = np.array(img.shape).flatten()
#     if len(np.array(img.shape).flatten()) == 3:
#         shape_size = shape[-1]
#     else:
#         shape_size = 1
#     assert (len(lower_mask) == shape_size)
#     assert (len(upper_mask) == shape_size)
#     color_min = np.array(lower_mask, np.uint8)
#     color_max = np.array(upper_mask, np.uint8)
#     new_img = cv2.inRange(img, color_min, color_max)
#     if apply_mask:
#         return cv2.bitwise_and(img, img, mask=new_img)
#     return new_img


import numpy as np
import cv2
import os
from matplotlib import pyplot as plt
from bot.common import mask_image
from sklearn.cluster import KMeans
from sklearn.metrics.pairwise import euclidean_distances
                continue
            self.circlePoints.append((i[0], i[1]))
        if self._debug:
            self.draw_circles(circles, cimg)

    def capture_white_circles(self, x_limit=480, y_limit=670):
        self.prep_for_white_circles()
        img = cv2.cvtColor(self.white_query, cv2.COLOR_BGR2GRAY)
        cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 40,
                                   param1=50, param2=30, minRadius=5, maxRadius=60)
        if circles is None:
            return
        circles = np.uint16(np.around(circles))
        for i in circles[0, :]:
            if i[0] <= x_limit and i[1] <= y_limit:
                self.circlePoints.append((i[0], i[1]))
        if self._debug:
            self.draw_circles(circles, cimg)

    def draw_circles(self, circles, cimg):
        for i in circles[0, :]:
            # draw the outer circle
            cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)
            # draw the center of the circle
            cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3)
        self.images.append(cimg)

    def prep_for_white_circles(self):
        lower, upper = ([215, 215, 215], [255, 255, 255])

# Path: bot/common.py
# def mask_image(lower_mask, upper_mask, img, apply_mask=False):
#     """" Masks an image according to the upper and lower bounds
#     Parameters
#     ----------
#     lower_mask : ndarray
#         lower mask to apply to image, length must match image channels
#     upper_mask : ndarray
#         upper mask to apply to image, length must match image channels
#     img :  ndarray
#         image to apply mask to
#     apply_mask : bool
#         returns the masked image instead of the mask itself
#     """
#     shape = np.array(img.shape).flatten()
#     if len(np.array(img.shape).flatten()) == 3:
#         shape_size = shape[-1]
#     else:
#         shape_size = 1
#     assert (len(lower_mask) == shape_size)
#     assert (len(upper_mask) == shape_size)
#     color_min = np.array(lower_mask, np.uint8)
#     color_max = np.array(upper_mask, np.uint8)
#     new_img = cv2.inRange(img, color_min, color_max)
#     if apply_mask:
#         return cv2.bitwise_and(img, img, mask=new_img)
#     return new_img


# Path: bot/providers/trainer_matches.py
import numpy as np
import cv2
import os
from matplotlib import pyplot as plt
from bot.common import mask_image
from sklearn.cluster import KMeans
from sklearn.metrics.pairwise import euclidean_distances

                continue
            self.circlePoints.append((i[0], i[1]))
        if self._debug:
            self.draw_circles(circles, cimg)

    def capture_white_circles(self, x_limit=480, y_limit=670):
        self.prep_for_white_circles()
        img = cv2.cvtColor(self.white_query, cv2.COLOR_BGR2GRAY)
        cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 40,
                                   param1=50, param2=30, minRadius=5, maxRadius=60)
        if circles is None:
            return
        circles = np.uint16(np.around(circles))
        for i in circles[0, :]:
            if i[0] <= x_limit and i[1] <= y_limit:
                self.circlePoints.append((i[0], i[1]))
        if self._debug:
            self.draw_circles(circles, cimg)

    def draw_circles(self, circles, cimg):
        for i in circles[0, :]:
            # draw the outer circle
            cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)
            # draw the center of the circle
            cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3)
        self.images.append(cimg)

    def prep_for_white_circles(self):
        lower, upper = ([215, 215, 215], [255, 255, 255])

        self.white_query = mask_image(lower, upper, self.query, apply_mask=True)