ACT-CV - Machine Vision for Cognitive Modeling
FrameLoop.h
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2 
3 /*
4 ACT-CV - Machine Vision for Cognitive Modeling
5 Copyright (c) 2008 Marc Halbruegge
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
22 
27 #ifndef FRAMELOOP_H_
28 #define FRAMELOOP_H_
29 
30 #include <vector>
31 #include <map>
32 #include <iostream>
33 
34 #include <mhexcptn.h>
35 #include <DynamicsHelpers.h>
36 #include <GCString.h>
37 
38 #if HAVE_ICE
39 # include <IceComm/ActCvImpl.h>
40 #endif
41 
42 #include <Threads/Condition.h>
43 #include <Threads/Mutex.h>
44 
45 #include <vision/FrameSource.h>
46 #include <vision/FrameObserver.h>
47 
48 #include <oflow/OpticalFlow.h>
49 
50 #include <cv.h>
51 #include <highgui.h>
52 
53 #include "Matcher.h"
54 
55 #include <tcpclient/TcpClient.h>
56 
57 extern "C" {
58 #include "server/ACT-CV.i"
59 }
60 
61 #include "server/ResultHolder.h"
62 
63 
67 class FrameLoop : virtual public ReferenceCount, public FrameObsSupport {
68 protected:
69 public:
71  virtual void Loop(FrameSource *frameSource) =0;
73  virtual void LoopUntilKeyPress(FrameSource *frameSource) =0;
75  virtual void LoopNFrames(FrameSource *frameSource, int nFrames) =0;
77  virtual void SingleFrame(FrameSource *frameSource) =0;
78 
79  virtual void WaitForFirstFrame() =0;
80  virtual void WaitForNextFrame() =0;
81 };
82 
89 class FrameLoopImpl : public FrameLoop {
91  std::vector<RegionInfo> regions;
92 #define WAIT_TIME 10
93 public:
95  for (;;) {
96  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
97  IplImage *img = frameSource->GetNextFrame();
98  FireFrameNotification(img,frameSource->GetFrameNr());
99  if (img==nullptr) break;
100  cvWaitKey(WAIT_TIME);
101  newFrame.Signal();
102  }
103  }
105  for (;;) {
106  IplImage *img = frameSource->GetNextFrame();
107  FireFrameNotification(img,frameSource->GetFrameNr());
108  std::vector<RegionInfo> curRegions;
109  curRegions.reserve(frameSource->GetNumInfos());
110  for(size_t u=0; u<frameSource->GetNumInfos(); ++u) {
111  curRegions.push_back(frameSource->GetInfo(u));
112  }
113  std::sort(curRegions.begin(), curRegions.end());
114  if(curRegions != regions) {
115  regions = curRegions;
117  }
118  if (img==nullptr) break;
119  if (cvWaitKey(WAIT_TIME) >= 0) break;
120  newFrame.Signal();
121  }
122  }
123  void LoopNFrames(FrameSource *frameSource, int nFrames) {
124  for (int i=0;i<nFrames;i++) {
125  IplImage *img = frameSource->GetNextFrame();
126  FireFrameNotification(img,frameSource->GetFrameNr());
127  if (img==nullptr) break;
128  cvWaitKey(WAIT_TIME);
129  newFrame.Signal();
130  }
131  }
133  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
134  IplImage *img = frameSource->GetNextFrame();
135  MHDBGMSG_P(__FILE__,__LINE__,"NextFrame",img);
136  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
137  FireFrameNotification(img,frameSource->GetFrameNr());
138  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
139  std::vector<RegionInfo> curRegions;
140  curRegions.reserve(frameSource->GetNumInfos());
141  for(size_t u=0; u<frameSource->GetNumInfos(); ++u) {
142  curRegions.push_back(frameSource->GetInfo(u));
143  }
144  std::sort(curRegions.begin(), curRegions.end());
145  if(curRegions != regions) {
146  regions = curRegions;
148  }
149  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
150  cvWaitKey(5);
151  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
152  newFrame.Signal();
153  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
154  }
160  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
161  newFrame.TimedWait(5*60,0);
162  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
163  }
169  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
170 
171  int res=newFrame.TimedWait(30,0);
172  //int res=newFrame.Wait();
173  if (res!=0) {
174  // no new frame! 110 = ETIMEDOUT
175  MHDBGMSG_I(__FILE__,__LINE__,"Error code:",res);
176  throw std::runtime_error("no new frame");
177  }
178  MHDBGMSG(__FILE__,__LINE__,__PRETTY_FUNCTION__);
179  }
180 };
181 
182 
183 #endif /*FRAMELOOP_H_*/


ACT-CV - Machine Vision for Cognitive Modeling
© 2015 Marc Halbruegge (actcvlibrary@googlemail.com)