Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/dltrack.py @ 1234:dd969637381e
work on tracker interface
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 14 Sep 2023 16:18:36 -0400 |
| parents | d5695e0b59d9 |
| children | 855abc69fa99 |
comparison
equal
deleted
inserted
replaced
| 1233:d5695e0b59d9 | 1234:dd969637381e |
|---|---|
| 6 import cv2 | 6 import cv2 |
| 7 | 7 |
| 8 from trafficintelligence import cvutils, moving, storage, utils | 8 from trafficintelligence import cvutils, moving, storage, utils |
| 9 | 9 |
| 10 parser = argparse.ArgumentParser(description='The program tracks objects following the ultralytics yolo executable.')#, epilog = 'Either the configuration filename or the other parameters (at least video and database filenames) need to be provided.') | 10 parser = argparse.ArgumentParser(description='The program tracks objects following the ultralytics yolo executable.')#, epilog = 'Either the configuration filename or the other parameters (at least video and database filenames) need to be provided.') |
| 11 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') | 11 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True) |
| 12 # detect model | 12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) |
| 13 # tracker model | 13 parser.add_argument('-m', dest = 'detectorFilename', help = 'name of the detection model file', required = True) |
| 14 parser.add_argument('-t', dest = 'trackerFilename', help = 'name of the tracker file', required = True) | |
| 14 parser.add_argument('--display', dest = 'display', help = 'show the results (careful with long videos, risk of running out of memory)', action = 'store_true') | 15 parser.add_argument('--display', dest = 'display', help = 'show the results (careful with long videos, risk of running out of memory)', action = 'store_true') |
| 15 #parser.add_argument('-f', dest = 'firstFrameNum', help = 'show the results (careful with long videos, risk of running out of memory)', action = 'store_true') | 16 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to process', type = int, default = 0) |
| 17 parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to process', type = int, default = float('Inf')) | |
| 16 args = parser.parse_args() | 18 args = parser.parse_args() |
| 17 | 19 |
| 18 # required functionality? | 20 # required functionality? |
| 19 # # filename of the video to process (can be images, eg image%04d.png) | 21 # # filename of the video to process (can be images, eg image%04d.png) |
| 20 # video-filename = laurier.avi | 22 # video-filename = laurier.avi |
| 57 | 59 |
| 58 | 60 |
| 59 # check if one can go to specific frame https://docs.ultralytics.com/modes/track/#persisting-tracks-loop | 61 # check if one can go to specific frame https://docs.ultralytics.com/modes/track/#persisting-tracks-loop |
| 60 | 62 |
| 61 # Load a model | 63 # Load a model |
| 62 model = YOLO('/home/nicolas/Research/Data/classification-models/yolov8x.pt') # seg yolov8x-seg.pt | 64 model = YOLO('/home/nicolas/Research/Data/classification-models/yolov8x.pt', ) # seg yolov8x-seg.pt |
| 63 # seg could be used on cropped image... if can be loaded and kept in memory | 65 # seg could be used on cropped image... if can be loaded and kept in memory |
| 64 # model = YOLO('/home/nicolas/Research/Data/classification-models/yolo_nas_l.pt ') # AttributeError: 'YoloNAS_L' object has no attribute 'get' | 66 # model = YOLO('/home/nicolas/Research/Data/classification-models/yolo_nas_l.pt ') # AttributeError: 'YoloNAS_L' object has no attribute 'get' |
| 65 | 67 |
| 66 # Track with the model | 68 # Track with the model |
| 67 #results = model.track(source=args.videoFilename, tracker="/home/nicolas/Research/Data/classification-models/bytetrack.yaml", classes=list(moving.cocoTypeNames.keys()), show=True) # , save_txt=True | 69 #results = model.track(source=args.videoFilename, tracker="/home/nicolas/Research/Data/classification-models/bytetrack.yaml", classes=list(moving.cocoTypeNames.keys()), show=True) # , save_txt=True |
| 68 if args.display: | 70 if args.display: |
| 69 windowName = 'frame' | 71 windowName = 'frame' |
| 70 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) | 72 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) |
| 71 | 73 |
| 72 results = model.track(source=args.videoFilename, tracker="/home/nicolas/Research/Data/classification-models/bytetrack.yaml", classes=list(moving.cocoTypeNames.keys()), stream=True) | 74 capture = cv2.VideoCapture(args.videoFilename) |
| 75 #results = model.track(source=args.videoFilename, tracker="/home/nicolas/Research/Data/classification-models/bytetrack.yaml", classes=list(moving.cocoTypeNames.keys()), stream=True) | |
| 73 objects = [] | 76 objects = [] |
| 74 currentObjects = {} | 77 currentObjects = {} |
| 75 featureNum = 0 | 78 featureNum = 0 |
| 79 | |
| 80 frameNum = args.firstFrameNum | |
| 81 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) | |
| 82 lastFrameNum = args.lastFrameNum | |
| 83 | |
| 84 success, frame = capture.read() | |
| 85 results = model.track(frame, tracker="/home/nicolas/Research/Data/classification-models/bytetrack.yaml", classes=list(moving.cocoTypeNames.keys()), persist=True) | |
| 76 # create object with user type and list of 3 features (bottom ones and middle) + projection | 86 # create object with user type and list of 3 features (bottom ones and middle) + projection |
| 77 for frameNum, result in enumerate(results): | 87 while capture.isOpened() and success and frameNum <= lastFrameNum: |
| 88 #for frameNum, result in enumerate(results): | |
| 89 result = results[0] | |
| 78 print(frameNum, len(result.boxes)) | 90 print(frameNum, len(result.boxes)) |
| 79 for box in result.boxes: | 91 for box in result.boxes: |
| 80 #print(box.cls, box.id, box.xyxy) | 92 #print(box.cls, box.id, box.xyxy) |
| 81 if box.id is not None: # None are objects with low confidence | 93 if box.id is not None: # None are objects with low confidence |
| 82 num = int(box.id) | 94 num = int(box.id) |
| 100 if args.display: | 112 if args.display: |
| 101 cvutils.cvImshow(windowName, result.plot()) # original image in orig_img | 113 cvutils.cvImshow(windowName, result.plot()) # original image in orig_img |
| 102 key = cv2.waitKey() | 114 key = cv2.waitKey() |
| 103 if cvutils.quitKey(key): | 115 if cvutils.quitKey(key): |
| 104 break | 116 break |
| 117 frameNum += 1 | |
| 118 success, frame = capture.read() | |
| 119 results = model.track(frame, persist=True) | |
| 105 | 120 |
| 106 # interpolate before saving | 121 # interpolate and generate velocity before saving |
| 107 for num, obj in currentObjects.items(): | 122 for num, obj in currentObjects.items(): |
| 108 obj.setUserType(utils.mostCommon(obj.userTypes)) | 123 obj.setUserType(utils.mostCommon(obj.userTypes)) |
| 109 obj.features[0].timeInterval = copy(obj.getTimeInterval()) | 124 obj.features[0].timeInterval = copy(obj.getTimeInterval()) |
| 110 obj.features[1].timeInterval = copy(obj.getTimeInterval()) | 125 obj.features[1].timeInterval = copy(obj.getTimeInterval()) |
| 111 if obj.length() != len(obj.features[0].tmpPositions): # interpolate | 126 if obj.length() != len(obj.features[0].tmpPositions): # interpolate |
| 113 obj.features[1].positions = moving.Trajectory.fromPointDict(obj.features[1].tmpPositions) | 128 obj.features[1].positions = moving.Trajectory.fromPointDict(obj.features[1].tmpPositions) |
| 114 else: | 129 else: |
| 115 obj.features[0].positions = moving.Trajectory.fromPointList(list(obj.features[0].tmpPositions.values())) | 130 obj.features[0].positions = moving.Trajectory.fromPointList(list(obj.features[0].tmpPositions.values())) |
| 116 obj.features[1].positions = moving.Trajectory.fromPointList(list(obj.features[1].tmpPositions.values())) | 131 obj.features[1].positions = moving.Trajectory.fromPointList(list(obj.features[1].tmpPositions.values())) |
| 117 | 132 |
| 118 storage.saveTrajectoriesToSqlite('test.sqlite', list(currentObjects.values()), 'object') | 133 storage.saveTrajectoriesToSqlite(args.databaseFilename, list(currentObjects.values()), 'object') |
| 119 | 134 |
| 120 # todo save bbox and mask to study localization / representation | 135 # todo save bbox and mask to study localization / representation |
| 121 # apply quality checks deviation and acceleration bounds? | 136 # apply quality checks deviation and acceleration bounds? |
