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