Add galactic search challenge ball detection
In the galactic search challenge for the first skills challenge,
the robot has to determine which path is present based on the
positions of the balls in the field, and run that respective spline.
This code has a UI for selecting the regions to look for balls in,
and uses those regions to determine the path. It sends the path
using aos_send to the /camera channel.
Change-Id: Ifd7aacc75eb830b762e14487bad0720dcc2a3944
diff --git a/y2020/vision/ball_detection.py b/y2020/vision/ball_detection.py
new file mode 100644
index 0000000..46faccc
--- /dev/null
+++ b/y2020/vision/ball_detection.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python3
+
+from rect import Rect
+
+import cv2 as cv
+import numpy as np
+
+# This function finds the percentage of yellow pixels in the rectangles
+# given that are regions of the given image. This allows us to determine
+# whether there is a ball in those rectangles
+def pct_yellow(img, rects):
+ hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
+ lower_yellow = np.array([23, 100, 75], dtype = np.uint8)
+ higher_yellow = np.array([40, 255, 255], dtype = np.uint8)
+ mask = cv.inRange(hsv, lower_yellow, higher_yellow)
+
+ pcts = np.zeros(len(rects))
+ for i in range(len(rects)):
+ rect = rects[i]
+ slice = mask[rect.y1 : rect.y2, rect.x1 : rect.x2]
+ yellow_px = np.count_nonzero(slice)
+ pcts[i] = 100 * (yellow_px / (slice.shape[0] * slice.shape[1]))
+
+ return pcts
+
+def capture_img():
+ video_stream = cv.VideoCapture(0)
+ frame = video_stream.read()[1]
+ video_stream.release()
+ frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
+ return frame