def train(ds): C = 2.0 gamma = 2.0 svm = cv2.ml.SVM_create() svm.setType(cv2.ml.SVM_C_SVC) svm.setKernel(cv2.ml.SVM_RBF) svm.setC(C) svm.setGamma(gamma) trainData = np.zeros([ds.frameCount, 3*3*4*9]) trainLabels = np.zeros([ds.frameCount, 1]) i = 0 while (ds.getNextFrame()): dets = Detection(ds.im) h, w, _ = np.shape(ds.im) hogs = dets.makeHogs(0, h, 0, w, 4, 4) util.plotHelper(dets.visualHogs(0, h, 0, w, hogs), "bla", True) v = dets.blockNormalization(hogs, 2, 2) trainData[i] = v if ds.getFilename()[0:3] == "pos": trainLabels[i,0] = 1 elif ds.getFilename()[0:3] == "neg": trainLabels[i,0] = 0 else: raise Exception("Picture is not annotated !") i = i + 1 #print(trainLabels) svm.train(np.float32(trainData), cv2.ml.ROW_SAMPLE, np.int32(trainLabels)) result = svm.predict(np.float32(trainData))[1] svm.save("model.dat"); #print("Result:", result) print("GT Predict Name") for i in range(ds.frameCount): print(trainLabels[i,0], " ", result[i, 0], " ", ds.getFilename(i) ) return svm
def abcdefg(): ds = Dataset.Dataset("../data/circles/train/many/", "jpg") ds.getNextFrame() maxH, maxW, _ = np.shape(ds.im) while ds.getNextFrame(): h, w, _ = np.shape(ds.im) maxH = max(h, maxH) maxW = max(w, maxW) ds.aktuFrameIndex = -1 #scale all to height max-height: ims = [] while ds.getNextFrame(): if not "pos" in ds.getFilename(): continue h = np.shape(ds.im)[0] f = maxH / h im = cv2.resize(ds.im, (0, 0), fx=f, fy=f, interpolation=cv2.INTER_LINEAR) h, w, _ = np.shape(im) print(h, w, maxH, maxW) if w < maxW: np.hstack((im, np.zeros([maxH, maxW - w, 3]))) im, _ = util.magAndAngle(im) ims.append(im) #util.plotHelper(im) imRes = np.zeros([maxH, maxW]) for im in ims: imRes = imRes + im imRes = np.log10(1 + imRes) util.plotHelper(imRes)
def testProjPic(): kam = kamera("c:/here_are_the_frames/calibration/iscam2.CALI", "") dstIm = cv2.imread("c:/here_are_the_frames/00000002.jpg") srcIm = cv2.imread("C:/here_are_the_frames/test2/123.jpg") fp = np.matrix([0, -1, 0]).T kam.projectPicture(fp, 0.7, 1.80, srcIm, dstIm) util.plotHelper(dstIm, "Ziel", False) util.plotHelper(srcIm, "Source")
def makeHogs(self, startY, height, startX, width, numOfCellsY, numOfCellsX): self.displayBild = self.mag.copy() self.startY = startY self.height = height self.startX = startX self.width = width deltaY = height / numOfCellsY #height of one cell deltaX = width / numOfCellsX #width of one cell hogs = np.zeros([numOfCellsY, numOfCellsX, 9]) for y in range(numOfCellsY): sY = startY + y * deltaY eY = round(sY + deltaY) sY = round(sY) for x in range(numOfCellsX): sX = startX + x * deltaX eX = round(sX + deltaX) sX = round(sX) hogs[y,x] = self.makeHog(sY, eY, sX, eX) util.plotHelper(self.displayBild, warte=False) return hogs
def makeManyTrainPics(srcPath, dstPath): pos = 0 neg = 0 dataS = Dataset.Dataset(srcPath, "jpg") while dataS.getNextFrame(): util.plotHelper(dataS.im, "bla") #if "pos" in dataS.getFilename(): if "pos" in dataS.getFilename(): for angle in range(0, 360, 20): im2 = util.rotatePicture(dataS.im, angle, True, (255, 255, 255)) util.plotHelper(im2, "rotated") cv2.imwrite(dstPath + "pos_" + "{:>04d}".format(pos) + ".jpg", im2) pos = pos + 1 elif "neg" in dataS.getFilename(): for angle in range(0, 360, 20): im2 = util.rotatePicture(dataS.im, angle, True, (255, 255, 255)) util.plotHelper(im2, "rotated") cv2.imwrite(dstPath + "neg_" + "{:>04d}".format(neg) + ".jpg", im2) neg = neg + 1 else: raise RuntimeError("File " + str(dataS.getFilename()) + " is neither pos nor neg !?")
def projectTestsOnFrame(): #srcPath = "../data/circles/test/" #dstPath = "../data/circles/test/many/" #for d in glob.glob(dstPath + "*.jpg"): # print ("Loesche: " + d) # os.remove(d) #makeManyTrainPics(srcPath, dstPath) cam = kamera.kamera("../data/calibration/iscam2.cali", "") dstPath = "C:/here_are_the_frames/00000002.jpg" dstIm = cv2.imread(dstPath) testCount = 5 srcPath = "../data/circles/test/many/" ds = Dataset.Dataset(srcPath, "jpg") r = np.random.randint(0, ds.frameCount, testCount) dstH, dstW, _ = np.shape(dstIm) for i in range(testCount): srcIm = ds.getFrame(r[i]) util.plotHelper(srcIm, "bla_" + str(i), False) fpUV = np.matrix([(dstW - 100) * np.random.rand() + 50, (dstH - 100) * np.random.rand() + 50]).T fp = cam.unproj(fpUV) cam.projectPicture(fp, 1, 1, srcIm, dstIm) util.plotHelper(dstIm)
def predict(ds, svm, maske): testData = np.zeros([ds.frameCount, 3*3*4*9]) gtLabels = np.zeros([ds.frameCount, 1]) i = 0 while (ds.getNextFrame()): det = Detection(ds.im, maske) h, w, _ = np.shape(ds.im) hogs = det.makeHogs(0, h, 0, w, 4, 4) util.plotHelper(det.visualHogs(0, h, 0, w, hogs), "bla", True) v = det.blockNormalization(hogs, 2, 2) testData[i] = v if ds.getFilename()[0:3] == "pos": gtLabels[i,0] = 1 elif ds.getFilename()[0:3] == "neg": gtLabels[i,0] = 0 else: print("Fehler") i = i + 1 result = svm.predict(np.float32(testData))[1] print("GT Predict Name") for i in range(ds.frameCount): print(gtLabels[i,0], " ", result[i, 0], " ", ds.getFilename(i) )
def slide(self): imRes = im.copy() for y in range(0, np.shape(im)[0], 4): for x in range(0, np.shape(im)[1], 4): dstIm = im.copy() fpUV = np.matrix([x, y]).T fp = self.cam.unproj(fpUV) upV = np.matrix([(0, ), (0, ), (1, )]) rightV = self.cam.kamkoord2weltkoord( np.matrix([(1, ), (0, ), (0, )])) - self.cam.kamkoord2weltkoord( np.matrix([(0, ), (0, ), (0, )])) #rightV = np.matrix( [(1,), (0,), (0,)] ) #print("Norm von rightV", np.linalg.norm(rightV)) rightV = (1.0 / np.linalg.norm(rightV)) * rightV #print("rightV: ", rightV) v1 = fp - (self.width / 2.0) * rightV v2 = fp + (self.width / 2.0) * rightV v3 = v1 + self.height * upV v4 = v2 + self.height * upV imaP1 = self.cam.proj(v1).T #row-vectors imaP2 = self.cam.proj(v2).T imaP3 = self.cam.proj(v3).T imaP4 = self.cam.proj(v4).T imaPFP = self.cam.proj(fp).T imaPHP = self.cam.proj(fp + upV).T util.plotLine(dstIm, imaPHP, imaPFP) util.plotLine(dstIm, imaP1, imaP2) util.plotCircle(dstIm, 3, (255, 255, 0), imaP1, imaP2) x1, y1, x2, y2 = util.boundingRect(imaP1, imaP2, imaP3, imaP4) util.plotCircle(dstIm, 3, (255, 255, 0), imaP1, imaP2, imaP3, imaP4) util.plotCircle(dstIm, 5, (255, 255, 255), imaPFP, imaPHP) util.plotRect(dstIm, x1, y1, x2, y2) x1 = int(x1) x2 = int(x2) y1 = int(y1) y2 = int(y2) imaP1 = np.squeeze(np.asarray(imaP1)) imaP2 = np.squeeze(np.asarray(imaP2)) imaP3 = np.squeeze(np.asarray(imaP3)) imaP4 = np.squeeze(np.asarray(imaP4)) dstH, dstW, _ = np.shape(dstIm) y1 = util.clamp(y1, 0, dstH - 1) y2 = util.clamp(y2, 0, dstH - 1) x1 = util.clamp(x1, 0, dstW - 1) x2 = util.clamp(x2, 0, dstW - 1) if y2 > y1 + 8 and x2 > x1 + 8: testData = np.zeros([1, 3 * 3 * 4 * 9]) h = y2 - y1 + 1 w = x2 - x1 + 1 hogs = self.det.makeHogs(y1, h, x1, w, 4, 4) #util.plotHelper(self.det.visualHogs(y1, h, x1, w, hogs), "bla", False) v = self.det.blockNormalization(hogs, 2, 2) testData[0] = v result = self.svm.predict(np.float32(testData))[1] #print("GT Predict Name") #for i in range(ds.frameCount): # print(gtLabels[i,0], " ", result[i, 0], " ", ds.getFilename(i) ) #print(result[0, 0]) if result[0, 0]: util.plotRect(imRes, x1, y1, x2, y2, (255, 0, 0)) print("!!!!!!!!!!!!!!!!!!!!!!!!!") #util.plotRect(dstIm, x1, y1, x2, y2, (255,0,0)) #util.plotHelper(dstIm, "In Bearbeitung", False) print(y) util.plotHelper(imRes, "Im Res")