File size: 5,881 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | import numpy as np
import cv2
import random
class Printer:
#print the labels and show the images
#pathRootImgs = path to the root directory containing the images
#dictImgsLabels = dictionary relating the path of each image and the annotations
def print(self,pathRootImgs, dictImgsLabels, subset_filenames):
for key in sorted(dictImgsLabels):
if subset_filenames and key not in subset_filenames:
continue
path = pathRootImgs + '/' + key
masks = dictImgsLabels[key].masks
occupied = dictImgsLabels[key].occupiedRRs
empty = dictImgsLabels[key].emptyRRs
unknown = dictImgsLabels[key].unknownRRs
print(path)
print('\tMasks: ', len(masks), ' Parking Spaces: ', len(occupied) + len(empty) + len(unknown),
' Occupied: ', len(occupied), ' Empty: ', len(empty), ' Unknown: ', len(unknown))
img = cv2.imread(path)
original = img.copy()
self._printBorders(img, dictImgsLabels[key].annotationsRectangleP1, dictImgsLabels[key].annotationsRectangleP2)
self._printPoligons(img, masks)
ret, mosaicOccupied, mosaicEmpty, mosaicUnknown = self._printParkingSpaces(original, occupied, empty, unknown)
cv2.imshow('Image',img)
cv2.imshow('Empty',mosaicEmpty)
cv2.imshow('Occupied',mosaicOccupied)
cv2.imshow('Unknown',mosaicUnknown)
k = cv2.waitKey(0)
if k == ord('q'): # wait for 's' key to save and exit
break
self._printBorders(ret, dictImgsLabels[key].annotationsRectangleP1, dictImgsLabels[key].annotationsRectangleP2)
cv2.imshow('Image',ret)
k = cv2.waitKey(0)
if k == ord('q'): # wait for 's' key to save and exit
break
def _printBorders(self, img, annotationsRectangleP1, annotationsRectangleP2):
cv2.rectangle(img, annotationsRectangleP1, annotationsRectangleP2, (180, 105, 255), 4)
def _printPoligons(self, img, poligonsList):
imgMask = img.copy()
for polys in poligonsList:#one instance may have multiple polygons demarcating it (due of occlusions)
for p in polys:
pts = np.array(p, np.int32)
pts = pts.reshape((1,-1,2))
cv2.drawContours(img, pts, -1, (255,0,0), 2)
cv2.drawContours(imgMask, pts, -1, (255,0,0), 2)
cv2.drawContours(imgMask, pts, -1, (random.randint(127, 255),
random.randint(127, 255),
random.randint(127, 255)), -1)
cv2.addWeighted(img, 0.5, imgMask, 0.5, 0.0,img)
return
def _printParkingSpaces(self, img, occupiedRRs, emptyRRs, unknownRRs = []):
ret = img.copy()
occupiedImgs = list()
emptyImgs = list()
unknownImgs = list()
imgsWidth = 96
imgsHeight = 96
for rro in occupiedRRs:
occupiedImgs.append(self.crop_minAreaRect(img, ((rro[0], rro[1]),(rro[2], rro[3]), rro[4]), imgsWidth))
box = cv2.boxPoints(((rro[0], rro[1]),(rro[2], rro[3]), rro[4]))
box = np.int0(box)
cv2.drawContours(ret, [box], -1, (0,0,255), 2)
for rre in emptyRRs:
emptyImgs.append(self.crop_minAreaRect(img, ((rre[0], rre[1]),(rre[2], rre[3]), rre[4]), imgsWidth))
box = cv2.boxPoints(((rre[0], rre[1]),(rre[2], rre[3]), rre[4]))
box = np.int0(box)
cv2.drawContours(ret, [box], -1, (0,255,0), 2)
for rru in unknownRRs:
unknownImgs.append(self.crop_minAreaRect(img, ((rru[0], rru[1]),(rru[2], rru[3]), rru[4]), imgsWidth))
box = cv2.boxPoints(((rru[0], rru[1]),(rru[2], rru[3]), rru[4]))
box = np.int0(box)
cv2.drawContours(ret, [box], -1, (255,0,0), 2)
mosaicOccupied = self._drawMosaic(occupiedImgs, imgsWidth, imgsHeight, mosaicWidth = 576)
mosaicEmpty = self._drawMosaic(emptyImgs, imgsWidth, imgsHeight, mosaicWidth = 576)
mosaicUnknown = self._drawMosaic(unknownImgs, imgsWidth, imgsHeight, mosaicWidth = 576)
return ret, mosaicOccupied, mosaicEmpty, mosaicUnknown
def _drawMosaic(self, images, imgsWidth, imgsHeight ,mosaicWidth = 576):
numImgs = len(images)
imgsByLine = mosaicWidth/imgsWidth
numCols = numImgs/imgsByLine
if numImgs % imgsByLine != 0:
numCols = numCols + 1;
if numCols == 0:
numCols = 1
mosaic = np.zeros((int(numCols*imgsHeight), mosaicWidth,3), np.uint8)
y_offset = 0
x_offset = 0;
for img in images:
mosaic[y_offset:y_offset+img.shape[0], x_offset:x_offset+img.shape[1]] = img
x_offset = x_offset + imgsWidth
if mosaicWidth - x_offset < imgsWidth:
x_offset = 0
y_offset = y_offset + imgsHeight
return mosaic
def crop_minAreaRect(self, img, rr, size):
box = cv2.boxPoints(rr)
box = np.int0(box)
width = int(rr[1][0])
height = int(rr[1][1])
srcPts = box.astype("float32")
dstPts = np.array([[0, height-1],
[0, 0],
[width-1, 0],
[width-1, height-1]], dtype="float32")
M = cv2.getPerspectiveTransform(srcPts, dstPts)
warped = cv2.warpPerspective(img, M, (width, height))
mx = max(width,height)
result = np.zeros((mx,mx,3),np.uint8)
cx,cy = (mx - width)//2,(mx - height)//2
result[cy:warped.shape[0]+cy,cx:cx+warped.shape[1]] = warped
result = cv2.resize(result, (size,size), interpolation = cv2.INTER_AREA)
return result |