import tempfile
import shutil
import os
from unittest import TestCase, mock
from utils.voc.converter import process_cvat_xml
XML_ANNOTATION_EXAMPLE = """
1.01063My annotation task75annotation02018-06-06 11:57:54.807162+03:002018-06-06 12:42:29.375251+03:003086074http://cvat.examle.com:8080/?id=3086admin2018-06-06 15:47:04.386866+03:00falseatrueafalsebfalsectruea
"""
XML_INTERPOLATION_EXAMPLE = """
1.01062My interpolation task30084interpolation202018-05-31 14:13:36.483219+03:002018-06-06 13:56:32.113705+03:003085030083http://cvat.example.com:8080/?id=3085admin10247682018-06-06 15:52:11.138470+03:00
"""
class TestProcessCvatXml(TestCase):
def setUp(self):
self.test_dir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.test_dir)
@mock.patch('utils.voc.converter.log')
def test_parse_annotation_xml(self, mock_log):
xml_filename = os.path.join(self.test_dir, 'annotations.xml')
with open(xml_filename, mode='x') as file:
file.write(XML_ANNOTATION_EXAMPLE)
voc_dir = os.path.join(self.test_dir, 'voc_dir')
images = ['C15_L1_0001', 'C15_L1_0002', 'C15_L1_0003', 'C15_L1_0040']
expected_xmls = [os.path.join(voc_dir, x + '.xml')
for x in images]
process_cvat_xml(xml_filename, 'img_dir', voc_dir)
for exp in expected_xmls:
self.assertTrue(os.path.exists(exp))
# We should add in some code to parse the resulting xml files
@mock.patch('utils.voc.converter.log')
def test_parse_interpolation_xml(self, mock_log):
xml_filename = os.path.join(self.test_dir, 'interpolations.xml')
with open(xml_filename, mode='x') as file:
file.write(XML_INTERPOLATION_EXAMPLE)
voc_dir = os.path.join(self.test_dir, 'voc_dir')
frames = [0, 1, 2, 110, 111, 112 ]
expected_xmls = [os.path.join(voc_dir, 'interpolations_%08d.xml' % x )
for x in frames]
process_cvat_xml(xml_filename, 'img_dir', voc_dir)
self.assertTrue(os.path.exists(voc_dir))
self.assertTrue(len(os.listdir(voc_dir)) == len(frames))
for exp in expected_xmls:
self.assertTrue(os.path.exists(exp))
# We should add in some code to parse the resulting xml files