import tempfile
import shutil
import os
from unittest import TestCase, mock
from utils.voc.converter import process_cvat_xml
XML_ANNOTATION_EXAMPLE = """
1.0
1063
My annotation task
75
annotation
0
2018-06-06 11:57:54.807162+03:00
2018-06-06 12:42:29.375251+03:00
3086
0
74
http://cvat.examle.com:8080/?id=3086
admin
2018-06-06 15:47:04.386866+03:00
false
a
true
a
false
b
false
c
true
a
"""
XML_INTERPOLATION_EXAMPLE = """
1.0
1062
My interpolation task
30084
interpolation
20
2018-05-31 14:13:36.483219+03:00
2018-06-06 13:56:32.113705+03:00
3085
0
30083
http://cvat.example.com:8080/?id=3085
admin
2018-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))
@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')
expected_warn = 'Cannot parse interpolation tracks, ignoring 2 tracks'
process_cvat_xml(xml_filename, 'img_dir', voc_dir)
self.assertTrue(os.path.exists(voc_dir))
self.assertTrue(len(os.listdir(voc_dir)) == 0)
mock_log.warn.assert_called_once_with(expected_warn)