"""只读原生音画验收图;证据图与原片、原声分开保存。""" import math def evidence_images(output, images, observations, speech, tail): import cv2 from PIL import Image, ImageDraw, ImageFont font = ImageFont.load_default() rows = {row["frame"]: row for row in observations} selected = sorted(images) columns, width, height = 4, 320, 330 sheet = Image.new( "RGB", (columns * width, 34 + math.ceil(len(selected) / columns) * height), "white", ) draw = ImageDraw.Draw(sheet) draw.text( (12, 10), "Read-only QC evidence full frame / face 24fps", fill="black", font=font, ) for order, index in enumerate(selected): frame, crop = images[index] x, y = order % columns * width, 34 + order // columns * height full = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) full.thumbnail((308, 96)) face = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)) face.thumbnail((196, 196)) sheet.paste(full, (x + 6 + (308 - full.width) // 2, y + 4)) sheet.paste(face, (x + (width - face.width) // 2, y + 104)) row = rows[index] draw.text( (x + 8, y + 301), f"f={index} t={index / 24:.3f}s", fill="black", font=font ) aperture, rms = row["mouth_aperture"], row["audio_rms"] draw.text( (x + 8, y + 315), f"mouth={aperture if aperture is None else round(aperture, 4)} RMS={rms if rms is None else round(rms, 4)}", fill="black", font=font, ) sheet.save(output / "native-av-evidence.png") graph = Image.new("RGB", (1280, 470), "white") draw = ImageDraw.Draw(graph) draw.text( (15, 12), "Actual word ranges / mouth aperture / audio RMS original samples unchanged", fill="black", font=font, ) left, right, total = 90, 1240, max(1, len(observations)) x = lambda value: left + value / total * (right - left) if tail: draw.rectangle((x(tail[0]), 45, x(tail[1]), 420), fill="#eef6ec") for word in speech["words"]: draw.rectangle( (x(word["start_sample"] / 2000), 50, x(word["end_sample"] / 2000), 76), fill="#b9cfe8", ) draw.text((8, 55), "ASR words", fill="black", font=font) for key, top, bottom, colour, label in ( ("mouth_aperture", 100, 245, "#a33535", "mouth"), ("audio_rms", 275, 420, "#285b96", "RMS"), ): values = [row[key] for row in observations if row[key] is not None] scale = max(max(values, default=0), 0.16 if key == "mouth_aperture" else 0.01) draw.line((left, top, left, bottom, right, bottom), fill="#666666") draw.text((8, top), f"{label}\nmax {scale:.4f}", fill="black", font=font) previous = None for row in observations: value = row[key] point = ( (x(row["frame"]), bottom - value / scale * (bottom - top)) if value is not None else None ) if point is not None and previous is not None: draw.line((*previous, *point), fill=colour, width=2) previous = point for second in range(math.ceil(total / 24)): draw.text((x(second * 24), 432), f"{second}s", fill="black", font=font) draw.text( (left, 454), "Green: after final ASR word + 250ms guard; RMS includes ambience and is not a speech classifier.", fill="black", font=font, ) graph.save(output / "native-av-mouth-timeline.png")