diff 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
line wrap: on
line diff
--- a/scripts/classify-objects.py	Mon Feb 05 17:06:01 2024 -0500
+++ b/scripts/classify-objects.py	Wed Feb 07 11:43:03 2024 -0500
@@ -52,10 +52,13 @@
 carNorm = norm(classifierParams.meanVehicleSpeed, classifierParams.stdVehicleSpeed)
 pedNorm = norm(classifierParams.meanPedestrianSpeed, classifierParams.stdPedestrianSpeed)
 # 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)
-bicLogNorm = lognorm(classifierParams.scaleCyclistSpeed, loc = 0., scale = np.exp(classifierParams.locationCyclistSpeed))
-speedProbabilities = {moving.userTypeNames[1]: lambda s: carNorm.pdf(s),
-                      moving.userTypeNames[2]: lambda s: pedNorm.pdf(s), 
-                      moving.userTypeNames[4]: lambda s: bicLogNorm.pdf(s)}
+cycLogNorm = lognorm(classifierParams.scaleCyclistSpeed, loc = 0., scale = np.exp(classifierParams.locationCyclistSpeed))
+speedProbabilities = {moving.userType2Num['car']: lambda s: carNorm.pdf(s),
+                      moving.userType2Num['pedestrian']: lambda s: pedNorm.pdf(s), 
+                      moving.userType2Num['cyclist']: lambda s: cycLogNorm.pdf(s)}
+if useYolo: # consider other user types
+    for i in [3, 5, 6, 7]:
+        speedProbabilities[i] = speedProbabilities[moving.userType2Num['car']]
 
 if args.plotSpeedDistribution:
     import matplotlib.pyplot as plt
@@ -106,7 +109,7 @@
             #if undistort:
             #    img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
             if useYolo:
-                results = yolo.predict(img, classes=list(moving.cocoTypeNames.keys()), verbose=False)
+                results = yolo.predict(img, conf = classifierParams.confidence, classes=list(moving.cocoTypeNames.keys()), verbose=False)
 
             for obj in objects[:]:
                 if obj.getFirstInstant() <= frameNum: # if images are skipped
@@ -116,15 +119,15 @@
 
             for obj in currentObjects[:]:
                 if obj.getLastInstant() <= frameNum:
-                    obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = classifierParams.minSpeedEquiprobable, speedProbabilities = speedProbabilities, maxPercentUnknown = classifierParams.maxPercentUnknown)
+                    obj.classifyUserTypeHoGSVM(classifierParams.minSpeedEquiprobable, speedProbabilities, classifierParams.maxPercentUnknown)
                     pastObjects.append(obj)
                     currentObjects.remove(obj)
                 else:
                     if useYolo:
                         # if one feature falls in bike, it's a bike
                         # could one count all hits in various objects, or one takes majority at the instant?
-                        # obj.classifyUserTypeYoloAtInstant(img, frameNum, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, results[0].boxes)
-                        pass
+                        #print('yolo', len(results[0].boxes))
+                        obj.classifyUserTypeYoloAtInstant(frameNum, results[0].boxes)
                     else:
                         obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels, classifierParams.hogRescaleSize, classifierParams.hogNOrientations, classifierParams.hogNPixelsPerCell, classifierParams.hogNCellsPerBlock, classifierParams.hogBlockNorm)
                     if args.verbose:
@@ -132,7 +135,7 @@
         frameNum += 1
     
     for obj in currentObjects:
-        obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = classifierParams.minSpeedEquiprobable, speedProbabilities = speedProbabilities, maxPercentUnknown = classifierParams.maxPercentUnknown)
+        obj.classifyUserTypeHoGSVM(classifierParams.minSpeedEquiprobable, speedProbabilities, classifierParams.maxPercentUnknown)
         pastObjects.append(obj)
     print('Saving user types')
     storage.setRoadUserTypes(databaseFilename, pastObjects)