Fix code for new ArUcO markers
This commit is contained in:
parent
9e0a3440cd
commit
9165cb9551
1 changed files with 24 additions and 13 deletions
37
bot.py
37
bot.py
|
@ -39,7 +39,7 @@ class Bot:
|
||||||
self.tapo = Tapo(self.camera_ip, self.camera_user, self.camera_password)
|
self.tapo = Tapo(self.camera_ip, self.camera_user, self.camera_password)
|
||||||
self.executor = ThreadPoolExecutor(max_workers=len(os.sched_getaffinity(0)))
|
self.executor = ThreadPoolExecutor(max_workers=len(os.sched_getaffinity(0)))
|
||||||
|
|
||||||
self.last_aruco_corners = {}
|
self.last_aruco_corners = [None] * 4
|
||||||
|
|
||||||
def _get_presets(self):
|
def _get_presets(self):
|
||||||
presets = self.tapo.getPresets()
|
presets = self.tapo.getPresets()
|
||||||
|
@ -82,6 +82,9 @@ class Bot:
|
||||||
vcap = cv2.VideoCapture(
|
vcap = cv2.VideoCapture(
|
||||||
f"rtsp://{self.camera_user}:{self.camera_password}@{self.camera_ip}:554/stream1"
|
f"rtsp://{self.camera_user}:{self.camera_password}@{self.camera_ip}:554/stream1"
|
||||||
)
|
)
|
||||||
|
if chat_action_fn:
|
||||||
|
chat_action_fn()
|
||||||
|
|
||||||
print("Taking color image...")
|
print("Taking color image...")
|
||||||
ret, pretty_image = vcap.read()
|
ret, pretty_image = vcap.read()
|
||||||
while pretty_image is None:
|
while pretty_image is None:
|
||||||
|
@ -94,22 +97,31 @@ class Bot:
|
||||||
self.tapo.setDayNightMode("on")
|
self.tapo.setDayNightMode("on")
|
||||||
|
|
||||||
# Iterate until we find all 4 aruco markers or timeout
|
# Iterate until we find all 4 aruco markers or timeout
|
||||||
aruco_corners = {}
|
aruco_corners = [None] * 4
|
||||||
|
|
||||||
|
def found_all(corners):
|
||||||
|
return all(c is not None for c in corners)
|
||||||
|
|
||||||
annotated_image = None
|
annotated_image = None
|
||||||
aruco_dict = aruco.getPredefinedDictionary(aruco.DICT_4X4_50)
|
aruco_dict = aruco.getPredefinedDictionary(aruco.DICT_4X4_50)
|
||||||
aruco_params = aruco.DetectorParameters()
|
aruco_params = aruco.DetectorParameters()
|
||||||
|
|
||||||
|
if chat_action_fn:
|
||||||
|
chat_action_fn()
|
||||||
|
|
||||||
t0 = time.time()
|
t0 = time.time()
|
||||||
last_chat_action = 0
|
last_chat_action = 0
|
||||||
print("Taking image with ArUco markers...")
|
print("Taking image with ArUco markers...")
|
||||||
while len(aruco_corners) < 4:
|
while not found_all(aruco_corners):
|
||||||
delta = time.time() - t0
|
delta = time.time() - t0
|
||||||
if delta > timeout:
|
if delta > timeout:
|
||||||
a = self.last_aruco_corners.copy()
|
a = self.last_aruco_corners.copy()
|
||||||
a.update(aruco_corners)
|
for i in range(4):
|
||||||
|
if aruco_corners[i] is not None:
|
||||||
|
a[i] = aruco_corners[i]
|
||||||
aruco_corners = a
|
aruco_corners = a
|
||||||
|
|
||||||
if len(aruco_corners) == 4:
|
if found_all(aruco_corners):
|
||||||
print("Timeout waiting for ArUco markers, using cached corners")
|
print("Timeout waiting for ArUco markers, using cached corners")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@ -146,17 +158,15 @@ class Bot:
|
||||||
self.tapo.setDayNightMode("auto")
|
self.tapo.setDayNightMode("auto")
|
||||||
self.tapo.setPrivacyMode(privacy_mode)
|
self.tapo.setPrivacyMode(privacy_mode)
|
||||||
|
|
||||||
corners = [aruco_corners[i] for i in range(1, 5)]
|
aruco.drawDetectedMarkers(annotated_image, aruco_corners, np.array(range(4)))
|
||||||
ids = np.array([[i] for i in range(1, 5)])
|
|
||||||
aruco.drawDetectedMarkers(annotated_image, corners, ids)
|
|
||||||
|
|
||||||
# Annotate the image with the detected markers and apply the perspective transform to the pretty image
|
# Annotate the image with the detected markers and apply the perspective transform to the pretty image
|
||||||
|
|
||||||
# Get the outermost points of each marker
|
# Get the outermost points of each marker
|
||||||
bl_marker = corners[0].squeeze()[3] # bottom left marker
|
tl_marker = aruco_corners[0].squeeze()[0] # top left marker
|
||||||
tl_marker = corners[1].squeeze()[0] # top left marker
|
tr_marker = aruco_corners[1].squeeze()[1] # top right marker
|
||||||
tr_marker = corners[2].squeeze()[1] # top right marker
|
bl_marker = aruco_corners[2].squeeze()[3] # bottom left marker
|
||||||
bc_marker = corners[3].squeeze()[3] # bottom center marker
|
bc_marker = aruco_corners[3].squeeze()[2] # bottom center marker
|
||||||
|
|
||||||
# Calculate the fourth point by computing the line through the bottom markers and intersecting with the vertical
|
# Calculate the fourth point by computing the line through the bottom markers and intersecting with the vertical
|
||||||
# line through the top right marker
|
# line through the top right marker
|
||||||
|
@ -202,9 +212,10 @@ class Bot:
|
||||||
|
|
||||||
await chat_action_fn()
|
await chat_action_fn()
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
photos = await self.take_photo(
|
photos = await self.take_photo(
|
||||||
adjust_perspective=adjust_perspective,
|
adjust_perspective=adjust_perspective,
|
||||||
chat_action_fn=lambda: asyncio.run(chat_action_fn())
|
chat_action_fn=lambda: loop.create_task(chat_action_fn()),
|
||||||
)
|
)
|
||||||
jpegs = [cv2.imencode(".jpg", photo)[1].tobytes() for photo in photos]
|
jpegs = [cv2.imencode(".jpg", photo)[1].tobytes() for photo in photos]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue