| import numpy as np |
| import cv2 |
| import random |
|
|
| class Printer: |
| |
| |
| |
| 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'): |
| break |
| self._printBorders(ret, dictImgsLabels[key].annotationsRectangleP1, dictImgsLabels[key].annotationsRectangleP2) |
| cv2.imshow('Image',ret) |
| k = cv2.waitKey(0) |
| if k == ord('q'): |
| 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: |
| 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 |