Esempio n. 1
0
    def raw1(self, img):
        self.counter += 1
        hBefore, wBefore, _ = img.shape
        img = self.transformer.resize(img, 0, 270, 40, 500)

        contrast = self.transform(img)
        rois = DetectionUtils.detectContours(contrast,
                                             widthLower=self.dim1Lower,
                                             widthUpper=self.dim1Upper,
                                             heightLower=self.dim2Lower,
                                             heigthUpper=self.dim2Upper)
        tracked, _ = self.tracker.track(rois)
        self.numObjects = self.tracker.N
        if self.guiMode:
            for roi in rois:
                ImgUtils.drawRect(roi, img)
                detectedCentroid = ImgUtils.findRoiCentroid(roi)
                ImgUtils.drawCircle(detectedCentroid, img, colour=(255, 0, 0))
            for objectId, centroid in tracked.items():
                ImgUtils.drawCircle((centroid[0], centroid[1]), img)
                ImgUtils.putText(coords=centroid,
                                 text=str(objectId % 1000),
                                 img=img,
                                 colour=(255, 0, 0))

        return img, contrast, []
Esempio n. 2
0
    def brick0(self, img):
        # img = img[550:, 350:-400, :]
        img = img[100:, :, :]

        contrast = np.copy(img)
        contrast = self.transform(contrast)
        contours, h = cv2.findContours(contrast, cv2.RETR_EXTERNAL,
                                       cv2.CHAIN_APPROX_SIMPLE)
        rois = []
        for c in contours:
            approx = cv2.approxPolyDP(c, 0.01 * cv2.arcLength(c, True), True)
            x, y, w, h = cv2.boundingRect(c)
            if len(approx) < 1 or w < 130 or h < 60:
                continue
            x1 = x
            x2 = x1 + w
            y1 = y
            y2 = y1 + h
            if y1 < 250 or x2 < 100:
                continue
            rois.append([x1, y1, x2, y2])
            # targetHeight = 130
            # numParts = h // targetHeight
            # if numParts < 1:
            #     rois.append([x1, y1, x2, y2])
            # else:
            #     step = (h % targetHeight)
            #     y = y1
            #     for i in range(0, numParts):
            #         r = [x1, y, x2, y + targetHeight + step]
            #         y += (step + targetHeight)
            #         rois.append(r)

        tracked, _ = self.tracker.track(rois)
        self.numObjects = self.tracker.N
        if self.guiMode:
            for roi in rois:
                ImgUtils.drawRect(roi, img)
                detectedCentroid = ImgUtils.findRoiCentroid(roi)
                ImgUtils.drawCircle(detectedCentroid, img, colour=(255, 0, 0))
            for objectId, centroid in tracked.items():
                ImgUtils.drawCircle((centroid[0], centroid[1]), img)
                ImgUtils.putText(coords=centroid,
                                 text=str(objectId % 1000),
                                 img=img,
                                 colour=(255, 0, 0))

        return img, contrast, []
Esempio n. 3
0
    def postbake1(self, img):
        hBefore, wBefore, _ = img.shape
        img = self.transformer.resize(img)
        origImg = np.copy(img)
        contrast = self.transform(img)
        rois, radii = DetectionUtils.houghDetect(contrast,
                                                 radiusMin=self.dim1Lower,
                                                 radiusMax=self.dim1Upper)
        tracked, newRois = self.tracker.track(rois)
        self.numObjects = self.tracker.N

        if self.guiMode:
            for roi in rois:

                ImgUtils.drawRect(roi, img)
                detectedCentroid = ImgUtils.findRoiCentroid(roi)
                ImgUtils.drawCircle(detectedCentroid, img, colour=(255, 0, 0))
                ImgUtils.putText(coords=(roi[0] + 50, roi[1] + 50),
                                 text=str(roi[2] - roi[0]),
                                 img=img,
                                 colour=(255, 255, 0),
                                 fontSize=3)
            for objectId, centroid in tracked.items():
                ImgUtils.drawCircle((centroid[0], centroid[1]), img)
                ImgUtils.putText(coords=centroid,
                                 text=str(objectId % 1000),
                                 img=img,
                                 colour=(255, 0, 0))

        out = []

        for roi in newRois:
            colour = self.colour(origImg[roi[1]:roi[3], roi[0]:roi[2]])
            self.averageColour[0] += colour[0]
            self.averageColour[1] += colour[1]
            self.averageColour[2] += colour[2]
            self.averageSize += roi[3] - roi[1]

        return img, contrast, out
Esempio n. 4
0
    def test(self):
        for imgName in glob.glob("/beta/Work/2/postbake/*.png",
                                 recursive=True):
            # for imgName in glob.glob("/beta/Work/2/Train/1/*.png", recursive=True):
            img = cv2.imread(imgName)
            # img = cv2.resize(img, dsize=None, fx=0.7, fy=0.7)
            img = img[50:-50, 500:850]
            # img = cv2.resize(img, (300, 300))
            # out = []
            out = self.P.predict(img, threshold=0.7)
            for i, x in enumerate(out):
                print(x)
                cid = x[0]
                score = x[1]
                roi = x[2]

                centre = ImgUtils.findRoiCentroid(roi)
                cv2.circle(img, centre, 20, (255, 0, 255), 3)

                label = '|{}|.{:.3f}'.format(cid, score)
                ImgUtils.drawRect(roi, img, colour=(255, 0, 0))
                cv2.putText(img=img,
                            text=label,
                            org=(int(roi[0]), int(roi[1])),
                            fontFace=cv2.FONT_HERSHEY_PLAIN,
                            thickness=1,
                            lineType=cv2.LINE_4,
                            fontScale=2,
                            color=(0, 255, 255))

            while True:
                ImgUtils.show("Feed", img, 0, 0)
                key = cv2.waitKey(30)
                if key == ord('q'):
                    return
                elif key == ord('v'):
                    break