I have been working on football player tracking project for the past week. I have working model that I got by following this tutorial Football Player Tracking using Roboflow. However, the tutorial does not mention how to classify players based on kit color (it mentions classifying players by color clusters but I did not really understand what it meant). I looked for other resources online and there were two solutions.
- Either use hard-coded colors to detect teams
- Use color segmentation and k-means clustering to extract the colors of each side and classify them based on color.
I tried solution one by masking the image with two colors(yellow and red) and I got some acceptable results
#detect team function that classifies teams based on color def detect_team(image, show = False): # define the list of boundaries boundaries = [ ([160, 0, 0], [179, 255, 255]), #red ([0, 0, 0], [100, 255, 255]) #yellow ] i = 0 for (lower, upper) in boundaries: # create NumPy arrays from the boundaries lower = np.array(lower, dtype = "uint8") upper = np.array(upper, dtype = "uint8") # find the colors within the specified boundaries and apply # the mask mask = cv2.inRange(image, lower, upper) output = cv2.bitwise_and(image, image, mask = mask) tot_pix = count_nonblack_np(image) color_pix = count_nonblack_np(output) ratio = color_pix/tot_pix #plt.imshow(output) print("ratio is:", ratio) if ratio > 0.01 and i == 0: return 'red' elif ratio > 0.01 and i == 1: return 'yellow' i += 1 return 'not sure'
I run this code block on on cropped images of each player detected (I use yolov5 results.crop() to get the cropped images without having to worry about setting the dimension). Here an example of player cropped image after converting it to HSV (red player)
My questions are how to get the kit color from player image and how to get the upper and lower boundary of each color.
I tried k-means clustering and getting the most dominant color from the image and use it for the masking of the image but it did not work (I could not get the upper and lower boundaries for each color without trying to eyeball it again)