Recycle previous corners on timeout

This commit is contained in:
Davide Depau 2024-02-22 01:04:15 +01:00
parent 83c2dd859b
commit ba3c9369e1

21
bot.py
View file

@ -12,7 +12,6 @@ import cv2
import numpy as np import numpy as np
import telegram import telegram
from aiohttp import BasicAuth from aiohttp import BasicAuth
from cv2 import aruco
from pytapo import Tapo from pytapo import Tapo
from telegram import Update, Message from telegram import Update, Message
from telegram.ext import Updater from telegram.ext import Updater
@ -39,6 +38,8 @@ 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 = {}
def _get_presets(self): def _get_presets(self):
presets = self.tapo.getPresets() presets = self.tapo.getPresets()
return {v: k for k, v in presets.items()} return {v: k for k, v in presets.items()}
@ -93,13 +94,21 @@ class Bot:
# Iterate until we find all 4 aruco markers or timeout # Iterate until we find all 4 aruco markers or timeout
aruco_corners = {} aruco_corners = {}
annotated_image = None annotated_image = None
aruco_dict = aruco.getPredefinedDictionary(aruco.DICT_6X6_250) aruco_dict = a.getPredefinedDictionary(a.DICT_6X6_250)
aruco_params = aruco.DetectorParameters() aruco_params = a.DetectorParameters()
t0 = time.time() t0 = time.time()
print("Taking image with ArUco markers...") print("Taking image with ArUco markers...")
while len(aruco_corners) < 4: while len(aruco_corners) < 4:
if time.time() - t0 > timeout: if time.time() - t0 > timeout:
a = self.last_aruco_corners.copy()
a.update(aruco_corners)
aruco_corners = a
if len(aruco_corners) == 4:
print("Timeout waiting for ArUco markers, using cached corners")
break
print( print(
"Timeout waiting for ArUco markers, returning only original image" "Timeout waiting for ArUco markers, returning only original image"
) )
@ -112,7 +121,7 @@ class Bot:
continue continue
# Detect the markers # Detect the markers
corners, ids, rejected = aruco.detectMarkers( corners, ids, rejected = a.detectMarkers(
annotated_image, aruco_dict, parameters=aruco_params annotated_image, aruco_dict, parameters=aruco_params
) )
for corner, i in zip(corners, ids): for corner, i in zip(corners, ids):
@ -129,7 +138,7 @@ class Bot:
corners = [aruco_corners[i] for i in range(1, 5)] corners = [aruco_corners[i] for i in range(1, 5)]
ids = np.array([[i] for i in range(1, 5)]) ids = np.array([[i] for i in range(1, 5)])
aruco.drawDetectedMarkers(annotated_image, corners, ids) a.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
@ -173,6 +182,8 @@ class Bot:
matrix = cv2.getPerspectiveTransform(expanded_rectangle_points, dst_pts) matrix = cv2.getPerspectiveTransform(expanded_rectangle_points, dst_pts)
warped = cv2.warpPerspective(pretty_image, matrix, (width, height)) warped = cv2.warpPerspective(pretty_image, matrix, (width, height))
self.last_aruco_corners = aruco_corners
return [warped, annotated_image] return [warped, annotated_image]
async def _photo_command(self, chat_id: int, adjust_perspective: bool = True): async def _photo_command(self, chat_id: int, adjust_perspective: bool = True):