import re
import sys

import ddddocr


def main() -> int:
    image = sys.stdin.buffer.read()
    if not image:
        print("captcha image is empty", file=sys.stderr)
        return 2
    result = ddddocr.DdddOcr(show_ad=False).classification(image)
    code = re.sub(r"[^0-9A-Za-z]", "", result).upper()
    if not re.fullmatch(r"[0-9A-Z]{4}", code):
        print(f"invalid OCR result: {code!r}", file=sys.stderr)
        return 3
    print(code)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
