本文整理匯總了Python中inselect.lib.document.InselectDocument類的典型用法代碼示例。如果您正苦於以下問題:Python InselectDocument類的具體用法?Python InselectDocument怎麽用?Python InselectDocument使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了InselectDocument類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_load_images
def test_load_images(self):
"Load document's images"
source = TESTDATA / 'shapes.inselect'
with temp_directory_with_files(TESTDATA / 'shapes.inselect') as tempdir:
doc_temp = tempdir / 'shapes.inselect'
with doc_temp.open('w') as outfile, source.open() as infile:
outfile.write(infile.read())
# Document load with neither scanned image file nor thumbnail
self.assertRaises(InselectError, InselectDocument.load, doc_temp)
# Document load with thumbnail but no scanned image file
thumbnail_temp = tempdir / 'shapes_thumbnail.jpg'
thumbnail_temp.touch() # File only needs to exist
doc = InselectDocument.load(doc_temp)
self.assertFalse(doc.scanned.available)
self.assertTrue(doc.thumbnail.available)
# Document load with both scanned and thumbnail files
scanned_temp = tempdir / 'shapes.png'
scanned_temp.touch() # File only needs to exist
actual = InselectDocument.load(doc_temp)
self.assertEqual(InselectDocument.load(source).items, actual.items)
self.assertTrue(actual.scanned.available)
self.assertTrue(actual.thumbnail.available)
# Document load with scanned image file but not thumbnail
os.unlink(str(thumbnail_temp))
actual = InselectDocument.load(doc_temp)
self.assertEqual(InselectDocument.load(source).items, actual.items)
self.assertTrue(actual.scanned.available)
self.assertFalse(actual.thumbnail.available)
示例2: test_path_is_thumbnail_file
def test_path_is_thumbnail_file(self):
with temp_directory_with_files() as tempdir:
thumbnail = tempdir / 'xx_thumbnail.jpg'
thumbnail.open('w') # File only needs to exist
# Thumbnail file exists but there is no corresponding .inselect doc
self.assertFalse(InselectDocument.path_is_thumbnail_file(thumbnail))
doc = tempdir / 'xx.inselect'
doc.open('w') # File only needs to exist
# Thumbnail file and corresponding .inselect file both exist
self.assertTrue(InselectDocument.path_is_thumbnail_file(thumbnail))
示例3: test_load
def test_load(self):
"Load a document from a file"
path = TESTDATA / 'test_segment.inselect'
doc = InselectDocument.load(path)
# Properties are as expected
self.assertEqual(doc.document_path, path)
self.assertEqual(5, len(doc.items))
self.assertEqual(5, doc.n_items)
self.assertEqual(doc.scanned.path, path.with_suffix('.png'))
self.assertTrue(doc.thumbnail is None)
self.assertEqual(TESTDATA / 'test_segment_crops', doc.crops_dir)
self.assertEqual('Lawrence Hudson', doc.properties['Created by'])
self.assertEqual("2015-03-14T09:19:47",
doc.properties['Created on'].strftime('%Y-%m-%dT%H:%M:%S'))
self.assertEqual('Lawrence Hudson', doc.properties['Saved by'])
self.assertEqual("2015-03-14T09:19:47",
doc.properties['Saved on'].strftime('%Y-%m-%dT%H:%M:%S'))
# Check read-only properties
with self.assertRaises(AttributeError):
doc.document_path = ''
with self.assertRaises(AttributeError):
doc.items = []
with self.assertRaises(AttributeError):
doc.crops_dir = ''
with self.assertRaises(AttributeError):
doc.n_items = 1
示例4: test_cancel_save_crops
def test_cancel_save_crops(self):
"User cancels save crops"
with temp_directory_with_files(TESTDATA / 'shapes.inselect',
TESTDATA / 'shapes.png') as tempdir:
doc = InselectDocument.load(tempdir / 'shapes.inselect')
# Create crops dir with some data
doc.crops_dir.mkdir()
with doc.crops_dir.joinpath('a_file').open('w') as outfile:
outfile.write('Some data\n')
class CancelExport(Exception):
pass
def progress(msg):
"A progress function that cancels the export"
raise CancelExport()
self.assertRaises(
CancelExport,
DocumentExport(self.TEMPLATE).save_crops, doc, progress=progress
)
# Nothing should have changed within tempdir
self.assertEqual(
['shapes.inselect', 'shapes.png', doc.crops_dir.name],
sorted(p.name for p in tempdir.iterdir()))
self.assertEqual(
['a_file'],
[p.name for p in doc.crops_dir.iterdir()]
)
示例5: save_crops
def save_crops(dir, overwrite_existing, template):
dir = Path(dir)
export = DocumentExport(UserTemplate.load(template) if template else DWC)
for p in dir.glob('*' + InselectDocument.EXTENSION):
try:
debug_print('Loading [{0}]'.format(p))
doc = InselectDocument.load(p)
validation = export.validation_problems(doc)
if validation.any_problems:
print(
u'Not saving crops for [{0}] because there are validation '
u'problems'.format(p)
)
for msg in format_validation_problems(validation):
print(msg)
elif not overwrite_existing and doc.crops_dir.is_dir():
print(u'Crops dir [{0}] exists - skipping'.format(doc.crops_dir))
else:
print(u'Will save crops for [{0}] to [{1}]'.format(p, doc.crops_dir))
debug_print(u'Loading full-resolution scanned image')
doc.scanned.array
debug_print(u'Saving crops')
export.save_crops(doc)
except Exception:
print(u'Error saving crops from [{0}]'.format(p))
traceback.print_exc()
示例6: test_ingest
def test_ingest(self):
"PNG image is ingested and document is created"
inbox_img = self.inbox / 'x.png'
docs_img = self.docs / 'x.png'
shutil.copy(str(TESTDATA / 'shapes.png'), str(inbox_img))
# Read the image for comparison test
original_image = cv2.imread(str(inbox_img))
main([str(self.inbox), str(self.docs)])
# Document, scan and thumbnail should all exists
self.assertTrue((self.docs / 'x.inselect').is_file())
self.assertTrue(docs_img.is_file())
self.assertTrue((self.docs / 'x_thumbnail.jpg').is_file())
# Scan should have been removed from inbox
self.assertFalse(inbox_img.is_file())
# Scan is as expected?
doc = InselectDocument.load(self.docs / 'x.inselect')
self.assertTrue(np.all(original_image == doc.scanned.array))
self.assertTrue(doc.thumbnail.available)
self.assertEqual(4096, doc.thumbnail.array.shape[1])
示例7: test_order_by_columns
def test_order_by_columns(self):
doc = InselectDocument.load(TESTDATA / 'shapes.inselect')
items = sort_document_items(doc.items, by_columns=True)
self.assertEqual(
['1', '4', '3', '2', '5'],
[item['fields']['catalogNumber'] for item in items]
)
示例8: test_export_csv
def test_export_csv(self):
"Export metadata to CSV"
with temp_directory_with_files(TESTDATA / 'shapes.inselect',
TESTDATA / 'shapes.png') as tempdir:
# Create an empty CSV file
csv = tempdir / 'shapes.csv'
with csv.open('w'):
pass
main([str(tempdir), '--overwrite'])
csv = tempdir / 'shapes.csv'
self.assertTrue(csv.is_file())
# Check CSV contents
doc = InselectDocument.load(tempdir / 'shapes.inselect')
with csv.open('rb') as f:
res = unicodecsv.DictReader(f, encoding='utf-8')
for index, item, row in zip(count(), doc.items, res):
expected = item['fields']
expected.update({
'ItemNumber': str(1+index),
'Cropped_image_name': '{0:04}.jpg'.format(1+index)
})
actual = {
k: v for k, v in row.items()
if v and k not in BOUNDING_BOX_FIELD_NAMES
}
self.assertEqual(expected, actual)
示例9: test_segment_document_sort_by_rows
def test_segment_document_sort_by_rows(self):
"Segment the document with boxes sorted by rows"
doc = InselectDocument.load(TESTDATA / 'shapes.inselect')
expected = doc.scanned.from_normalised(
[i['rect'] for i in doc.items]
)
self._segment(doc, False, expected)
示例10: export_csv
def export_csv(dir, overwrite_existing, template):
dir = Path(dir)
export = DocumentExport(UserTemplate.load(template) if template else DWC)
for p in dir.glob('*' + InselectDocument.EXTENSION):
try:
debug_print('Loading [{0}]'.format(p))
doc = InselectDocument.load(p)
validation = export.validation_problems(doc)
csv_path = export.csv_path(doc)
if validation.any_problems:
print(
'Not exporting metadata for [{0}] because there are '
'validation problems'.format(p)
)
for msg in format_validation_problems(validation):
print(msg)
elif not overwrite_existing and csv_path.is_file():
print('CSV file [{0}] exists - skipping'.format(csv_path))
else:
print('Writing CSV for [{0}]'.format(p))
export.export_csv(doc)
except KeyboardInterrupt:
raise
except Exception:
print('Error saving CSV from [{0}]'.format(p))
traceback.print_exc()
示例11: test_csv_export
def test_csv_export(self):
"CSV data are exported as expected"
with temp_directory_with_files(TESTDATA / 'shapes.inselect',
TESTDATA / 'shapes.png') as tempdir:
doc = InselectDocument.load(tempdir / 'shapes.inselect')
csv_path = DocumentExport(self.TEMPLATE).export_csv(doc)
self.assertEqual(csv_path, tempdir / 'shapes.csv')
# Check CSV contents
with csv_path.open('rb') as f:
reader = unicodecsv.reader(f, encoding='utf8')
headers = [
'Cropped_image_name', 'ItemNumber',
'NormalisedLeft', 'NormalisedTop', 'NormalisedRight',
'NormalisedBottom', 'ThumbnailLeft', 'ThumbnailTop',
'ThumbnailRight', 'ThumbnailBottom', 'OriginalLeft',
'OriginalTop', 'OriginalRight', 'OriginalBottom',
'catalogNumber', 'Department', 'scientificName',
'scientificName-value'
]
self.assertEqual(headers, next(reader))
# Check only the metadata columns and 'original' coordinates
# columns, ignoring thumbnail (which doesn't exist)
# and normalised (which are floating point) coordinates
metadata_cols = itemgetter(0, 1, 10, 11, 12, 13, 14, 15, 16, 17)
self.assertEqual(
('01_1.png', '1',
'0', '0', '189', '189',
'1', 'Entomology', 'A', '1'),
metadata_cols(next(reader))
)
self.assertEqual(
('02_2.png', '2',
'271', '0', '459', '189',
'2', 'Entomology', 'B', '2'),
metadata_cols(next(reader))
)
self.assertEqual(
('03_10.png', '3',
'194', '196', '257', '232',
'3', 'Entomology', 'インセクト', '10'),
metadata_cols(next(reader))
)
self.assertEqual(
('04_3.png', '4',
'0', '248', '189', '437',
'4', 'Entomology', 'Elsinoë', '3'),
metadata_cols(next(reader))
)
self.assertEqual(
('05_4.png', '5',
'271', '248', '459', '437',
'5', 'Entomology', 'D', '4'),
metadata_cols(next(reader))
)
self.assertIsNone(next(reader, None))
示例12: test_display_role
def test_display_role(self):
m = Model()
m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))
# First four characters only - remainder depend upon current template
self.assertEqual('0001', m.data(m.index(0, 0), Qt.DisplayRole)[:4])
self.assertEqual('0003', m.data(m.index(2, 0), Qt.DisplayRole)[:4])
self.assertEqual('0004', m.data(m.index(3, 0), Qt.DisplayRole)[:4])
示例13: test_remove_rows
def test_remove_rows(self):
m = Model()
m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))
m.removeRows(0, 1)
self.assertEqual(4, m.rowCount())
expected = {"catalogNumber": "2", "scientificName": "B"}
self.assertEqual(expected, m.data(m.index(0, 0), MetadataRole))
示例14: test_set_invalid_rotation
def test_set_invalid_rotation(self):
m = Model()
m.from_document(InselectDocument.load(TESTDATA / 'test_segment.inselect'))
i = m.index(0, 0)
self.assertRaises(ValueError, m.setData, i, 'not an integer', RotationRole)
self.assertRaises(ValueError, m.setData, i, -1, RotationRole)
self.assertRaises(ValueError, m.setData, i, 2, RotationRole)
示例15: test_segment_document_sort_by_columns
def test_segment_document_sort_by_columns(self):
"Segment the document with boxes sorted by columns"
doc = InselectDocument.load(TESTDATA / 'shapes.inselect')
items = doc.items
expected = doc.scanned.from_normalised(
[items[index]['rect'] for index in (0, 3, 2, 1, 4)]
)
self._segment(doc, True, expected)