|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from argparse import ArgumentParser |
| 4 | +import os |
| 5 | +import cv2 |
| 6 | +import numpy as np |
| 7 | +import apriltag # https://github.com/Tinker-Twins/AprilTag |
| 8 | + |
| 9 | +################################################################################ |
| 10 | + |
| 11 | +def autodrive_ips(input_stream='IPS_Test.mp4', |
| 12 | + output_stream=True, |
| 13 | + display_stream=True, |
| 14 | + detection_window_name='AutoDRIVE IPS', |
| 15 | + ): |
| 16 | + |
| 17 | + ''' |
| 18 | + AutoDRIVE IPS |
| 19 | +
|
| 20 | + Args: input_stream [str]: Camera IP address or footage name to run detection algorithm on |
| 21 | + output_stream [bool]: Boolean flag to save/not stream annotated with detections |
| 22 | + display_stream [bool]: Boolean flag to display/not stream annotated with detections |
| 23 | + detection_window_name [str]: Title of displayed (output) tag detection window |
| 24 | + ''' |
| 25 | + |
| 26 | + parser = ArgumentParser(description='AutoDRIVE IPS') |
| 27 | + apriltag.add_arguments(parser) |
| 28 | + options = parser.parse_args() |
| 29 | + |
| 30 | + ''' |
| 31 | + Set up a reasonable search path for the apriltag DLL. |
| 32 | + Either install the DLL in the appropriate system-wide |
| 33 | + location, or specify your own search paths as needed. |
| 34 | + ''' |
| 35 | + |
| 36 | + detector = apriltag.Detector(options, searchpath=apriltag._get_dll_path()) |
| 37 | + |
| 38 | + video = cv2.VideoCapture(input_stream) |
| 39 | + |
| 40 | + output = None |
| 41 | + |
| 42 | + if output_stream: |
| 43 | + width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) |
| 44 | + height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
| 45 | + fps = int(video.get(cv2.CAP_PROP_FPS)) |
| 46 | + codec = cv2.VideoWriter_fourcc(*'XVID') |
| 47 | + output_path = str(os.path.split(input_stream)[1]).replace(str(os.path.splitext(input_stream)[1]), '.avi') |
| 48 | + output = cv2.VideoWriter(output_path, codec, fps, (width, height)) |
| 49 | + |
| 50 | + while(video.isOpened()): |
| 51 | + |
| 52 | + success, frame = video.read() |
| 53 | + if not success: |
| 54 | + break |
| 55 | + |
| 56 | + result, overlay = apriltag.detect_tags(frame, |
| 57 | + detector, |
| 58 | + camera_params=(3156.71852, 3129.52243, 359.097908, 239.736909), |
| 59 | + tag_size=0.0762, |
| 60 | + vizualization=2, |
| 61 | + verbose=0, |
| 62 | + annotation=False |
| 63 | + ) |
| 64 | + |
| 65 | + if len(result)==8: |
| 66 | + map_x_cam = result[1][0][3] # result[1] is map apriltag pose w.r.t. camera |
| 67 | + map_y_cam = -result[1][1][3] |
| 68 | + vehicle_x_cam = result[5][0][3] - 0.035125 # result[5] is vehicle apriltag pose w.r.t. camera |
| 69 | + vehicle_y_cam = -result[5][1][3] |
| 70 | + vehicle_position = [vehicle_x_cam - map_x_cam, |
| 71 | + vehicle_y_cam - map_y_cam] |
| 72 | + print(vehicle_position) |
| 73 | + |
| 74 | + if output_stream: |
| 75 | + output.write(overlay) |
| 76 | + |
| 77 | + if display_stream: |
| 78 | + cv2.imshow(detection_window_name, overlay) |
| 79 | + if cv2.waitKey(1) & 0xFF == ord(' '): # Press space bar to terminate |
| 80 | + break |
| 81 | + |
| 82 | +################################################################################ |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + autodrive_ips() |
0 commit comments