blob: 7f2dd6f004647ebd5bb5b41b977786a2769e2221 [file] [log] [blame]
milind upadhyay96016ca2021-02-20 15:28:50 -08001#!/usr/bin/python3
2
3from rect import Rect
4
5import cv2 as cv
6import numpy as np
milind upadhyay96016ca2021-02-20 15:28:50 -08007# This function finds the percentage of yellow pixels in the rectangles
8# given that are regions of the given image. This allows us to determine
9# whether there is a ball in those rectangles
10def pct_yellow(img, rects):
11 hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
12 lower_yellow = np.array([23, 100, 75], dtype = np.uint8)
13 higher_yellow = np.array([40, 255, 255], dtype = np.uint8)
14 mask = cv.inRange(hsv, lower_yellow, higher_yellow)
15
16 pcts = np.zeros(len(rects))
17 for i in range(len(rects)):
18 rect = rects[i]
19 slice = mask[rect.y1 : rect.y2, rect.x1 : rect.x2]
20 yellow_px = np.count_nonzero(slice)
21 pcts[i] = 100 * (yellow_px / (slice.shape[0] * slice.shape[1]))
22
23 return pcts
24
25def capture_img():
26 video_stream = cv.VideoCapture(0)
27 frame = video_stream.read()[1]
28 video_stream.release()
milind upadhyay96016ca2021-02-20 15:28:50 -080029 return frame