|
| 1 | +import cv2 as cv |
| 2 | +import argparse |
| 3 | + |
| 4 | +# Check OpenCV version |
| 5 | +assert cv.__version__ > "4.8.0", \ |
| 6 | + "Please install latest opencv-python to try this demo: python3 -m pip install --upgrade opencv-python" |
| 7 | + |
| 8 | +parser = argparse.ArgumentParser( |
| 9 | + description="VIT track opencv API") |
| 10 | +parser.add_argument('--input', '-i', type=str, |
| 11 | + help='Usage: Set path to the input video. Omit for using default camera.') |
| 12 | +parser.add_argument('--model_path', type=str, default='vitTracker.onnx', |
| 13 | + help='Usage: Set model path, defaults to vitTracker.onnx.') |
| 14 | +args = parser.parse_args() |
| 15 | + |
| 16 | +def visualize(image, bbox, score, isLocated, fps=None, box_color=(0, 255, 0),text_color=(0, 255, 0), fontScale = 1, fontSize = 1): |
| 17 | + output = image.copy() |
| 18 | + h, w, _ = output.shape |
| 19 | + |
| 20 | + if fps is not None: |
| 21 | + cv.putText(output, 'FPS: {:.2f}'.format(fps), (0, 30), cv.FONT_HERSHEY_DUPLEX, fontScale, text_color, fontSize) |
| 22 | + |
| 23 | + if isLocated and score >= 0.3: |
| 24 | + # bbox: Tuple of length 4 |
| 25 | + x, y, w, h = bbox |
| 26 | + cv.rectangle(output, (x, y), (x+w, y+h), box_color, 2) |
| 27 | + cv.putText(output, '{:.2f}'.format(score), (x, y+20), cv.FONT_HERSHEY_DUPLEX, fontScale, text_color, fontSize) |
| 28 | + else: |
| 29 | + text_size, baseline = cv.getTextSize('Target lost!', cv.FONT_HERSHEY_DUPLEX, fontScale, fontSize) |
| 30 | + text_x = int((w - text_size[0]) / 2) |
| 31 | + text_y = int((h - text_size[1]) / 2) |
| 32 | + cv.putText(output, 'Target lost!', (text_x, text_y), cv.FONT_HERSHEY_DUPLEX, fontScale, (0, 0, 255), fontSize) |
| 33 | + |
| 34 | + return output |
| 35 | + |
| 36 | +if __name__ == '__main__': |
| 37 | + |
| 38 | + params = cv.TrackerVit_Params() |
| 39 | + params.net = args.model_path |
| 40 | + model = cv.TrackerVit_create(params) |
| 41 | + |
| 42 | + # Read from args.input |
| 43 | + _input = args.input |
| 44 | + if args.input is None: |
| 45 | + device_id = 0 |
| 46 | + _input = device_id |
| 47 | + video = cv.VideoCapture(_input) |
| 48 | + |
| 49 | + # Select an object |
| 50 | + has_frame, first_frame = video.read() |
| 51 | + if not has_frame: |
| 52 | + print('No frames grabbed!') |
| 53 | + exit() |
| 54 | + first_frame_copy = first_frame.copy() |
| 55 | + cv.putText(first_frame_copy, "1. Drag a bounding box to track.", (0, 15), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0)) |
| 56 | + cv.putText(first_frame_copy, "2. Press ENTER to confirm", (0, 35), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0)) |
| 57 | + roi = cv.selectROI('vitTrack Demo', first_frame_copy) |
| 58 | + print("Selected ROI: {}".format(roi)) |
| 59 | + |
| 60 | + # Init tracker with ROI |
| 61 | + model.init(first_frame, roi) |
| 62 | + |
| 63 | + # Track frame by frame |
| 64 | + tm = cv.TickMeter() |
| 65 | + while cv.waitKey(1) < 0: |
| 66 | + has_frame, frame = video.read() |
| 67 | + if not has_frame: |
| 68 | + print('End of video') |
| 69 | + break |
| 70 | + # Inference |
| 71 | + tm.start() |
| 72 | + isLocated, bbox = model.update(frame) |
| 73 | + score = model.getTrackingScore() |
| 74 | + tm.stop() |
| 75 | + # Visualize |
| 76 | + frame = visualize(frame, bbox, score, isLocated, fps=tm.getFPS()) |
| 77 | + cv.imshow('vittrack Demo', frame) |
| 78 | + tm.reset() |
| 79 | + |
0 commit comments