File size: 2,052 Bytes
c6827c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import torch
import numpy as np
from torchvision import transforms as T
from torch.utils.data import Dataset
from PIL import Image
from torchvision.ops.boxes import box_convert
import glob
import json

# dataset
#       |_ train
#       |   |_ .BMP files
#       |   |_ annotation
#       |           |_ classes.txt (one class per line)
#       |           |_ .txt anno files (class x_center y_center width height)
#       |_ test
#       |_ val

class Therin(Dataset):   # Therin: Intruder thermal dataset
    def __init__(self, dir, set):
        self._dir = dir + '/' + set 
        # self._imglist = glob.glob(self._dir + '/*.BMP')
        self._json_path = dir + '/' + set + '.json'
        with open(self._json_path) as anno_file:
            self._anno = json.load(anno_file)["annotations"]
        with open(self._json_path) as anno_file:
            self._imglist = json.load(anno_file)["images"]
        self._transform = T.Compose([T.ToTensor()])

    def __len__(self):
        return len(self._imglist)

    def __getitem__(self, index):
        image = Image.open(self._dir + "/" + self._imglist[index]["file_name"])

        boxes = np.zeros((1, 4), dtype=np.float32)
        boxes[0] = self._anno[index]['bbox']
        boxes = torch.as_tensor(boxes, dtype=torch.float32)
        boxes = box_convert(boxes, in_fmt='xywh', out_fmt='xyxy')

        gt_classes = np.zeros((1), dtype=np.int32)
        gt_classes[0] = self._anno[index]['category_id']-1
        gt_classes = torch.as_tensor(gt_classes, dtype=torch.int64)

        image_id = self._anno[index]['image_id']
        image_id = torch.as_tensor(image_id, dtype=torch.int64)

        area = np.zeros((1), dtype=np.int32)
        area[0] = self._anno[index]['area']
        area = torch.as_tensor(area, dtype=torch.int64)

        target = {"labels": gt_classes, "boxes": boxes, "image_id": image_id, "area": area, "iscrowd": torch.tensor([0])}

        image = self._transform(image)
        return image, target

    def collate_fn(self, batch):
        return tuple(zip(*batch))