OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ViewTrackingCamera.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
26 #include "DirectShowToCV.h"
28 #include "directx_camera_server.h"
29 
30 // Library/third-party includes
31 #include <opencv2/highgui/highgui.hpp>
32 
33 // Standard includes
34 #include <memory>
35 #include <iostream>
36 #include <chrono>
37 #include <sstream>
38 
41 static const std::string windowNameAndInstructions(
42  "OSVR tracking camera preview | q or esc to quit");
43 
44 static const auto FPS_MEASUREMENT_PERIOD = std::chrono::seconds(3);
45 class FrameCounter {
46  public:
47  FrameCounter() { reset(); }
48  void gotFrame() {
49  ++m_frames;
50  auto now = clock::now();
51  if (now >= m_end) {
52  auto duration =
53  std::chrono::duration_cast<std::chrono::duration<double>>(
54  now - m_begin);
55  std::cout << m_frames / duration.count() << " FPS read from camera"
56  << std::endl;
57  reset();
58  }
59  }
60 
61  void reset() {
62  m_begin = clock::now();
63  m_end = m_begin + FPS_MEASUREMENT_PERIOD;
64  m_frames = 0;
65  }
66 
67  private:
68  using clock = std::chrono::system_clock;
69  using time_point = std::chrono::time_point<clock>;
70  time_point m_begin;
71  time_point m_end;
72  std::size_t m_frames = 0;
73 };
74 
75 int main(int argc, char *argv[]) {
76  auto cam = getDirectShowHDKCamera();
77  if (!cam || !cam->read_image_to_memory()) {
78  std::cerr << "Couldn't find, open, or read from the OSVR HDK tracking "
79  "camera.\n"
80  << "Press enter to exit." << std::endl;
81  std::cin.ignore();
82  return -1;
83  }
84  auto FRAME_DISPLAY_STRIDE = 3u;
85  if (argc > 1) {
86  auto is = std::istringstream{argv[1]};
87  if (is >> FRAME_DISPLAY_STRIDE) {
88  std::cout << "Custom display stride passed: "
89  << FRAME_DISPLAY_STRIDE << std::endl;
90  } else {
91  std::cout << "Could not parse first command-line argument as a "
92  "display stride : '"
93  << argv[1] << "' (will use default)" << std::endl;
94  }
95  }
96  std::cout << "Will display 1 out of every " << FRAME_DISPLAY_STRIDE
97  << " frames captured." << std::endl;
98  auto frame = cv::Mat{};
99 
100  auto savedFrame = false;
101  static const auto FILENAME = "capture.png";
102  static const auto FILENAME_STEM = "image";
103  static const auto EXTENSION = ".png";
104  auto captures = std::size_t{0};
105  FrameCounter counter;
106  cv::namedWindow(windowNameAndInstructions);
107  auto frameCount = std::size_t{0};
108  do {
109  frame = retrieve(*cam);
110  counter.gotFrame();
111  ++frameCount;
112  if (frameCount % FRAME_DISPLAY_STRIDE == 0) {
113  frameCount = 0;
114  cv::imshow(windowNameAndInstructions, frame);
115  if (!savedFrame) {
116  savedFrame = true;
117  cv::imwrite(FILENAME, frame);
118  std::cout << "Wrote a captured frame to " << FILENAME
119  << std::endl;
120  }
121  char key = static_cast<char>(cv::waitKey(1)); // wait 1 ms for a key
122  if ('q' == key || 'Q' == key || 27 /*esc*/ == key) {
123  break;
124  } else if ('c' == key) {
125  // capture
126  std::ostringstream os;
127  os << FILENAME_STEM << captures << EXTENSION;
128  cv::imwrite(os.str(), frame);
129  std::cout << "Captured frame to " << os.str() << std::endl;
130  captures++;
131  }
132  }
133  } while (cam->read_image_to_memory());
134  return 0;
135 }
double duration(TimeValue const &a, TimeValue const &b)
Get a double containing seconds between the time points.
Definition: TimeValue.h:62