tvaLib
Public Member Functions | Public Attributes | List of all members
lib.scene.Scene Class Reference

Declare Scene descriptor object. More...

Inheritance diagram for lib.scene.Scene:

Public Member Functions

def __init__ (self, scene, config)
 
def new (self, camIdx, session, sqlFields={})
 
def write (self, scene)
 
def getHomography (self, type='undistorted')
 
def setHomography (self, value)
 
def getBaseDirectory (self)
 
def getClassificationTrainingBaseDirectory (self, type)
 
def getFullHomographyFilename (self, force=False)
 
def getFullCameraCalibrationFilename (self)
 
def getFullConfigurationFilename (self)
 
def getFullMaskFilename (self)
 
def getStartTime (self)
 
def getEndTime (self)
 
def getADT (self, hourly_expansion_factors, exposure='traffic', customCount=None, customHour=None, kwargs)
 
def getAADT (self, hourly_expansion_factors, doweek_expansion_factors, monthly_expansion_factors, kwargs)
 
def getCountObjects (self)
 
def getCountObjOld (self)
 
def getDuration (self)
 
def getDurationUnit (self)
 
def exportToCode (self, moduleName='tvaMetaData', justifyCol1=20, justifyCol2=35, recursive=True)
 

Public Attributes

 config
 
 idx
 
 site
 
 name
 
 homographyFilename
 
 homographyDistanceUnit
 
 cameraCalibrationFilename
 
 configurationFilename
 
 defaultFullCfgFilename
 Setup defaults from config. More...
 
 maskFilename
 
 camHeight
 
 homography
 Create and link abstract objects from data. More...
 
 mask
 
 camOrigin
 
 camera
 

Detailed Description

Declare Scene descriptor object.

Definition at line 208 of file scene.py.

Constructor & Destructor Documentation

◆ __init__()

def lib.scene.Scene.__init__ (   self,
  scene,
  config 
)

Definition at line 209 of file scene.py.

209  def __init__(self, scene, config):
210  tvaLib.Constructors.SuperList.__init__(self)
211  self.config = config
212  self.idx = scene.idx
213  self.site = scene.site
214  self.name = scene.name
215  self.homographyFilename = scene.homographyFilename
216  self.homographyDistanceUnit = scene.homographyDistanceUnit
217  self.cameraCalibrationFilename = scene.cameraCalibrationFilename
218  self.configurationFilename = scene.configurationFilename
219  self.defaultFullCfgFilename = 'tracking.cfg'
220  self.maskFilename = scene.maskFilename
221  self.camHeight = scene.camHeight
222 
223  self.homography = tvaLib.Constructors.SuperListParse(scene.homography, dimension=2, datatype='float')
224  self.mask = tvaLib.Constructors.SuperListParse(scene.mask, dimension=3, datatype='point')
225  self.camOrigin = tvaLib.Constructors.SuperListParse(scene.camOrigin, dimension=1, datatype='float')
226  self.camera = CameraType(scene.camera, self.config)
227  for sequence in scene.video_sequences:
228  self.data.append(Sequence(sequence, self.config))
229 
230  self.defaultFullCfgFilename = os.path.join(self.config.path_to_trafint, 'tracking.cfg')
231  return
232 

Member Function Documentation

◆ exportToCode()

def lib.scene.Scene.exportToCode (   self,
  moduleName = 'tvaMetaData',
  justifyCol1 = 20,
  justifyCol2 = 35,
  recursive = True 
)

Definition at line 397 of file scene.py.

397  def exportToCode(self, moduleName='tvaMetaData', justifyCol1=20, justifyCol2=35, recursive=True):
398  print("cameras.append("+moduleName+".CameraView(([x for x in sites if x.name=='"+(self.site.name+"'][0],").ljust(justifyCol1)+" [x for x in camTypes if x.name=='"+(self.camera.name+"'][0],").ljust(justifyCol2)+" '"+self.name+"', homographyFilename='"+self.homographyFilename+"'))")
399  if(recursive):
400  for sequence in self: sequence.exportToCode(moduleName, recursive=recursive)
401 

