import os from pathlib import Path import numpy as np import cv2 from AbstractControllerInterface import AbstractControllerInterface from EnumInterfaceModes import EnumInterfaceModes from EnumKeys import EnumKeys from EnumParkingStatus import EnumParkingStatus from StatusMarkerDrawer import StatusMarkerDrawer from JSonEditor import JSonEditor from Constants import Constants class ControllerStatusMarker(AbstractControllerInterface): def __init__(self, rootOriginalDataset, pathJson, subset): super().__init__(rootOriginalDataset) self.pathJson = pathJson self.subset = subset self.jsonEditor = JSonEditor(pathJson) self.createMapImages() self.loadAnnotations() self.renderMode = EnumInterfaceModes.RENDER_POLY def createPolygonsDrawer(self): return StatusMarkerDrawer("Status Marker", self) def printInstructions(self): print("Click on the parking space to change its status.") EnumKeys.print_keys() def checkNonStandardKeys(self, key): if key == EnumKeys.RENDER_MODE: self._changeRenderMode() return True if key == EnumKeys.RENDER_ALL_SPACES: self._changeRenderAll() return True if key == EnumKeys.SAVE_FILE: print("Savind File", self.pathJson) self.jsonEditor.save_json(self.pathJson) print("Savind Done") return True def _changeRenderAll(self): self.drawer.renderAllSpaces = not self.drawer.renderAllSpaces def _changeRenderMode(self): if (self.drawer.render == EnumInterfaceModes.RENDER_POLY): self.drawer.render = EnumInterfaceModes.RENDER_MASK else: self.drawer.render = EnumInterfaceModes.RENDER_POLY def updateImage(self): print(str(self.idx_image + 1) + "/" + str(len(self.pathsArray)) + "\t" + self.pathsArray[self.idx_image]["path"]) fileName = os.path.basename(self.pathsArray[self.idx_image]["path"]) image = cv2.imread(self.pathsArray[self.idx_image]["path"]) id = self.mapImgIds[fileName] if id in self.dictAnnotsVal: self.drawer.currentAnnotationsVal = self.dictAnnotsVal[id] else: self.drawer.currentAnnotationsVal = [] if id in self.dictAnnotsOriginalDataset: self.drawer.currentAnnotationsOriginal = self.dictAnnotsOriginalDataset[id] else: self.drawer.currentAnnotationsOriginal = [] if id in self.dictSegVehicles: self.drawer.currentVehicleSegs = self.dictSegVehicles[id] else: self.drawer.currentVehicleSegs = [] self.drawer.annotationRectangle = self.mapImgAnnotRectangle[id] self.drawer.changeImage(image) def createMapImages(self): self.mapImgIds = {} self.mapIdsImgs = {} self.mapImgAnnotRectangle = {} for image in self.jsonEditor.json[Constants.JSON_IMAGES_KEY]: fileName = os.path.basename(image[Constants.JSON_IMAGE_NAME_KEY]) imgId = int(image[Constants.JSON_IMAGE_ID_KEY]) self.mapImgIds[fileName] = imgId self.mapIdsImgs[imgId] = fileName annot_rectangle = image[Constants.JSON_ANNOTATIONS_RECTANGLE_KEY] self.mapImgAnnotRectangle[imgId] = annot_rectangle def loadAnnotations(self): self.dictAnnotsVal = {} self.dictAnnotsOriginalDataset = {} self.dictSegVehicles = {} for annot in self.jsonEditor.json[Constants.JSON_ANNOTATIONS_KEY]: imgId = annot[Constants.JSON_LINK_IMAGE_ID_KEY] if(int(annot[Constants.JSON_LINK_CATEG_KEY]) == Constants.JSON_PARKING_SPACE_KEY): if(imgId not in self.mapIdsImgs): print("SEVERE ERROR: image_id", imgId, "does not map to an image id") else: status = int(annot[Constants.JSON_PARKING_STATUS_ID_KEY]) if(status == EnumParkingStatus.EMPTY.value or status == EnumParkingStatus.OCCUPIED.value): if imgId not in self.dictAnnotsOriginalDataset: self.dictAnnotsOriginalDataset[imgId] = [] self.dictAnnotsOriginalDataset[imgId].append(annot) else: if imgId not in self.dictAnnotsVal: self.dictAnnotsVal[imgId] = [] self.dictAnnotsVal[imgId].append(annot) else: if(imgId not in self.mapIdsImgs): print("SEVERE ERROR FOR MASK: image_id", imgId, "does not map to an image id") else: if imgId not in self.dictSegVehicles: self.dictSegVehicles[imgId] = [] self.dictSegVehicles[imgId].append(annot)