milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3
|
| 2 |
|
| 3 | from rect import Rect
|
| 4 |
|
| 5 | import cv2 as cv
|
| 6 | import numpy as np
|
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 7 | # 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
|
| 10 | def 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 |
|
| 25 | def capture_img():
|
| 26 | video_stream = cv.VideoCapture(0)
|
| 27 | frame = video_stream.read()[1]
|
| 28 | video_stream.release()
|
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 29 | return frame
|