hkust-aaron's picture
Physician review recorded; updated labels metadata and showcase videos
b91b0f5 verified
Raw History Blame Contribute Delete
13.4 kB
"""Layer 3: clip-level captions merged with the automatic quality and motion columns.
Usage: python 05_clip_captions.py <per_second_timeline.csv> <clip_motion.csv> <out_jsonl> <out_csv>
Clips are consecutive 250-frame (10 s) windows of the pack video; the last one
is 120 frames. Captions were written by Claude (AI) from one frame every 2 s
(five frames per clip), so they describe what is visible at that sampling rate
and can miss events shorter than 2 s. They name no procedure, anatomy,
pathology or device model. Reviewed and approved by the contributing physician (2026-11-17).
Instrument codes: W white_insulated_jawed, R dark_rod, F fenestrated_grasper,
U unclassified thin jawed instrument.
Flags: g gauze visible, h haze, n instrument shaft close to the lens,
b pooled blood, e field free of instruments for most of the clip.
"""
import csv
import json
import sys
FPS, CLIP, TOTAL = 25, 250, 17370
NAMES = {"W": "white_insulated_jawed", "R": "dark_rod", "F": "fenestrated_grasper", "U": "unclassified_jawed"}
FLAGS = {"g": "gauze_visible", "h": "haze", "n": "near_lens_instrument", "b": "pooled_blood",
"e": "mostly_instrument_free"}
C = [
("FR", "", "Wide view of a smooth pink rounded surface carrying a strip of pale adherent material, with yellow fatty tissue below. A fenestrated grasper and a dark rod enter from the bottom and probe the fatty tissue under the pale strip."),
("FR", "", "The camera moves closer to a dark bleeding spot beneath the pale strip. The grasper and the rod lift and shift the yellow fatty tissue beside it."),
("FR", "g", "The grasper holds tissue next to the bleeding spot while the rod pushes fat aside. The view pulls back to show the pale strip again, and the edge of a gauze appears at the lower left."),
("FR", "g", "Close view of a dark red bleeding area. The fenestrated jaw approaches and touches tissue at its edge while the rod holds the neighbouring tissue; gauze stays at the lower left."),
("FR", "gn", "The bleeding area sits above small yellow lobules. The grasper's dark shaft passes close to the lens and the rod rests on the tissue; gauze at the lower left."),
("FR", "g", "A fresh white gauze with a blue marker thread is carried in from the left by the grasper and pressed toward the bleeding area. The rod stays upright in the centre."),
("FR", "g", "The white gauze fills the left half of the frame. The rod presses on it and holds it in place; there is little other movement."),
("WR", "g", "A jawed instrument with white insulation enters from the lower left and works with the rod at a dark area just below the pale material. Blood-stained gauze lies under its shaft."),
("WRU", "g", "Thin jawed instruments at the left edge reposition while the rod stays central. The white-insulated jaws open near the dark area; yellow lobules are visible below."),
("WR", "gn", "The view moves from the lower field with stained gauze up to the pale material. The white-insulated instrument grasps next to it and stretched translucent strands come into view."),
("WR", "", "Dark area below the pale material. The white-insulated jaws and the rod tip work at a small yellow-brown crusted spot."),
("WR", "gn", "Lower field. The white-insulated instrument passes over the stained gauze toward the fatty lobules, close to the lens."),
("WR", "g", "Brief return to the dark area below the pale material, then back to the lower field with the gauze. Both instruments stay in view."),
("WR", "gn", "Lower field. The white-insulated shaft crosses the gauze close to the lens and its jaws reach the edge of a yellow lobule, with the rod beside it."),
("WR", "g", "The white-insulated jaws and the rod work at the base of an elongated yellow lobule with a dark purplish end. Gauze at the left."),
("WR", "ge", "The lobule with the purplish end lies beside a broad yellow fold. Few instruments are visible in the first half; the white-insulated jaws return at the end."),
("WR", "gb", "The white-insulated jaws open and close on tissue at the dark end of the lobule. The camera then drops to smooth pink tissue and a small pool of blood."),
("WR", "g", "The white-insulated jaws grasp and spread thin tissue layers next to the lobule while the rod holds tissue on the right."),
("WR", "gn", "The white-insulated jaws open repeatedly on tissue under the lobule. Gauze at the upper left."),
("WR", "gb", "Bleeding increases around the working site. The white-insulated instrument moves over reddened tissue; gauze at the left."),
("WR", "gn", "The view swings from the lobules to a smooth pale surface with diffuse oozing. An instrument shaft passes close to the lens."),
("WR", "n", "The white-insulated jaws grasp the edge of a tissue flap against the pale oozing surface, with the rod beside them."),
("WR", "gb", "Pale surface with oozing and a growing pool of blood. The instruments briefly work at greyish darkened tissue, then gauze appears at the left."),
("WR", "gb", "Pooled blood covers the centre of the frame and the gauze at the left edge soaks it up. The rod dips into the pool; the clip ends with a return to the lobule."),
("WR", "", "Pale strip at the top with oozing below. The rod and the white-insulated jaws handle greyish darkened tissue, then the view returns to the lobules."),
("WR", "", "The lobule with the dark end is centred. The white-insulated jaws open below it; movement is slight."),
("WR", "g", "Quiet view of the lobule. The rod and the small white-insulated jaws make minor adjustments at its base; gauze at the upper left."),
("WR", "g", "The gauze is picked up and re-placed with the white-insulated instrument. The camera then jumps to a bleeding surface."),
("WR", "n", "Close view of an oozing surface. The tip of the white-insulated instrument touches several points on it."),
("WR", "gn", "A shaft passes close to the lens over the gauze. The white-insulated jaws work near darkened tissue, then at the lobule with the dark end."),
("WR", "b", "The white-insulated jaws are applied to tissue at the base of small lobules with the rod alongside. Dark blood is pooled at the left."),
("WR", "g", "The lobule with the dark end and the gauze are in view. The rod stays central and the white-insulated instrument returns large in frame at the end."),
("WR", "n", "The white-insulated jaws open on tissue and grasp it. The instrument then moves to the upper left with the rod upright in the centre."),
("WR", "gb", "Pooled blood and soaked gauze fill the frame. The white-insulated jaws then grasp tissue at the lobule with the rod beside them."),
("WR", "g", "The white-insulated jaws grasp a pale strand and then open under the lobule. The rod and the gauze stay in view."),
("WR", "b", "The white-insulated jaws and the rod work under the lobule. Dark blood is pooled at the left."),
("WR", "gb", "Nearly static view. The white-insulated jaws rest against tissue at the base of a rounded yellow lobule and the rod stands to the right. Dark blood and soaked gauze fill the lower left."),
("WR", "gb", "Nearly static view of the same site. The white-insulated jaws make small adjustments at the base of the lobule."),
("WR", "gb", "Same site. A streak of fresh blood appears on the tissue to the right of the jaws."),
("WR", "gb", "Same site, with the lobule larger in frame. The jaws shift lower onto dark purplish tissue."),
("WR", "gb", "The white-insulated jaws open and close on the purplish tissue. The tip area darkens at the end of the clip."),
("WR", "gh", "Dark charred debris is visible on the jaws. Haze drifts through the view and the camera swings to the left."),
("WR", "h", "The white-insulated tip touches an oozing spot on the pale surface in light haze. The view then moves up to the pale material and a dark crusted patch."),
("WR", "", "The white-insulated jaws grasp the pale adherent material and pull it off the smooth surface while the rod assists. Strands of the material stretch."),
("WR", "hn", "The field is free of instruments at first. The white-insulated jaws then grasp the pale material again and the rod passes close to the lens in light haze."),
("WRU", "", "The rod tip touches the loosened pale material. A jawed instrument at the lower left holds a piece of it; part of the material has been removed."),
("", "e", "Static view with no instruments. The smooth surface shows the remaining pale material and a dark crusted area below it."),
("WR", "n", "The white-insulated instrument enters at the lower left beside a dark crusted area and a lobule. Its tip then touches an oozing spot and a dark red clot-like area close to the lens."),
("WR", "h", "Nearly static. The white-insulated tip is held against grey crusted tissue with the rod below it; light haze."),
("WRF", "g", "Open metal fenestrated jaws appear at the lower left. The white-insulated jaws and the rod then work on purplish tissue; gauze appears at the end."),
("WR", "gn", "A shaft fills much of the frame close to the lens above soaked gauze. The white-insulated jaws then grasp purplish tissue beside a lobule."),
("WR", "gn", "The white-insulated jaws hold and pull purplish tissue with the rod beside them. Soaked gauze lies below; the shaft blurs close to the lens at the end."),
("R", "ge", "Quiet view of a yellow lobule with purplish tissue and soaked gauze. Only the rod is seen, briefly and blurred."),
("WR", "gh", "The white-insulated jaws return in light haze and grasp purplish tissue while the rod holds the right side."),
("WR", "g", "Torn tissue with small yellow fragments. The white-insulated instrument works from the upper left and the rod from the right."),
("WR", "g", "A lobule with ragged purplish tissue attached. The rod holds it and the white-insulated jaws approach from the lower right."),
("WR", "gn", "A bright shaft fills the frame close to the lens above the gauze. The white-insulated jaws then open under the lobule."),
("WR", "gn", "A shaft crosses the upper left over the gauze. The rod then works alone at ragged tissue on the lobule."),
("WR", "n", "The rod holds the lobule. The white-insulated instrument returns close to the lens."),
("WR", "hn", "The white-insulated jaws carry tissue in light haze. A shaft passes close to the lens with the rod beside ragged tissue."),
("WR", "g", "The white-insulated jaws grasp a pale rounded piece of tissue with the rod at the right. Small yellow fragments and strands are visible above the gauze."),
("WR", "", "The rod is large in frame beside yellow fragments. After a moment without instruments, the white-insulated jaws return at the lower right."),
("WR", "g", "Steady view. The white-insulated jaws work in the groove between a lobule and the wall, where yellow nodular fragments lie; the rod is at the right."),
("WR", "", "The rod stands in the centre beside the fragments. The white-insulated jaws work next to it and their tip looks dark at the end."),
("WRF", "g", "The rod works at crusted tissue and passes over the gauze. Metal fenestrated jaws appear blurred at the left at the end."),
("WR", "b", "The white-insulated jaws rest on a lobule, then work with the rod at the edge of a cavity holding blood."),
("WR", "", "The white-insulated jaws are small at the bottom of the frame beside a lobule. The rod enters from the right."),
("WR", "g", "The white-insulated instrument and the rod move around a gauze with a blue thread. Parts of the clip are blurred by motion."),
("WR", "", "The white-insulated jaws and the rod work at the bottom of the frame. One blurred moment; the tip shows a red marking at the end."),
("WR", "g", "Last seconds before the camera leaves. The rod, the white-insulated jaws, lobules and a gauze with a blue thread are in view."),
]
assert len(C) == 70
sec = list(csv.DictReader(open(sys.argv[1])))
motion = {int(r["clip_id"]): r for r in csv.DictReader(open(sys.argv[2]))}
rows = []
for cid, (ins, flags, cap) in enumerate(C):
a, b = cid * CLIP, min(TOTAL, (cid + 1) * CLIP)
s = [r for r in sec if a <= int(r["first_frame"]) < b and r["scene_state"] == "intracorporeal"]
rows.append({
"clip_id": cid, "video": "video/laparoscopic_sample_001_intracorporeal.mp4",
"start_frame": a, "end_frame_exclusive": b, "start_s": a / FPS, "end_s": b / FPS,
"num_frames": b - a, "caption": cap,
"instruments": [NAMES[c] for c in ins],
**{v: (k in flags) for k, v in FLAGS.items()},
"motion_level_auto": motion[cid]["motion_level"],
"blur_seconds_auto": sum(int(r["blur"]) for r in s),
"glare_seconds_auto": sum(int(r["glare"]) for r in s),
"median_sharpness_auto": round(sorted(float(r["median_sharpness"]) for r in s)[len(s) // 2], 1),
"caption_source": "AI draft from 5 frames per clip; reviewed and approved by the contributing physician on 2026-11-17",
})
with open(sys.argv[3], "w") as f:
for r in rows:
f.write(json.dumps(r, ensure_ascii=False) + "\n")
with open(sys.argv[4], "w", newline="") as f:
w = csv.DictWriter(f, fieldnames=list(rows[0]))
w.writeheader()
for r in rows:
w.writerow({**r, "instruments": "|".join(r["instruments"])})
words = [len(r["caption"].split()) for r in rows]
print("clips", len(rows), "frames", sum(r["num_frames"] for r in rows), "caption words min/mean/max",
min(words), round(sum(words) / len(words), 1), max(words))
for k in FLAGS.values():
print(k, sum(r[k] for r in rows))