supper/scale_config.py

45 lines
1.6 KiB
Python

#!/usr/bin/env python3
import argparse
import json
from typing import Tuple
def parse_resolution(resolution: str) -> Tuple[int, int]:
a, b = resolution.split('x')
return int(a), int(b)
def scale_x_y(x, y, from_resolution, to_resolution):
return (x * to_resolution[0] / from_resolution[0], y * to_resolution[1] / from_resolution[1])
def scale_region(region, from_resolution, to_resolution):
x, y = scale_x_y(region['x'], region['y'], from_resolution, to_resolution)
width, height = scale_x_y(region['width'], region['height'], from_resolution, to_resolution)
region['x'] = round(x)
region['y'] = round(y)
region['width'] = round(width)
region['height'] = round(height)
if '_time' in region['name'] and to_resolution[1] < 1440:
region['threshold'] = 0.88
def main():
argparser = argparse.ArgumentParser()
argparser.add_argument("--from_res", help="From resolution", default="2560x1440")
argparser.add_argument("--to_res", help="To resolution (e.g. 1920x1080)")
argparser.add_argument("--config", help="Config file", default="config.json")
args = argparser.parse_args()
from_resolution = parse_resolution(args.from_res)
to_resolution = parse_resolution(args.to_res)
config = json.load(open(args.config, 'r'))
for region in config['ocr_regions']:
scale_region(region, from_resolution, to_resolution)
scale_region(config['track_region'], from_resolution, to_resolution)
scale_region(config['penalty_orange_region'], from_resolution, to_resolution)
print(json.dumps(config, indent=4))
if __name__ == '__main__':
main()