◆ getAADT()

def lib.scene.Scene.getAADT (   self,
  hourly_expansion_factors,
  doweek_expansion_factors,
  monthly_expansion_factors,
  kwargs 
)
Compute, update, and return Average Daily Traffic. See
    Scene.getADT() for list of exposure types.
    
    Notes:
    ======
    * All expansion factors are simple lists of normalised fractional
    proportions, the sum of which should equal 1.0.
    * The first item in doweek_expansion_factors is Monday.

Definition at line 367 of file scene.py.

367  def getAADT(self, hourly_expansion_factors, doweek_expansion_factors, monthly_expansion_factors, **kwargs):
368  ''' Compute, update, and return Average Daily Traffic. See
369  Scene.getADT() for list of exposure types.
370 
371  Notes:
372  ======
373  * All expansion factors are simple lists of normalised fractional
374  proportions, the sum of which should equal 1.0.
375  * The first item in doweek_expansion_factors is Monday.
376  '''
377  ADT = self.getADT(hourly_expansion_factors, **kwargs)
378  if(ADT is None): return None
379  doweek_index = self.data[0].getWeekday()-1
380  monthly_index = self.data[0].getMonth()-1
381  return ADT*doweek_expansion_factors[doweek_index]*7.0*monthly_expansion_factors[monthly_index]*12.0
382 

◆ getADT()

def lib.scene.Scene.getADT (   self,
  hourly_expansion_factors,
  exposure = 'traffic',
  customCount = None,
  customHour = None,
  kwargs 
)
Calculate and update Average Daily Traffic. 

    Input:
    ======
    exposure:    type of data to use (see below)
    customCount: custom count to use (leave blank if estimating from tracking)
    customHour:  custom hour to use with customCount

    Exposure options include:
    =========================
    exposure=='traffic' or 't':                    Average Daily Traffic (ADT)                        [Default]
    exposure=='pair' or 'pe':                      Average Daily user Pair Exposure (ADPE)
    exposure=='instance' or 'interaction' or 'ie': Average Daily interaction Instant Exposure (ADIE)
    exposure=='pairind' or 'pi':                   Average Daily user Pair Indicator Exposure (ADPIE)
    exposure=='instanceind' or 'ii':               Average Daily interaction Instant Indicator Exposure (ADIIE)
    exposure==int between 0 and 6:                 Average Daily [corresponding user type] (AD[UT])
    
    
    Notes:
    ======
    * All expansion factors are simple lists of normalised fractional
    proportions, the sum of which should equal 1.0.
    * Optionally, you can pass a custom exposure input and sequence time
    which will be used instead of exposure

Definition at line 308 of file scene.py.

