Files
Table-Python/hud.py
T
DuOtto 225ecc2e17 Idk
2025-11-04 21:40:06 +01:00

61 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import cv2, numpy as np
from config import LOWER_SHAPE, UPPER_SHAPE
import cv2
import numpy as np
import time
import fitz # PyMuPDF for PDF rendering
def find_hud_placement(frame_vis):
# ----- Arrow Shape Detection & HUD Placement -----
# 1) Build a clean orange mask
hsv = cv2.cvtColor(frame_vis, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, LOWER_SHAPE, UPPER_SHAPE)
kern = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kern)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kern)
# 2) Find and filter contours
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
area = cv2.contourArea(cnt)
if area < 1000:
continue
# Approximate to polygon and require exactly 5 corners
peri = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)
if len(approx) != 5:
continue
pts = approx.reshape(-1,2)
centroid = np.mean(pts, axis=0)
# Find the arrow tip as the corner farthest from centroid
dists = [np.linalg.norm(pt - centroid) for pt in pts]
tip_pt = pts[int(np.argmax(dists))]
arrow_tip = (int(tip_pt[0]), int(tip_pt[1]))
# Place the HUD box in the arrows pointing direction
dir_vec = tip_pt - centroid
#ToDo remove
cv2.drawContours(frame_vis, [pts], -1, (0,255,255), 2)
return arrow_tip, dir_vec
# hud.py
class HUD:
def __init__(self, marker_id, pdf_pages, default_page=0):
self.id = marker_id
self.page_index = default_page
self.pdf_pages = pdf_pages # list of preloaded page images
self.ref_quad = None # the 4-corner quad where to draw the PDF
self.last_seen = 0 # for timeout if arrow goes away
def update(self, marker):
return
def draw(self, frame):
return