comparison python/cvutils.py @ 725:35bc5e30a53f

slight reorganization of display-trajectories (more efficient filtering of list of objects for long sequences)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 07 Aug 2015 01:05:01 -0400
parents 94b291a5f933
children 43ae3a1af290
comparison
equal deleted inserted replaced
724:b75d0c258ca9 725:35bc5e30a53f
288 from sys import maxint 288 from sys import maxint
289 lastFrameNum = maxint 289 lastFrameNum = maxint
290 else: 290 else:
291 lastFrameNum = lastFrameNumArg 291 lastFrameNum = lastFrameNumArg
292 nZerosFilename = int(ceil(log10(lastFrameNum))) 292 nZerosFilename = int(ceil(log10(lastFrameNum)))
293 objectToDeleteIds = []
293 while ret and not quitKey(key) and frameNum <= lastFrameNum: 294 while ret and not quitKey(key) and frameNum <= lastFrameNum:
294 ret, img = capture.read() 295 ret, img = capture.read()
295 if ret: 296 if ret:
296 if undistort: 297 if undistort:
297 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) 298 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
298 if printFrames: 299 if printFrames:
299 print('frame {0}'.format(frameNum)) 300 print('frame {0}'.format(frameNum))
301 if len(objectToDeleteIds) > 0:
302 objects = [o for o in objects if o.getNum() not in objectToDeleteIds]
303 objectToDeleteIds = []
300 for obj in objects: 304 for obj in objects:
301 if obj.existsAtInstant(frameNum): 305 if obj.existsAtInstant(frameNum):
306 if obj.getLastInstant() == frameNum:
307 objectToDeleteIds.append(obj.getNum())
302 if not hasattr(obj, 'projectedPositions'): 308 if not hasattr(obj, 'projectedPositions'):
303 if homography is not None: 309 if homography is not None:
304 obj.projectedPositions = obj.positions.project(homography) 310 obj.projectedPositions = obj.positions.project(homography)
305 else: 311 else:
306 obj.projectedPositions = obj.positions 312 obj.projectedPositions = obj.positions
307 cvPlot(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant()) 313 cvPlot(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant())
308 if frameNum in boundingBoxes.keys(): 314 if frameNum not in boundingBoxes.keys() and obj.hasFeatures():
309 for rect in boundingBoxes[frameNum]:
310 cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvRed)
311 elif obj.hasFeatures():
312 imgcrop, yCropMin, yCropMax, xCropMin, xCropMax = imageBox(img, obj, frameNum, homography, width, height) 315 imgcrop, yCropMin, yCropMax, xCropMin, xCropMax = imageBox(img, obj, frameNum, homography, width, height)
313 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue, 1) 316 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue, 1)
314 objDescription = '{} '.format(obj.num) 317 objDescription = '{} '.format(obj.num)
315 if userTypeNames[obj.userType] != 'unknown': 318 if userTypeNames[obj.userType] != 'unknown':
316 objDescription += userTypeNames[obj.userType][0].upper() 319 objDescription += userTypeNames[obj.userType][0].upper()
317 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed) 320 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed)
321 # plot object bounding boxes
322 if frameNum in boundingBoxes.keys():
323 for rect in boundingBoxes[frameNum]:
324 cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvRed)
318 if not saveAllImages: 325 if not saveAllImages:
319 cvImshow(windowName, img, rescale) 326 cvImshow(windowName, img, rescale)
320 key = cv2.waitKey() 327 key = cv2.waitKey()
321 if saveAllImages or saveKey(key): 328 if saveAllImages or saveKey(key):
322 cv2.imwrite('image-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) 329 cv2.imwrite('image-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img)