WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
VisionRunner.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <atomic>
8#include <functional>
9#include <memory>
10
11#include "cscore_cv.h"
13
14namespace frc {
15
16/**
17 * Non-template base class for VisionRunner.
18 */
20 public:
21 /**
22 * Creates a new vision runner. It will take images from the {@code
23 * videoSource}, and call the virtual DoProcess() method.
24 *
25 * @param videoSource the video source to use to supply images for the
26 * pipeline
27 */
28 explicit VisionRunnerBase(cs::VideoSource videoSource);
29
31
34
35 /**
36 * Runs the pipeline one time, giving it the next image from the video source
37 * specified in the constructor. This will block until the source either has
38 * an image or throws an error. If the source successfully supplied a frame,
39 * the pipeline's image input will be set, the pipeline will run, and the
40 * listener specified in the constructor will be called to notify it that the
41 * pipeline ran. This must be run in a dedicated thread, and cannot be used in
42 * the main robot thread because it will freeze the robot program.
43 *
44 * <p>This method is exposed to allow teams to add additional functionality or
45 * have their own ways to run the pipeline. Most teams, however, should just
46 * use RunForever() in its own thread using a std::thread.</p>
47 */
48 void RunOnce();
49
50 /**
51 * A convenience method that calls runOnce() in an infinite loop. This must be
52 * run in a dedicated thread, and cannot be used in the main robot thread
53 * because it will freeze the robot program.
54 *
55 * <strong>Do not call this method directly from the main thread.</strong>
56 */
57 void RunForever();
58
59 /**
60 * Stop a RunForever() loop.
61 */
62 void Stop();
63
64 protected:
65 virtual void DoProcess(cv::Mat& image) = 0;
66
67 private:
68 std::unique_ptr<cv::Mat> m_image;
69 cs::CvSink m_cvSink;
70 std::atomic_bool m_enabled;
71};
72
73/**
74 * A vision runner is a convenient wrapper object to make it easy to run vision
75 * pipelines from robot code. The easiest way to use this is to run it in a
76 * std::thread and use the listener to take snapshots of the pipeline's outputs.
77 *
78 * @see VisionPipeline
79 */
80template <typename T>
82 public:
83 /**
84 * Creates a new vision runner. It will take images from the {@code
85 * videoSource}, send them to the {@code pipeline}, and call the {@code
86 * listener} when the pipeline has finished to alert user code when it is safe
87 * to access the pipeline's outputs.
88 *
89 * @param videoSource The video source to use to supply images for the
90 * pipeline
91 * @param pipeline The vision pipeline to run
92 * @param listener A function to call after the pipeline has finished
93 * running
94 */
95 VisionRunner(cs::VideoSource videoSource, T* pipeline,
96 std::function<void(T&)> listener)
97 : VisionRunnerBase(videoSource),
98 m_pipeline(pipeline),
99 m_listener(listener) {}
100
101 virtual ~VisionRunner() = default;
102
103 protected:
104 void DoProcess(cv::Mat& image) override {
105 m_pipeline->Process(image);
106 m_listener(*m_pipeline);
107 }
108
109 private:
110 T* m_pipeline;
111 std::function<void(T&)> m_listener;
112};
113
114} // namespace frc
A source for user code to accept video frames as OpenCV images.
Definition cscore_cv.h:86
A source for video that provides a sequence of frames.
Definition cscore_oo.h:253
Non-template base class for VisionRunner.
Definition VisionRunner.h:19
void RunOnce()
Runs the pipeline one time, giving it the next image from the video source specified in the construct...
virtual void DoProcess(cv::Mat &image)=0
VisionRunnerBase & operator=(const VisionRunnerBase &)=delete
void Stop()
Stop a RunForever() loop.
VisionRunnerBase(const VisionRunnerBase &)=delete
void RunForever()
A convenience method that calls runOnce() in an infinite loop.
VisionRunnerBase(cs::VideoSource videoSource)
Creates a new vision runner.
A vision runner is a convenient wrapper object to make it easy to run vision pipelines from robot cod...
Definition VisionRunner.h:81
VisionRunner(cs::VideoSource videoSource, T *pipeline, std::function< void(T &)> listener)
Creates a new vision runner.
Definition VisionRunner.h:95
void DoProcess(cv::Mat &image) override
Definition VisionRunner.h:104
virtual ~VisionRunner()=default
Definition CAN.h:11