LoanMaikon's picture
Add files using upload-large-folder tool
07444d1 verified
Raw
History Blame Contribute Delete
3.33 kB
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