Correct yet more python type hinting (#1555)

This commit is contained in:
James Ward
2024-11-13 03:17:27 +11:00
committed by GitHub
parent af03ae0a8b
commit 744e522aea
7 changed files with 38 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
import math
from typing import Any, Tuple
from typing import Any
import cv2 as cv
import numpy as np
@@ -48,13 +48,13 @@ class OpenCVHelp:
)
@staticmethod
def avgPoint(points: list[Tuple[float, float]]) -> Tuple[float, float]:
def avgPoint(points: np.ndarray) -> np.ndarray:
x = 0.0
y = 0.0
for p in points:
x += p[0]
y += p[1]
return (x / len(points), y / len(points))
x += p[0, 0]
y += p[0, 1]
return np.array([[x / len(points), y / len(points)]])
@staticmethod
def pointsToTargetCorners(points: np.ndarray) -> list[TargetCorner]:
@@ -126,6 +126,10 @@ class OpenCVHelp:
objectMat = np.array(OpenCVHelp.translationToTVec(modelTrls))
alt: Transform3d | None = None
reprojectionError: cv.typing.MatLike | None = None
best: Transform3d = Transform3d()
alt: Transform3d | None = None
for tries in range(2):
retval, rvecs, tvecs, reprojectionError = cv.solvePnPGeneric(
objectMat,
@@ -145,7 +149,9 @@ class OpenCVHelp:
OpenCVHelp.rVecToRotation(rvecs[1]),
)
if not math.isnan(reprojectionError[0, 0]):
if reprojectionError is not None and not math.isnan(
reprojectionError[0, 0]
):
break
else:
pt = imagePoints[0]
@@ -153,7 +159,7 @@ class OpenCVHelp:
pt[0, 1] -= 0.001
imagePoints[0] = pt
if math.isnan(reprojectionError[0, 0]):
if reprojectionError is None or math.isnan(reprojectionError[0, 0]):
print("SolvePNP_Square failed!")
return None