Repository navigation
Expand file tree
/
Copy pathpipeline.py
More file actions
139 lines (120 loc) · 4.54 KB
/
Copy pathpipeline.py
File metadata and controls
139 lines (120 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Call an OpenAI vision model from an hflow quality-check step.
Run from the repository root with:
OPENAI_API_KEY=... OPENAI_MODEL=gpt-5.4-mini \
uv run --extra openai python examples/openai_vision/pipeline.py [episode.mcap]
"""
import base64
import os
import sys
from pathlib import Path
from openai import OpenAI
import hflow
OPENAI_BASE_URL = os.environ.get("OPENAI_BASE_URL", "https://api.openai.com/v1")
ACTIVITY_PROMPT = (
"Describe the physical activity across this timestamped sequence in one concise sentence. "
"State when the visual evidence is insufficient."
)
HAND_VISIBILITY_PROMPT = (
"This image is a grid of {tile_count} frames sampled from an egocentric recording. "
"In how many of the {tile_count} frames are the operator's hands clearly visible "
"(not missing from view, not occluded by objects)? Answer with a single integer."
)
app = hflow.App(
"openai-vision-example",
data_root="./data/openai-vision",
)
def _image_data_url(image_path: Path) -> str:
encoded_image = base64.b64encode(image_path.read_bytes()).decode("ascii")
return f"data:image/jpeg;base64,{encoded_image}"
@app.check(requires=("vision-model",), version="responses-contact-sheet-v1")
def describe_activity(episode: hflow.Episode) -> hflow.CheckResult:
contact_sheet = hflow.ffmpeg.contact_sheet(
episode.frames(fps=0.5),
episode.workdir / "openai-vision-contact-sheet.jpg",
columns=4,
max_tiles=12,
)
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
base_url=OPENAI_BASE_URL,
)
model_name = os.environ["OPENAI_MODEL"]
response = client.responses.create(
model=model_name,
input=[
{
"role": "user",
"content": [
{"type": "input_text", "text": ACTIVITY_PROMPT},
{
"type": "input_image",
"image_url": _image_data_url(contact_sheet.path),
},
],
}
],
)
return hflow.CheckResult(
measurements={
"activity_description": response.output_text,
"vision_model": model_name,
"sampled_frame_count": len(contact_sheet.tile_log_times_ns),
}
)
@app.check(requires=("vision-model",), version="hand-visibility-contact-sheet-v1")
def hand_visibility(episode: hflow.Episode) -> hflow.CheckResult:
"""Measure hand visibility as a model judgment rather than a signal statistic.
The check samples frames, asks the model, and records the visible fraction
as evidence. The keep/drop threshold stays a curation query.
"""
contact_sheet = hflow.ffmpeg.contact_sheet(
episode.frames(fps=0.5),
episode.workdir / "hand-visibility-contact-sheet.jpg",
columns=4,
max_tiles=12,
)
tile_count = len(contact_sheet.tile_log_times_ns)
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
base_url=OPENAI_BASE_URL,
)
response = client.responses.create(
model=os.environ["OPENAI_MODEL"],
input=[
{
"role": "user",
"content": [
{
"type": "input_text",
"text": HAND_VISIBILITY_PROMPT.format(tile_count=tile_count),
},
{
"type": "input_image",
"image_url": _image_data_url(contact_sheet.path),
},
],
}
],
)
answer_text = response.output_text.strip()
measurements: dict[str, hflow.MeasurementValue] = {
"hands_visible_raw_answer": answer_text,
"hands_sampled_frame_count": tile_count,
}
# An unparsable answer is an unknown, not zero visible hands: record the
# raw text and a tag instead of inventing a fraction.
if answer_text.isdigit():
measurements["hands_visible_fraction"] = min(int(answer_text), tile_count) / tile_count
return hflow.CheckResult(measurements=measurements)
return hflow.CheckResult(measurements=measurements, tags=["hands_visible_unparsed"])
def main() -> None:
if len(sys.argv) > 1:
episode_path = Path(sys.argv[1])
else:
episode_path = Path("./data/openai-vision/sample.mcap")
if not episode_path.is_file():
print(f"synthesizing a sample episode at {episode_path} ...")
hflow.testing.synthesize_episode(episode_path)
app.test(episode_path)
if __name__ == "__main__":
main()