annotate python/cvutils.py @ 411:31604ef1cad4

added hog functions and the display of road user types if known
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 21 Aug 2013 00:00:23 -0400
parents 91954c76d12c
children 8fdbc13dad8b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
1 #! /usr/bin/env python
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
2 '''Image/Video utilities'''
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
3
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
4 try:
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
5 import cv2
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
6 opencvAvailable = True
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
7 except ImportError:
410
91954c76d12c minor changes to error messages when loading libraries
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 406
diff changeset
8 print('OpenCV library could not be loaded (video replay functions will not be available)')
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
9 opencvAvailable = False
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
10 try:
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
11 import skimage
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
12 skimageAvailable = True
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
13 except ImportError:
410
91954c76d12c minor changes to error messages when loading libraries
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 406
diff changeset
14 print('Scikit-image library could not be loaded (HoG-based classification methods will not be available)')
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
15 skimageAvailable = False
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
16
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
17 from sys import stdout
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
18
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
19 import utils
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
20
44
be3ae926e4e8 added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 28
diff changeset
21 #import aggdraw # agg on top of PIL (antialiased drawing)
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
22 #import utils
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
23
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
24 __metaclass__ = type
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
25
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
26 cvRed = (0,0,255)
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
27 cvGreen = (0,255,0)
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
28 cvBlue = (255,0,0)
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
29 cvColors = utils.PlottingPropertyValues([cvRed,
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
30 cvGreen,
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
31 cvBlue])
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
32
305
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
33 def quitKey(key):
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
34 return chr(key&255)== 'q' or chr(key&255) == 'Q'
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
35
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
36 def saveKey(key):
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
37 return chr(key&255) == 's'
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
38
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
39 def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'):
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
40 '''Draws lines over the image '''
410
91954c76d12c minor changes to error messages when loading libraries
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 406
diff changeset
41 import Image, ImageDraw # PIL
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
42
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
43 img = Image.open(filename)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
44
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
45 draw = ImageDraw.Draw(img)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
46 #draw = aggdraw.Draw(img)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
47 #pen = aggdraw.Pen("red", width)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
48 for p1, p2 in zip(origins, destinations):
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
49 draw.line([p1.x, p1.y, p2.x, p2.y], width = w, fill = (256,0,0))
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
50 #draw.line([p1.x, p1.y, p2.x, p2.y], pen)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
51 del draw
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
52
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
53 #out = utils.openCheck(resultFilename)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
54 img.save(resultFilename)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
55
159
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 156
diff changeset
56 def matlab2PointCorrespondences(filename):
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 156
diff changeset
57 '''Loads and converts the point correspondences saved
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 156
diff changeset
58 by the matlab camera calibration tool'''
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 156
diff changeset
59 from numpy.lib.io import loadtxt, savetxt
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 156
diff changeset
60 from numpy.lib.function_base import append
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 156
diff changeset
61 points = loadtxt(filename, delimiter=',')
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 156
diff changeset
62 savetxt(utils.removeExtension(filename)+'-point-correspondences.txt',append(points[:,:2].T, points[:,3:].T, axis=0))
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 156
diff changeset
63
160
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
64 def loadPointCorrespondences(filename):
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
65 '''Loads and returns the corresponding points in world (first 2 lines) and image spaces (last 2 lines)'''
233
ab1a11176d7b initial sqlite code and bug corrected in cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 227
diff changeset
66 from numpy.lib.npyio import loadtxt
160
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
67 from numpy import float32
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
68 points = loadtxt(filename, dtype=float32)
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
69 return (points[:2,:].T, points[2:,:].T) # (world points, image points)
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
70
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
71 def cvMatToArray(cvmat):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
72 '''Converts an OpenCV CvMat to numpy array.'''
384
6da9cf5609aa adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 381
diff changeset
73 print('Deprecated, use new interface')
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
74 from numpy.core.multiarray import zeros
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
75 a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height)
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
76 for i in xrange(cvmat.rows):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
77 for j in xrange(cvmat.cols):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
78 a[i,j] = cvmat[i,j]
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
79 return a
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
80
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
81 if opencvAvailable:
402
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
82 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=3.0):
302
9d88a4d97473 corrected bug in compute-homography
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 266
diff changeset
83 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)'''
9d88a4d97473 corrected bug in compute-homography
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 266
diff changeset
84 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold)
9d88a4d97473 corrected bug in compute-homography
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 266
diff changeset
85 return H
9d88a4d97473 corrected bug in compute-homography
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 266
diff changeset
86
406
37c7b46f6e21 update to OpenCV 2.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 402
diff changeset
87 def arrayToCvMat(a, t = cv2.CV_64FC1):
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
88 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.'''
384
6da9cf5609aa adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 381
diff changeset
89 print('Deprecated, use new interface')
152
74b1fc68d4df re-organized code to avoid cyclic python module dependencies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 151
diff changeset
90 cvmat = cv2.cv.CreateMat(a.shape[0], a.shape[1], t)
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
91 for i in range(cvmat.rows):
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
92 for j in range(cvmat.cols):
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
93 cvmat[i,j] = a[i,j]
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
94 return cvmat
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
95
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
96 def draw(img, positions, color, lastCoordinate = None):
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
97 last = lastCoordinate+1
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
98 if lastCoordinate != None and lastCoordinate >=0:
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
99 last = min(positions.length()-1, lastCoordinate)
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
100 for i in range(0, last-1):
223
c31722fcc9de minor modifications for integer drawing in OpenCV
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 218
diff changeset
101 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color)
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
102
385
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
103 def cvImshow(windowName, img, rescale = 1.0):
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
104 'Rescales the image (in particular if too large)'
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
105 from cv2 import resize
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
106 if rescale != 1.:
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
107 size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale)))
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
108 resizedImg = resize(img, size)
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
109 cv2.imshow(windowName, resizedImg)
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
110 else:
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
111 cv2.imshow(windowName, img)
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
112
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
113 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.):
150
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
114 '''Plays the video'''
305
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
115 wait = 5
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
116 if frameRate > 0:
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
117 wait = int(round(1000./frameRate))
381
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
118 if interactive:
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
119 wait = 0
149
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
120 capture = cv2.VideoCapture(filename)
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
121 if capture.isOpened():
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
122 key = -1
227
b7612c6d5702 cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 226
diff changeset
123 ret = True
b7612c6d5702 cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 226
diff changeset
124 frameNum = firstFrameNum
406
37c7b46f6e21 update to OpenCV 2.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 402
diff changeset
125 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum)
305
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
126 while ret and not quitKey(key):
149
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
127 ret, img = capture.read()
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
128 if ret:
381
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
129 if printFrames:
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
130 print('frame {0}'.format(frameNum))
226
91197f6a03fe added goto framenum
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 224
diff changeset
131 frameNum+=1
381
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
132 if text != None:
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
133 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
385
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
134 cvImshow('frame', img, rescale)
305
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
135 key = cv2.waitKey(wait)
346
5f75d6c23ed5 added opencv function to destroy OpenCV windows (seems to work only on Windows)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 319
diff changeset
136 cv2.destroyAllWindows()
149
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
137
396
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
138 def getImagesFromVideo(videoFilename, firstFrameNum = 0, nFrames = 1, saveImage = False, outputPrefix = 'image'):
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
139 '''Returns nFrames images from the video sequence'''
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
140 from math import floor, log10
150
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
141 images = []
396
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
142 capture = cv2.VideoCapture(videoFilename)
398
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
143 if capture.isOpened():
406
37c7b46f6e21 update to OpenCV 2.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 402
diff changeset
144 nDigits = int(floor(log10(capture.get(cv2.CAP_PROP_FRAME_COUNT))))+1
150
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
145 ret = False
406
37c7b46f6e21 update to OpenCV 2.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 402
diff changeset
146 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum)
396
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
147 imgNum = 0
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
148 while imgNum<nFrames:
171
8e7b354666ec corrected bug in to get images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
149 ret, img = capture.read()
216
51acf43e421a modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 204
diff changeset
150 i = 0
51acf43e421a modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 204
diff changeset
151 while not ret and i<10:
150
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
152 ret, img = capture.read()
216
51acf43e421a modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 204
diff changeset
153 i += 1
171
8e7b354666ec corrected bug in to get images from video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
154 if img.size>0:
216
51acf43e421a modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 204
diff changeset
155 if saveImage:
396
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
156 imgNumStr = format(firstFrameNum+imgNum, '0{}d'.format(nDigits))
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
157 cv2.imwrite(outputPrefix+imgNumStr+'.png', img)
216
51acf43e421a modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 204
diff changeset
158 else:
51acf43e421a modified the function to read and save images from a movie
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 204
diff changeset
159 images.append(img)
396
167f6ec44ec5 cleaned function to save images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 393
diff changeset
160 imgNum +=1
398
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
161 capture.release()
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
162 else:
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
163 print('Video capture failed')
150
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
164 return images
398
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
165
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
166 def getFPS(videoFilename):
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
167 capture = cv2.VideoCapture(videoFilename)
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
168 if capture.isOpened():
406
37c7b46f6e21 update to OpenCV 2.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 402
diff changeset
169 fps = capture.get(cv2.CAP_PROP_FPS)
398
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
170 capture.release()
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
171 return fps
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
172 else:
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
173 print 'Cannot load file ' + videoFilename
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
174 return None
3399bd48cb40 Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents: 396
diff changeset
175
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
176 def imageBox(img, obj, frameNum, homography, width, height, px=.2, py=.2, PixelThreshold=800):
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
177 x = []
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
178 y = []
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
179 for f in obj.features:
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
180 if f.existsAtInstant(frameNum):
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
181 x.append(f.getPositionAtInstant(frameNum).project(homography).x)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
182 y.append(f.getPositionAtInstant(frameNum).project(homography).y)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
183 Xmin = min(x)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
184 Xmax = max(x)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
185 Ymin = min(y)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
186 Ymax = max(y)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
187 XMm = px * (Xmax - Xmin)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
188 YMm = py * (Ymax - Ymin)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
189 a = max(Ymax - Ymin + (2 * YMm), Xmax - (Xmin + 2 * XMm))
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
190 Y_Crop_min = int(max(0, .5 * (Ymin + Ymax - a)))
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
191 Y_Crop_Max = int(min(height - 1, .5 * (Ymin + Ymax + a)))
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
192 X_Crop_min = int(max(0, .5 * (Xmin + Xmax - a)))
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
193 X_Crop_Max = int(min(width - 1, .5 * (Xmin + Xmax + a)))
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
194 if Y_Crop_Max != Y_Crop_min and X_Crop_Max != X_Crop_min and (Y_Crop_Max - Y_Crop_min) * (X_Crop_Max - X_Crop_min) > PixelThreshold:
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
195 img_crop = img[Y_Crop_min : Y_Crop_Max, X_Crop_min : X_Crop_Max]
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
196 else:
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
197 img_crop = []
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
198 return img_crop, Y_Crop_min, Y_Crop_Max, X_Crop_min, X_Crop_Max
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
199
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
200
393
eaf7765221d9 added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 385
diff changeset
201 def displayTrajectories(videoFilename, objects, boundingBoxes, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.):
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
202 '''Displays the objects overlaid frame by frame over the video '''
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
203 from moving import userTypeNames
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
204
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
205 capture = cv2.VideoCapture(videoFilename)
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
206 width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
207 height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
208 if capture.isOpened():
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
209 key = -1
227
b7612c6d5702 cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 226
diff changeset
210 ret = True
b7612c6d5702 cleaned the code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 226
diff changeset
211 frameNum = firstFrameNum
406
37c7b46f6e21 update to OpenCV 2.6
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 402
diff changeset
212 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum)
248
571ba5ed22e2 added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 243
diff changeset
213 if not lastFrameNumArg:
571ba5ed22e2 added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 243
diff changeset
214 from sys import maxint
571ba5ed22e2 added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 243
diff changeset
215 lastFrameNum = maxint
571ba5ed22e2 added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 243
diff changeset
216 else:
571ba5ed22e2 added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 243
diff changeset
217 lastFrameNum = lastFrameNumArg
305
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
218 while ret and not quitKey(key) and frameNum < lastFrameNum:
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
219 ret, img = capture.read()
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
220 if ret:
381
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
221 if printFrames:
387cc0142211 script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 365
diff changeset
222 print('frame {0}'.format(frameNum))
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
223 for obj in objects:
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
224 if obj.existsAtInstant(frameNum):
236
eb4525853030 added script to display trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 235
diff changeset
225 if not hasattr(obj, 'projectedPositions'):
218
b5772df11b37 corrected bugs to load objects and display trajectories over videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 216
diff changeset
226 if homography != None:
b5772df11b37 corrected bugs to load objects and display trajectories over videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 216
diff changeset
227 obj.projectedPositions = obj.positions.project(homography)
b5772df11b37 corrected bugs to load objects and display trajectories over videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 216
diff changeset
228 else:
b5772df11b37 corrected bugs to load objects and display trajectories over videos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 216
diff changeset
229 obj.projectedPositions = obj.positions
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
230 draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant())
393
eaf7765221d9 added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 385
diff changeset
231 if frameNum in boundingBoxes.keys():
eaf7765221d9 added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 385
diff changeset
232 for rect in boundingBoxes[frameNum]:
eaf7765221d9 added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 385
diff changeset
233 cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvRed)
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
234 objDescription = '{} '.format(obj.num)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
235 if userTypeNames[obj.userType] != 'unknown':
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
236 objDescription += userTypeNames[obj.userType][0].upper()
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
237 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
238 if obj.features != None:
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
239 img_crop, Y_Crop_min, Y_Crop_Max, X_Crop_min, X_Crop_Max = imageBox(img, obj, frameNum, homography, width, height)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
240 cv2.rectangle(img, (X_Crop_min, Y_Crop_min), (X_Crop_Max, Y_Crop_Max), cvBlue, 1)
385
1917db662aa7 added rescaling options to scripts play-video and display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 384
diff changeset
241 cvImshow('frame', img, rescale)
266
aba9711b3149 small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 248
diff changeset
242 key = cv2.waitKey()
305
ca9131968bce added sample to replay video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 303
diff changeset
243 if saveKey(key):
266
aba9711b3149 small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 248
diff changeset
244 cv2.imwrite('image.png', img)
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
245 frameNum += 1
346
5f75d6c23ed5 added opencv function to destroy OpenCV windows (seems to work only on Windows)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 319
diff changeset
246 cv2.destroyAllWindows()
402
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
247
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
248 def computeHomographyFromPDTV(cameraFilename, method=0, ransacReprojThreshold=3.0):
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
249 '''Returns the homography matrix at ground level from PDTV format
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
250 https://bitbucket.org/hakanardo/pdtv'''
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
251 import pdtv
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
252 from numpy import array
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
253 camera = pdtv.load(cameraFilename)
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
254 srcPoints = [[x,y] for x, y in zip([1.,2.,2.,1.],[1.,1.,2.,2.])] # need floats!!
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
255 dstPoints = []
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
256 for srcPoint in srcPoints:
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
257 projected = camera.image_to_world(tuple(srcPoint))
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
258 dstPoints.append([projected[0], projected[1]])
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
259 H, mask = cv2.findHomography(array(srcPoints), array(dstPoints), method, ransacReprojThreshold)
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
260 return H
f29204e68aab function to generate homography from PDTV Tsai format and script to generate trajectories from sqlite bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 398
diff changeset
261
203
e2f31813ade6 added code to display trajectories on videa
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 171
diff changeset
262
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
263 def printCvMat(cvmat, out = stdout):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
264 '''Prints the cvmat to out'''
384
6da9cf5609aa adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 381
diff changeset
265 print('Deprecated, use new interface')
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
266 for i in xrange(cvmat.rows):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
267 for j in xrange(cvmat.cols):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
268 out.write('{0} '.format(cvmat[i,j]))
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
269 out.write('\n')
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
270
98
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
271 def projectArray(homography, points):
384
6da9cf5609aa adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 381
diff changeset
272 '''Returns the coordinates of the projected points through homography
6da9cf5609aa adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 381
diff changeset
273 (format: array 2xN points)'''
315
82b9be447608 bugfix for dot import
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 305
diff changeset
274 from numpy.core import dot
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
275 from numpy.core.multiarray import array
98
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
276 from numpy.lib.function_base import append
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
277
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
278 if points.shape[0] != 2:
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
279 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1]))
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
280
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
281 if (homography!=None) and homography.size>0:
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
282 augmentedPoints = append(points,[[1]*points.shape[1]], 0)
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
283 prod = dot(homography, augmentedPoints)
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
284 return prod[0:2]/prod[2]
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
285 else:
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
286 return p
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
287
98
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
288 def project(homography, p):
319
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 315
diff changeset
289 '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1]
98
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
290 through homography'''
243
e0988a8ace0c started adapting and moving to other modules Mohamed's work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 236
diff changeset
291 from numpy import array
e0988a8ace0c started adapting and moving to other modules Mohamed's work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 236
diff changeset
292 return projectArray(homography, array([[p[0]],[p[1]]]))
98
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
293
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
294 def projectTrajectory(homography, trajectory):
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
295 '''Projects a series of points in the format
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
296 [[x1, x2, ...],
98
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
297 [y1, y2, ...]]'''
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
298 from numpy.core.multiarray import array
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
299 return projectArray(homography, array(trajectory))
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
300
48
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
301 def invertHomography(homography):
384
6da9cf5609aa adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 381
diff changeset
302 '''Returns an inverted homography
6da9cf5609aa adding deprecated messages if old cvmat format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 381
diff changeset
303 Unnecessary for reprojection over camera image'''
48
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
304 from numpy.linalg.linalg import inv
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
305 invH = inv(homography)
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
306 invH /= invH[2,2]
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
307 return invH
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
308
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
309 if opencvAvailable:
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
310 def computeTranslation(img1, img2, img1Points, maxTranslation2, minNMatches, windowSize = (5,5), level = 5, criteria = (cv2.TERM_CRITERIA_EPS, 0, 0.01)):
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
311 '''Computes the translation of img2 with respect to img1
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
312 (loaded using OpenCV as numpy arrays)
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
313 img1Points are used to compute the translation
100
2a3cafcf5faf added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 99
diff changeset
314
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
315 TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom, other non linear distortion)'''
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
316 from numpy.core.multiarray import array
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
317 from numpy.lib.function_base import median
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
318 from numpy.core.fromnumeric import sum
100
2a3cafcf5faf added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 99
diff changeset
319
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
320 nextPoints = array([])
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
321 (img2Points, status, track_error) = cv2.calcOpticalFlowPyrLK(img1, img2, img1Points, nextPoints, winSize=windowSize, maxLevel=level, criteria=criteria)
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
322 # calcOpticalFlowPyrLK(prevImg, nextImg, prevPts[, nextPts[, status[, err[, winSize[, maxLevel[, criteria[, derivLambda[, flags]]]]]]]]) -> nextPts, status, err
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
323 delta = []
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
324 for (k, (p1,p2)) in enumerate(zip(img1Points, img2Points)):
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
325 if status[k] == 1:
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
326 dp = p2-p1
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
327 d = sum(dp**2)
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
328 if d < maxTranslation2:
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
329 delta.append(dp)
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
330 if len(delta) >= minNMatches:
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 152
diff changeset
331 return median(delta, axis=0)
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
332 else:
156
2eef5620c0b3 added key values for opencv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 154
diff changeset
333 print(dp)
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
334 return None
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
335
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
336 if skimageAvailable:
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
337 def HOG(image, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False):
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
338 from skimage.feature import hog
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
339 from skimage import color, transform
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
340
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
341 bwImg = color.rgb2gray(image)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
342 inputImg = transform.resize(bwImg, rescaleSize)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
343 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
344 if visualize:
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
345 from matplotlib.pyplot import imshow, figure, subplot
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
346 hogViz = features[1]
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
347 features = features[0]
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
348 figure()
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
349 subplot(1,2,1)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
350 imshow(img)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
351 subplot(1,2,2)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
352 imshow(hogViz)
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
353 return features
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
354
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
355 def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False):
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
356 from os import listdir
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
357 from numpy import array, float32
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
358 from matplotlib.pyplot import imread
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
359
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
360 inputData = []
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
361 for filename in listdir(imageDirectory):
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
362 img = imread(imageDirectory+filename)
411
31604ef1cad4 added hog functions and the display of road user types if known
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 410
diff changeset
363 features = HOG(img, rescaleSize, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize)
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
364 inputData.append(features)
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
365
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
366 nImages = len(inputData)
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
367 return array(inputData, dtype = float32), array([classLabel]*nImages, dtype = float32)
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
368
22ddb8f85495 added function to create HOG training set
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 346
diff changeset
369