Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/classify-objects.py @ 1242:4cd8ace3552f
major update for classification, allowing the use of neural network classification
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 07 Feb 2024 11:43:03 -0500 |
| parents | ab4c72b9475c |
| children | 88eedf79f16a |
comparison
equal
deleted
inserted
replaced
| 1241:ab4c72b9475c | 1242:4cd8ace3552f |
|---|---|
| 50 | 50 |
| 51 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.) | 51 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.) |
| 52 carNorm = norm(classifierParams.meanVehicleSpeed, classifierParams.stdVehicleSpeed) | 52 carNorm = norm(classifierParams.meanVehicleSpeed, classifierParams.stdVehicleSpeed) |
| 53 pedNorm = norm(classifierParams.meanPedestrianSpeed, classifierParams.stdPedestrianSpeed) | 53 pedNorm = norm(classifierParams.meanPedestrianSpeed, classifierParams.stdPedestrianSpeed) |
| 54 # numpy lognorm shape, loc, scale: shape for numpy is scale (std of the normal) and scale for numpy is exp(location) (loc=mean of the normal) | 54 # numpy lognorm shape, loc, scale: shape for numpy is scale (std of the normal) and scale for numpy is exp(location) (loc=mean of the normal) |
| 55 bicLogNorm = lognorm(classifierParams.scaleCyclistSpeed, loc = 0., scale = np.exp(classifierParams.locationCyclistSpeed)) | 55 cycLogNorm = lognorm(classifierParams.scaleCyclistSpeed, loc = 0., scale = np.exp(classifierParams.locationCyclistSpeed)) |
| 56 speedProbabilities = {moving.userTypeNames[1]: lambda s: carNorm.pdf(s), | 56 speedProbabilities = {moving.userType2Num['car']: lambda s: carNorm.pdf(s), |
| 57 moving.userTypeNames[2]: lambda s: pedNorm.pdf(s), | 57 moving.userType2Num['pedestrian']: lambda s: pedNorm.pdf(s), |
| 58 moving.userTypeNames[4]: lambda s: bicLogNorm.pdf(s)} | 58 moving.userType2Num['cyclist']: lambda s: cycLogNorm.pdf(s)} |
| 59 if useYolo: # consider other user types | |
| 60 for i in [3, 5, 6, 7]: | |
| 61 speedProbabilities[i] = speedProbabilities[moving.userType2Num['car']] | |
| 59 | 62 |
| 60 if args.plotSpeedDistribution: | 63 if args.plotSpeedDistribution: |
| 61 import matplotlib.pyplot as plt | 64 import matplotlib.pyplot as plt |
| 62 plt.figure() | 65 plt.figure() |
| 63 speeds = np.arange(0.1, args.maxSpeedDistributionPlot, 0.1) | 66 speeds = np.arange(0.1, args.maxSpeedDistributionPlot, 0.1) |
| 104 if frameNum%50 == 0: | 107 if frameNum%50 == 0: |
| 105 print('frame number: {}'.format(frameNum)) | 108 print('frame number: {}'.format(frameNum)) |
| 106 #if undistort: | 109 #if undistort: |
| 107 # img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) | 110 # img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) |
| 108 if useYolo: | 111 if useYolo: |
| 109 results = yolo.predict(img, classes=list(moving.cocoTypeNames.keys()), verbose=False) | 112 results = yolo.predict(img, conf = classifierParams.confidence, classes=list(moving.cocoTypeNames.keys()), verbose=False) |
| 110 | 113 |
| 111 for obj in objects[:]: | 114 for obj in objects[:]: |
| 112 if obj.getFirstInstant() <= frameNum: # if images are skipped | 115 if obj.getFirstInstant() <= frameNum: # if images are skipped |
| 113 obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, classifierParams.maxPedestrianSpeed, classifierParams.maxCyclistSpeed, classifierParams.nFramesIgnoreAtEnds, invHomography, intrinsicCameraMatrix, distortionCoefficients) | 116 obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, classifierParams.maxPedestrianSpeed, classifierParams.maxCyclistSpeed, classifierParams.nFramesIgnoreAtEnds, invHomography, intrinsicCameraMatrix, distortionCoefficients) |
| 114 currentObjects.append(obj) | 117 currentObjects.append(obj) |
| 115 objects.remove(obj) | 118 objects.remove(obj) |
| 116 | 119 |
| 117 for obj in currentObjects[:]: | 120 for obj in currentObjects[:]: |
| 118 if obj.getLastInstant() <= frameNum: | 121 if obj.getLastInstant() <= frameNum: |
| 119 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = classifierParams.minSpeedEquiprobable, speedProbabilities = speedProbabilities, maxPercentUnknown = classifierParams.maxPercentUnknown) | 122 obj.classifyUserTypeHoGSVM(classifierParams.minSpeedEquiprobable, speedProbabilities, classifierParams.maxPercentUnknown) |
| 120 pastObjects.append(obj) | 123 pastObjects.append(obj) |
| 121 currentObjects.remove(obj) | 124 currentObjects.remove(obj) |
| 122 else: | 125 else: |
| 123 if useYolo: | 126 if useYolo: |
| 124 # if one feature falls in bike, it's a bike | 127 # if one feature falls in bike, it's a bike |
| 125 # could one count all hits in various objects, or one takes majority at the instant? | 128 # could one count all hits in various objects, or one takes majority at the instant? |
| 126 # obj.classifyUserTypeYoloAtInstant(img, frameNum, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, results[0].boxes) | 129 #print('yolo', len(results[0].boxes)) |
| 127 pass | 130 obj.classifyUserTypeYoloAtInstant(frameNum, results[0].boxes) |
| 128 else: | 131 else: |
| 129 obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels, classifierParams.hogRescaleSize, classifierParams.hogNOrientations, classifierParams.hogNPixelsPerCell, classifierParams.hogNCellsPerBlock, classifierParams.hogBlockNorm) | 132 obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels, classifierParams.hogRescaleSize, classifierParams.hogNOrientations, classifierParams.hogNPixelsPerCell, classifierParams.hogNCellsPerBlock, classifierParams.hogBlockNorm) |
| 130 if args.verbose: | 133 if args.verbose: |
| 131 print('obj {}@{}: {}'.format(obj.getNum(), frameNum, moving.userTypeNames[obj.userTypes[frameNum]])) | 134 print('obj {}@{}: {}'.format(obj.getNum(), frameNum, moving.userTypeNames[obj.userTypes[frameNum]])) |
| 132 frameNum += 1 | 135 frameNum += 1 |
| 133 | 136 |
| 134 for obj in currentObjects: | 137 for obj in currentObjects: |
| 135 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = classifierParams.minSpeedEquiprobable, speedProbabilities = speedProbabilities, maxPercentUnknown = classifierParams.maxPercentUnknown) | 138 obj.classifyUserTypeHoGSVM(classifierParams.minSpeedEquiprobable, speedProbabilities, classifierParams.maxPercentUnknown) |
| 136 pastObjects.append(obj) | 139 pastObjects.append(obj) |
| 137 print('Saving user types') | 140 print('Saving user types') |
| 138 storage.setRoadUserTypes(databaseFilename, pastObjects) | 141 storage.setRoadUserTypes(databaseFilename, pastObjects) |
