annotate scripts/extract-appearance-images.py @ 1240:bb14f919d1cb

cleaned use of centile (np only) and added info in classify-objects
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 05 Feb 2024 14:14:14 -0500
parents cc5cb04b04b0
children 2397de73770d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 947
diff changeset
1 #! /usr/bin/env python3
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
3 import numpy as np, cv2
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
4 import argparse, os
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
5 from pandas import read_csv
909
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
6 from matplotlib.pyplot import imshow, figure
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7
1028
cc5cb04b04b0 major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
8 from trafficintelligence import cvutils, moving, ml, storage
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10 parser = argparse.ArgumentParser(description='The program extracts labeled image patches to train the HoG-SVM classifier, and optionnally speed information')
901
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
11 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file', required = True)
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)')
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
13 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)')
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
14 parser.add_argument('--gt', dest = 'classificationAnnotationFilename', help = 'name of the file containing the correct classes (user types)', required = True)
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
15 parser.add_argument('--delimiter', dest = 'classificationAnnotationFilenameDelimiter', help = 'delimiter for the fields in the correct classification file', default= ' ')
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
16 parser.add_argument('-s', dest = 'nFramesStep', help = 'number of frames between each saved patch', default = 50, type = int)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
17 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to use to extract patches from', type = int, default = None)
911
3dd5acfa1899 corrected potential issues with videos where one cannot reach a give frame from its number
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 909
diff changeset
18 parser.add_argument('--start-frame0', dest = 'startFrame0', help = 'starts with first frame for videos with index problem where frames cannot be reached', action = 'store_true')
909
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
19 parser.add_argument('-o', dest = 'overlap', help = 'maximum intersection over union of the features nFramesStep apart to save image', type = float, default = 0.2)
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
20 parser.add_argument('--extract-all', dest = 'extractAllObjectImages', help = 'extracts the images for all objects, well classified or not (otherwise, extracts only for the misclassified)', action = 'store_true')
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
21 parser.add_argument('--prefix', dest = 'imagePrefix', help = 'image prefix', default = 'img')
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
22 parser.add_argument('--ouput', dest = 'directoryName', help = 'parent directory name for the directories containing the samples for the different road users', default = '.')
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
23 parser.add_argument('--compute-speed-distributions', dest = 'computeSpeedDistribution', help = 'computes the distribution of the road users of each type and fits parameters to each', action = 'store_true')
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
24
901
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
25 args = parser.parse_args()
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
26 params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args)
901
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
27 classifierParams = storage.ClassifierParameters(params.classifierFilename)
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
28
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
29 classificationAnnotations = read_csv(args.classificationAnnotationFilename, index_col=0, delimiter = args.classificationAnnotationFilenameDelimiter, names = ["object_num", "road_user_type"])
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
30 annotatedObjectNumbers = classificationAnnotations.index.tolist()
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
31
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
32 # objects has the objects for which we want to extract labeled images
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
33 if args.extractAllObjectImages:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
34 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', args.nObjects, withFeatures = True)
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
35 else:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
36 if len(annotatedObjectNumbers) > args.nObjects:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
37 classificationAnnotations = classificationAnnotations[:args.nObjects]
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
38 annotatedObjectNumbers = classificationAnnotations.index.tolist()
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
39 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', annotatedObjectNumbers, withFeatures = True)
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
40 for obj in objects:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
41 if obj.getNum() in annotatedObjectNumbers:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
42 obj.setUserType(classificationAnnotations.loc[obj.getNum(), 'road_user_type'])
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
43 timeInterval = moving.TimeInterval.unionIntervals([obj.getTimeInterval() for obj in objects])
911
3dd5acfa1899 corrected potential issues with videos where one cannot reach a give frame from its number
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 909
diff changeset
44 if args.startFrame0:
3dd5acfa1899 corrected potential issues with videos where one cannot reach a give frame from its number
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 909
diff changeset
45 timeInterval.first = 0
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
46
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
47 for userType in classificationAnnotations['road_user_type'].unique():
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
48 if not os.path.exists(args.directoryName+os.sep+moving.userTypeNames[userType]):
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
49 os.mkdir(args.directoryName+os.sep+moving.userTypeNames[userType])
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
50
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
51 capture = cv2.VideoCapture(videoFilename)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
52 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
53 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
54
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
55 if undistort: # setup undistortion
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
56 [map1, map2] = cvutils.computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
905
0e017178f7ab correct bug in classify-objects script when correcting for distrotion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 904
diff changeset
57 height, width = map1.shape
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
58
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
59 if capture.isOpened():
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
60 ret = True
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
61 frameNum = timeInterval.first
911
3dd5acfa1899 corrected potential issues with videos where one cannot reach a give frame from its number
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 909
diff changeset
62 if not args.startFrame0:
3dd5acfa1899 corrected potential issues with videos where one cannot reach a give frame from its number
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 909
diff changeset
63 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum)
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
64 lastFrameNum = timeInterval.last
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
65 while ret and frameNum <= timeInterval.last:
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
66 ret, img = capture.read()
909
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
67 distorted = True
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
68 if ret:
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
69 if frameNum%50 == 0:
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
70 print('frame number: {}'.format(frameNum))
947
053484e08947 found a more elegant solution, making a copy of the list to iterate
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 946
diff changeset
71 for obj in objects[:]:
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
72 if obj.existsAtInstant(frameNum):
906
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 905
diff changeset
73 if (10+frameNum-obj.getFirstInstant())%args.nFramesStep == 0:
909
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
74 currentImageFeatures = set([f.num for f in obj.getFeatures() if f.existsAtInstant(frameNum)])
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
75 if not hasattr(obj, 'lastImageFeatures') or len(currentImageFeatures.intersection(obj.lastImageFeatures))/len(currentImageFeatures.union(obj.lastImageFeatures)) < args.overlap:
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
76 obj.lastImageFeatures = currentImageFeatures
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
77 if undistort and distorted: # undistort only if necessary
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
78 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
79 distorted = False
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
80 croppedImg = cvutils.imageBox(img, obj, frameNum, invHomography, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels)
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
81 if croppedImg is not None:
cd038493f8c6 finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 906
diff changeset
82 cv2.imwrite(args.directoryName+os.sep+moving.userTypeNames[obj.getUserType()]+os.sep+args.imagePrefix+'-{}-{}.png'.format(obj.getNum(), frameNum), croppedImg)
947
053484e08947 found a more elegant solution, making a copy of the list to iterate
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 946
diff changeset
83 elif obj.getLastInstant() == frameNum:
053484e08947 found a more elegant solution, making a copy of the list to iterate
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 946
diff changeset
84 objects.remove(obj)
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
85 frameNum += 1
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
86
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
87 # todo speed info: distributions AND min speed equiprobable
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
88