One source: the whole screen (a game, a desktop) with the person composited into a corner. It is how OBS records and how any stream VOD looks, and it is the case screencast_layout got wrong: there the speaker band is a large crop taken AROUND the face, which on a full-screen source means more screen. On the measured Excel clip the output showed the same spreadsheet twice, whole and enlarged. What is needed is the inset's own rectangle, and finding it has three traps, each of which cost an iteration: - The inset does not touch two edges. YOLO returns an upper-body box that stops at the chest, so a webcam pinned to the right side measured 18% clear of the bottom and the "corner" filter threw it away. The test that works is subject SMALL and OFF-CENTRE. - A presenter standing in front of a screen passes any geometric filter. What separates them is that a composited inset DOES NOT MOVE: 3-11px between samples versus 316px for a person. With a plain min-max range the filter was too brittle (one lost sample killed three good detections), so it clusters around the median and requires a majority. - The height cannot be scaled from the detection: for a webcam of about 325x180 YOLO returned 246x86, and growing that shape still cut the head off. It is derived from the width assuming 16:9 and pinned to the edge the inset touches. The filtergraph keeps the screen at full width (the HUD and the edges are the content) and the camera band takes its height from the inset's real aspect: forcing a fixed ratio stretched faces 12% horizontally. Verified on 10 clips: finds the 4 with a composited camera and rejects the 6 without one (presenter in front of a screen, talking heads, presenter composited full-screen). The miss is a 720p clip whose webcam is too small for either detector.
120 lines
4.8 KiB
Python
120 lines
4.8 KiB
Python
from camera_inset import (
|
|
INSET_ASPECT,
|
|
inset_box,
|
|
inset_filtergraph,
|
|
is_cornered,
|
|
nearest_corner,
|
|
usable,
|
|
)
|
|
|
|
HD = (1920, 1080)
|
|
|
|
|
|
class TestNearestCorner:
|
|
def test_reads_the_quadrant(self):
|
|
assert nearest_corner([10, 10, 100, 100], *HD) == ("left", "top")
|
|
assert nearest_corner([1800, 980, 100, 100], *HD) == ("right", "bottom")
|
|
assert nearest_corner([1800, 10, 100, 100], *HD) == ("right", "top")
|
|
assert nearest_corner([10, 980, 100, 100], *HD) == ("left", "bottom")
|
|
|
|
|
|
class TestIsCornered:
|
|
def test_small_subject_against_an_edge_qualifies(self):
|
|
# A webcam pinned to the bottom-right: the exact case YOLO reports as a
|
|
# short, wide upper-body box.
|
|
assert is_cornered([1500, 900, 350, 120], *HD)
|
|
|
|
def test_a_presenter_filling_the_shot_is_rejected(self):
|
|
assert not is_cornered([600, 100, 700, 900], *HD)
|
|
|
|
def test_a_small_but_centred_subject_is_rejected(self):
|
|
assert not is_cornered([900, 500, 150, 100], *HD)
|
|
|
|
def test_box_not_touching_the_bottom_still_counts(self):
|
|
# Measured failure: an inset pinned to the right edge sat 18% clear of
|
|
# the bottom because the detector stopped at the chest. Requiring two
|
|
# edges threw it away.
|
|
assert is_cornered([991 * 1.5, 502 * 1.5, 246 * 1.5, 86 * 1.5], *HD)
|
|
|
|
def test_a_tall_subject_is_never_an_inset(self):
|
|
assert not is_cornered([1700, 100, 200, 800], *HD)
|
|
|
|
|
|
class TestInsetBox:
|
|
def test_box_stays_inside_the_frame(self):
|
|
for box in ([0, 980, 200, 100], [1800, 0, 120, 120],
|
|
[1850, 1000, 70, 80]):
|
|
x, y, w, h = inset_box(box, *HD)
|
|
assert 0 <= x and x + w <= 1920
|
|
assert 0 <= y and y + h <= 1080
|
|
|
|
def test_bottom_anchored_inset_reaches_the_bottom_edge(self):
|
|
# The head sits above the detection, so the box must extend upward
|
|
# while staying pinned to the edge the inset actually touches.
|
|
x, y, w, h = inset_box([1500, 950, 300, 100], *HD)
|
|
assert y + h == 1080
|
|
assert y < 950
|
|
|
|
def test_height_follows_the_16_9_assumption(self):
|
|
# A wide, flat torso detection must still yield a webcam-shaped box.
|
|
_x, _y, w, h = inset_box([1000, 800, 300, 80], *HD)
|
|
assert h >= w / INSET_ASPECT - 1
|
|
|
|
def test_box_grows_around_the_subject(self):
|
|
x, y, w, h = inset_box([1600, 900, 200, 120], *HD)
|
|
assert w >= 200 and h >= 120
|
|
|
|
|
|
class TestUsable:
|
|
def test_rejects_a_speck(self):
|
|
assert not usable((0, 0, 40, 20), 1080)
|
|
|
|
def test_rejects_something_that_fills_the_frame(self):
|
|
assert not usable((0, 0, 1920, 900), 1080)
|
|
|
|
def test_accepts_a_normal_webcam(self):
|
|
assert usable((1500, 850, 350, 200), 1080)
|
|
|
|
|
|
class TestInsetFiltergraph:
|
|
def test_graph_places_screen_and_camera(self):
|
|
graph = inset_filtergraph(1920, 1080, 1080, 1920, (1500, 880, 350, 200))
|
|
assert "split=3" in graph
|
|
assert "[screen]" in graph and "[cam]" in graph
|
|
assert graph.endswith("[v]")
|
|
|
|
def test_screen_band_is_never_cropped(self):
|
|
# The whole point: a game HUD or a desktop keeps its edges.
|
|
graph = inset_filtergraph(1920, 1080, 1080, 1920, (1500, 880, 350, 200))
|
|
screen = graph.split("[sa]")[-1].split("[screen]")[0]
|
|
assert "crop" not in screen
|
|
assert "scale=1080:608" in screen
|
|
|
|
def test_camera_band_keeps_the_inset_aspect(self):
|
|
# A 16:9 inset must not be stretched into a wider band; that shows up
|
|
# instantly on a face.
|
|
graph = inset_filtergraph(1920, 1080, 1080, 1920, (1500, 880, 356, 200))
|
|
cam = graph.split("[ca]")[-1].split("[cam]")[0]
|
|
crop_w = int(cam.split("w=")[1].split(":")[0])
|
|
crop_h = int(cam.split("h=")[1].split(":")[0])
|
|
band_h = int(cam.split("scale=1080:")[1].split("[")[0])
|
|
assert abs((crop_w / crop_h) - (1080 / band_h)) < 0.05
|
|
|
|
def test_camera_band_is_capped(self):
|
|
# A near-square inset would otherwise claim most of the frame.
|
|
graph = inset_filtergraph(1920, 1080, 1080, 1920, (1500, 700, 300, 290))
|
|
band_h = int(graph.split("[cam]")[0].split("scale=1080:")[-1])
|
|
assert band_h <= 1920 * 0.40 + 2
|
|
|
|
def test_crop_coordinates_stay_inside_the_source(self):
|
|
graph = inset_filtergraph(1920, 1080, 1080, 1920, (1850, 1000, 70, 80))
|
|
cam = graph.split("[ca]")[-1].split("[cam]")[0]
|
|
x = int(cam.split("x=")[1].split(":")[0])
|
|
w = int(cam.split("w=")[1].split(":")[0])
|
|
assert x >= 0 and x + w <= 1920
|
|
|
|
def test_every_dimension_is_even(self):
|
|
graph = inset_filtergraph(1920, 1080, 1080, 1920, (1501, 881, 351, 201))
|
|
cam = graph.split("[ca]")[-1].split("[cam]")[0]
|
|
for key in ("w=", "h=", "x=", "y="):
|
|
assert int(cam.split(key)[1].split(":")[0].split(",")[0]) % 2 == 0
|