示例#1
0
def test_bottom_lane_4():
    cube_detector = CubeDetector()
    frame = cv2.imread("../anki_object_detection/images/cube_up_lane_4.jpg")

    max_left_lane = Line(294, 181, 68, 415)
    max_right_lane = Line(490, 654, 61, 400)
    max_horizontal_upper_lane = Line(106, 700, 130, 116)
    max_horizontal_lower_lane = Line(10, 795, 247, 223)

    cube = cube_detector.detect(frame)
    horizontal_lane, vertical_lane = LaneCalculator.get_lane_for_cube(
        frame, cube, max_left_lane, max_right_lane, max_horizontal_upper_lane,
        max_horizontal_lower_lane)

    assert 4 == horizontal_lane
    assert -1 == vertical_lane
def test_bottom_lane_4():
    cube_detector = CubeDetector()
    frame = cv2.imread(
        "../anki_object_detection/images/camera_top_bottom_4.jpg")

    max_left_lane = Line(306, 312, 5, 486)
    max_right_lane = Line(484, 496, 2, 481)
    max_horizontal_upper_lane = Line(154, 638, 154, 143)
    max_horizontal_lower_lane = Line(157, 646, 337, 328)

    cube = cube_detector.detect(frame)
    horizontal_lane, vertical_lane = LaneCalculator.get_lane_for_cube(
        frame, cube, max_left_lane, max_right_lane, max_horizontal_upper_lane,
        max_horizontal_lower_lane)

    assert 4 == horizontal_lane
    assert -1 == vertical_lane
def test_bottom_lane_3():
    cube_detector = CubeDetector()
    frame = cv2.imread(
        "../anki_object_detection/images/camera_top_bottom_3.jpg")

    max_left_lane = Line(306, 312, 5, 486)
    max_right_lane = Line(484, 496, 2, 481)
    max_horizontal_upper_lane = Line(154, 638, 154, 143)
    max_horizontal_lower_lane = Line(157, 646, 337, 328)

    cube = cube_detector.detect(frame)
    horizontal_lane, vertical_lane = LaneCalculator.get_lane_for_cube(
        frame, cube, max_left_lane, max_right_lane, max_horizontal_upper_lane,
        max_horizontal_lower_lane)
    cv2.namedWindow('test', cv2.WINDOW_NORMAL)
    cv2.imshow('test', frame)
    cv2.waitKey(0)

    assert 3 == horizontal_lane
    assert -1 == vertical_lane
示例#4
0
def main():
    settings.init()

    parser = argparse.ArgumentParser(description='Detect a blue cube')
    parser.add_argument('configFile', nargs='?',  default='config.json')

    #parser.add_argument('verticalRightLane', type=int, nargs=4,
    #                    help='the vertical right lane x1 y1 x2 y2')

    #parser.add_argument('horizontalTopLane', type=int, nargs=4,
    #                    help='the horizontal top lane x1 y1 x2 y2')

    #parser.add_argument('horizontalBottomLane', type=int, nargs=4,
    #                    help='the horizontal bottom lane x1 y1 x2 y2')

    args = parser.parse_args()

    with open(args.configFile) as json_data:
        config = json.load(json_data)
    print(config)

    #kafka = Kafka()
    #loop = asyncio.get_event_loop()
    #messageGateway = Websocket(loop)

    max_left_lane = Line(config['maxLeftLane']['x1'], config['maxLeftLane']['x2'], config['maxLeftLane']['y1'], config['maxLeftLane']['y2'])
    max_right_lane = Line(config['maxRightLane']['x1'], config['maxRightLane']['x2'], config['maxRightLane']['y1'], config['maxRightLane']['y2'])
    max_horizontal_upper_lane = Line(config['maxHorizontalUpperLane']['x1'], config['maxHorizontalUpperLane']['x2'], config['maxHorizontalUpperLane']['y1'],
                                     config['maxHorizontalUpperLane']['y2'])
    max_horizontal_lower_lane = Line(config['maxHorizontalLowerLane']['x1'], config['maxHorizontalLowerLane']['x2'], config['maxHorizontalLowerLane']['y1'],
                                     config['maxHorizontalLowerLane']['y2'])

    lower_color_range = np.array([config['lowerColorRange']['hue'], config['lowerColorRange']['saturation'], config['lowerColorRange']['luminance']])
    upper_color_range = np.array([config['upperColorRange']['hue'], config['upperColorRange']['saturation'], config['upperColorRange']['luminance']])

    ankiCamera = AnkiCamera(config["cameraDeviceId"])
    ankiCamera.run(max_left_lane, max_right_lane, max_horizontal_upper_lane, max_horizontal_lower_lane, lower_color_range, upper_color_range)
    def _interpolate_vetical_lines(self, lines, threshold=10):
        """ Returns all the aggregated vertical lines that point to the same direction
        :param self: 
        :param lines: The lines found with hough transform
        :param threshold: Direction threshold for aggregating same lines
        :return: 
        """
        interpolated_lines = {}
        # interpolate lines by aggregating all the vertical lines that point in the same direction (+, - threshold
        for line in lines:
            for x1, y1, x2, y2 in line:

                # swap orientation if line goes bottom up (not up down)
                if y1 > y2:
                    x1, x2, y1, y2 = self._swap_line_orientation(
                        x1, x2, y1, y2)

                # calculate the angle
                angle = int(self._calc_angle((x2 - x1, y2 - y1)))

                # ignore lines that do not point vertically
                if angle < 110 and angle > 80:
                    print(angle)
                    closest_line = None
                    for keyAngle in interpolated_lines:
                        if abs(keyAngle - angle) < threshold:
                            closest_line = interpolated_lines[keyAngle]

                            if closest_line.in_area(x1, x2, y1, y2):
                                # Calculate x values (for horizontally aligned lines)
                                if closest_line.y1 > y1:
                                    closest_line.x1 = x1

                                if closest_line.y2 < y2:
                                    closest_line.x2 = x2

                                closest_line.y1 = min(y1, closest_line.y1)
                                closest_line.y2 = max(y2, closest_line.y2)
                                break

                    if closest_line is None:
                        interpolated_lines[angle] = Line(x1, x2, y1, y2)

        return interpolated_lines