Commit for Gitea
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import cv2, numpy as np
|
||||
|
||||
from Tbd.helper import CameraObject
|
||||
|
||||
from PySide6.QtCore import Qt, QTimer, QSize
|
||||
from PySide6.QtGui import QImage, QPixmap
|
||||
|
||||
from PySide6.QtWidgets import QWidget, QLabel, QVBoxLayout, QSizePolicy, QHBoxLayout, QLineEdit, QPushButton
|
||||
from typing import List
|
||||
from PySide6.QtWidgets import QScrollArea, QFrame
|
||||
|
||||
|
||||
class CameraSetupWidget(QWidget):
|
||||
|
||||
def __init__(self, cameraObject: CameraObject):
|
||||
super().__init__()
|
||||
|
||||
self.cameraObject = cameraObject
|
||||
|
||||
self.mainLayout = QHBoxLayout(self)
|
||||
|
||||
self.frame_preview_widget = QLabel()
|
||||
self.preview_max_size = QSize(800, 600)
|
||||
self.frame_preview_widget.setMaximumSize(self.preview_max_size)
|
||||
self.camerOptionsLayout = QVBoxLayout()
|
||||
|
||||
self.start_preview_button = QPushButton("Start")
|
||||
self.stop_preview_button = QPushButton("Stop")
|
||||
|
||||
|
||||
|
||||
self.camerOptionsLayout.addWidget(self.start_preview_button)
|
||||
self.camerOptionsLayout.addWidget(self.stop_preview_button)
|
||||
|
||||
self.mainLayout.addLayout(self.camerOptionsLayout)
|
||||
self.mainLayout.addWidget(self.frame_preview_widget)
|
||||
|
||||
self.start_preview_button.clicked.connect(self._start_camera_preview)
|
||||
self.stop_preview_button.clicked.connect(self._stop_camera_preview)
|
||||
|
||||
self.frame_update_timer = QTimer(self)
|
||||
self.frame_update_timer.timeout.connect(self._update_frame)
|
||||
|
||||
|
||||
def _start_camera_preview(self):
|
||||
self.frame_update_timer.start(30)
|
||||
|
||||
def _stop_camera_preview(self):
|
||||
self.frame_update_timer.stop()
|
||||
|
||||
|
||||
|
||||
def _update_frame(self):
|
||||
ok, frame = self.cameraObject.capture.read()
|
||||
if not ok or frame is None:
|
||||
return
|
||||
|
||||
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||
height, width, ch = frame_rgb.shape
|
||||
qimg = QImage(frame_rgb.data, width, height, ch * width, QImage.Format_RGB888)
|
||||
pixmap = QPixmap.fromImage(qimg)
|
||||
pixmap = pixmap.scaled(
|
||||
self.frame_preview_widget.maximumSize(),
|
||||
Qt.KeepAspectRatio,
|
||||
Qt.SmoothTransformation,
|
||||
)
|
||||
self.frame_preview_widget.setPixmap(pixmap)
|
||||
@@ -0,0 +1,333 @@
|
||||
import cv2
|
||||
import numpy
|
||||
import mediapipe
|
||||
from Tbd.helper import CameraObject, Hand2D
|
||||
from PySide6.QtCore import Qt, QTimer
|
||||
from PySide6.QtWidgets import QMainWindow, QLabel, QVBoxLayout, QWidget
|
||||
from PySide6.QtGui import QImage, QPixmap
|
||||
from typing import List
|
||||
from collections import defaultdict
|
||||
import winsound
|
||||
|
||||
mediapipe_options = mediapipe.solutions.hands
|
||||
|
||||
class GameWindow(QMainWindow):
|
||||
"""
|
||||
Separate window for the “game” part.
|
||||
You can put your rendering, controls, etc. here.
|
||||
"""
|
||||
|
||||
def __init__(self, cameraList: List[CameraObject], width_px=1920, height_px=1080, fps=30, max_num_hands=4):
|
||||
super().__init__()
|
||||
|
||||
self.P_mats = {}
|
||||
|
||||
self.cameraList = cameraList
|
||||
self._hands = mediapipe_options.Hands(
|
||||
static_image_mode=False,
|
||||
max_num_hands=max_num_hands,
|
||||
model_complexity=1,
|
||||
min_detection_confidence=0.5,
|
||||
min_tracking_confidence=0.5,
|
||||
)
|
||||
|
||||
self.setWindowTitle("Game Window")
|
||||
self.canvas_w = width_px
|
||||
self.canvas_h = height_px
|
||||
|
||||
|
||||
self.view = QLabel("HUD")
|
||||
self.view.setAlignment(Qt.AlignCenter)
|
||||
self.view.setScaledContents(False) # keep aspect ratio
|
||||
|
||||
|
||||
central = QWidget(self)
|
||||
self.setCentralWidget(central)
|
||||
layout = QVBoxLayout(central)
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
layout.addWidget(self.view, 1)
|
||||
|
||||
# Create a Timer for my _tick function and start it
|
||||
self.timer = QTimer(self)
|
||||
self.timer.timeout.connect(self._tick)
|
||||
self.timer.start(30) # ~33 FPS
|
||||
|
||||
def _tick(self):
|
||||
|
||||
# 1) create a black BGR canvas
|
||||
frame_bgr = numpy.zeros((self.canvas_h, self.canvas_w, 3), dtype=numpy.uint8)
|
||||
|
||||
# 2) (optional) draw HUD widgets here
|
||||
cv2.putText(frame_bgr, "HUD ready", (40, 60), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (200,200,200), 2, cv2.LINE_AA)
|
||||
|
||||
hands2d = self._detectHands()
|
||||
|
||||
self.draw_hands_on_frame(frame_bgr, hands2d)
|
||||
|
||||
# 3) draw detected hands (in canvas pixel coords)
|
||||
for h in hands2d:
|
||||
for (x, y) in h.landmarks_px.astype(int):
|
||||
cv2.circle(frame_bgr, (x, y), 4, (0, 255, 255), -1, cv2.LINE_AA)
|
||||
|
||||
# 4) show it
|
||||
frame_rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
|
||||
qimg = QImage(frame_rgb.data, self.canvas_w, self.canvas_h,
|
||||
self.canvas_w * 3, QImage.Format_RGB888)
|
||||
pix = QPixmap.fromImage(qimg)
|
||||
# keep aspect ratio when fitting into the label
|
||||
self.view.setPixmap(pix.scaled(self.view.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
|
||||
|
||||
hand_groups = self._allocateDetectedHands(hands2d)
|
||||
|
||||
hands3d = self._triangulateHands(hand_groups)
|
||||
print(hands3d)
|
||||
self._updateGeasture()
|
||||
self._checkInteractions()
|
||||
self._upateHUD()
|
||||
self._drawHUD()
|
||||
|
||||
|
||||
# Loop all Cameras and return a List of all found Hands in all Cameras
|
||||
def _detectHands(self):
|
||||
out: List[Hand2D] = [] # List of all found Hands
|
||||
|
||||
|
||||
for camera in self.cameraList:
|
||||
|
||||
# Get the frame of the camera and check if the camera is available
|
||||
ok, frameBGR = camera.capture.read()
|
||||
if not ok or frameBGR is None:
|
||||
continue
|
||||
|
||||
frameHight, frameWidht = frameBGR.shape[:2] # Take only the the frame hight and widht from the Tuple
|
||||
|
||||
frameRGB = cv2.cvtColor(frameBGR, cv2.COLOR_BGR2RGB) # Convert to RGB becaus,e MediaPipe expects RGB
|
||||
frameRGB.flags.writeable = False # (minor speed gain)
|
||||
|
||||
# Detect Hands in the frame and return a list of all found Hands, then check if
|
||||
foundHands = self._hands.process(frameRGB)
|
||||
if not foundHands.multi_hand_landmarks:
|
||||
continue
|
||||
|
||||
# handedness info aligns with landmarks list
|
||||
handed = foundHands.multi_handedness
|
||||
|
||||
for handIndex, landmarlList in enumerate(foundHands.multi_hand_landmarks):
|
||||
landmarks_px = numpy.array([[landmark.x*frameWidht, landmark.y*frameHight] for landmark in landmarlList.landmark], dtype=numpy.float32)
|
||||
|
||||
label = "Unknown"
|
||||
score = 0.0
|
||||
|
||||
if handIndex < len(handed):
|
||||
classifications = handed[handIndex].classification
|
||||
if classifications:
|
||||
label = classifications[0].label # "Left" / "Right"
|
||||
score = classifications[0].score # confidence
|
||||
|
||||
out.append(Hand2D(
|
||||
camera_id=camera.index,
|
||||
handedness=label,
|
||||
score=score,
|
||||
landmarks_px=landmarks_px
|
||||
))
|
||||
|
||||
|
||||
return out
|
||||
|
||||
def _allocateDetectedHands(self, hands2d: list):
|
||||
"""
|
||||
Take flat list of Hand2D from all cameras and group them into physical hands.
|
||||
Writes self._hand_groups = [ {cam_id: Hand2D, ...}, ... ]
|
||||
"""
|
||||
|
||||
# Group by camera
|
||||
frames_by_camera = defaultdict(list)
|
||||
for hand in hands2d:
|
||||
frames_by_camera[hand.camera_id].append(hand)
|
||||
|
||||
# We will mark which Hand2D detections are already consumed
|
||||
used = set() # set of id(hand2d)
|
||||
hand_groups = []
|
||||
|
||||
# Threshold in pixels for “same hand”
|
||||
reproj_threshold = 8.0 # tune this
|
||||
|
||||
camera_ids = sorted(frames_by_camera.keys())
|
||||
|
||||
for cam_a in camera_ids:
|
||||
for hand_a in frames_by_camera[cam_a]:
|
||||
if id(hand_a) in used:
|
||||
continue
|
||||
|
||||
# Start a new group with this detection as the seed
|
||||
group = {cam_a: hand_a}
|
||||
used.add(id(hand_a))
|
||||
|
||||
P_a = self.P_mats[cam_a]
|
||||
|
||||
# Try to find matching hands in all other cameras
|
||||
for cam_b in camera_ids:
|
||||
if cam_b == cam_a:
|
||||
continue
|
||||
if cam_b not in self.P_mats:
|
||||
continue
|
||||
|
||||
P_b = self.P_mats[cam_b]
|
||||
|
||||
best_hand = None
|
||||
best_err = numpy.inf
|
||||
|
||||
for hand_b in frames_by_camera[cam_b]:
|
||||
if id(hand_b) in used:
|
||||
continue
|
||||
|
||||
err = self.pair_reprojection_error(
|
||||
hand_a, hand_b,
|
||||
P_a, P_b,
|
||||
key_idxs=[0, 9] # wrist + middle MCP for example
|
||||
)
|
||||
|
||||
if err < best_err:
|
||||
best_err = err
|
||||
best_hand = hand_b
|
||||
|
||||
if best_hand is not None and best_err < reproj_threshold:
|
||||
group[cam_b] = best_hand
|
||||
used.add(id(best_hand))
|
||||
|
||||
hand_groups.append(group)
|
||||
|
||||
return hand_groups
|
||||
|
||||
|
||||
def _triangulateHands(self, hand_groups):
|
||||
|
||||
hands3d = []
|
||||
|
||||
for group in hand_groups:
|
||||
# use all cameras in 'group' to triangulate each landmark
|
||||
cams = list(group.keys())
|
||||
hands = [group[c] for c in cams]
|
||||
|
||||
# Example: simple pairwise triangulation using first two cameras
|
||||
if len(cams) < 2:
|
||||
continue # need at least 2 views
|
||||
|
||||
P1 = self.P_mats[cams[0]]
|
||||
P2 = self.P_mats[cams[1]]
|
||||
lm1 = hands[0].landmarks_px
|
||||
lm2 = hands[1].landmarks_px
|
||||
|
||||
pts3d = []
|
||||
for i in range(lm1.shape[0]):
|
||||
X = self.triangulate_point(P1, P2, lm1[i], lm2[i])
|
||||
pts3d.append(X)
|
||||
|
||||
pts3d = numpy.array(pts3d, dtype=numpy.float32)
|
||||
hands3d.append(pts3d)
|
||||
|
||||
return hands3d
|
||||
|
||||
def _updateGeasture(self):
|
||||
|
||||
return
|
||||
|
||||
def _getMostConfindentHand(self):
|
||||
|
||||
return
|
||||
|
||||
def _checkInteractions(self):
|
||||
|
||||
return
|
||||
|
||||
def _upateHUD(self):
|
||||
|
||||
return
|
||||
|
||||
def _drawHUD(self):
|
||||
|
||||
return
|
||||
|
||||
|
||||
def close(self):
|
||||
print("What?")
|
||||
self.timer.stop()
|
||||
return super().close()
|
||||
|
||||
|
||||
def draw_hands_on_frame(self, frame_bgr: numpy.ndarray, hands2d: list):
|
||||
# Draw connections first, then points
|
||||
for h in hands2d:
|
||||
pts = h.landmarks_px.astype(int) # (21,2) in pixels
|
||||
|
||||
# bones
|
||||
for a, b in mediapipe_options.HAND_CONNECTIONS:
|
||||
cv2.line(frame_bgr,
|
||||
(int(pts[a,0]), int(pts[a,1])),
|
||||
(int(pts[b,0]), int(pts[b,1])),
|
||||
(0, 255, 0), 2, cv2.LINE_AA)
|
||||
|
||||
# joints
|
||||
for (x, y) in pts:
|
||||
cv2.circle(frame_bgr, (int(x), int(y)), 3, (0, 0, 255), -1, cv2.LINE_AA)
|
||||
|
||||
# optional label
|
||||
cv2.putText(frame_bgr, f"{h.handedness} {h.score:.2f}",
|
||||
(int(pts[0,0]), int(pts[0,1])-8),
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (80,255,80), 1, cv2.LINE_AA)
|
||||
|
||||
def project_point(self, P, X):
|
||||
"""
|
||||
P: 3x4 projection matrix
|
||||
X: 3D point (X, Y, Z)
|
||||
returns: 2D point (u, v) in pixels
|
||||
"""
|
||||
X_h = numpy.array([X[0], X[1], X[2], 1.0], dtype=numpy.float32)
|
||||
x = P @ X_h
|
||||
return numpy.array([x[0] / x[2], x[1] / x[2]], dtype=numpy.float32)
|
||||
|
||||
def pair_reprojection_error(self, hand_a, hand_b, P_a, P_b, key_idxs):
|
||||
"""
|
||||
hand_a, hand_b: Hand2D objects
|
||||
P_a, P_b: 3x4 projection matrices
|
||||
key_idxs: list of landmark indices to use (e.g. [0, 9])
|
||||
returns: average reprojection error in pixels
|
||||
"""
|
||||
errors = []
|
||||
|
||||
for idx in key_idxs:
|
||||
x1 = hand_a.landmarks_px[idx] # (u, v)
|
||||
x2 = hand_b.landmarks_px[idx]
|
||||
|
||||
X = self.triangulate_point(P_a, P_b, x1, x2)
|
||||
|
||||
x1_hat = self.project_point(P_a, X)
|
||||
x2_hat = self.project_point(P_b, X)
|
||||
|
||||
e1 = numpy.linalg.norm(x1_hat - x1)
|
||||
e2 = numpy.linalg.norm(x2_hat - x2)
|
||||
|
||||
errors.append(e1)
|
||||
errors.append(e2)
|
||||
|
||||
return float(numpy.mean(errors))
|
||||
|
||||
|
||||
def triangulate_point(self, P1, P2, x1, x2):
|
||||
"""
|
||||
P1, P2: 3x4 projection matrices
|
||||
x1, x2: 2D points (u, v) in pixels (float)
|
||||
returns: 3D point in world coords (X, Y, Z)
|
||||
"""
|
||||
A = numpy.zeros((4, 4), dtype=numpy.float32)
|
||||
A[0] = x1[0] * P1[2] - P1[0]
|
||||
A[1] = x1[1] * P1[2] - P1[1]
|
||||
A[2] = x2[0] * P2[2] - P2[0]
|
||||
A[3] = x2[1] * P2[2] - P2[1]
|
||||
|
||||
# Solve A * X = 0, X is homogeneous 4D
|
||||
_, _, Vt = numpy.linalg.svd(A)
|
||||
X_h = Vt[-1]
|
||||
X_h /= X_h[3]
|
||||
return X_h[:3] # (X, Y, Z)
|
||||
|
||||
@@ -0,0 +1,505 @@
|
||||
import cv2
|
||||
import numpy
|
||||
|
||||
from Tbd.helper import CameraObject, CharucoDetection, CalibrationSample
|
||||
from UI.CameraSetupWidget import CameraSetupWidget
|
||||
from Helpers import saveManager as sm
|
||||
|
||||
from PySide6.QtCore import Qt, QTimer
|
||||
from PySide6.QtGui import QImage, QPixmap
|
||||
|
||||
from PySide6.QtWidgets import QWidget, QLabel, QVBoxLayout, QSizePolicy
|
||||
from typing import List
|
||||
from PySide6.QtWidgets import QScrollArea, QFrame, QPushButton
|
||||
|
||||
from UI.UILogger import UILogger
|
||||
|
||||
MIN_CHARUCO_CORNERS = 15 # absolute minimum
|
||||
#GOOD_CHARUCO_CORNERS = 25 # ideal
|
||||
|
||||
DICT_ID = cv2.aruco.DICT_4X4_50
|
||||
SQUARES_X = 6 # number of chessboard squares in X
|
||||
SQUARES_Y = 9 # number of chessboard squares in Y
|
||||
SQUARE_LEN_MM = 25.0 # square size in mm
|
||||
MARKER_LEN_MM = 18.0 # marker size in mm (must be < square)
|
||||
|
||||
#FRAME_SIZE = [3264, 2448]
|
||||
#FRAME_SIZE = [1536, 2048]
|
||||
FRAME_SIZE = [2048, 1536]
|
||||
|
||||
REFERENCE_CAMERA_INDEX = 1
|
||||
|
||||
class HomeWindow(QWidget):
|
||||
|
||||
def __init__(self, cameraList: List[CameraObject], on_cameraList_changed, P_mats, logger : UILogger):
|
||||
super().__init__()
|
||||
|
||||
# Initiating variables
|
||||
self.P_mats = P_mats
|
||||
self.on_cameraList_changed = on_cameraList_changed
|
||||
self.cameraList = cameraList
|
||||
self.logger = logger
|
||||
self.calibration_samples: list[CalibrationSample] = []
|
||||
self.camera_Setup_Widget_List = []
|
||||
|
||||
|
||||
# Charcuo detection stuff
|
||||
dictionary = cv2.aruco.getPredefinedDictionary(DICT_ID)
|
||||
self.board = cv2.aruco.CharucoBoard(
|
||||
(SQUARES_X, SQUARES_Y),
|
||||
SQUARE_LEN_MM,
|
||||
MARKER_LEN_MM,
|
||||
dictionary
|
||||
)
|
||||
self.charuco_detector = cv2.aruco.CharucoDetector(self.board)
|
||||
# Detector parameters
|
||||
detector_params = cv2.aruco.DetectorParameters()
|
||||
# You can tweak these if detection is unstable:
|
||||
# detector_params.adaptiveThreshWinSizeMin = 3
|
||||
# detector_params.adaptiveThreshWinSizeMax = 23
|
||||
# detector_params.adaptiveThreshWinSizeStep = 10
|
||||
|
||||
self.arcuo_detector = cv2.aruco.ArucoDetector(dictionary, detector_params)
|
||||
|
||||
# Creating the main_Layout and other Widgets
|
||||
self.main_Layout = QVBoxLayout(self)
|
||||
self.scroll_Box = QScrollArea()
|
||||
self.calibration_button = QPushButton("Calibrate")
|
||||
self.caputre_calibration_sample_button = QPushButton("Capture calibration sample")
|
||||
self.clear_calibration_samples_button = QPushButton("Clear calibration samples")
|
||||
self.load_calibration_button = QPushButton("Load calibration")
|
||||
self.save_calibration_button = QPushButton("Save calibration")
|
||||
|
||||
|
||||
# Creating the Scrollable area
|
||||
self.scroll_Box.setWidgetResizable(True)
|
||||
self.scroll_Content = QWidget()
|
||||
self.scroll_Layout = QVBoxLayout(self.scroll_Content)
|
||||
self.scroll_Layout.setAlignment(Qt.AlignTop)
|
||||
self.scroll_Box.setWidget(self.scroll_Content)
|
||||
|
||||
# Adding Primary show elements
|
||||
self.main_Layout.addWidget(self.clear_calibration_samples_button)
|
||||
self.main_Layout.addWidget(self.load_calibration_button)
|
||||
self.main_Layout.addWidget(self.save_calibration_button)
|
||||
self.main_Layout.addWidget(self.caputre_calibration_sample_button)
|
||||
self.main_Layout.addWidget(self.calibration_button)
|
||||
self.main_Layout.addWidget(self.scroll_Box)
|
||||
|
||||
# Connecting Buttons to functions
|
||||
self.calibration_button.clicked.connect(self._calibrate_camera)
|
||||
self.caputre_calibration_sample_button.clicked.connect(self._caputre_calibration_sample)
|
||||
self.clear_calibration_samples_button.clicked.connect(self._clear_calibration_samples)
|
||||
self.load_calibration_button.clicked.connect(self._load_calibration)
|
||||
self.save_calibration_button.clicked.connect(self._save_calibration)
|
||||
|
||||
|
||||
def _clear_calibration_samples(self):
|
||||
self.calibration_samples.clear()
|
||||
self.logger.log("Calibration samples where cleared.")
|
||||
|
||||
def _load_calibration(self):
|
||||
loaded = sm.load_calibration_json("C:\\git\\Table\\Test\\test.json", self.logger)
|
||||
self.cameraList.clear()
|
||||
self.cameraList.extend(loaded)
|
||||
self.P_mats.clear()
|
||||
self.P_mats.update({
|
||||
camera.index: camera.camera_projection_matrix
|
||||
for camera in self.cameraList
|
||||
if camera.camera_projection_matrix is not None
|
||||
})
|
||||
self.on_cameraList_changed()
|
||||
|
||||
def _save_calibration(self):
|
||||
sm.save_calibration_json(self.cameraList, "C:\\git\\Table\\Test\\test.json", self.logger)
|
||||
|
||||
def _calibrate_camera(self):
|
||||
self.logger.log("Fuck you")
|
||||
|
||||
if(len(self.cameraList) < 2):
|
||||
self.logger.log("You need at least 2 cameras for calibration")
|
||||
return
|
||||
|
||||
self._clear_camera_calibrations()
|
||||
|
||||
REFERENCE_CAMERA_INDEX = self.cameraList[0].index
|
||||
|
||||
for camera in self.cameraList:
|
||||
reprojection_error_rms, camera_matrix, distortion_coefficients = self._calibrate_intrinsics_characuo_for_camera(
|
||||
sample_list = self.calibration_samples,
|
||||
camera_index = camera.index,
|
||||
board = self.board
|
||||
)
|
||||
|
||||
camera.camera_matrix = camera_matrix
|
||||
camera.distortion_coefficients = distortion_coefficients
|
||||
|
||||
self.logger.log(f"[Intrinsics] cam{camera.index}: RMS={reprojection_error_rms:.4f} fx={camera_matrix[0,0]:.2f} fy={camera_matrix[1,1]:.2f} cx={camera_matrix[0,2]:.2f} cy={camera_matrix[1,2]:.2f}")
|
||||
|
||||
reference_camera = next(camera for camera in self.cameraList if camera.index == REFERENCE_CAMERA_INDEX)
|
||||
|
||||
self.logger.log("Intrinsics done")
|
||||
|
||||
self.logger.log("Start extrinsics1")
|
||||
# Reference camera projection
|
||||
reference_camera.rotation_matrix_world_to_camera = numpy.eye(3)
|
||||
reference_camera.translation_vector_world_to_camera = numpy.zeros((3,1))
|
||||
reference_camera.camera_projection_matrix = (
|
||||
reference_camera.camera_matrix
|
||||
@ numpy.hstack([numpy.eye(3), numpy.zeros((3,1))])
|
||||
)
|
||||
self.logger.log("Start extrinsics2")
|
||||
self.P_mats.clear()
|
||||
self.P_mats[reference_camera.index] = reference_camera.camera_projection_matrix
|
||||
self.logger.log("Start extrinsics3")
|
||||
for camera_to_calibrate in self.cameraList:
|
||||
if camera_to_calibrate.index == REFERENCE_CAMERA_INDEX:
|
||||
continue
|
||||
|
||||
|
||||
stereo_rms,R_ref_to_cam,t_ref_to_cam,E, F, P_ref, P_cam = self._stereo_calibrate_from_charuco_samples(
|
||||
sample_list=self.calibration_samples,
|
||||
board=self.board,
|
||||
camera_refernece=reference_camera,
|
||||
camera_to_calibrate=camera_to_calibrate
|
||||
)
|
||||
|
||||
|
||||
camera_to_calibrate.rotation_matrix_world_to_camera = R_ref_to_cam
|
||||
camera_to_calibrate.translation_vector_world_to_camera = t_ref_to_cam
|
||||
camera_to_calibrate.camera_projection_matrix = P_cam
|
||||
|
||||
self.P_mats[camera_to_calibrate.index] = camera_to_calibrate.camera_projection_matrix
|
||||
|
||||
|
||||
self.logger.log("Extrinsics done")
|
||||
self.logger.log("Amount of P_mats:" + str(len(self.P_mats)))
|
||||
self.logger.log("Amount of cameras:" + str(len(self.cameraList)))
|
||||
self.log_all_camera_calibration(self.cameraList)
|
||||
|
||||
|
||||
def rotation_matrix_to_rpy_deg(self, R: numpy.ndarray):
|
||||
"""
|
||||
Convert rotation matrix to roll, pitch, yaw in degrees.
|
||||
Assumes right-handed, OpenCV convention.
|
||||
"""
|
||||
sy = numpy.sqrt(R[0,0]*R[0,0] + R[1,0]*R[1,0])
|
||||
singular = sy < 1e-6
|
||||
|
||||
if not singular:
|
||||
roll = numpy.arctan2(R[2,1], R[2,2])
|
||||
pitch = numpy.arctan2(-R[2,0], sy)
|
||||
yaw = numpy.arctan2(R[1,0], R[0,0])
|
||||
else:
|
||||
roll = numpy.arctan2(-R[1,2], R[1,1])
|
||||
pitch = numpy.arctan2(-R[2,0], sy)
|
||||
yaw = 0.0
|
||||
|
||||
return numpy.degrees([roll, pitch, yaw])
|
||||
|
||||
def log_all_camera_calibration(self, camera_list):
|
||||
self.logger.log("========== CAMERA CALIBRATION SUMMARY ==========")
|
||||
|
||||
for cam in camera_list:
|
||||
self.logger.log(f"--- Camera {cam.index} ---")
|
||||
|
||||
# -------- Intrinsics --------
|
||||
if cam.camera_matrix is not None:
|
||||
K = cam.camera_matrix
|
||||
fx, fy = K[0,0], K[1,1]
|
||||
cx, cy = K[0,2], K[1,2]
|
||||
|
||||
self.logger.log(
|
||||
f"Intrinsics:"
|
||||
f" fx={fx:.2f}, fy={fy:.2f},"
|
||||
f" cx={cx:.2f}, cy={cy:.2f}"
|
||||
)
|
||||
|
||||
if cam.distortion_coefficients is not None:
|
||||
d = cam.distortion_coefficients.flatten()
|
||||
d_short = ", ".join(f"{v:.4f}" for v in d[:5])
|
||||
self.logger.log(f"Distortion: [{d_short}{'...' if len(d) > 5 else ''}]")
|
||||
else:
|
||||
self.logger.log("Intrinsics: NOT SET")
|
||||
|
||||
# -------- Extrinsics --------
|
||||
if cam.rotation_matrix_world_to_camera is not None and cam.translation_vector_world_to_camera is not None:
|
||||
R = cam.rotation_matrix_world_to_camera
|
||||
t = cam.translation_vector_world_to_camera.reshape(3)
|
||||
|
||||
roll, pitch, yaw = self.rotation_matrix_to_rpy_deg(R)
|
||||
dist = numpy.linalg.norm(t)
|
||||
|
||||
self.logger.log(
|
||||
f"Extrinsics (world → cam):"
|
||||
f" t=({t[0]:.1f}, {t[1]:.1f}, {t[2]:.1f})"
|
||||
f" | |t|={dist:.1f}"
|
||||
)
|
||||
self.logger.log(
|
||||
f"Rotation (deg):"
|
||||
f" roll={roll:.2f}, pitch={pitch:.2f}, yaw={yaw:.2f}"
|
||||
)
|
||||
else:
|
||||
self.logger.log("Extrinsics: NOT SET")
|
||||
|
||||
# -------- Projection --------
|
||||
if cam.camera_projection_matrix is not None:
|
||||
P = cam.camera_projection_matrix
|
||||
self.logger.log(f"Projection matrix: shape={P.shape}")
|
||||
else:
|
||||
self.logger.log("Projection matrix: NOT SET")
|
||||
|
||||
self.logger.log("==============================================")
|
||||
|
||||
def _build_stereo_correspondences_from_samples(
|
||||
self,
|
||||
sample_list,
|
||||
board,
|
||||
camera_index_reference,
|
||||
camera_index_to_calibrate
|
||||
):
|
||||
object_points_per_frame = []
|
||||
frame_points_per_frame_reference = []
|
||||
frame_points_per_frame_to_calibrate = []
|
||||
|
||||
for sample in sample_list:
|
||||
|
||||
# Get detections for both cameras and check if valide
|
||||
detection_reference = sample.detections.get(camera_index_reference)
|
||||
detection_to_calibrate = sample.detections.get(camera_index_to_calibrate)
|
||||
|
||||
if detection_reference is None or detection_to_calibrate is None:
|
||||
continue
|
||||
|
||||
if detection_reference.charuco_ids is None or detection_reference.charuco_corners is None:
|
||||
continue
|
||||
|
||||
if detection_to_calibrate.charuco_ids is None or detection_to_calibrate.charuco_corners is None:
|
||||
continue
|
||||
|
||||
# Find corner IDs that both cameras can see and check if the amount is enough
|
||||
ids_reference = detection_reference.charuco_ids.reshape(-1)
|
||||
ids_to_calibrate = detection_to_calibrate.charuco_ids.reshape(-1)
|
||||
|
||||
common_ids = numpy.intersect1d(ids_reference, ids_to_calibrate)
|
||||
|
||||
if len(common_ids) < MIN_CHARUCO_CORNERS:
|
||||
continue
|
||||
|
||||
# Build ordered correspondences by common_ids
|
||||
# Map id -> corner for each cam
|
||||
map_a = {int(i): detection_reference.charuco_corners[idx] for idx, i in enumerate(ids_reference)}
|
||||
map_b = {int(i): detection_to_calibrate.charuco_corners[idx] for idx, i in enumerate(ids_to_calibrate)}
|
||||
|
||||
# Assemble corners/ids arrays in matching order
|
||||
corners_reference = numpy.array([map_a[int(i)] for i in common_ids], dtype=numpy.float32).reshape(-1, 1, 2)
|
||||
corners_to_calibrate = numpy.array([map_b[int(i)] for i in common_ids], dtype=numpy.float32).reshape(-1, 1, 2)
|
||||
ids_common = common_ids.astype(numpy.int32).reshape(-1, 1)
|
||||
|
||||
# Convert ChArUco corners+ids -> (objectPts, imagePts) for the board
|
||||
object_points_reference, frame_pts_reference = board.matchImagePoints(corners_reference, ids_common)
|
||||
_, frame_pts_to_calibrate = board.matchImagePoints(corners_to_calibrate, ids_common)
|
||||
|
||||
if object_points_reference is None or frame_pts_reference is None or frame_pts_to_calibrate is None:
|
||||
continue
|
||||
|
||||
object_points_reference = self._as_np_float32(object_points_reference)
|
||||
frame_pts_reference = self._as_np_float32(frame_pts_reference)
|
||||
frame_pts_to_calibrate = self._as_np_float32(frame_pts_to_calibrate)
|
||||
|
||||
if len(object_points_reference) < MIN_CHARUCO_CORNERS:
|
||||
continue
|
||||
|
||||
object_points_per_frame.append(object_points_reference)
|
||||
frame_points_per_frame_reference.append(frame_pts_reference)
|
||||
frame_points_per_frame_to_calibrate.append(frame_pts_to_calibrate)
|
||||
|
||||
return object_points_per_frame, frame_points_per_frame_reference, frame_points_per_frame_to_calibrate
|
||||
|
||||
def _as_np_float32(self, x):
|
||||
return numpy.asarray(x, dtype=numpy.float32)
|
||||
|
||||
def _stereo_calibrate_from_charuco_samples(
|
||||
self,
|
||||
sample_list,
|
||||
board,
|
||||
camera_refernece,
|
||||
camera_to_calibrate
|
||||
):
|
||||
|
||||
if camera_refernece.camera_matrix is None or camera_refernece.distortion_coefficients is None:
|
||||
self.logger.log(f"cam{camera_refernece.index} missing intrinsics")
|
||||
if camera_to_calibrate.camera_matrix is None or camera_to_calibrate.distortion_coefficients is None:
|
||||
self.logger.log(f"cam{camera_to_calibrate.index} missing intrinsics")
|
||||
|
||||
object_points_per_frame, frame_points_per_frame_reference, frame_points_per_frame_to_calibrate = self._build_stereo_correspondences_from_samples(
|
||||
sample_list=sample_list,
|
||||
board=board,
|
||||
camera_index_reference=camera_refernece.index,
|
||||
camera_index_to_calibrate=camera_to_calibrate.index
|
||||
)
|
||||
|
||||
frame_width, frame_height = FRAME_SIZE
|
||||
|
||||
# Keep intrinsics fixed (recommended since you already calibrated them)
|
||||
flags = cv2.CALIB_FIX_INTRINSIC
|
||||
|
||||
# Termination criteria for stereo calibration optimizer
|
||||
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-6)
|
||||
|
||||
stereo_rms_reprojection_error_px, camera_matrix_reference, dist_reference, camera_matrix_to_calibrate, dist_to_calibrate, \
|
||||
rotation_matrix_reference_to_calibrate, translation_vector_reference_to_calibrate, \
|
||||
essential_matrix, fundamental_matrix = cv2.stereoCalibrate(
|
||||
objectPoints=object_points_per_frame,
|
||||
imagePoints1=frame_points_per_frame_reference,
|
||||
imagePoints2=frame_points_per_frame_to_calibrate,
|
||||
cameraMatrix1=camera_refernece.camera_matrix,
|
||||
distCoeffs1=camera_refernece.distortion_coefficients,
|
||||
cameraMatrix2=camera_to_calibrate.camera_matrix,
|
||||
distCoeffs2=camera_to_calibrate.distortion_coefficients,
|
||||
imageSize=(frame_width, frame_height),
|
||||
criteria=criteria,
|
||||
flags=flags
|
||||
)
|
||||
|
||||
projection_matrix_camA = camera_refernece.camera_matrix @ numpy.hstack([numpy.eye(3, dtype=numpy.float64), numpy.zeros((3,1), dtype=numpy.float64)])
|
||||
projection_matrix_camB = camera_to_calibrate.camera_matrix @ numpy.hstack([rotation_matrix_reference_to_calibrate, translation_vector_reference_to_calibrate])
|
||||
|
||||
|
||||
|
||||
return (
|
||||
float(stereo_rms_reprojection_error_px),
|
||||
rotation_matrix_reference_to_calibrate,
|
||||
translation_vector_reference_to_calibrate,
|
||||
essential_matrix,
|
||||
fundamental_matrix,
|
||||
projection_matrix_camA,
|
||||
projection_matrix_camB,
|
||||
)
|
||||
|
||||
def _clear_camera_calibrations(self):
|
||||
for camera in self.cameraList:
|
||||
camera.camera_matrix = None
|
||||
camera.distortion_coefficients = None
|
||||
camera.rotation_matrix_world_to_camera = None
|
||||
camera.translation_vector_world_to_camera = None
|
||||
|
||||
def _calibrate_intrinsics_characuo_for_camera(
|
||||
self,
|
||||
sample_list: CalibrationSample,
|
||||
camera_index: int,
|
||||
board
|
||||
):
|
||||
object_points_per_frame = []
|
||||
frame_points_per_frame = []
|
||||
|
||||
for sample in sample_list:
|
||||
detection = sample.detections.get(camera_index)
|
||||
|
||||
if detection is None:
|
||||
continue
|
||||
|
||||
if detection.charuco_ids is None or detection.charuco_corners is None:
|
||||
continue
|
||||
|
||||
if len(detection.charuco_ids) < MIN_CHARUCO_CORNERS:
|
||||
continue
|
||||
|
||||
# map detected 2D corners + ids to corresponding 3D board points
|
||||
object_points, frame_points = board.matchImagePoints(detection.charuco_corners, detection.charuco_ids)
|
||||
|
||||
if object_points is None or frame_points is None:
|
||||
continue
|
||||
|
||||
object_points_per_frame.append(object_points)
|
||||
frame_points_per_frame.append(frame_points)
|
||||
|
||||
|
||||
|
||||
frame_width, frame_height = FRAME_SIZE
|
||||
self.logger.log(f"Calibrating cam{camera_index} with imageSize={FRAME_SIZE} (w,h)")
|
||||
self.logger.log(f"H= {frame_width} W= {frame_height}")
|
||||
|
||||
reprojection_error_rms, camera_matrix, distortion_coefficients, rvecs, tvecs = cv2.calibrateCamera(
|
||||
objectPoints=object_points_per_frame,
|
||||
imagePoints=frame_points_per_frame,
|
||||
imageSize=(frame_width, frame_height),
|
||||
cameraMatrix=None,
|
||||
distCoeffs=None
|
||||
)
|
||||
|
||||
return float(reprojection_error_rms), camera_matrix, distortion_coefficients
|
||||
|
||||
def _caputre_calibration_sample(self):
|
||||
detections = {}
|
||||
|
||||
for camera in self.cameraList: # Loop all Cameras
|
||||
ok, frame = camera.capture.read() #Capture a frame
|
||||
|
||||
if not ok: # Check if captured frame is valid, if even one frame is invalide the whole capture failed
|
||||
self.logger.log(f"Could not grab frame for calibration sample, from camera {camera.index}.")
|
||||
return
|
||||
|
||||
h, w = frame.shape[:2]
|
||||
self.logger.log(f"cam{camera.index} capture size = {w}x{h}")
|
||||
|
||||
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Turn the frame black and white
|
||||
gray = 255 - gray # Invert the color of the frame, as the ChArCuo board I use is inverted (to save printer Ink)
|
||||
|
||||
h, w = gray.shape[:2]
|
||||
self.logger.log(f"cam{camera.index} capture size = {w}x{h}")
|
||||
|
||||
#This is apparently a fast check, bevor i detect the actuall board
|
||||
corner, ids, _ = self.arcuo_detector.detectMarkers(gray) #Detect ChArCuo Markers
|
||||
if ids is None or len(ids) == 0: #Check if Markers where found, if none where found the sample is invalid
|
||||
self.logger.log(f"Not enought markers found for calibration sample, from camera {camera.index}.")
|
||||
return
|
||||
|
||||
# Detecting the Charcuo board and evaluating if its good as a sample
|
||||
result = self.charuco_detector.detectBoard(gray)
|
||||
|
||||
charuco_corners = result[0]
|
||||
charuco_ids = result[1]
|
||||
|
||||
if charuco_ids is None:
|
||||
self.logger.log(f"Not enought markers found for calibration sample, from camera {camera.index}.")
|
||||
return
|
||||
|
||||
if len(charuco_ids) < MIN_CHARUCO_CORNERS:
|
||||
self.logger.log(f"Not enought markers found for calibration sample, from camera {camera.index}.")
|
||||
return
|
||||
|
||||
# Adding the sample for the current camera to detections
|
||||
detections[camera.index] = CharucoDetection(
|
||||
camera_id = camera.index,
|
||||
charuco_corners = charuco_corners,
|
||||
charuco_ids=charuco_ids,
|
||||
gray_frame = gray
|
||||
)
|
||||
|
||||
# All cameras found valide markers so the sample capture was succesfull
|
||||
self.calibration_samples.append(
|
||||
CalibrationSample(detections=detections)
|
||||
)
|
||||
|
||||
self.logger.log("Added calibration sample.")
|
||||
|
||||
def updateCameras(self):
|
||||
self._clearLayout()
|
||||
|
||||
for camera_Object in self.cameraList:
|
||||
camera_Setup_Widget = CameraSetupWidget(camera_Object)
|
||||
|
||||
self.scroll_Layout.addWidget(camera_Setup_Widget)
|
||||
self.camera_Setup_Widget_List.append(camera_Setup_Widget)
|
||||
|
||||
def _clearLayout(self):
|
||||
self.camera_Setup_Widget_List.clear()
|
||||
|
||||
while self.scroll_Layout.count():
|
||||
item = self.scroll_Layout.takeAt(0)
|
||||
widget = item.widget()
|
||||
if widget is not None:
|
||||
widget.setParent(None)
|
||||
widget.deleteLater()
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
import cv2, numpy as np
|
||||
import os
|
||||
|
||||
|
||||
from Tbd.helper import CameraObject
|
||||
from UI.UILogger import UILogger
|
||||
|
||||
from PySide6.QtCore import Qt, QTimer
|
||||
from PySide6.QtGui import QImage, QPixmap
|
||||
from PySide6.QtWidgets import QLineEdit
|
||||
from PySide6.QtGui import QIntValidator
|
||||
from PySide6.QtWidgets import QWidget, QLabel, QVBoxLayout, QPushButton, QHBoxLayout
|
||||
from typing import List
|
||||
|
||||
|
||||
|
||||
|
||||
class SetupPage(QWidget):
|
||||
"""
|
||||
Page that handles camera preview & cycling through cameras.
|
||||
"""
|
||||
|
||||
def __init__(self, cameraList: List[CameraObject], on_cameraList_changed, logger : UILogger):
|
||||
super().__init__()
|
||||
|
||||
self.on_cameraList_changed = on_cameraList_changed
|
||||
self.cameraList = cameraList
|
||||
self.logger = logger
|
||||
self.cameraObject = CameraObject(
|
||||
capture=None,
|
||||
index=None
|
||||
)
|
||||
|
||||
# --- UI ---
|
||||
self.setUpLayout = QVBoxLayout(self)
|
||||
|
||||
self.setUpLayout.addLayout(self._make_Setup_Controls())
|
||||
self.setUpLayout.addLayout(self._make_Setup_Options())
|
||||
|
||||
def _make_Setup_Options(self):
|
||||
setupOptions = QHBoxLayout()
|
||||
setupOptions2 = QVBoxLayout()
|
||||
|
||||
self.cameraIndexLable = QLabel("Camera Index:")
|
||||
self.cameraIndex = QLineEdit("0")
|
||||
self.cameraIndex.setValidator(QIntValidator(0, 9999, self))
|
||||
|
||||
self.addCameraBTN = QPushButton("Add Camera")
|
||||
self.nextCameraBTN = QPushButton("Next Camera")
|
||||
self.previousCameraBTN = QPushButton("Previous Camera")
|
||||
|
||||
setupOptions.addWidget(self.cameraIndexLable)
|
||||
setupOptions.addWidget(self.cameraIndex, 1)
|
||||
setupOptions.addWidget(self.addCameraBTN)
|
||||
setupOptions.addWidget(self.nextCameraBTN)
|
||||
setupOptions.addWidget(self.previousCameraBTN)
|
||||
|
||||
self.nextCameraBTN.clicked.connect(self._nextIndex)
|
||||
self.previousCameraBTN.clicked.connect(self._previousIndex)
|
||||
#self.cameraIndex.editingFinished.connect(self._updateCamera)
|
||||
self.addCameraBTN.clicked.connect(self._addCamera)
|
||||
|
||||
# ---
|
||||
self.camera_preview = QLabel("No camera")
|
||||
self.camera_preview.setAlignment(Qt.AlignCenter)
|
||||
self.camera_preview.setMinimumSize(640, 360)
|
||||
self.camera_preview.setStyleSheet("background: #222; color: #aaa;")
|
||||
|
||||
self.timer = QTimer(self)
|
||||
self.timer.timeout.connect(self._updateCamera)
|
||||
|
||||
setupOptions2.addLayout(setupOptions)
|
||||
setupOptions2.addWidget(self.camera_preview)
|
||||
|
||||
|
||||
return setupOptions2
|
||||
|
||||
def _addCamera(self):
|
||||
|
||||
if any(camera.index == int(self.cameraIndex.text()) for camera in self.cameraList):
|
||||
print("Camera already added.")
|
||||
return
|
||||
|
||||
self.cameraList.append(self.cameraObject)
|
||||
self._cameraList_changed()
|
||||
|
||||
|
||||
def load_setup(self):
|
||||
if (os.path.exists(self.setupFilePath.text())):
|
||||
self.logger.log("Path found")
|
||||
else:
|
||||
self.logger.log("Path not found")
|
||||
|
||||
|
||||
indexToAdd = [0, 3]
|
||||
#self.cameraObject = CameraObject(
|
||||
# capture=None,
|
||||
# index=None
|
||||
#)
|
||||
for index in indexToAdd:
|
||||
capture = cv2.VideoCapture(index, cv2.CAP_MSMF)
|
||||
#capture = cv2.VideoCapture(index, cv2.CAP_DSHOW)
|
||||
|
||||
capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
|
||||
capture.set(cv2.CAP_PROP_FPS, 30)
|
||||
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 2592)
|
||||
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1944)
|
||||
|
||||
#capture.set(cv2.CAP_PROP_FRAME_WIDTH, 2048.0)
|
||||
#capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1536.0)
|
||||
width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
|
||||
height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
|
||||
fps = capture.get(cv2.CAP_PROP_FPS)
|
||||
|
||||
fourcc = int(capture.get(cv2.CAP_PROP_FOURCC))
|
||||
fourcc_str = "".join([chr((fourcc >> 8*i) & 0xFF) for i in range(4)])
|
||||
|
||||
print(f"Resolution: {width}x{height}")
|
||||
print(f"FPS: {fps}")
|
||||
print(f"Format (FOURCC): {fourcc_str}")
|
||||
self.logger.log(f"Resolution: {width}x{height}")
|
||||
self.logger.log(f"FPS: {fps}")
|
||||
self.logger.log(f"Format (FOURCC): {fourcc_str}")
|
||||
#currentcapture = cv2.VideoCapture(index, cv2.CAP_MSMF)
|
||||
|
||||
#self.logger.log("Initial frame:", w, "x", h)
|
||||
self.cameraList.append(CameraObject(
|
||||
capture=capture,
|
||||
index=index
|
||||
))
|
||||
self._cameraList_changed()
|
||||
|
||||
|
||||
return
|
||||
|
||||
|
||||
|
||||
def save_setup(self):
|
||||
|
||||
print("ToDo")
|
||||
return True
|
||||
|
||||
def _cameraList_changed(self):
|
||||
if self.on_cameraList_changed:
|
||||
self.on_cameraList_changed()
|
||||
|
||||
def _updateCamera(self):
|
||||
if self.cameraObject.capture is None:
|
||||
return
|
||||
ret, frame = self.cameraObject.capture.read()
|
||||
if not ret:
|
||||
return
|
||||
|
||||
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||
h, w, ch = frame_rgb.shape
|
||||
bytes_per_line = ch * w
|
||||
qimg = QImage(frame_rgb.data, w, h, bytes_per_line, QImage.Format_RGB888)
|
||||
self.camera_preview.setPixmap(QPixmap.fromImage(qimg))
|
||||
|
||||
def _nextIndex(self):
|
||||
cameraIndex = int(self.cameraIndex.text())
|
||||
self.cameraIndex.setText(str(cameraIndex + 1))
|
||||
|
||||
if self.open_camera():
|
||||
if not self.timer.isActive():
|
||||
self.timer.start(30)
|
||||
|
||||
self._updateCamera()
|
||||
|
||||
def _previousIndex(self):
|
||||
cameraIndex = int(self.cameraIndex.text())
|
||||
|
||||
if((cameraIndex - 1) < 0):
|
||||
self.cameraIndex.setText("0")
|
||||
|
||||
self.cameraIndex.setText(str(cameraIndex - 1))
|
||||
|
||||
if self.open_camera():
|
||||
if not self.timer.isActive():
|
||||
self.timer.start(30)
|
||||
|
||||
self._updateCamera()
|
||||
|
||||
def open_camera(self):
|
||||
index = int(self.cameraIndex.text())
|
||||
|
||||
self.release_camera()
|
||||
self.cameraObject.capture = cv2.VideoCapture(index)
|
||||
self.cameraObject.index = index
|
||||
|
||||
if not self.cameraObject.capture.isOpened():
|
||||
self.cameraObject.capture = None
|
||||
self.cameraObject.index = None
|
||||
self.camera_preview.setText(f"Failed to open camera {index}")
|
||||
return False
|
||||
|
||||
self.camera_preview.setText("")
|
||||
|
||||
return True
|
||||
|
||||
def release_camera(self):
|
||||
if self.cameraObject.capture is not None:
|
||||
self.timer.stop()
|
||||
self.cameraObject.capture.release()
|
||||
self.cameraObject.capture = None
|
||||
self.cameraObject.index = None
|
||||
|
||||
def closeEvent(self, event):
|
||||
self.release_camera()
|
||||
event.accept()
|
||||
|
||||
def _make_Setup_Controls(self):
|
||||
# Create Setup Input
|
||||
setupFileLayout = QHBoxLayout()
|
||||
self.setupFilePathLable = QLabel("Setupfile path:")
|
||||
self.setupFilePath = QLineEdit()
|
||||
self.loadBTN = QPushButton("Load")
|
||||
self.saveBTN = QPushButton("Save")
|
||||
|
||||
setupFileLayout.addWidget(self.setupFilePathLable)
|
||||
setupFileLayout.addWidget(self.setupFilePath, 1)
|
||||
setupFileLayout.addWidget(self.loadBTN)
|
||||
setupFileLayout.addWidget(self.saveBTN)
|
||||
|
||||
self.loadBTN.clicked.connect(self.load_setup)
|
||||
self.saveBTN.clicked.connect(self.save_setup)
|
||||
|
||||
return setupFileLayout
|
||||
@@ -0,0 +1,23 @@
|
||||
from PySide6.QtCore import QObject, Signal, Slot
|
||||
from PySide6.QtWidgets import QPlainTextEdit
|
||||
import winsound
|
||||
|
||||
class UILogger(QObject):
|
||||
append_line = Signal(str)
|
||||
|
||||
def __init__(self, widget: QPlainTextEdit):
|
||||
super().__init__()
|
||||
|
||||
self.widget = widget
|
||||
self.widget.setReadOnly(True)
|
||||
self.widget.setMaximumBlockCount(2000)
|
||||
self.append_line.connect(self._append)
|
||||
|
||||
|
||||
@Slot(str)
|
||||
def _append(self, text: str):
|
||||
self.widget.appendPlainText(text)
|
||||
|
||||
def log(self, text:str):
|
||||
self.append_line.emit(text)
|
||||
winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS | winsound.SND_ASYNC)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user