示例#1
0
    def slideshow(self):
        for imgName in glob.glob("/beta/Work/2/postbake/*.png", recursive=True):
            img = cv2.imread(imgName)
            # img = cv2.resize(img, dsize=None, fx=0.7, fy=0.7)
            img = img[:, 100:-300]
            # img = cv2.resize(img, (800, 800))
            # out = []
            out = self.P.predict(img, threshold=0.07)
            for i, x in enumerate(out):
                cid = x[0]
                score = x[1]
                roi = x[2:]

                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))
            print('\n')
            while True:
                ImgUtils.show("Feed", img, 0, 0)
                key = cv2.waitKey(30)
                if key == ord('q'):
                    return
                elif key == ord('v'):
                    break
示例#2
0
    def main(self, draw=False):
        clock = 0
        while True:
            clock += 1
            _, self.liveFeed = self.camera.read()
            if self.liveFeed is None:
                continue

            self.frame = StandardDetectionTrans.prepareResized(self.liveFeed)
            # detect
            contrast = StandardDetectionTrans.prepareMono(self.liveFeed)

            rois = self.D.detect(contrast)
            if draw:
                for roi in rois:
                    ImgUtils.drawRect(roi, self.frame, colour=(0, 255, 255))

            # track
            self.T.track(rois)
            print('--', self.T.N)

            ###
            ImgUtils.show("Live", self.frame, 800, 0)
            ImgUtils.show("Frame", contrast, 0, 0)
            keyboard = cv2.waitKey(30)
            if keyboard == 'q' or keyboard == 27:
                break
示例#3
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, []
示例#4
0
    def extractAndCopy(self, srcDirPath, extractor=0):
        counter = 0

        for i in range(10000):
            imgPath = srcDirPath + str(i) + '.png'
            # print(imgPath)
            img = cv2.imread(imgPath)
            if img is None:
                continue
            dets = self.extactors[str(extractor)].detectDebug(img)
            # print(len(dets))
            for det in dets:
                ImgUtils.drawRect(det, img)
                try:
                    counter += 1
                except:
                    print("out of bounds")
            if True:
                ImgUtils.show('Img', img, 0, 0)
                key = cv2.waitKey(30)
                if key == ord('v'):
                    break
                elif key == ord('q'):
                    return
        return

        counter = 0
        filepathPrefix = "/beta/Work/2/Train/postbake/"
        for imgName in glob.glob("/beta/Work/2/postbake/*.png",
                                 recursive=True):
            img = cv2.imread(imgName)
            dets = self.D.detectDebug(img)
            for (cX, cY), rad in dets:
                if 600 < cX < 760 and 160 < cY < 560:
                    cv2.imwrite(
                        filepathPrefix + str(counter) + ".png",
                        ImgUtils.findCentroidVicinity(img, cX, cY, 300, 300))
                    counter += 1

            while True:
                ImgUtils.show('Img', img, 0, 0)
                # if X is not None:
                #     ImgUtils.show('X', X, 800, 900)
                key = cv2.waitKey(30)
                if key == 27 or key == ord('v'):
                    break
                elif key == ord('q'):
                    return
示例#5
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, []
示例#6
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
示例#7
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