1 Star 0 Fork 0

liuguoxionglang/TextRecognitionDataGenerator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
tests.py 35.53 KB
一键复制 编辑 原始数据 按行查看 历史
Edouard Belval 提交于 2020-04-24 20:54 . Add Arabic and Hindi
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
import os
import sys
import unittest
import subprocess
import hashlib
import string
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "./trdg")))
try:
os.mkdir("tests/out")
except:
pass
from diffimg import diff
from trdg.data_generator import FakeTextDataGenerator
from trdg import background_generator
from trdg.generators import (
GeneratorFromDict,
GeneratorFromRandom,
GeneratorFromStrings,
GeneratorFromWikipedia,
)
from trdg.string_generator import (
create_strings_from_file,
create_strings_from_dict,
create_strings_from_wikipedia,
create_strings_randomly,
)
def empty_directory(path):
for f in os.listdir(path):
os.remove(os.path.join(path, f))
class Generators(unittest.TestCase):
def test_generator_from_dict(self):
generator = GeneratorFromDict()
i = 0
while i < 100:
img, lbl = next(generator)
self.assertTrue(img.size[1] == 32, "Shape is not right")
i += 1
def test_generator_from_random(self):
generator = GeneratorFromRandom()
i = 0
while i < 100:
img, lbl = next(generator)
self.assertTrue(img.size[1] == 32, "Shape is not right")
i += 1
def test_generator_from_strings(self):
generator = GeneratorFromStrings(["TEST TEST TEST"])
i = 0
while i < 100:
img, lbl = next(generator)
self.assertTrue(img.size[1] == 32, "Shape is not right")
i += 1
def test_generator_from_wikipedia(self):
generator = GeneratorFromWikipedia()
i = 0
while i < 100:
img, lbl = next(generator)
self.assertTrue(img.size[1] == 32, "Shape is not right")
i += 1
def test_generator_from_dict_stops(self):
generator = GeneratorFromDict(count=1)
next(generator)
self.assertRaises(StopIteration, generator.next)
def test_generator_from_random_stops(self):
generator = GeneratorFromRandom(count=1)
next(generator)
self.assertRaises(StopIteration, generator.next)
def test_generator_from_strings_stops(self):
generator = GeneratorFromStrings(["TEST TEST TEST"], count=1)
next(generator)
self.assertRaises(StopIteration, generator.next)
def test_generator_from_wikipedia_stops(self):
generator = GeneratorFromWikipedia(count=1)
next(generator)
self.assertRaises(StopIteration, generator.next)
class DataGenerator(unittest.TestCase):
def test_create_string_from_wikipedia(self):
"""
Test that the function returns different output if called twice.
(And that it doesn't throw of course)
"""
strings = create_strings_from_wikipedia(20, 2, "en")
self.assertTrue(
len(strings) == 2
and strings[0] != strings[1]
and len(strings[0].split(" ")) >= 20
and len(strings[1].split(" ")) >= 20
)
def test_create_string_from_file(self):
strings = create_strings_from_file("tests/test.txt", 6)
self.assertTrue(
len(strings) == 6 and strings[0] != strings[1] and strings[0] == strings[3]
)
def test_create_strings_from_dict(self):
strings = create_strings_from_dict(
3, False, 2, ["TEST", "TEST", "TEST", "TEST"]
)
self.assertTrue(len(strings) == 2 and len(strings[0].split(" ")) == 3)
def test_generate_data_with_format(self):
FakeTextDataGenerator.generate(
0,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
0,
False,
0,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_0.jpg",
"tests/expected_results/TEST TEST TEST_0.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_0.jpg")
def test_generate_data_with_extension(self):
FakeTextDataGenerator.generate(
1,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
32,
"png",
0,
False,
0,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_1.png",
"tests/expected_results/TEST TEST TEST_1.png",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_1.png")
def test_generate_data_with_skew_angle(self):
FakeTextDataGenerator.generate(
2,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
15,
False,
0,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_2.jpg",
"tests/expected_results/TEST TEST TEST_2.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_2.jpg")
def test_generate_data_with_blur(self):
FakeTextDataGenerator.generate(
3,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
0,
False,
3,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_3.jpg",
"tests/expected_results/TEST TEST TEST_3.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_3.jpg")
def test_generate_data_with_sine_distorsion(self):
FakeTextDataGenerator.generate(
4,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
0,
False,
3,
False,
1,
1,
2,
False,
0,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_4.jpg",
"tests/expected_results/TEST TEST TEST_4.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_4.jpg")
def test_generate_data_with_cosine_distorsion(self):
FakeTextDataGenerator.generate(
5,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
0,
False,
3,
False,
1,
2,
2,
False,
0,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_5.jpg",
"tests/expected_results/TEST TEST TEST_5.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_5.jpg")
def test_generate_data_with_left_alignment(self):
FakeTextDataGenerator.generate(
6,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
0,
False,
0,
False,
1,
0,
0,
False,
0,
600,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_6.jpg",
"tests/expected_results/TEST TEST TEST_6.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_6.jpg")
def test_generate_data_with_center_alignment(self):
FakeTextDataGenerator.generate(
7,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
0,
False,
0,
False,
1,
0,
0,
False,
0,
800,
1,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_7.jpg",
"tests/expected_results/TEST TEST TEST_7.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_7.jpg")
def test_generate_data_with_right_alignment(self):
FakeTextDataGenerator.generate(
8,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
0,
False,
0,
False,
1,
0,
0,
False,
0,
1000,
2,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_8.jpg",
"tests/expected_results/TEST TEST TEST_8.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_8.jpg")
def test_raise_if_handwritten_and_vertical(self):
try:
FakeTextDataGenerator.generate(
9,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
0,
False,
0,
False,
1,
0,
0,
True,
0,
1000,
2,
"#010101",
1,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
raise Exception("Vertical handwritten did not throw")
except ValueError:
pass
def test_generate_vertical_text(self):
FakeTextDataGenerator.generate(
10,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
32,
"jpg",
0,
False,
0,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
1,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_10.jpg",
"tests/expected_results/TEST TEST TEST_10.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_10.jpg")
def test_generate_horizontal_text_with_variable_space(self):
FakeTextDataGenerator.generate(
11,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
32,
"jpg",
0,
False,
0,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
0,
4,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_11.jpg",
"tests/expected_results/TEST TEST TEST_11.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_11.jpg")
def test_generate_vertical_text_with_variable_space(self):
FakeTextDataGenerator.generate(
12,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
32,
"jpg",
0,
False,
0,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
1,
2,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_12.jpg",
"tests/expected_results/TEST TEST TEST_12.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_12.jpg")
def test_generate_text_with_unknown_orientation(self):
try:
FakeTextDataGenerator.generate(
12,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
32,
"jpg",
0,
False,
0,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
100,
2,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
raise Exception("Unknown orientation did not throw")
except ValueError:
pass
def test_generate_data_with_fit(self):
FakeTextDataGenerator.generate(
13,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
0,
False,
0,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
0,
1,
0,
(0, 0, 0, 0),
1,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_13.jpg",
"tests/expected_results/TEST TEST TEST_13.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_13.jpg")
def test_generate_data_with_word_split(self):
FakeTextDataGenerator.generate(
14,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"png",
0,
False,
0,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
True,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_14.png",
"tests/expected_results/TEST TEST TEST_14.png",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_14.png")
def test_generate_data_with_first_name_format(self):
FakeTextDataGenerator.generate(
15,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"png",
0,
False,
0,
False,
1,
0,
0,
False,
0,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
True,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_15.png",
"tests/expected_results/TEST TEST TEST_15.png",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_15.png")
def test_generate_data_with_second_name_format(self):
FakeTextDataGenerator.generate(
16,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"png",
0,
False,
0,
False,
1,
0,
0,
False,
1,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
True,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/16_TEST TEST TEST.png",
"tests/expected_results/16_TEST TEST TEST.png",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/16_TEST TEST TEST.png")
def test_generate_data_with_third_name_format(self):
FakeTextDataGenerator.generate(
17,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"png",
0,
False,
0,
False,
1,
0,
0,
False,
2,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
True,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/17.png",
"tests/expected_results/17.png",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/17.png")
def test_generate_data_with_wrong_name_format(self):
FakeTextDataGenerator.generate(
18,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"png",
0,
False,
0,
False,
1,
0,
0,
False,
3,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
True,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/TEST TEST TEST_18.png",
"tests/expected_results/TEST TEST TEST_18.png",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/TEST TEST TEST_18.png")
def test_generate_data_with_quasicrystal_background_from_generate(self):
FakeTextDataGenerator.generate(
19,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"png",
0,
False,
0,
False,
2,
0,
0,
False,
0,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
True,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
os.remove("tests/out/TEST TEST TEST_19.png")
def test_raise_if_invalid_orientation(self):
try:
FakeTextDataGenerator.generate(
20,
"TEST TEST TEST",
"tests/font.ttf",
"tests/out/",
64,
"jpg",
0,
False,
0,
False,
1,
0,
0,
False,
0,
1000,
2,
"#010101",
3,
1,
0,
(5, 5, 5, 5),
0,
0,
False,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
raise Exception("Invalid orientation did not throw")
except ValueError:
pass
def test_generate_data_with_arabic_text(self):
FakeTextDataGenerator.generate(
21,
"اختبار اختبار اختبار",
"tests/font_ar.ttf",
"tests/out/",
64,
"png",
0,
False,
0,
False,
1,
0,
0,
False,
1,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
True,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/21_اختبار اختبار اختبار.png",
"tests/expected_results/21_اختبار اختبار اختبار.png",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/21_اختبار اختبار اختبار.png")
def test_generate_data_with_hindi_text(self):
FakeTextDataGenerator.generate(
22,
"परीक्षा परीक्षा परीक्षा",
"tests/font_hi.ttf",
"tests/out/",
64,
"png",
0,
False,
0,
False,
1,
0,
0,
False,
1,
-1,
0,
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
0,
True,
os.path.join(os.path.split(os.path.realpath(__file__))[0], "trdg/images"),
)
self.assertTrue(
diff(
"tests/out/22_परीक्षा परीक्षा परीक्षा.png",
"tests/expected_results/22_परीक्षा परीक्षा परीक्षा.png",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/22_परीक्षा परीक्षा परीक्षा.png")
def test_generate_string_with_letters(self):
s = create_strings_randomly(1, False, 1, True, False, False, "en")[0]
self.assertTrue(all([l in string.ascii_letters for l in s]))
def test_generate_string_with_numbers(self):
s = create_strings_randomly(1, False, 1, False, True, False, "en")[0]
self.assertTrue(all([l in "0123456789" for l in s]))
def test_generate_string_with_symbols(self):
s = create_strings_randomly(1, False, 1, False, False, True, "en")[0]
self.assertTrue(all([l in "!\"#$%&'()*+,-./:;?@[\\]^_`{|}~" for l in s]))
def test_generate_chinese_string(self):
s = create_strings_randomly(1, False, 1, True, False, False, "cn")[0]
cn_chars = [chr(i) for i in range(19968, 40908)]
self.assertTrue(all([l in cn_chars for l in s]))
def test_generate_data_with_white_background(self):
background_generator.plain_white(64, 128).convert("RGB").save(
"tests/out/white_background.jpg"
)
self.assertTrue(
diff(
"tests/out/white_background.jpg",
"tests/expected_results/white_background.jpg",
delete_diff_file=True,
)
< 0.01
)
os.remove("tests/out/white_background.jpg")
def test_generate_data_with_quasicrystal_background(self):
bkgd = background_generator.quasicrystal(64, 128)
self.assertTrue(len(bkgd.histogram()) > 20 and bkgd.size == (128, 64))
class CommandLineInterface(unittest.TestCase):
def test_output_dir(self):
args = ["python3", "run.py", "-c", "1", "--output_dir", "../tests/out_2/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out_2/")) == 1)
empty_directory("tests/out_2/")
def test_language_english(self):
args = [
"python3",
"run.py",
"-l",
"en",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")
def test_language_french(self):
args = [
"python3",
"run.py",
"-l",
"fr",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")
def test_language_spanish(self):
args = [
"python3",
"run.py",
"-l",
"es",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")
def test_language_german(self):
args = [
"python3",
"run.py",
"-l",
"de",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")
def test_language_chinese(self):
args = [
"python3",
"run.py",
"-l",
"cn",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")
def test_count_parameter(self):
args = ["python3", "run.py", "-c", "10", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 10)
empty_directory("tests/out/")
def test_random_sequences_letter_only(self):
args = [
"python3",
"run.py",
"-rs",
"-let",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(
all(
[
c in string.ascii_letters
for f in os.listdir("tests/out/")
for c in f.split("_")[0]
]
)
)
empty_directory("tests/out/")
def test_random_sequences_number_only(self):
args = [
"python3",
"run.py",
"-rs",
"-num",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(
all(
[
c in "0123456789"
for f in os.listdir("tests/out/")
for c in f.split("_")[0]
]
)
)
empty_directory("tests/out/")
def test_random_sequences_symbols_only(self):
args = [
"python3",
"run.py",
"-rs",
"-sym",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
with open("tests/out/labels.txt", "r") as f:
self.assertTrue(
all(
[
c in "!\"#$%&'()*+,-./:;?@[\\]^_`{|}~"
for c in f.readline().split(" ")[1][:-1]
]
)
)
empty_directory("tests/out/")
def test_handwritten(self):
args = ["python3", "run.py", "-c", "1", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")
def test_personalfont(self):
args = [
"python3",
"run.py",
"--font",
"fonts/latin/Aller_Bd.ttf",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")
def test_personalfont_unlocated(self):
args = [
"python3",
"run.py",
"--font",
"fonts/latin/unlocatedFont.ttf",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 0)
empty_directory("tests/out/")
def test_personalfont_directory(self):
args = [
"python3",
"run.py",
"--font_dir",
"fonts/latin/",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")
def test_personalfont_directory_unlocated(self):
args = [
"python3",
"run.py",
"--font_dir",
"fonts/void/",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 0)
empty_directory("tests/out/")
def test_personaldict(self):
args = [
"python3",
"run.py",
"--dict",
"dicts/en.txt",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")
def test_personaldict_unlocated(self):
args = [
"python3",
"run.py",
"--dict",
"dicts/unlocatedDict.txt",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 0)
empty_directory("tests/out/")
def test_first_name_format(self):
args = [
"python3",
"run.py",
"--dict",
"dicts/unlocatedDict.txt",
"-c",
"1",
"--output_dir",
"../tests/out/",
]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 0)
empty_directory("tests/out/")
# def test_word_count(self):
# args = ['python3', 'run.py', '-c', '1', '-w', '5']
# subprocess.Popen(args, cwd="trdg/").wait()
# self.assertTrue(False)
# empty_directory('tests/out/')
#
# def test_extension_jpg(self):
# args = ['python3', 'run.py', '-c', '1', '-e', 'jpg']
# subprocess.Popen(args, cwd="trdg/").wait()
# self.assertTrue(False)
# empty_directory('tests/out/')
#
# def test_extension_png(self):
# args = ['python3', 'run.py', '-c', '1', '-e', 'png']
# subprocess.Popen(args, cwd="trdg/").wait()
# self.assertTrue(False)
# empty_directory('tests/out/')
#
# def test_name_format_0(self):
# args = ['python3', 'run.py', '-c', '1', '-na', '0']
# subprocess.Popen(args, cwd="trdg/").wait()
# self.assertTrue(False)
# empty_directory('tests/out/')
#
# def test_name_format_1(self):
# args = ['python3', 'run.py', '-c', '1', '-na', '1']
# subprocess.Popen(args, cwd="trdg/").wait()
# self.assertTrue(False)
# empty_directory('tests/out/')
#
# def test_name_format_2(self):
# args = ['python3', 'run.py', '-c', '1', '-na', '2']
# subprocess.Popen(args, cwd="trdg/").wait()
# self.assertTrue(False)
# empty_directory('tests/out/')
if __name__ == "__main__":
unittest.main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lgx_com_cn/TextRecognitionDataGenerator.git
git@gitee.com:lgx_com_cn/TextRecognitionDataGenerator.git
lgx_com_cn
TextRecognitionDataGenerator
TextRecognitionDataGenerator
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385