milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
milind upadhyay | d9a0f83 | 2021-03-20 15:15:17 -0700 | [diff] [blame] | 3 | import cv2 as cv |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 4 | from enum import Enum |
| 5 | import glog |
| 6 | import json |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 7 | import matplotlib.pyplot as plt |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 8 | import numpy as np |
| 9 | import os |
| 10 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 11 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 12 | class Rect: |
| 13 | |
| 14 | # x1 and y1 are top left corner, x2 and y2 are bottom right |
| 15 | def __init__(self, x1, y1, x2, y2): |
| 16 | self.x1 = x1 |
| 17 | self.y1 = y1 |
| 18 | self.x2 = x2 |
| 19 | self.y2 = y2 |
| 20 | |
| 21 | def __str__(self): |
| 22 | return "({}, {}), ({}, {})".format(self.x1, self.y1, self.x2, self.y2) |
| 23 | |
| 24 | def to_list(self): |
| 25 | return [self.x1, self.y1, self.x2, self.y2] |
| 26 | |
| 27 | @classmethod |
| 28 | def from_list(cls, list): |
| 29 | rect = None |
| 30 | if len(list) == 4: |
| 31 | rect = cls(list[0], list[1], list[2], list[3]) |
| 32 | else: |
| 33 | glog.error("Expected list len to be 4 but it was %u", len(list)) |
| 34 | rect = cls(None, None, None, None) |
| 35 | return rect |
| 36 | |
| 37 | |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 38 | class Alliance(Enum): |
| 39 | kRed = "red" |
| 40 | kBlue = "blue" |
| 41 | kUnknown = None |
| 42 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 43 | @staticmethod |
| 44 | def from_value(value): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 45 | return (Alliance.kRed |
| 46 | if value == Alliance.kRed.value else Alliance.kBlue) |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 47 | |
| 48 | @staticmethod |
| 49 | def from_name(name): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 50 | return (Alliance.kRed |
| 51 | if name == Alliance.kRed.name else Alliance.kBlue) |
| 52 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 53 | |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 54 | class Letter(Enum): |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 55 | kA = 'A' |
| 56 | kB = 'B' |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 57 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 58 | @staticmethod |
| 59 | def from_value(value): |
| 60 | return (Letter.kA if value == Letter.kA.value else Letter.kB) |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 61 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 62 | @staticmethod |
| 63 | def from_name(name): |
| 64 | return (Letter.kA if name == Letter.kA.name else Letter.kB) |
| 65 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 66 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 67 | class Path: |
| 68 | |
| 69 | def __init__(self, letter, alliance, rects): |
| 70 | self.letter = letter |
| 71 | self.alliance = alliance |
| 72 | self.rects = rects |
| 73 | |
| 74 | def __str__(self): |
| 75 | return "%s %s: " % (self.alliance.value, self.letter.value) |
| 76 | |
| 77 | def to_dict(self): |
| 78 | return {"alliance": self.alliance.name, "letter": self.letter.name} |
| 79 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 80 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 81 | RECTS_JSON_PATH = "rects.json" |
| 82 | |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 83 | AOS_SEND_PATH = "bazel-bin/aos/aos_send" |
Jim Ostrowski | 4b2d1e5 | 2021-03-11 12:42:40 -0800 | [diff] [blame] | 84 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 85 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 86 | def setup_if_pi(): |
Austin Schuh | c0ec2a8 | 2022-02-24 17:26:29 -0800 | [diff] [blame] | 87 | if os.path.isdir("/home/pi/bin"): |
| 88 | AOS_SEND_PATH = "/home/pi/bin/aos_send.stripped" |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 89 | os.system("./starter_cmd stop camera_reader") |
| 90 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 91 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 92 | setup_if_pi() |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 93 | |
| 94 | # The minimum percentage of yellow for a region of a image to |
| 95 | # be considered to have a ball |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 96 | BALL_PCT_THRESHOLD = 0.1 |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 97 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 98 | _paths = [] |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 99 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 100 | |
milind upadhyay | 1a4663c | 2021-04-03 19:41:34 -0700 | [diff] [blame] | 101 | def load_json(): |
| 102 | rects_dict = None |
| 103 | with open(RECTS_JSON_PATH, 'r') as rects_json: |
| 104 | rects_dict = json.load(rects_json) |
| 105 | return rects_dict |
| 106 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 107 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 108 | def _run_detection_loop(): |
| 109 | global img_fig, rects_dict |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 110 | |
milind upadhyay | 1a4663c | 2021-04-03 19:41:34 -0700 | [diff] [blame] | 111 | rects_dict = load_json() |
| 112 | for letter in rects_dict: |
| 113 | for alliance in rects_dict[letter]: |
| 114 | rects = [] |
| 115 | for rect_list in rects_dict[letter][alliance]: |
| 116 | rects.append(Rect.from_list(rect_list)) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 117 | _paths.append( |
| 118 | Path(Letter.from_name(letter), Alliance.from_name(alliance), |
| 119 | rects)) |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 120 | |
milind upadhyay | d9a0f83 | 2021-03-20 15:15:17 -0700 | [diff] [blame] | 121 | plt.ion() |
| 122 | img_fig = plt.figure() |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 123 | |
milind upadhyay | d9a0f83 | 2021-03-20 15:15:17 -0700 | [diff] [blame] | 124 | running = True |
| 125 | while running: |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 126 | _detect_path() |
milind upadhyay | d9a0f83 | 2021-03-20 15:15:17 -0700 | [diff] [blame] | 127 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 128 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 129 | def _detect_path(): |
| 130 | img = capture_img() |
milind upadhyay | d9a0f83 | 2021-03-20 15:15:17 -0700 | [diff] [blame] | 131 | img_fig.figimage(img) |
| 132 | plt.show() |
milind upadhyay | 1a4663c | 2021-04-03 19:41:34 -0700 | [diff] [blame] | 133 | |
milind upadhyay | d9a0f83 | 2021-03-20 15:15:17 -0700 | [diff] [blame] | 134 | plt.pause(0.001) |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 135 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 136 | mask = _create_mask(img) |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 137 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 138 | current_path = None |
| 139 | num_current_paths = 0 |
| 140 | for path in _paths: |
| 141 | pcts = _pct_yellow(mask, path.rects) |
| 142 | if len(pcts) == len(path.rects): |
| 143 | glog.info(path) |
| 144 | for i in range(len(pcts)): |
| 145 | glog.info("Percent yellow of %s: %f", path.rects[i], pcts[i]) |
| 146 | glog.info("") |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 147 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 148 | # If all the balls in a path were detected then that path is present |
| 149 | rects_with_balls = np.where(pcts >= BALL_PCT_THRESHOLD)[0].size |
| 150 | if rects_with_balls == len(path.rects): |
| 151 | current_path = path |
| 152 | num_current_paths += 1 |
| 153 | else: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 154 | glog.error("Error: len of pcts (%u) != len of rects: (%u)", |
| 155 | len(pcts), len(rects)) |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 156 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 157 | if num_current_paths != 1: |
| 158 | if num_current_paths == 0: |
| 159 | current_path = Path(Letter.kA, None, None) |
| 160 | current_path.alliance = Alliance.kUnknown |
| 161 | glog.warn("Expected 1 path but detected %u", num_current_paths) |
milind upadhyay | 1a4663c | 2021-04-03 19:41:34 -0700 | [diff] [blame] | 162 | return |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 163 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 164 | path_dict = current_path.to_dict() |
| 165 | glog.info("Path is %s", path_dict) |
| 166 | os.system(AOS_SEND_PATH + |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 167 | " /pi2/camera y2020.vision.GalacticSearchPath '" + |
| 168 | json.dumps(path_dict) + "'") |
| 169 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 170 | |
milind upadhyay | 1a4663c | 2021-04-03 19:41:34 -0700 | [diff] [blame] | 171 | KERNEL = np.ones((5, 5), np.uint8) |
| 172 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 173 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 174 | def _create_mask(img): |
| 175 | hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 176 | lower_yellow = np.array([23, 100, 75], dtype=np.uint8) |
| 177 | higher_yellow = np.array([40, 255, 255], dtype=np.uint8) |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 178 | mask = cv.inRange(hsv, lower_yellow, higher_yellow) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 179 | mask = cv.erode(mask, KERNEL, iterations=1) |
| 180 | mask = cv.dilate(mask, KERNEL, iterations=3) |
milind upadhyay | 1a4663c | 2021-04-03 19:41:34 -0700 | [diff] [blame] | 181 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 182 | return mask |
| 183 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 184 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 185 | # This function finds the percentage of yellow pixels in the rectangles |
| 186 | # given that are regions of the given image. This allows us to determine |
| 187 | # whether there is a ball in those rectangles |
| 188 | def _pct_yellow(mask, rects): |
| 189 | pcts = np.zeros(len(rects)) |
| 190 | for i in range(len(rects)): |
| 191 | rect = rects[i] |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 192 | slice = mask[rect.y1:rect.y2, rect.x1:rect.x2] |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 193 | yellow_px = np.count_nonzero(slice) |
| 194 | pcts[i] = yellow_px / (slice.shape[0] * slice.shape[1]) |
| 195 | |
| 196 | return pcts |
| 197 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 198 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 199 | _video_stream = cv.VideoCapture(0) |
| 200 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 201 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 202 | def capture_img(): |
| 203 | global _video_stream |
| 204 | return _video_stream.read()[1] |
| 205 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 206 | |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 207 | def release_stream(): |
| 208 | global _video_stream |
| 209 | _video_stream.release() |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 210 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 211 | |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 212 | def main(): |
Milind | da9723d | 2021-04-02 09:14:34 -0700 | [diff] [blame] | 213 | _run_detection_loop() |
| 214 | release_stream() |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 215 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 216 | |
milind upadhyay | 96016ca | 2021-02-20 15:28:50 -0800 | [diff] [blame] | 217 | if __name__ == "__main__": |
| 218 | main() |