-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMAV2023_ArucoMarkerV2.py
114 lines (91 loc) · 4.03 KB
/
IMAV2023_ArucoMarkerV2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# --------------------------------------------------------------------------
# Author: Kevin Malkow
# Date: 05/07/23
# Affiliation: TU Delft, IMAV 2023
#
# Version: 2.0
#
# Description:
# Detect Aruco marker from pre-recorded video.
#
# Upcoming Version: 3.0
# Detect AND track Aruco marker from images -> requires camera calibration step (done in seperate code)
#
# Upcoming Version: 4.0
# Detect AND track Aruco marker from pre-recorded videos.
#
# Upcoming Version: 5.0
# Detect AND track Aruco marker from video live-stream.
# -------------------------------------------------------------------------
import glob
import cv2
import cv2.aruco
import numpy as np
# ------------------------------------------------------------------------- #
# FUNCTIONS #
# ------------------------------------------------------------------------- #
# -------------- Frame Rescaling Function --------------
def frame_rescale(frame, scale_percent):
# Frame downscaling -> USED AS FRAME INPUT AND AFFECTS PERFORMANCE OF DETECTOR
scale_percent = 30 # Percent of original size
width = int(frame.shape[1] * scale_percent / 100)
height = int(frame.shape[0] * scale_percent / 100)
dim = (width, height)
return cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
# ------------------------------------------------------------------------- #
# VIDEO PROCESSING #
# ------------------------------------------------------------------------- #
# -------------- Load Video --------------
# Define image path
path = '/home/kevin/IMAV2023/Aruco_Marker_Data/04_07_2023/Videos/Storey_1/2023_0704_035222_004.MP4'
# Create a VideoCapture object and read from input file
cap = cv2.VideoCapture(path)
# -------------- Detect Aruco Markers, Write Video, and View Video --------------
# Check if camera opened successfully
if (cap.isOpened()== False):
print("Error opening video file or stream!")
# Default resolutions of the frame are obtained and converted from float to integer
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# Define video codec (FOURCC code)
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
# Create VideoWriter object
out = cv2.VideoWriter('/home/kevin/IMAV2023/Aruco_Marker_Data/04_07_2023/Videos/Storey_1/Results/ArucoMarker_Video_Detected_4_4m_2.mp4', fourcc, 30, (frame_width, frame_height))
# Read until video is completed
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
# Rescale frame
# frame = frame_rescale(frame, 30)
if ret == True:
# -------------- Aruco Marker Detection --------------
# Load dictionary for aruco marker
arucoDictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_5X5_1000)
# Initiate aruco marker detection parameters
arucoParameters = cv2.aruco.DetectorParameters()
# Aruco marker detection setup
arucoDetector = cv2.aruco.ArucoDetector(arucoDictionary, arucoParameters)
# List of aruco marker detected corners, IDs corresponding to each aruco marker, and rejected aruco markers
(markerCorners, markerIDs, rejectedCandidates) = arucoDetector.detectMarkers(frame)
# AT LEAST ONE MARKER DETECTED
if len(markerCorners) > 0:
for i in range(0, len(markerIDs)):
# Draw around the correctly detected aruco markers
cv2.aruco.drawDetectedMarkers(frame, markerCorners, markerIDs)
# Draw around the rejected candidates
# cv2.aruco.drawDetectedMarkers(frame, rejectedCandidates, borderColor=(100, 200, 255))
# Write the frame into the file
out.write(frame)
# Display the resulting frame
cv2.imshow('Frame', frame)
# Wait 1 [ms] between each frame until it ends or press 'q' on keyboard to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# Release the video capture and video write objects
cap.release()
out.release()
# Close all the frames
cv2.destroyAllWindows()