forked from chenxinpeng/SSD_scene_text_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert the ground-truth text file to XML file of Pascal VOC
- Loading branch information
1 parent
c1d738e
commit aca5721
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#! /usr/bin/python | ||
|
||
import os, sys | ||
import glob | ||
from PIL import Image | ||
|
||
src_img_dir = "/media/chenxp/Datadisk/ocr_dataset/ICDAR2011/train-textloc" | ||
src_txt_dir = "/media/chenxp/Datadisk/ocr_dataset/ICDAR2011/train-textloc" | ||
|
||
img_Lists = glob.glob(src_img_dir + '/*.jpg') | ||
|
||
img_basenames = [] # e.g. 100.jpg | ||
for item in img_Lists: | ||
img_basenames.append(os.path.basename(item)) | ||
|
||
img_names = [] # e.g. 100 | ||
for item in img_basenames: | ||
temp1, temp2 = os.path.splitext(item) | ||
img_names.append(temp1) | ||
|
||
for img in img_names: | ||
im = Image.open((src_img_dir + '/' + img + '.jpg')) | ||
width, height = im.size | ||
|
||
# open the crospronding txt file | ||
gt = open(src_txt_dir + '/gt_' + img + '.txt').read().splitlines() | ||
|
||
# write in xml file | ||
os.mknod(src_txt_dir + '/' + img + '.xml') | ||
xml_file = open((src_txt_dir + '/' + img + '.xml'), 'w') | ||
xml_file.write('<annotation>\n') | ||
xml_file.write(' <folder>VOC2007</folder>\n') | ||
xml_file.write(' <filename>' + str(img) + '.jpg' + '</filename>\n') | ||
xml_file.write(' <size>\n') | ||
xml_file.write(' <width>' + str(width) + '</width>\n') | ||
xml_file.write(' <height>' + str(height) + '</height>\n') | ||
xml_file.write(' <depth>3</depth>\n') | ||
xml_file.write(' </size>\n') | ||
|
||
# write the region of text on xml file | ||
for img_each_label in gt: | ||
spt = img_each_label.split(',') | ||
xml_file.write(' <object>\n') | ||
xml_file.write(' <name>text</name>\n') | ||
xml_file.write(' <pose>Unspecified</pose>\n') | ||
xml_file.write(' <truncated>0</truncated>\n') | ||
xml_file.write(' <difficult>0</difficult>\n') | ||
xml_file.write(' <bndbox>\n') | ||
xml_file.write(' <xmin>' + str(spt[0]) + '</xmin>\n') | ||
xml_file.write(' <ymin>' + str(spt[1]) + '</ymin>\n') | ||
xml_file.write(' <xmax>' + str(spt[2]) + '</xmax>\n') | ||
xml_file.write(' <ymax>' + str(spt[3]) + '</ymax>\n') | ||
xml_file.write(' </bndbox>\n') | ||
xml_file.write(' </object>\n') | ||
|
||
xml_file.write('</annotation>') |