def test_main_bytes(capsysbin): """Test CLI --bytes""" N = 123 # test --delim IN_DATA = '\0'.join(map(str, _range(N))).encode() with closing(BytesIO()) as sys.stdin: sys.stdin.write(IN_DATA) # sys.stdin.write(b'\xff') # TODO sys.stdin.seek(0) main(sys.stderr, [ '--desc', 'Test CLI delim', '--ascii', 'True', '--delim', r'\0', '--buf_size', '64' ]) out, err = capsysbin.readouterr() assert out == IN_DATA assert str(N) + "it" in err.decode("U8") # test --bytes IN_DATA = IN_DATA.replace(b'\0', b'\n') with closing(BytesIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) main(sys.stderr, ['--ascii', '--bytes=True', '--unit_scale', 'False']) out, err = capsysbin.readouterr() assert out == IN_DATA assert str(len(IN_DATA)) + "B" in err.decode("U8")
def test_comppath(): """Test CLI --comppath""" if IS_WIN: skip("no manpages on windows") tmp = mkdtemp() man = path.join(tmp, "tqdm_completion.sh") assert not path.exists(man) try: main(argv=['--comppath', tmp], fp=NULL) except SystemExit: pass else: raise SystemExit("Expected system exit") assert path.exists(man) # check most important options appear with io_open(man, mode='r', encoding='utf-8') as fd: script = fd.read() opts = set([ '--help', '--desc', '--total', '--leave', '--ncols', '--ascii', '--dynamic_ncols', '--position', '--bytes', '--nrows', '--delim', '--manpath', '--comppath' ]) assert all(args in script for args in opts) rmtree(tmp, True)
def test_exceptions(capsysbin): """Test CLI Exceptions""" N = 123 sys.stdin = [str(i) + '\n' for i in _range(N)] IN_DATA = ''.join(sys.stdin).encode() with raises(TqdmKeyError, match="bad_arg_u_ment"): main(sys.stderr, argv=['-ascii', '-unit_scale', '--bad_arg_u_ment', 'foo']) out, _ = capsysbin.readouterr() assert norm(out) == IN_DATA with raises(TqdmTypeError, match="invalid_bool_value"): main(sys.stderr, argv=['-ascii', '-unit_scale', 'invalid_bool_value']) out, _ = capsysbin.readouterr() assert norm(out) == IN_DATA with raises(TqdmTypeError, match="invalid_int_value"): main(sys.stderr, argv=['-ascii', '--total', 'invalid_int_value']) out, _ = capsysbin.readouterr() assert norm(out) == IN_DATA with raises(TqdmKeyError, match="Can only have one of --"): main(sys.stderr, argv=['--update', '--update_to']) out, _ = capsysbin.readouterr() assert norm(out) == IN_DATA # test SystemExits for i in ('-h', '--help', '-v', '--version'): with raises(SystemExit): main(argv=[i])
def test_manpath(tmp_path): """Test CLI --manpath""" man = tmp_path / "tqdm.1" assert not man.exists() with raises(SystemExit): main(argv=['--manpath', str(tmp_path)]) assert man.is_file()
def test_exceptions(): """Test CLI Exceptions""" _SYS = sys.stdin, sys.argv sys.stdin = map(str, _range(123)) sys.argv = ['', '-ascii', '-unit_scale', '--bad_arg_u_ment', 'foo'] try: main(fp=NULL) except TqdmKeyError as e: if 'bad_arg_u_ment' not in str(e): raise else: raise TqdmKeyError('bad_arg_u_ment') sys.argv = ['', '-ascii', '-unit_scale', 'invalid_bool_value'] try: main(fp=NULL) except TqdmTypeError as e: if 'invalid_bool_value' not in str(e): raise else: raise TqdmTypeError('invalid_bool_value') sys.argv = ['', '-ascii', '--total', 'invalid_int_value'] try: main(fp=NULL) except TqdmTypeError as e: if 'invalid_int_value' not in str(e): raise else: raise TqdmTypeError('invalid_int_value') sys.argv = ['', '--update', '--update_to'] try: main(fp=NULL) except TqdmKeyError as e: if 'Can only have one of --' not in str(e): raise else: raise TqdmKeyError('Cannot have both --update --update_to') # test SystemExits for i in ('-h', '--help', '-v', '--version'): sys.argv = ['', i] try: main(fp=NULL) except SystemExit: pass else: raise ValueError('expected SystemExit') # clean up sys.stdin, sys.argv = _SYS
def test_manpath(): """Test CLI --manpath""" tmp = mkdtemp() man = path.join(tmp, "tqdm.1") assert not path.exists(man) try: main(argv=['--manpath', tmp], fp=NULL) except SystemExit: pass else: raise SystemExit("Expected system exit") assert path.exists(man) rmtree(tmp, True)
def test_comppath(tmp_path): """Test CLI --comppath""" man = tmp_path / "tqdm_completion.sh" assert not man.exists() with raises(SystemExit): main(argv=['--comppath', str(tmp_path)]) assert man.is_file() # check most important options appear script = man.read_text() opts = { '--help', '--desc', '--total', '--leave', '--ncols', '--ascii', '--dynamic_ncols', '--position', '--bytes', '--nrows', '--delim', '--manpath', '--comppath' } assert all(args in script for args in opts)
def test_main_log(capsysbin, caplog): """Test CLI --log""" _SYS = sys.stdin, sys.argv N = 123 sys.stdin = [(str(i) + '\n').encode() for i in _range(N)] IN_DATA = b''.join(sys.stdin) try: with caplog.at_level(logging.INFO): main(sys.stderr, ['--log', 'INFO']) out, err = capsysbin.readouterr() assert norm(out) == IN_DATA and b"123/123" in err assert not caplog.record_tuples with caplog.at_level(logging.DEBUG): main(sys.stderr, ['--log', 'DEBUG']) out, err = capsysbin.readouterr() assert norm(out) == IN_DATA and b"123/123" in err assert caplog.record_tuples finally: sys.stdin, sys.argv = _SYS
def test_main(): """Test command line pipes""" ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh(sys.executable, '-c', 'from tqdm.cli import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: assert ls_out in res.replace('\r\n', '\n') # semi-fake test which gets coverage: _SYS = sys.stdin, sys.argv with closing(StringIO()) as sys.stdin: sys.argv = [ '', '--desc', 'Test CLI --delim', '--ascii', 'True', '--delim', r'\0', '--buf_size', '64' ] sys.stdin.write('\0'.join(map(str, _range(int(123))))) # sys.stdin.write(b'\xff') # TODO sys.stdin.seek(0) main() sys.stdin = IN_DATA_LIST sys.argv = [ '', '--desc', 'Test CLI pipes', '--ascii', 'True', '--unit_scale', 'True' ] import tqdm.__main__ # NOQA with closing(StringIO()) as sys.stdin: IN_DATA = '\0'.join(IN_DATA_LIST) sys.stdin.write(IN_DATA) sys.stdin.seek(0) sys.argv = ['', '--ascii', '--bytes=True', '--unit_scale', 'False'] with closing(UnicodeIO()) as fp: main(fp=fp) assert str(len(IN_DATA)) in fp.getvalue() sys.stdin = IN_DATA_LIST # test --log with closing(StringIO()) as sys.stdin: sys.stdin.write('\0'.join(map(str, _range(int(123))))) sys.stdin.seek(0) # with closing(UnicodeIO()) as fp: main(argv=['--log', 'DEBUG'], fp=NULL) # assert "DEBUG:" in sys.stdout.getvalue() sys.stdin = IN_DATA_LIST # clean up sys.stdin, sys.argv = _SYS
def test_exceptions(): """Test CLI Exceptions""" _SYS = sys.stdin, sys.argv sys.stdin = IN_DATA_LIST sys.argv = ['', '-ascii', '-unit_scale', '--bad_arg_u_ment', 'foo'] try: main(fp=NULL) except TqdmKeyError as e: if 'bad_arg_u_ment' not in str(e): raise else: raise TqdmKeyError('bad_arg_u_ment') sys.argv = ['', '-ascii', '-unit_scale', 'invalid_bool_value'] try: main(fp=NULL) except TqdmTypeError as e: if 'invalid_bool_value' not in str(e): raise else: raise TqdmTypeError('invalid_bool_value') sys.argv = ['', '-ascii', '--total', 'invalid_int_value'] try: main(fp=NULL) except TqdmTypeError as e: if 'invalid_int_value' not in str(e): raise else: raise TqdmTypeError('invalid_int_value') # test SystemExits for i in ('-h', '--help', '-v', '--version'): sys.argv = ['', i] try: main(fp=NULL) except SystemExit: pass else: raise ValueError('expected SystemExit') # clean up sys.stdin, sys.argv = _SYS
def test_main(capsysbin): """Test misc CLI options""" N = 123 sys.stdin = [(str(i) + '\n').encode() for i in _range(N)] IN_DATA = b''.join(sys.stdin) # test --tee main(sys.stderr, ['--mininterval', '0', '--miniters', '1']) out, err = capsysbin.readouterr() assert norm(out) == IN_DATA and b"123/123" in err assert N <= len(err.split(b"\r")) < N + 5 len_err = len(err) main(sys.stderr, ['--tee', '--mininterval', '0', '--miniters', '1']) out, err = capsysbin.readouterr() assert norm(out) == IN_DATA and b"123/123" in err # spaces to clear intermediate lines could increase length assert len_err + len(norm(out)) <= len(err) # test --null main(sys.stderr, ['--null']) out, err = capsysbin.readouterr() assert not out and b"123/123" in err # test integer --update main(sys.stderr, ['--update']) out, err = capsysbin.readouterr() assert norm(out) == IN_DATA assert (str(N // 2 * N) + "it").encode() in err, "expected arithmetic sum formula" # test integer --update_to main(sys.stderr, ['--update-to']) out, err = capsysbin.readouterr() assert norm(out) == IN_DATA assert (str(N - 1) + "it").encode() in err assert (str(N) + "it").encode() not in err with closing(BytesIO()) as sys.stdin: sys.stdin.write(IN_DATA.replace(b'\n', b'D')) # test integer --update --delim sys.stdin.seek(0) main(sys.stderr, ['--update', '--delim', 'D']) out, err = capsysbin.readouterr() assert out == IN_DATA.replace(b'\n', b'D') assert (str(N // 2 * N) + "it").encode() in err, "expected arithmetic sum" # test integer --update_to --delim sys.stdin.seek(0) main(sys.stderr, ['--update-to', '--delim', 'D']) out, err = capsysbin.readouterr() assert out == IN_DATA.replace(b'\n', b'D') assert (str(N - 1) + "it").encode() in err assert (str(N) + "it").encode() not in err # test float --update_to sys.stdin = [(str(i / 2.0) + '\n').encode() for i in _range(N)] IN_DATA = b''.join(sys.stdin) main(sys.stderr, ['--update-to']) out, err = capsysbin.readouterr() assert norm(out) == IN_DATA assert (str((N - 1) / 2.0) + "it").encode() in err assert (str(N / 2.0) + "it").encode() not in err
def test_main(): """Test misc CLI options""" _SYS = sys.stdin, sys.argv _STDOUT = sys.stdout N = 123 # test direct import sys.stdin = [str(i).encode() for i in _range(N)] sys.argv = [ '', '--desc', 'Test CLI import', '--ascii', 'True', '--unit_scale', 'True' ] import tqdm.__main__ # NOQA sys.stderr.write("Test misc CLI options ... ") # test --delim IN_DATA = '\0'.join(map(str, _range(N))).encode() with closing(BytesIO()) as sys.stdin: sys.argv = [ '', '--desc', 'Test CLI delim', '--ascii', 'True', '--delim', r'\0', '--buf_size', '64' ] sys.stdin.write(IN_DATA) # sys.stdin.write(b'\xff') # TODO sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(fp=fp) assert str(N) + "it" in fp.getvalue() # test --bytes IN_DATA = IN_DATA.replace(b'\0', b'\n') with closing(BytesIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) sys.argv = ['', '--ascii', '--bytes=True', '--unit_scale', 'False'] with closing(UnicodeIO()) as fp: main(fp=fp) assert str(len(IN_DATA)) in fp.getvalue() # test --log sys.stdin = [str(i).encode() for i in _range(N)] # with closing(UnicodeIO()) as fp: main(argv=['--log', 'DEBUG'], fp=NULL) main(argv=['--log=DEBUG'], fp=NULL) # assert "DEBUG:" in sys.stdout.getvalue() try: # test --tee IN_DATA = IN_DATA.decode() with closing(StringIO()) as sys.stdout: with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--mininterval', '0', '--miniters', '1'], fp=fp) res = len(fp.getvalue()) # assert len(fp.getvalue()) < len(sys.stdout.getvalue()) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=[ '--tee', '--mininterval', '0', '--miniters', '1' ], fp=fp) # spaces to clear intermediate lines could increase length assert len(fp.getvalue()) >= res + len(IN_DATA) # test --null with closing(StringIO()) as sys.stdout: with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--null'], fp=fp) assert not sys.stdout.getvalue() with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=[], fp=fp) assert sys.stdout.getvalue() finally: sys.stdout = _STDOUT # test integer --update IN_DATA = IN_DATA.encode() with closing(BytesIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--update'], fp=fp) res = fp.getvalue() assert str(N // 2 * N) + "it" in res # arithmetic sum formula # test integer --update --delim with closing(BytesIO()) as sys.stdin: sys.stdin.write(IN_DATA.replace(b'\n', b'D')) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--update', '--delim', 'D'], fp=fp) res = fp.getvalue() assert str(N // 2 * N) + "it" in res # arithmetic sum formula # test integer --update_to with closing(BytesIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--update-to'], fp=fp) res = fp.getvalue() assert str(N - 1) + "it" in res assert str(N) + "it" not in res # test integer --update_to --delim with closing(BytesIO()) as sys.stdin: sys.stdin.write(IN_DATA.replace(b'\n', b'D')) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--update-to', '--delim', 'D'], fp=fp) res = fp.getvalue() assert str(N - 1) + "it" in res assert str(N) + "it" not in res # test float --update_to IN_DATA = '\n'.join((str(i / 2.0) for i in _range(N))).encode() with closing(BytesIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--update-to'], fp=fp) res = fp.getvalue() assert str((N - 1) / 2.0) + "it" in res assert str(N / 2.0) + "it" not in res # clean up sys.stdin, sys.argv = _SYS
from tqdm import cli try: cli.main() except: pass
# -*- coding: utf-8 -*- import re import sys from tqdm.cli import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit(main())
args = parser.parse_args() current_dir = os.getcwd() image_lst = list(list_images(current_dir)) img_lst = [ name for name in image_lst if os.path.basename(name).startswith('img') ] id_lst = [ name for name in image_lst if os.path.basename(name).startswith('id') ] for name in tqdm(img_lst): mask_name = name.replace("img", "id").replace(".jpg", ".png") print(mask_name) mask_img = cv2.imread(mask_name, 0) _, mask_img = cv2.threshold(mask_img, 10, 255, cv2.THRESH_BINARY) xmin, xmax, ymin, ymax = get_annotation_from_mask(mask_img) image_file_name = os.path.basename(name) img_folder_name = os.path.dirname(name) img_size = mask_img.shape bndbox = (xmin, ymin, xmax, ymax) label = args.label write_xml(image_file_name, img_folder_name, img_size, bndbox, label) if __name__ == '__main__': main()