import json
from jsonschema import validate
count_schema = {
"type": "object",
"properties": {
"helmet": {"type": "integer", "minimum": 0},
"vest": {"type": "integer", "minimum": 0},
},
"required": ["helmet", "vest"],
"additionalProperties": False,
}
response = client.chat.completions.create(
model="perceptron-mk1.5",
messages=[{
"role": "user",
"content": [
image(image_url),
{"type": "text", "text": (
"Count visible helmets and safety vests, counting each physical item "
"once. Count the items, not the people. Return zero for a category "
"with no visible instances. Return the requested JSON counts."
)},
],
}],
reasoning_effort="high",
max_completion_tokens=1024,
response_format={
"type": "json_schema",
"json_schema": {"name": "ppe_counts", "strict": True, "schema": count_schema},
},
)
choice = response.choices[0]
if choice.finish_reason != "stop" or choice.message.tool_calls:
raise RuntimeError("The count response did not complete")
counts = json.loads(choice.message.content or "")
validate(instance=counts, schema=count_schema)
print(counts)