308  def getADT(self, hourly_expansion_factors, exposure='traffic', customCount=None, customHour=None, **kwargs):
309  ''' Calculate and update Average Daily Traffic.
310 
311  Input:
312  ======
313  exposure: type of data to use (see below)
314  customCount: custom count to use (leave blank if estimating from tracking)
315  customHour: custom hour to use with customCount
316 
317  Exposure options include:
318  =========================
319  exposure=='traffic' or 't': Average Daily Traffic (ADT) [Default]
320  exposure=='pair' or 'pe': Average Daily user Pair Exposure (ADPE)
321  exposure=='instance' or 'interaction' or 'ie': Average Daily interaction Instant Exposure (ADIE)
322  exposure=='pairind' or 'pi': Average Daily user Pair Indicator Exposure (ADPIE)
323  exposure=='instanceind' or 'ii': Average Daily interaction Instant Indicator Exposure (ADIIE)
324  exposure==int between 0 and 6: Average Daily [corresponding user type] (AD[UT])
325 
326 
327  Notes:
328  ======
329  * All expansion factors are simple lists of normalised fractional
330  proportions, the sum of which should equal 1.0.
331  * Optionally, you can pass a custom exposure input and sequence time
332  which will be used instead of exposure
333  '''
334 
335  if(customCount and customHour):
336  for sequence in self.data:
337  if(sequence.getHour() == customHour): return customCount*3600.0/float(hourly_expansion_factors[sequence.getHour()]*sequence.duration)
338  return None
339 
340 
341 
342 
343  if(type(exposure) is str): exposure=exposure.lower()
344  if(exposure=='pair' or exposure=='pe'): exposure_type = 1 #(ADPE)
345  elif(exposure=='instance' or exposure=='interaction' or exposure=='ie'): exposure_type = 2 #(ADIE)
346  elif(exposure=='pairind' or exposure=='pi'): exposure_type = 3 #(ADPIE)
347  elif(exposure=='instanceind' or exposure=='ii'): exposure_type = 4 #(ADIIE)
348  elif(type(exposure) is int and exposure>=0 and exposure<=6): exposure_type = 5 #(AD[UT])
349  else: exposure_type = 0 #(ADT)
350 
351 
352  ADTs = []
353  for sequence in self.data:
354  if(sequence.duration > 0):
355  if(exposure_type==1 and sequence.countExpPairs > 0): ADTs.append(sequence.countExpPairs *3600.0/float(hourly_expansion_factors[sequence.getHour()]*sequence.duration))
356  elif(exposure_type==2 and sequence.countExpInstances > 0): ADTs.append(sequence.countExpInstances *3600.0/float(hourly_expansion_factors[sequence.getHour()]*sequence.duration))
357  elif(exposure_type==3 and sequence.countExpPairsWInd > 0): ADTs.append(sequence.countExpPairsWInd *3600.0/float(hourly_expansion_factors[sequence.getHour()]*sequence.duration))
358  elif(exposure_type==4 and sequence.countExpInstancesWInd > 0): ADTs.append(sequence.countExpInstancesWInd*3600.0/float(hourly_expansion_factors[sequence.getHour()]*sequence.duration))
359  elif(exposure_type==5 and sequence.countByType[exposure] > 0): ADTs.append(sequence.countByType[exposure]*3600.0/float(hourly_expansion_factors[sequence.getHour()]*sequence.duration))
360  elif(exposure_type==0 and sequence.countObjects > 0): ADTs.append(sequence.countObjects *3600.0/float(hourly_expansion_factors[sequence.getHour()]*sequence.duration))
361 
362 
363  if(not ADTs): return None
364  else: return sum(ADTs)/float(len(ADTs))
365 
366 

◆ getBaseDirectory()

def lib.scene.Scene.getBaseDirectory (   self)

Definition at line 284 of file scene.py.

284  def getBaseDirectory(self): return os.path.join(self.config.dir, self.site.name, self.name)

◆ getClassificationTrainingBaseDirectory()

def lib.scene.Scene.getClassificationTrainingBaseDirectory (   self,
  type 
)

Definition at line 285 of file scene.py.

285  def getClassificationTrainingBaseDirectory(self, type): return os.path.join(self.config.dir, 'Calibration', 'Data', type, self.site.name+'_'+self.name)

◆ getCountObjects()

def lib.scene.Scene.getCountObjects (   self)

Definition at line 383 of file scene.py.

383  def getCountObjects(self):
384  if(len(self.data) > 0 and hasattr(self.data[0], 'countObjects')): return sum([x.countObjects for x in self.data])
385  else: return 0

◆ getCountObjOld()

def lib.scene.Scene.getCountObjOld (   self)

Definition at line 386 of file scene.py.

386  def getCountObjOld(self):
387  if(len(self.data) > 0 and hasattr(self.data[0], 'countObjOld')): return sum([x.countObjOld for x in self.data])
388  else: return 0

◆ getDuration()

def lib.scene.Scene.getDuration (   self)

Definition at line 389 of file scene.py.

389  def getDuration(self):
390  if(len(self.data) > 0 and hasattr(self.data[0], 'duration')): return sum([x.duration for x in self.data])
391  else: return 0

◆ getDurationUnit()

