File size: 4,875 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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)