This commit is contained in:
DuOtto
2025-11-04 21:40:06 +01:00
parent 1d63671fad
commit 225ecc2e17
16 changed files with 244 additions and 109 deletions
-75
View File
@@ -45,81 +45,6 @@ def find_hud_placement(frame_vis):
return arrow_tip, dir_vec
# ——————————————————————————————————————————————
# PDF loading / rasterization at import time
# ——————————————————————————————————————————————
PDF_PATH = "DarkAngles.pdf" # change this to your file
PDF_DPI = 600 # controls resolution of rasterization
_doc = fitz.open(PDF_PATH)
_pdf_pages = []
for page in _doc:
# render page to pixmap at desired zoom
zoom = PDF_DPI / 72.0
mat = fitz.Matrix(zoom, zoom)
pix = page.get_pixmap(matrix=mat, alpha=False)
# convert pixmap to ndarray
img = np.frombuffer(pix.samples, dtype=np.uint8)
img = img.reshape(pix.height, pix.width, pix.n)
if pix.n == 4:
img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR)
_pdf_pages.append(img)
_doc.close()
# ——————————————————————————————————————————————
# HUD drawing routines
# ——————————————————————————————————————————————
# rotated rectangle → 4 pts
def _rect_to_pts(center, size, angle_rad):
cx, cy = center
w, h = size
# local corners
pts = np.array([
[-w/2, -h/2],
[ w/2, -h/2],
[ w/2, h/2],
[-w/2, h/2],
])
# rotation
c, s = np.cos(angle_rad), np.sin(angle_rad)
R = np.array([[c, -s],[s, c]])
pts = pts.dot(R.T)
pts += np.array([cx, cy])
return pts.astype(np.float32)
def draw_hud_box(frame, tip, angle_rad, size=(120, 60), color=(255,0,255), thickness=2):
"""Draw a rotated HUD rectangle at `tip` pointing along `angle_rad`."""
# compute box corners
pts = _rect_to_pts(center=tip, size=size, angle_rad=angle_rad)
cv2.drawContours(frame, [pts.astype(int)], -1, color, thickness)
# label
cv2.putText(frame, "HUD", (int(tip[0]+5), int(tip[1]+5)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
return pts # return the quad for PDF warping
def draw_pdf_page(frame, page_index, dst_quad):
"""
Warp PDF page image #page_index into the quadrilateral dst_quad.
dst_quad: 4×2 float32 array of destination corners in clock-wise order.
"""
if page_index < 0 or page_index >= len(_pdf_pages):
return
src = _pdf_pages[page_index]
h, w = src.shape[:2]
# source corners (tl, tr, br, bl)
src_quad = np.array([[0,0], [w,0], [w,h], [0,h]], dtype=np.float32)
# compute homography
M = cv2.getPerspectiveTransform(src_quad, dst_quad)
# warp PDF page into place (transparent where outside)
warp = cv2.warpPerspective(src, M, (frame.shape[1], frame.shape[0]))
mask = cv2.warpPerspective(np.ones((h,w), dtype=np.uint8)*255, M, (frame.shape[1], frame.shape[0]))
# composite onto frame
inv = cv2.bitwise_not(mask)
bg = cv2.bitwise_and(frame, frame, mask=inv)
fg = cv2.bitwise_and(warp, warp, mask=mask)
np.copyto(frame, bg+fg)
# hud.py
class HUD:
def __init__(self, marker_id, pdf_pages, default_page=0):