def lib.scene.Scene.getDurationUnit (   self)

Definition at line 392 of file scene.py.

392  def getDurationUnit(self):
393  if(len(self.data) > 0 and hasattr(self.data[0], 'durationUnit')): return self.data[0].durationUnit
394  else: return 's'
395 
396 

◆ getEndTime()

def lib.scene.Scene.getEndTime (   self)

Definition at line 303 of file scene.py.

303  def getEndTime(self):
304  if(not self.data): return None
305  return max([x.getEndTime() for x in self.data])
306 
307 

◆ getFullCameraCalibrationFilename()

def lib.scene.Scene.getFullCameraCalibrationFilename (   self)

Definition at line 294 of file scene.py.

294  def getFullCameraCalibrationFilename(self): return os.path.join(self.config.dir, self.site.name, self.name, self.cameraCalibrationFilename)

◆ getFullConfigurationFilename()

def lib.scene.Scene.getFullConfigurationFilename (   self)

Definition at line 295 of file scene.py.

295  def getFullConfigurationFilename(self):
296  if(self.configurationFilename and os.path.exists(os.path.join(self.config.dir, self.site.name, self.name, self.configurationFilename))): return os.path.join(self.config.dir, self.site.name, self.name, self.configurationFilename)
297  elif(self.configurationFilename and os.path.exists(os.path.join(self.config.dir, self.configurationFilename))): return os.path.join(self.config.dir, self.configurationFilename)
298  else: return self.defaultFullCfgFilename

◆ getFullHomographyFilename()

def lib.scene.Scene.getFullHomographyFilename (   self,
  force = False 
)

Definition at line 286 of file scene.py.

286  def getFullHomographyFilename(self, force=False):
287  if(os.path.exists(os.path.join(self.config.dir, self.site.name, self.name, self.homographyFilename)) or force): return os.path.join(self.config.dir, self.site.name, self.name, self.homographyFilename)
288  elif(self.homography):
289  with open(os.path.join(self.config.temp_dir, tvaLib.Parse.clean_file_name('homography-'+str(self.config.dbn)[:-7]+'-'+str(self.idx)+'-'+str(self.name)+'.txt')), 'wb') as f:
290  for line in self.homography:
291  f.write(' '.join([str(x) for x in line])+'\n')
292  return os.path.join(self.config.temp_dir, tvaLib.Parse.clean_file_name('homography-'+str(self.config.dbn)[:-7]+'-'+str(self.idx)+'-'+str(self.name)+'.txt'))
293  return ''
def join(obj1, obj2, postSmoothing=True)
Definition: tools_obj.py:816

◆ getFullMaskFilename()

def lib.scene.Scene.getFullMaskFilename (   self)

Definition at line 299 of file scene.py.

299  def getFullMaskFilename(self): return os.path.join(self.config.dir, self.site.name, self.name, self.maskFilename)

◆ getHomography()

def lib.scene.Scene.getHomography (   self,
  type = 'undistorted' 
)
Start with the premise that the existing homographies are in
    undistorted space (legacy). TODO: Invert this with a config option.

Definition at line 258 of file scene.py.

258  def getHomography(self, type='undistorted'):
259  ''' Start with the premise that the existing homographies are in
260  undistorted space (legacy). TODO: Invert this with a config option.
261  '''
262 
263  if(self.homography): h = self.homography
264  elif(self.homographyFilename):
265  if(self.getFullHomographyFilename()):
266  self.setHomography(np.loadtxt(self.getFullHomographyFilename()))
267  h = self.homography
268  else: return None
269  else: return None
270 
271 
272  if(type=='undistorted'): return h
273 
274  elif(type=='distorted'):
275  new_h = np.dot(np.array(h.data), np.linalg.inv(np.array(self.camera.getCamMatrix().data)))
276  return new_h/new_h[2][2]
277  else: return None
278 
def getHomography(homoSrcFrame='', orthoSrcPath='', savePath='homography.txt', tsaiCameraSrcPath='', nPoints=4, unitsPerPixel=0.1, videoPts=None, worldPts=None, pointDrawSize=3, pointTargetDrawSize=7, pointTargetDrawThickness=2, maxDisplayResolutionX=900, fig_name='', verbose=0)
Definition: tools_obj.py:403

