1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 '''
35 Created on Oct 31, 2011
36
37 @author: bolme
38 '''
39
40 import pyvision as pv
41 import cv
42 import PIL.Image as pil
43
46
48 '''
49 This object handles the data mangagement and display of the capture clicks window.
50 '''
51
52 - def __init__(self,im,default_points=[],keep_window_open = False,window="PyVision Capture Points"):
53 '''
54 Initialize the data.
55 '''
56 self.window = window
57 self.im = im.copy()
58 self.keep_window_open = keep_window_open
59 self.reset()
60 for pt in default_points:
61 self.mouseCallback(cv.CV_EVENT_LBUTTONDOWN,pt.X(),pt.Y(),None,None)
62
64 '''
65 Display the window and run the main event loop.
66 '''
67
68 cv.NamedWindow(self.window)
69 cv.SetMouseCallback(self.window, self.mouseCallback)
70
71 while True:
72 key_press = self.im.show(self.window,delay=100)
73
74
75 if key_press == ord(' '):
76 break
77
78 if key_press == ord('r'):
79 self.reset()
80
81 if not self.keep_window_open:
82 cv.DestroyWindow(self.window)
83
84 return self.points
85
95
96
105
107 '''
108 This object handles the data mangagement and display of the capture clicks window.
109 '''
110
111 - def __init__(self, video, buffer_size = 60, callback = None, keep_window_open=False):
112 '''
113 Initialize the data.
114 '''
115 self.callback = callback
116 self.video = video
117 self.points = {}
118 self.buffer = []
119 self.frame = -1
120 self.buffer_index = -1
121 self.buffer_size = buffer_size
122 self.keep_window_open = keep_window_open
123 self.next()
124
125
127 '''
128 Display the window and run the main event loop.
129 '''
130
131 cv.NamedWindow("PyVision Capture Points")
132
133
134
135 cv.SetMouseCallback("PyVision Capture Points", self.mouseCallback)
136
137
138 while True:
139 key_press = self.im.show("PyVision Capture Points",delay=100)
140
141
142 if key_press == ord('r'):
143 self.reset()
144
145 if key_press == ord('p'):
146 self.prev()
147
148 if key_press == ord('P'):
149 for _ in range(10):
150 self.prev()
151
152 if key_press == ord(' ') or key_press == ord('n'):
153 self.next()
154
155 if key_press == ord('N'):
156 for _ in range(10):
157 self.next()
158
159 if key_press == ord('q'):
160 break
161
162
163 del self.buffer
164
165 if not self.keep_window_open:
166 cv.DestroyWindow("PyVision Capture Points")
167
168 return self.points
169
174
176 '''
177 Clear the points and start over.
178 '''
179 im = self.buffer[self.buffer_index]
180 w,h = im.size
181 nim = pil.new('RGB',(w,h+100))
182 nim.paste(im.asPIL(),(0,0))
183 self.im = pv.Image(nim)
184
185 if self.callback != None:
186 self.callback(self.im,self.frame)
187
188 self.im.annotateLabel(pv.Point(10,h+10), "Frame: %d"%self.frame,color='yellow')
189 self.im.annotateLabel(pv.Point(10,h+20), "Click anywhere in the image to select a point.",color='yellow')
190 self.im.annotateLabel(pv.Point(10,h+30), "Press 'r' to reset.",color='yellow')
191 self.im.annotateLabel(pv.Point(10,h+40), "Press the space bar or 'n' for the next frame.",color='yellow')
192 self.im.annotateLabel(pv.Point(10,h+50), "Press 'p' for the previous frame.",color='yellow')
193 self.im.annotateLabel(pv.Point(10,h+60), "Press 'N' or 'P' to skip 10 frames.",color='yellow')
194 self.im.annotateLabel(pv.Point(10,h+70), "Press 'q' when finished.",color='yellow')
195 if self.points.has_key(self.frame):
196 points = self.points[self.frame]
197 for i in range(len(points)):
198 pt = points[i]
199 self.im.annotateLabel(pt,'%d'% i,mark='below')
200
202 if self.buffer_index == -1:
203 try:
204 self.buffer.append(self.video.next())
205 self.frame += 1
206 except StopIteration:
207 print "End of video."
208 self.buffer = self.buffer [-self.buffer_size:]
209 else:
210 self.buffer_index += 1
211 self.frame += 1
212
213 print self.buffer_index,self.frame,len(self.buffer),self.points
214 self.render()
215
216
218 if self.buffer_index == -len(self.buffer):
219 print "Buffer exceed. Cannot display previous frame"
220 else:
221 self.buffer_index -= 1
222 self.frame -= 1
223 self.render()
224
225
237
239 '''
240 This function opens a high gui window that displays the image. Any
241 points that are clicked will be returned after the user presses the
242 space bar.
243
244 @param im: An image to display.
245 @param default_points: Some default points to display.
246 @type default_points: list of pv.Point
247 @type default_points: list
248 @param keep_window_open: keep the window open after point were captured
249 @type True|False
250 @param window: The name of the window
251 @type window: string
252 @returns: a list of points that were clicked by the user.
253 '''
254 if isinstance(im, pv.Image):
255 cap = CaptureClicks(im,*args,**kwargs)
256 clicks = cap.display()
257 else:
258 cap = CaptureClicksVideo(im,*args,**kwargs)
259 clicks = cap.display()
260 return clicks
261
262
263 if __name__ == '__main__':
264
265
266
267 video = pv.Video(pv.TAZ_VIDEO)
268 ccv = capturePointsFromMouse(video)
269