File size: 3,328 Bytes
07444d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from pathlib import Path
import json
from ImgLabels import ImgLabels
#from collections import namedtuple

class Checker:
    def __init__(self, rootJsons):
        self.root = rootJsons
        pathJSons = Path(rootJsons).rglob('*.json')
        self.data = []
        for path in pathJSons:
            print('Loading ', path)
            with open(path, 'r') as read_file:
                self.data.append(json.load(read_file))

    #return a dictionary with images and annotations, list of images without annotations, list of annotations without images
    def check(self,rootImages):
        #Labels = namedtuple('Labels', ['polygons', 'spots'])
        dictImgId = {}
        dictImgsLabels = {}
        allPaths = []
        inexistentFiles = []
        if self.root[-1] != '/':
            self.root += '/'
        for dict in self.data:
            pathImgs = Path(rootImages).rglob('*.jpg')
            for path in pathImgs:
                new_path = str(path).replace(self.root, '')
                allPaths.append(new_path)
                # str = path.as_posix()
                # allPaths.append(str[lenRoot:])

        allPaths = list(set(allPaths))
        print(len(allPaths), 'images found in the entire dataset')
        for js in self.data:
            print(len(js['images']), 'images in', js['images'][0]['subset'])
            for imgData in js['images']:
                imgPath = imgData['file_name']
                # try:q
                # print(imgPath)
                # print(imgData in allPaths)
                allPaths.remove(imgPath)
                coords = imgData ['annotationsRectangle']
                dictImgId[imgData['id']] = (imgPath, (coords[0], coords[1]), (coords[0]+coords[2], coords[1]+coords[3]))
                # except ValueError:
                # inexistentFiles.append(imgPath)

            #Reading masks
            for mask in js['annotations']:
                imgId = mask['image_id']
                # try:
                path = (dictImgId[imgId])[0]
                if path in dictImgsLabels:
                    dictImgsLabels[path].masks.append(mask['segmentation'])
                else:
                    dictImgsLabels[path] = ImgLabels(masks = [mask['segmentation']], occupiedRRs = [],
                    emptyRRs = [], unknownRRs = [], annotationsRectangleP1 = (dictImgId[imgId])[1], annotationsRectangleP2 = (dictImgId[imgId])[2])
                # except:
                #     print(f'Critical: image {imgId} have annotations, but does not reference an image! Id {mask["id"]}')
            #Reading parking spots
            for ps in js['parkingSpots']:
                imgId = ps['image_id']
                # try:
                path = (dictImgId[imgId])[0]
                rr = ps['rotatedRect']
                if ps['status_id'] == 1:#empty
                    dictImgsLabels[path].emptyRRs.append(rr)
                else:
                    if ps['status_id'] == 2:#occupied
                        dictImgsLabels[path].occupiedRRs.append(rr)
                    else:#unknown labels
                        dictImgsLabels[path].unknownRRs.append(rr)
                # except:
                #     print('Critical: Id ', imgId, ' have annotations, but does not reference an image!')
        return dictImgsLabels, allPaths, inexistentFiles