diff options
Diffstat (limited to 'FTNoIR_Tracker_PT/camera.cpp')
-rw-r--r-- | FTNoIR_Tracker_PT/camera.cpp | 135 |
1 files changed, 107 insertions, 28 deletions
diff --git a/FTNoIR_Tracker_PT/camera.cpp b/FTNoIR_Tracker_PT/camera.cpp index 21a910c1..a372373d 100644 --- a/FTNoIR_Tracker_PT/camera.cpp +++ b/FTNoIR_Tracker_PT/camera.cpp @@ -10,6 +10,69 @@ using namespace cv;
+#if defined(OPENTRACK_API) && (defined(__unix) || defined(__linux))
+#include <unistd.h>
+#endif
+
+#ifdef OPENTRACK_API
+void get_camera_device_names(std::vector<std::string>& device_names) {
+# if defined(_WIN32)
+ // Create the System Device Enumerator.
+ HRESULT hr;
+ ICreateDevEnum *pSysDevEnum = NULL;
+ hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum);
+ if (FAILED(hr))
+ {
+ return ret;
+ }
+ // Obtain a class enumerator for the video compressor category.
+ IEnumMoniker *pEnumCat = NULL;
+ hr = pSysDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnumCat, 0);
+
+ if (hr == S_OK) {
+ // Enumerate the monikers.
+ IMoniker *pMoniker = NULL;
+ ULONG cFetched;
+ while (pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK) {
+ IPropertyBag *pPropBag;
+ hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pPropBag);
+ if (SUCCEEDED(hr)) {
+ // To retrieve the filter's friendly name, do the following:
+ VARIANT varName;
+ VariantInit(&varName);
+ hr = pPropBag->Read(L"FriendlyName", &varName, 0);
+ if (SUCCEEDED(hr))
+ {
+ device_names.push_back(std::string(reinterpret_cast<char const*>(varName.bstrVal)));
+ }
+ VariantClear(&varName);
+
+ ////// To create an instance of the filter, do the following:
+ ////IBaseFilter *pFilter;
+ ////hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter,
+ //// (void**)&pFilter);
+ // Now add the filter to the graph.
+ //Remember to release pFilter later.
+ pPropBag->Release();
+ }
+ pMoniker->Release();
+ }
+ pEnumCat->Release();
+ }
+ pSysDevEnum->Release();
+# else
+ for (int i = 0; i < 16; i++) {
+ char buf[128];
+ sprintf(buf, "/dev/video%d", i);
+ if (access(buf, R_OK | W_OK) == 0) {
+ device_names.push_back(std::string(buf));
+ } else {
+ continue;
+ }
+ }
+# endif
+}
+#else
// ----------------------------------------------------------------------------
void get_camera_device_names(std::vector<std::string>& device_names)
{
@@ -22,6 +85,7 @@ void get_camera_device_names(std::vector<std::string>& device_names) device_names.push_back(device_name);
}
}
+#endif
// ----------------------------------------------------------------------------
void Camera::set_device_index(int index)
@@ -81,43 +145,49 @@ bool Camera::get_frame(float dt, cv::Mat* frame) }
// ----------------------------------------------------------------------------
-/*
+#ifdef OPENTRACK_API
void CVCamera::start()
{
- cap = cvCreateCameraCapture(desired_index);
+ cap = new VideoCapture(desired_index);
// extract camera info
- if (cap)
+ if (cap->isOpened())
{
active = true;
active_index = desired_index;
- cam_info.res_x = cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH);
- cam_info.res_y = cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT);
+ cam_info.res_x = cap->get(CV_CAP_PROP_FRAME_WIDTH);
+ cam_info.res_y = cap->get(CV_CAP_PROP_FRAME_HEIGHT);
}
+ else {
+ delete cap;
+ cap = NULL;
+ }
}
void CVCamera::stop()
{
- if (cap) cvReleaseCapture(&cap);
+ if (cap)
+ {
+ cap->release();
+ delete cap;
+ }
active = false;
}
bool CVCamera::_get_frame(Mat* frame)
{
- if (cap && cvGrabFrame(cap) != 0)
+ if (cap)
{
- // retrieve frame
- IplImage* _img = cvRetrieveFrame(cap, 0);
- if(_img)
- {
- if(_img->origin == IPL_ORIGIN_TL)
- *frame = Mat(_img);
- else
- {
- Mat temp(_img);
- flip(temp, *frame, 0);
- }
- return true;
- }
+ Mat img;
+ /*
+ * XXX some Windows webcams fail to decode first
+ * frames and then some every once in a while
+ * -sh
+ */
+ while (!cap->read(img))\
+ ;;
+
+ *frame = img;
+ return true;
}
return false;
}
@@ -134,21 +204,30 @@ void CVCamera::_set_f() void CVCamera::_set_fps()
{
- if (cap) cvSetCaptureProperty(cap, CV_CAP_PROP_FPS, cam_desired.fps);
+ if (cap) cap->set(CV_CAP_PROP_FPS, cam_desired.fps);
}
void CVCamera::_set_res()
{
if (cap)
{
- cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, cam_desired.res_x);
- cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, cam_desired.res_y);
- cam_info.res_x = cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH);
- cam_info.res_y = cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT);
+ cap->set(CV_CAP_PROP_FRAME_WIDTH, cam_desired.res_x);
+ cap->set(CV_CAP_PROP_FRAME_HEIGHT, cam_desired.res_y);
+ cam_info.res_x = cap->get(CV_CAP_PROP_FRAME_WIDTH);
+ cam_info.res_y = cap->get(CV_CAP_PROP_FRAME_HEIGHT);
}
}
-*/
+void CVCamera::_set_device_index()
+{
+ if (cap)
+ {
+ cap->release();
+ delete cap;
+ }
+ cap = new VideoCapture(desired_index);
+}
+#else
// ----------------------------------------------------------------------------
VICamera::VICamera() : frame_buffer(NULL)
{
@@ -234,7 +313,7 @@ void VICamera::_set_res() {
if (active) restart();
}
-
+#endif
// ----------------------------------------------------------------------------
Mat FrameRotation::rotate_frame(Mat frame)
@@ -260,4 +339,4 @@ Mat FrameRotation::rotate_frame(Mat frame) default:
return frame;
}
-}
\ No newline at end of file +}
|