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
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, []
def showFeed(self): while True: _, feed = self.camera.read() if feed is None: continue # self.frame = ImageTransforms.prepareResized(self.liveFeed) # contrast = ImageTransforms.prepareMono(self.liveFeed) ImgUtils.show("1", feed, 0, 0) keyboard = cv2.waitKey(30) if keyboard == 'q' or keyboard == 27: break
def houghDetect(img, radiusMin, radiusMax): """ :param img: grayscale image with some circles :return:[bounding rectangles] """ circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 150, param1=101, param2=11, minRadius=radiusMin, maxRadius=radiusMax) rois = [] radii = [] if circles is not None: circles = np.uint16(np.around(circles)) for i in circles[0, :]: center = (i[0], i[1]) cv2.circle(img, center, 1, (0, 100, 100), 3) radius = i[2] if radiusMin < radius < radiusMax: cv2.circle(img, center, radius, (255, 0, 255), 3) roi = ImgUtils.findBoxAroundCircle(center, radius) rois.append(roi) radii.append(radius) return rois, radii
def test(self): while True: _, feed = self.camera.read() if feed is None: continue frame = StandardDetectionTrans.prepareMono(feed) for i in range(3): ImgUtils.show(str(i), frame[:, :, i], 0, 300 * i) # ImgUtils.show('sum', cv2.bitwise_or(frame[:, :, 0], frame[:, :, 1]), 0, 800) ImgUtils.show('Feed', StandardDetectionTrans.prepareResized(feed), 800, 0) keyboard = cv2.waitKey(30) if keyboard == 'q' or keyboard == 27: break
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, []
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
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
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
def desample(self, path, batchSize=3): for i in range(0, 1000000, batchSize): imgs = [] gmis = [] gims = [] for j in range(batchSize): imgPath = path+str(i+j)+'.png' img = cv2.imread(imgPath) if img is None: continue img = img[60:240, 60:240, :] imgs.append(img) for j, img in enumerate(imgs): # imgs[0] = ImgUtils.randomImage(red=(80, 240), blue=(10, 100), green=(80, 240), imgLike=img) # imgs[1] = ImgUtils.randomImage(red=(0, 120), blue=(0, 120), green=(0, 120), imgLike=img) # imgs[2] = ImgUtils.randomImage(red=(0, 255), blue=(0, 255), green=(0, 255), imgLike=img) # imgs[3] = ImgUtils.randomImage(red=(120, 255), blue=(120, 255), green=(120, 255), imgLike=img) # imgs[4] = ImgUtils.randomImage(red=(150, 170), blue=(30, 50), green=(30, 50), imgLike=img) gmi = self.transform(imgs[j]) gmis.append(gmi) ImgUtils.putText(img, str(np.mean(img[:, :, 2])), (10, 50), colour=(200, 0, 255)) img = ImgUtils.hconcat(imgs, pad=50) gmi = ImgUtils.hconcat(gmis, pad=50) # Y = ImgUtils.hconcat(imgs, channel=1) # gmi[i, j] = (np.random.randint(10, 100), np.random.randint(70, 100), np.random.randint(80, 240)) while True: ImgUtils.show('Img', img, 0, 0) ImgUtils.show('Gmi', gmi, 0, 260) # ImgUtils.show('Y', Y, 0, 720) key = cv2.waitKey(500) if key == ord('v'): break elif key == ord('q'): return
def postbake1Debug(self, feed): radiusMin = self.dim1Lower radiusMax = self.dim1Upper img = self.transform(feed) circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 150, param1=101, param2=11, minRadius=radiusMin, maxRadius=radiusMax) dets = [] if circles is not None: circles = np.uint16(np.around(circles)) for i in circles[0, :]: center = (i[0], i[1]) cv2.circle(img, center, 1, (0, 100, 100), 3) radius = i[2] if radiusMin < radius < radiusMax: cv2.circle(img, center, radius, (255, 0, 255), 3) dets.append(ImgUtils.findBoxAroundCircle(center, radius)) return dets
from detect import Detector from imgUtils import ImgUtils if __name__ == "__main__": param = { (1, 'postbake'): { 'expectedWidth': 120, 'expectedHeight': 110, # detection params 'transformationTarget': 'cool', # select correct image transformations 'upperKillzone': 550, 'lowerKillzone': 220, # select correct tracking parameters 'rightKillzone': 3000, 'leftKillzone': -3000, # select correct tracking parameters 'timeToDie': 1, 'timeToLive': 0, 'partioningRequired': True } } D = Detector(**param.get((1, 'postbake'))) for i in range(49, 100): img = cv2.imread("PostBakeSamples/postbake" + str(i) + ".png") x = D.getImgWithBoxes(img) while True: ImgUtils.show("x", x, 0, 0) key = cv2.waitKey(30) if key == ord('q'): break
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