baseball-engine / tools /setup_camera_up.py
github-actions[bot]
Automated Deploy: e8f9b601dd5464c98dfeaaab4cccd6af68ee2d82
6b95c6c
Raw History Blame Contribute Delete
1.48 kB
"""Build-time official GeoCalib checkpoint acquisition and offline preflight."""
import argparse
import hashlib
import json
from pathlib import Path
import sys
import urllib.request
from baseball_batting.camera_up_provider import REVISION,WEIGHTS_URL,WEIGHTS_SHA256
def main():
p=argparse.ArgumentParser();p.add_argument('--output',required=True);args=p.parse_args()
root=Path(args.output);root.mkdir(parents=True,exist_ok=True);weights=root/'geocalib-pinhole.tar'
if not weights.is_file() or hashlib.sha256(weights.read_bytes()).hexdigest()!=WEIGHTS_SHA256:
temporary=weights.with_suffix('.part')
with urllib.request.urlopen(WEIGHTS_URL,timeout=90) as response,temporary.open('wb') as out:
while block:=response.read(1024*1024):out.write(block)
if hashlib.sha256(temporary.read_bytes()).hexdigest()!=WEIGHTS_SHA256:raise ValueError('GeoCalib checkpoint checksum mismatch')
temporary.replace(weights)
from geocalib import GeoCalib
GeoCalib(weights=str(weights)).eval() # Offline model construction, no video or provider credentials.
(root/'provider.json').write_text(json.dumps({'worker_python':sys.executable,'weights':str(weights.resolve()),
'weights_sha256':WEIGHTS_SHA256,'source_revision':REVISION,'provider':'cvg/GeoCalib',
'weights_license':'CC-BY-4.0; Copyright 2024 ETH Zurich','code_license':'Apache-2.0'},indent=2))
return 0
if __name__=='__main__':raise SystemExit(main())