Run yapf on all python files in the repo
Signed-off-by: Ravago Jones <ravagojones@gmail.com>
Change-Id: I221e04c3f517fab8535b22551553799e0fee7a80
diff --git a/y2020/vision/galactic_search_path.py b/y2020/vision/galactic_search_path.py
index 00b572a..11ec73d 100755
--- a/y2020/vision/galactic_search_path.py
+++ b/y2020/vision/galactic_search_path.py
@@ -8,6 +8,7 @@
import numpy as np
import os
+
class Rect:
# x1 and y1 are top left corner, x2 and y2 are bottom right
@@ -41,11 +42,14 @@
@staticmethod
def from_value(value):
- return (Alliance.kRed if value == Alliance.kRed.value else Alliance.kBlue)
+ return (Alliance.kRed
+ if value == Alliance.kRed.value else Alliance.kBlue)
@staticmethod
def from_name(name):
- return (Alliance.kRed if name == Alliance.kRed.name else Alliance.kBlue)
+ return (Alliance.kRed
+ if name == Alliance.kRed.name else Alliance.kBlue)
+
class Letter(Enum):
kA = 'A'
@@ -59,6 +63,7 @@
def from_name(name):
return (Letter.kA if name == Letter.kA.name else Letter.kB)
+
class Path:
def __init__(self, letter, alliance, rects):
@@ -72,15 +77,18 @@
def to_dict(self):
return {"alliance": self.alliance.name, "letter": self.letter.name}
+
RECTS_JSON_PATH = "rects.json"
AOS_SEND_PATH = "bazel-bin/aos/aos_send"
+
def setup_if_pi():
if os.path.isdir("/home/pi/bin"):
AOS_SEND_PATH = "/home/pi/bin/aos_send.stripped"
os.system("./starter_cmd stop camera_reader")
+
setup_if_pi()
# The minimum percentage of yellow for a region of a image to
@@ -89,12 +97,14 @@
_paths = []
+
def load_json():
rects_dict = None
with open(RECTS_JSON_PATH, 'r') as rects_json:
rects_dict = json.load(rects_json)
return rects_dict
+
def _run_detection_loop():
global img_fig, rects_dict
@@ -104,7 +114,9 @@
rects = []
for rect_list in rects_dict[letter][alliance]:
rects.append(Rect.from_list(rect_list))
- _paths.append(Path(Letter.from_name(letter), Alliance.from_name(alliance), rects))
+ _paths.append(
+ Path(Letter.from_name(letter), Alliance.from_name(alliance),
+ rects))
plt.ion()
img_fig = plt.figure()
@@ -113,6 +125,7 @@
while running:
_detect_path()
+
def _detect_path():
img = capture_img()
img_fig.figimage(img)
@@ -138,7 +151,8 @@
current_path = path
num_current_paths += 1
else:
- glog.error("Error: len of pcts (%u) != len of rects: (%u)", len(pcts), len(rects))
+ glog.error("Error: len of pcts (%u) != len of rects: (%u)",
+ len(pcts), len(rects))
if num_current_paths != 1:
if num_current_paths == 0:
@@ -147,24 +161,27 @@
glog.warn("Expected 1 path but detected %u", num_current_paths)
return
-
path_dict = current_path.to_dict()
glog.info("Path is %s", path_dict)
os.system(AOS_SEND_PATH +
- " /pi2/camera y2020.vision.GalacticSearchPath '" + json.dumps(path_dict) + "'")
+ " /pi2/camera y2020.vision.GalacticSearchPath '" +
+ json.dumps(path_dict) + "'")
+
KERNEL = np.ones((5, 5), np.uint8)
+
def _create_mask(img):
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)
+ 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)
- mask = cv.erode(mask, KERNEL, iterations = 1)
- mask = cv.dilate(mask, KERNEL, iterations = 3)
+ mask = cv.erode(mask, KERNEL, iterations=1)
+ mask = cv.dilate(mask, KERNEL, iterations=3)
return mask
+
# 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
@@ -172,25 +189,30 @@
pcts = np.zeros(len(rects))
for i in range(len(rects)):
rect = rects[i]
- slice = mask[rect.y1 : rect.y2, rect.x1 : rect.x2]
+ slice = mask[rect.y1:rect.y2, rect.x1:rect.x2]
yellow_px = np.count_nonzero(slice)
pcts[i] = yellow_px / (slice.shape[0] * slice.shape[1])
return pcts
+
_video_stream = cv.VideoCapture(0)
+
def capture_img():
global _video_stream
return _video_stream.read()[1]
+
def release_stream():
global _video_stream
_video_stream.release()
+
def main():
_run_detection_loop()
release_stream()
+
if __name__ == "__main__":
main()