1 from __future__ 
import print_function
 
    6 from chainercv.chainer_experimental.datasets.sliceable 
import GetterDataset
 
   10 import xml.etree.ElementTree 
as ET
 
   15         super(BboxDetectionDataset, self).
__init__()
 
   18         class_names_path = osp.join(root_dir, 
'class_names.txt')
 
   19         with open(class_names_path, 
'r') 
as f:
 
   20             class_names = f.readlines()
 
   27         imgs_dir = osp.join(root_dir, 
'JPEGImages')
 
   28         annotation_dir = osp.join(root_dir, 
'Annotations')
 
   30         for img_ 
in sorted(os.listdir(imgs_dir)):
 
   31             img_path = osp.join(imgs_dir, img_)
 
   32             basename = img_.rstrip(
'.jpg')
 
   33             annotation_path = osp.join(annotation_dir, basename + 
'.xml')
 
   34             self.
_imgs.append(img_path)
 
   37         self.add_getter((
'img', 
'bbox', 
'label'), self.
_get_example)
 
   40         return len(self.
_imgs)
 
   43         img_path = self.
_imgs[i]
 
   46         img = cv2.imread(img_path)
 
   47         assert img.dtype == np.uint8
 
   53         tree = ET.parse(annotation_path)
 
   56         for obj 
in root.findall(
"object"):
 
   57             label.append(np.where(self.
fg_class_names == obj.find(
"name").text)[0][0])
 
   58             bbox.append([obj.find(
"bndbox").find(
"ymin").text,
 
   59                          obj.find(
"bndbox").find(
"xmin").text,
 
   60                          obj.find(
"bndbox").find(
"ymax").text,
 
   61                          obj.find(
"bndbox").find(
"xmax").text])
 
   63         label = np.array(label, dtype=np.int32)
 
   64         bbox = np.array(bbox, dtype=np.float32)
 
   67         img = img.transpose((2, 0, 1))
 
   68         return img, bbox, label