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
|
| 7 |
|
| 8 | # This function finds the percentage of yellow pixels in the rectangles
|
| 9 | # given that are regions of the given image. This allows us to determine
|
| 10 | # whether there is a ball in those rectangles
|
| 11 | def pct_yellow(img, rects):
|
| 12 | hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
|
| 13 | lower_yellow = np.array([23, 100, 75], dtype = np.uint8)
|
| 14 | higher_yellow = np.array([40, 255, 255], dtype = np.uint8)
|
| 15 | mask = cv.inRange(hsv, lower_yellow, higher_yellow)
|
| 16 |
|
| 17 | pcts = np.zeros(len(rects))
|
| 18 | for i in range(len(rects)):
|
| 19 | rect = rects[i] |
| 20 | slice = mask[rect.y1 : rect.y2, rect.x1 : rect.x2]
|
| 21 | yellow_px = np.count_nonzero(slice)
|
| 22 | pcts[i] = 100 * (yellow_px / (slice.shape[0] * slice.shape[1])) |
| 23 |
|
| 24 | return pcts
|
| 25 |
|
| 26 | def capture_img():
|
| 27 | video_stream = cv.VideoCapture(0)
|
| 28 | frame = video_stream.read()[1]
|
| 29 | video_stream.release()
|
| 30 | frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
|
| 31 | return frame
|