コード例 #1
0
 def test_current_frame_does_not_block(self):
     c = Camera(4)
     start = perf_counter()
     c.current_frame()
     c.current_frame()
     end = perf_counter()
     self.assertTrue(end - start < 0.01)
コード例 #2
0
    def test_start_handling_frames_fails_when_using_incorrect_callback(self):
        c = Camera()

        def callback(a, b):
            return

        with self.assertRaises(ValueError):
            c.start_handling_frames(callback)
コード例 #3
0
    def test_start_detecting_motion_fails_when_using_incorrect_callback(self):
        c = Camera()

        def callback(a, b):
            return

        with self.assertRaises(ValueError):
            c.start_detecting_motion(callback)
コード例 #4
0
 def test_get_frame_does_block(self):
     c = Camera(4)
     c._new_frame_event.clear()
     start = perf_counter()
     c.get_frame()
     c.get_frame()
     end = perf_counter()
     self.assertTrue(end - start > 0.01)
コード例 #5
0
    def test_start_handling_frames_registers_action_on_frame_handler(self):
        c = Camera()

        def callback(frame):
            return

        c.start_handling_frames(callback)
        self.assertTrue(
            c._frame_handler.is_running_action(CaptureActions.HANDLE_FRAME))
コード例 #6
0
    def test_start_detecting_motion_registers_action_on_frame_handler(self):
        c = Camera()

        def callback(frame):
            return

        c.start_detecting_motion(callback)
        self.assertTrue(
            c._frame_handler.is_running_action(CaptureActions.DETECT_MOTION))
コード例 #7
0
 def test_stop_video_capture_removes_action_on_frame_handler(self):
     c = Camera()
     c.start_video_capture()
     c.stop_video_capture()
     self.assertFalse(
         c._frame_handler.is_running_action(
             CaptureActions.CAPTURE_VIDEO_TO_FILE))
コード例 #8
0
    def test_stop_handling_frames_motion_removes_action_on_frame_handler(self):
        c = Camera()

        def callback(frame):
            return

        c.start_handling_frames(callback)
        c.stop_handling_frames()
        self.assertFalse(
            c._frame_handler.is_running_action(CaptureActions.HANDLE_FRAME))
コード例 #9
0
 def test_from_file_system_classmethod(self):
     c = Camera.from_file_system(path_to_images="/")
     self.assertIsInstance(c._camera, FileSystemCamera)
コード例 #10
0
 def test_current_frame_opencv(self):
     c = Camera(4)
     frame = c.current_frame(format="OpenCV")
     self.assertIsInstance(frame, Mock)
コード例 #11
0
 def test_stops_background_thread_by_changing_an_attribute(self):
     c = Camera()
     c._continue_processing = False
     sleep(1)
     self.assertFalse(c._process_image_thread.is_alive())
コード例 #12
0
 def test_stops_background_thread_if_camera_is_not_opened(self):
     c = Camera()
     c._camera.is_opened = Mock()
     c._camera.is_opened.return_value = False
     sleep(1)
     self.assertFalse(c._process_image_thread.is_alive())
コード例 #13
0
 def test_runs_thread_to_process_images(self):
     c = Camera()
     self.assertIsInstance(c._process_image_thread, Thread)
     self.assertTrue(c._process_image_thread.is_alive())
コード例 #14
0
 def test_has_frame_handler_attribute(self):
     c = Camera()
     self.assertIsInstance(c._frame_handler, FrameHandler)
コード例 #15
0
 def test_uses_usb_camera_by_default(self):
     c = Camera(4)
     self.assertIsInstance(c._camera, UsbCamera)
コード例 #16
0
 def test_capture_image_registers_action_on_frame_handler(self):
     c = Camera()
     c.capture_image()
     self.assertTrue(
         c._frame_handler.is_running_action(
             CaptureActions.CAPTURE_SINGLE_FRAME))
コード例 #17
0
 def test_uses_file_system_camera_if_required(self):
     c = Camera(camera_type=CameraTypes.FILE_SYSTEM_CAMERA,
                path_to_images="/")
     self.assertIsInstance(c._camera, FileSystemCamera)
コード例 #18
0
 def test_from_usb(self):
     c = Camera.from_usb()
     self.assertIsInstance(c._camera, UsbCamera)
コード例 #19
0
    red_ball = detected_balls.red
    if red_ball.found:
        print(f"Red ball center: {red_ball.center}")
        print(f"Red ball radius: {red_ball.radius}")
        print(f"Red ball angle: {red_ball.angle}")
        print()

    green_ball = detected_balls.green
    if green_ball.found:
        print(f"Green ball center: {green_ball.center}")
        print(f"Green ball radius: {green_ball.radius}")
        print(f"Green ball angle: {green_ball.angle}")
        print()

    blue_ball = detected_balls.blue
    if blue_ball.found:
        print(f"Blue ball center: {blue_ball.center}")
        print(f"Blue ball radius: {blue_ball.radius}")
        print(f"Blue ball angle: {blue_ball.angle}")
        print()

    cv2.imshow("Image", detected_balls.robot_view)
    cv2.waitKey(1)


ball_detector = BallDetector()
camera = Camera(resolution=(640, 480))
camera.on_frame = process_frame

pause()