◆ getStartTime()

def lib.scene.Scene.getStartTime (   self)

Definition at line 300 of file scene.py.

300  def getStartTime(self):
301  if(not self.data): return None
302  return min([x.getStartTime() for x in self.data])

◆ new()

def lib.scene.Scene.new (   self,
  camIdx,
  session,
  sqlFields = {} 
)

Definition at line 233 of file scene.py.

233  def new(self, camIdx, session, sqlFields={}):
234  cameras = session.query(tvaMetaData.CameraView).all()
235  data = tvaMetaData.VideoSequence([x for x in cameras if x.idx==camIdx][0], **sqlFields)
236  session.add_all([data])
237  session.commit()
238  self.data.append(Sequence(data, self.config))
239  return True
240 

◆ setHomography()

def lib.scene.Scene.setHomography (   self,
  value 
)

Definition at line 279 of file scene.py.

279  def setHomography(self, value):
280  if(type(value) is np.ndarray): self.homography = tvaLib.Constructors.SuperListParse(value.tolist(), dimension=2, datatype='float')
281  elif(type(value) is list): self.homography = tvaLib.Constructors.SuperListParse(value, dimension=2, datatype='float')
282  else: self.homography = value
283 

◆ write()

def lib.scene.Scene.write (   self,
  scene 
)

Definition at line 241 of file scene.py.

241  def write(self, scene):
242  scene.idx = self.idx
243  scene.name = self.name
244  scene.homographyFilename = self.homographyFilename
245  scene.homographyDistanceUnit = self.homographyDistanceUnit
246  scene.cameraCalibrationFilename = self.cameraCalibrationFilename
247  scene.configurationFilename = self.configurationFilename
248  scene.maskFilename = self.maskFilename
249  scene.camHeight = self.camHeight
250 
251  scene.homography = self.homography.write()
252  scene.mask = self.mask.write()
253  scene.camOrigin = self.camOrigin.write()
254  for fileIx in range(len(scene.video_sequences)):
255  scene.video_sequences[fileIx] = self.data[fileIx].write(scene.video_sequences[fileIx])
256  return scene
257 

Member Data Documentation

◆ camera

lib.scene.Scene.camera

Definition at line 226 of file scene.py.

◆ cameraCalibrationFilename

lib.scene.Scene.cameraCalibrationFilename

Definition at line 217 of file scene.py.

◆ camHeight

lib.scene.Scene.camHeight

Definition at line 221 of file scene.py.

◆ camOrigin

lib.scene.Scene.camOrigin

Definition at line 225 of file scene.py.

◆ config

lib.scene.Scene.config

Definition at line 211 of file scene.py.

◆ configurationFilename

lib.scene.Scene.configurationFilename

Definition at line 218 of file scene.py.

◆ defaultFullCfgFilename

lib.scene.Scene.defaultFullCfgFilename

Setup defaults from config.

Definition at line 219 of file scene.py.

◆ homography

lib.scene.Scene.homography

Create and link abstract objects from data.

Definition at line 223 of file scene.py.

◆ homographyDistanceUnit

lib.scene.Scene.homographyDistanceUnit

Definition at line 216 of file scene.py.

◆ homographyFilename

lib.scene.Scene.homographyFilename

Definition at line 215 of file scene.py.

◆ idx

lib.scene.Scene.idx

Definition at line 212 of file scene.py.

◆ mask

lib.scene.Scene.mask

Definition at line 224 of file scene.py.

◆ maskFilename

lib.scene.Scene.maskFilename

Definition at line 220 of file scene.py.

◆ name

lib.scene.Scene.name

Definition at line 214 of file scene.py.

◆ site

lib.scene.Scene.site

Definition at line 213 of file scene.py.


The documentation for this class was generated from the following file: