def _recognize_status(img: Image) -> Tuple[int, Text]: cv_img = imagetools.cv_image(imagetools.resize(img.convert("L"), height=64)) cv_img = imagetools.level( cv_img, np.percentile(cv_img, 5), np.percentile(cv_img, 95) ) cv_img = cv2.copyMakeBorder(cv_img, 4, 4, 4, 4, cv2.BORDER_CONSTANT, value=(255,)) blurred_img = cv2.medianBlur(cv_img, 7) text_img = cv2.adaptiveThreshold( blurred_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, -1 ) text_img = 255 - cast.instance( np.maximum(text_img, imagetools.border_flood_fill(text_img)), np.ndarray ) text_img = cv2.medianBlur(text_img, 5) h = cv_img.shape[0] imagetools.fill_area( text_img, (0,), mode=cv2.RETR_LIST, size_lt=round(h * 0.2 ** 2) ) if os.getenv("DEBUG") == __name__: cv2.imshow("cv_img", cv_img) cv2.imshow("blurred_img", blurred_img) cv2.imshow("text_img", text_img) cv2.waitKey() cv2.destroyAllWindows() text = ocr.text(imagetools.pil_image(text_img)) for i in Context.ALL_STATUSES: if i[1] == text: return i raise ValueError("_recognize_status: unknown status: %s" % text)
def latest() -> Text: # Use `requests` if we have more http related feature resp = cast.instance( urllib.request.urlopen(_VERSION_URL), http.client.HTTPResponse, ) return cast.text(resp.read())
def reload(): g.plugins.clear() for i in Path(g.path).glob("*.py"): spec = importlib.util.spec_from_file_location(i.stem, i) assert spec module = importlib.util.module_from_spec(spec) loader = cast.instance(spec.loader, SourceFileLoader) loader.exec_module(module) LOGGER.debug("loaded: %s", ", ".join(g.plugins.keys()))
def distance(a: Tuple[int, ...], b: Tuple[int, ...]) -> float: assert len(a) == len( b), f"length must be same, got len(a)={len(a)} len(b)={len(b)}" return cast.instance( np.sqrt(np.sum((np.array(a) - np.array(b))**2, axis=0)), float)
def test_match_tuple(): obj1 = Class1() assert cast.instance(obj1, (Class1, Class2)) is obj1
def test_not_match(): obj1 = Class1() cast.instance.__name__ with pytest.raises(cast.CastError): cast.instance(obj1, Class2)
def test_match(): obj1 = Class1() assert cast.instance(obj1, Class1) is obj1