Mercurial Hosting > traffic-intelligence
diff 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 |
line wrap: on
line diff
--- a/trafficintelligence/moving.py Mon Feb 05 14:14:14 2024 -0500 +++ b/trafficintelligence/moving.py Mon Feb 05 17:06:01 2024 -0500 @@ -305,6 +305,9 @@ projected = cvutils.homographyProject(array([[self.x], [self.y]]), homography) return Point(projected[0], projected[1]) + def inRectangle(self, xmin, xmax, ymin, ymax): + return (xmin <= p.x <= xmax) and (ymin <= p.y <= ymax) + def inPolygon(self, polygon): '''Indicates if the point x, y is inside the polygon (array of Nx2 coordinates of the polygon vertices) @@ -2051,12 +2054,12 @@ self.setUserType(utils.argmaxDict(userTypeProbabilities)) return userTypeProbabilities - def initClassifyUserTypeHoGSVM(self, aggregationFunc, pedBikeCarSVM, bikeCarSVM = None, pedBikeSpeedTreshold = float('Inf'), bikeCarSpeedThreshold = float('Inf'), nInstantsIgnoredAtEnds = 0, homography = None, intrinsicCameraMatrix = None, distortionCoefficients = None): + def initClassifyUserTypeHoGSVM(self, aggregationFunc, pedBikeCarSVM, bikeCarSVM = None, pedBikeSpeedThreshold = float('Inf'), bikeCarSpeedThreshold = float('Inf'), nInstantsIgnoredAtEnds = 0, homography = None, intrinsicCameraMatrix = None, distortionCoefficients = None): '''Initializes the data structures for classification TODO? compute speed for longest feature?''' self.aggregatedSpeed = aggregationFunc(self.getSpeeds(nInstantsIgnoredAtEnds)) - if self.aggregatedSpeed < pedBikeSpeedTreshold or bikeCarSVM is None: + if self.aggregatedSpeed < pedBikeSpeedThreshold or bikeCarSVM is None: self.appearanceClassifier = pedBikeCarSVM elif self.aggregatedSpeed < bikeCarSpeedThreshold: self.appearanceClassifier = bikeCarSVM @@ -2082,7 +2085,24 @@ else: self.userTypes[instant] = userType2Num['unknown'] - 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)): + def classifyUserTypeYoloAtInstant(self, img, instant, width, height, px, py, minNPixels, bboxes): + '''Finds the user type based on where the feature fall in detected bboxes''' + userTypes = [] + if self.hasFeatures(): + for f in self.getFeatures(): + if f.existsAtInstant(frameNum): + p = f.getPositionAtInstant(frameNum) + for box in bboxes: + if box.id is not None: + xyxy = box.xyxy[0].tolist() + if p.inRectangle(xyxy[0], xyxy[1], xyxy[2], xyxy[3]): + userTypes.append(moving.coco2Types[int(box.cls.item())]) + if len(userTypes) > 0: + pass + else: + self.userTypes[instant] = userType2Num['unknown'] + + def classifyUserTypeHoGSVM(self, minSpeedEquiprobable = -1, speedProbabilities = None, aggregationFunc = median, maxPercentUnknown = 0.5): '''Agregates SVM detections in each image and returns probability (proportion of instants with classification in each category) @@ -2090,14 +2110,14 @@ With default parameters, the general (ped-bike-car) classifier will be used Considered categories are the keys of speedProbabilities''' - if not hasattr(self, 'aggregatedSpeed') or not hasattr(self, 'userTypes'): - print('Initializing the data structures for classification by HoG-SVM') - self.initClassifyUserTypeHoGSVM(aggregationFunc, pedBikeCarSVM, bikeCarSVM, pedBikeSpeedTreshold, bikeCarSpeedThreshold, nInstantsIgnoredAtEnds) + #if not hasattr(self, 'aggregatedSpeed') or not hasattr(self, 'userTypes'): + # print('Initializing the data structures for classification by HoG-SVM') + # self.initClassifyUserTypeHoGSVM(aggregationFunc, pedBikeCarSVM, bikeCarSVM, pedBikeSpeedTreshold, bikeCarSpeedThreshold, nInstantsIgnoredAtEnds) - if len(self.userTypes) != self.length() and images is not None: # if classification has not been done previously - for t in self.getTimeInterval(): - if t not in self.userTypes: - self.classifyUserTypeHoGSVMAtInstant(images[t], t, homography, width, height, px, py, minNPixels, rescaleSize, orientations, pixelsPerCell, cellsPerBlock) + # if len(self.userTypes) != self.length() and images is not None: # if classification has not been done previously + # for t in self.getTimeInterval(): + # if t not in self.userTypes: + # self.classifyUserTypeHoGSVMAtInstant(images[t], t, homography, width, height, px, py, minNPixels, rescaleSize, orientations, pixelsPerCell, cellsPerBlock) # compute P(Speed|Class) if speedProbabilities is None or self.aggregatedSpeed < minSpeedEquiprobable: # equiprobable information from speed userTypeProbabilities = {userType2Num['car']: 1., userType2Num['pedestrian']: 1., userType2Num['bicycle']: 1.}