comparison trafficintelligence/moving.py @ 1241:ab4c72b9475c

work in progress
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 05 Feb 2024 17:06:01 -0500
parents bb14f919d1cb
children 4cd8ace3552f
comparison
equal deleted inserted replaced
1240:bb14f919d1cb 1241:ab4c72b9475c
303 303
304 def homographyProject(self, homography): 304 def homographyProject(self, homography):
305 projected = cvutils.homographyProject(array([[self.x], [self.y]]), homography) 305 projected = cvutils.homographyProject(array([[self.x], [self.y]]), homography)
306 return Point(projected[0], projected[1]) 306 return Point(projected[0], projected[1])
307 307
308 def inRectangle(self, xmin, xmax, ymin, ymax):
309 return (xmin <= p.x <= xmax) and (ymin <= p.y <= ymax)
310
308 def inPolygon(self, polygon): 311 def inPolygon(self, polygon):
309 '''Indicates if the point x, y is inside the polygon 312 '''Indicates if the point x, y is inside the polygon
310 (array of Nx2 coordinates of the polygon vertices) 313 (array of Nx2 coordinates of the polygon vertices)
311 314
312 taken from http://www.ariel.com.au/a/python-point-int-poly.html 315 taken from http://www.ariel.com.au/a/python-point-int-poly.html
2049 for userTypename in speedProbabilities: 2052 for userTypename in speedProbabilities:
2050 userTypeProbabilities[userType2Num[userTypename]] = speedProbabilities[userTypename](self.aggregatedSpeed) 2053 userTypeProbabilities[userType2Num[userTypename]] = speedProbabilities[userTypename](self.aggregatedSpeed)
2051 self.setUserType(utils.argmaxDict(userTypeProbabilities)) 2054 self.setUserType(utils.argmaxDict(userTypeProbabilities))
2052 return userTypeProbabilities 2055 return userTypeProbabilities
2053 2056
2054 def initClassifyUserTypeHoGSVM(self, aggregationFunc, pedBikeCarSVM, bikeCarSVM = None, pedBikeSpeedTreshold = float('Inf'), bikeCarSpeedThreshold = float('Inf'), nInstantsIgnoredAtEnds = 0, homography = None, intrinsicCameraMatrix = None, distortionCoefficients = None): 2057 def initClassifyUserTypeHoGSVM(self, aggregationFunc, pedBikeCarSVM, bikeCarSVM = None, pedBikeSpeedThreshold = float('Inf'), bikeCarSpeedThreshold = float('Inf'), nInstantsIgnoredAtEnds = 0, homography = None, intrinsicCameraMatrix = None, distortionCoefficients = None):
2055 '''Initializes the data structures for classification 2058 '''Initializes the data structures for classification
2056 2059
2057 TODO? compute speed for longest feature?''' 2060 TODO? compute speed for longest feature?'''
2058 self.aggregatedSpeed = aggregationFunc(self.getSpeeds(nInstantsIgnoredAtEnds)) 2061 self.aggregatedSpeed = aggregationFunc(self.getSpeeds(nInstantsIgnoredAtEnds))
2059 if self.aggregatedSpeed < pedBikeSpeedTreshold or bikeCarSVM is None: 2062 if self.aggregatedSpeed < pedBikeSpeedThreshold or bikeCarSVM is None:
2060 self.appearanceClassifier = pedBikeCarSVM 2063 self.appearanceClassifier = pedBikeCarSVM
2061 elif self.aggregatedSpeed < bikeCarSpeedThreshold: 2064 elif self.aggregatedSpeed < bikeCarSpeedThreshold:
2062 self.appearanceClassifier = bikeCarSVM 2065 self.appearanceClassifier = bikeCarSVM
2063 else: 2066 else:
2064 self.appearanceClassifier = carClassifier 2067 self.appearanceClassifier = carClassifier
2080 hog = cvutils.HOG(croppedImg, rescaleSize, orientations, pixelsPerCell, cellsPerBlock, blockNorm) 2083 hog = cvutils.HOG(croppedImg, rescaleSize, orientations, pixelsPerCell, cellsPerBlock, blockNorm)
2081 self.userTypes[instant] = self.appearanceClassifier.predict(hog.reshape(1,hog.size)) 2084 self.userTypes[instant] = self.appearanceClassifier.predict(hog.reshape(1,hog.size))
2082 else: 2085 else:
2083 self.userTypes[instant] = userType2Num['unknown'] 2086 self.userTypes[instant] = userType2Num['unknown']
2084 2087
2085 def classifyUserTypeHoGSVM(self, pedBikeCarSVM = None, width = 0, height = 0, homography = None, images = None, bikeCarSVM = None, pedBikeSpeedTreshold = float('Inf'), bikeCarSpeedThreshold = float('Inf'), minSpeedEquiprobable = -1, speedProbabilities = None, aggregationFunc = median, maxPercentUnknown = 0.5, nInstantsIgnoredAtEnds = 0, px = 0.2, py = 0.2, minNPixels = 800, rescaleSize = (64, 64), orientations = 9, pixelsPerCell = (8,8), cellsPerBlock = (2,2)): 2088 def classifyUserTypeYoloAtInstant(self, img, instant, width, height, px, py, minNPixels, bboxes):
2089 '''Finds the user type based on where the feature fall in detected bboxes'''
2090 userTypes = []
2091 if self.hasFeatures():
2092 for f in self.getFeatures():
2093 if f.existsAtInstant(frameNum):
2094 p = f.getPositionAtInstant(frameNum)
2095 for box in bboxes:
2096 if box.id is not None:
2097 xyxy = box.xyxy[0].tolist()
2098 if p.inRectangle(xyxy[0], xyxy[1], xyxy[2], xyxy[3]):
2099 userTypes.append(moving.coco2Types[int(box.cls.item())])
2100 if len(userTypes) > 0:
2101 pass
2102 else:
2103 self.userTypes[instant] = userType2Num['unknown']
2104
2105 def classifyUserTypeHoGSVM(self, minSpeedEquiprobable = -1, speedProbabilities = None, aggregationFunc = median, maxPercentUnknown = 0.5):
2086 '''Agregates SVM detections in each image and returns probability 2106 '''Agregates SVM detections in each image and returns probability
2087 (proportion of instants with classification in each category) 2107 (proportion of instants with classification in each category)
2088 2108
2089 images is a dictionary of images indexed by instant 2109 images is a dictionary of images indexed by instant
2090 With default parameters, the general (ped-bike-car) classifier will be used 2110 With default parameters, the general (ped-bike-car) classifier will be used
2091 2111
2092 Considered categories are the keys of speedProbabilities''' 2112 Considered categories are the keys of speedProbabilities'''
2093 if not hasattr(self, 'aggregatedSpeed') or not hasattr(self, 'userTypes'): 2113 #if not hasattr(self, 'aggregatedSpeed') or not hasattr(self, 'userTypes'):
2094 print('Initializing the data structures for classification by HoG-SVM') 2114 # print('Initializing the data structures for classification by HoG-SVM')
2095 self.initClassifyUserTypeHoGSVM(aggregationFunc, pedBikeCarSVM, bikeCarSVM, pedBikeSpeedTreshold, bikeCarSpeedThreshold, nInstantsIgnoredAtEnds) 2115 # self.initClassifyUserTypeHoGSVM(aggregationFunc, pedBikeCarSVM, bikeCarSVM, pedBikeSpeedTreshold, bikeCarSpeedThreshold, nInstantsIgnoredAtEnds)
2096 2116
2097 if len(self.userTypes) != self.length() and images is not None: # if classification has not been done previously 2117 # if len(self.userTypes) != self.length() and images is not None: # if classification has not been done previously
2098 for t in self.getTimeInterval(): 2118 # for t in self.getTimeInterval():
2099 if t not in self.userTypes: 2119 # if t not in self.userTypes:
2100 self.classifyUserTypeHoGSVMAtInstant(images[t], t, homography, width, height, px, py, minNPixels, rescaleSize, orientations, pixelsPerCell, cellsPerBlock) 2120 # self.classifyUserTypeHoGSVMAtInstant(images[t], t, homography, width, height, px, py, minNPixels, rescaleSize, orientations, pixelsPerCell, cellsPerBlock)
2101 # compute P(Speed|Class) 2121 # compute P(Speed|Class)
2102 if speedProbabilities is None or self.aggregatedSpeed < minSpeedEquiprobable: # equiprobable information from speed 2122 if speedProbabilities is None or self.aggregatedSpeed < minSpeedEquiprobable: # equiprobable information from speed
2103 userTypeProbabilities = {userType2Num['car']: 1., userType2Num['pedestrian']: 1., userType2Num['bicycle']: 1.} 2123 userTypeProbabilities = {userType2Num['car']: 1., userType2Num['pedestrian']: 1., userType2Num['bicycle']: 1.}
2104 else: 2124 else:
2105 userTypeProbabilities = {userType2Num[userTypename]: speedProbabilities[userTypename](self.aggregatedSpeed) for userTypename in speedProbabilities} 2125 userTypeProbabilities = {userType2Num[userTypename]: speedProbabilities[userTypename](self.aggregatedSpeed) for userTypename in speedProbabilities}