def LoadImages(self): """处理每一个训练的image. 处理每一个image,来初始化初始化self.images(存入所有训练图像矩阵),用mean_shape缩放得到的 landmark初始化self.initLandmarks,用self.origLandmarks(通过读取.pts文件获得)来初始化self.gtLandmarks(gr- ound truth landmark) """ pass print('Loading Images......') self.imgs = [] self.initLandmarks = [] self.gtLandmarks = [] for i in range(len(self.filenames)): print('Loading ......%dth image.....'%(i)) # ndimage.imread类似matlab的imread函数,对于RGB图像,返回height*width*3的矩阵, 对于灰度图返回height*width img = ndimage.imread(self.filenames[i]) # 如果self.color为true保持rgb图不变灰度图叠加三次,结果为height*width*3 的img # 如果self.color为false保持灰度图不变rgb图rgb取平均,结果为height*width的img if self.color: if len(img.shape) == 2: # 类似于np.concarenate img = np.dstack((img, img, img)) else: if len(img.shape) > 2: # 转换为灰度图像,将RGB三值取平均 img = np.mean(img, axis=2) img = img.astype(np.uint8) if self.mirrors[i]: # 对需要做特征变换的图像,特征点做一个镜像变换 self.origLandmarks[i] = utils.mirrorShape(self.origLandmarks[i], img.shape) # 对图像做镜像变换,fliplr即数组左右翻转, img = np.fliplr(img) # 如果self.color为true,则调整矩阵维度先后,变为3*height*width # 如果self.color为false,则增加一个维度, 变为1*height*width if self.color: img = np.transpose(img, (2, 0, 1)) else: img = img[np.newaxis] groundTruth = self.origLandmarks[i] if self.initialization == 'rect': bestFit = utils.bestFitRect(groundTruth, self.meanShape) elif self.initialization == 'similarity': bestFit = utils.bestFit(groundTruth, self.meanShape) elif self.initialization == 'box': bestFit = utils.bestFitRect(groundTruth, self.meanShape, box=self.boundingBoxes[i]) self.imgs.append(img) self.initLandmarks.append(bestFit) self.gtLandmarks.append(groundTruth) self.initLandmarks = np.array(self.initLandmarks) self.gtLandmarks = np.array(self.gtLandmarks) print('Loading Images finished')
def landmark_to_gtlandmark(landmark, meanshape, box=None, mode='rect'): best_fit = [] if mode == 'rect': best_fit = bestFitRect(landmark, meanshape) elif mode == 'similarity': best_fit = bestFit(landmark, meanshape) elif mode == 'box': best_fit = bestFitRect(landmark, meanshape, box) return best_fit
def LoadImages(self): self.imgs = [] self.initLandmarks = [] self.gtLandmarks = [] for i in range(len(self.filenames)): # 이 코드는 잘 작성되지 않았으며 self.color는 실제적인 의미가 없습니다. <<< 这段代码写得不好,self.color并没有实际意义 img = ndimage.imread(self.filenames[i]) if self.color: if len(img.shape) == 2: img = np.dstack((img, img, img)) else: # img = ndimage.imread(self.filenames[i], mode='L') if len(img.shape) > 2: img = np.mean(img, axis=2) img = img.astype(np.uint8) if self.mirrors[i]: self.origLandmarks[i] = utils.mirrorShape( self.origLandmarks[i], img.shape) img = np.fliplr(img) if not self.color: # img = np.transpose(img, (2, 0, 1)) # else: img = img[ np. newaxis] # Img는 모양 (H, W)에서 모양 (1, H, W)로 바뀝니다. <<< img从shape(H,W)变成shape(1,H,W) # img = np.transpose(img, (1, 2, 0)) groundTruth = self.origLandmarks[i] if self.initialization == 'rect': bestFit = utils.bestFitRect( groundTruth, self.meanShape ) # 랜드 마크에 의해 결정된 상자에 도구 모양을 맞추기 만하면됩니다. <<< 仅仅把meanshape适应进入由landmark确定的框中 elif self.initialization == 'similarity': bestFit = utils.bestFit( groundTruth, self.meanShape ) # gt에 대한 meanShape의 최상의 변환을 찾아 변환하십시오. <<< 找到meanShape到gt的最优变换,并变换之 elif self.initialization == 'box': bestFit = utils.bestFitRect( groundTruth, self.meanShape, box=self.boundingBoxes[i] ) # 감지 된 bbx에 의해 결정된 상자에 도구 모양을 맞 춥니 다. <<< 仅仅把meanshape适应进入由检测到的bbx确定的框中 self.imgs.append(img) self.initLandmarks.append(bestFit) self.gtLandmarks.append(groundTruth) self.initLandmarks = np.array(self.initLandmarks) self.gtLandmarks = np.array(self.gtLandmarks)
def LoadImages(self): self.imgs = [] self.initLandmarks = [] self.gtLandmarks = [] for i in range(len(self.filenames)): # 读取文件夹下面的每个图片 img = ndimage.imread(self.filenames[i]) # 图片shape:539 * 410 * 3 if self.color: if len(img.shape) == 2: img = np.dstack((img, img, img)) else: if len(img.shape) > 2: # img的信息坐标轴个数,row,column,channel共3个坐标轴 img = np.mean( img, axis=2 ) # Compute the arithmetic mean along the specified axis.将rgb数据求平均,融合成新的数据 img = img.astype(np.uint8) if self.mirrors[i]: self.origLandmarks[i] = utils.mirrorShape( self.origLandmarks[i], img.shape) img = np.fliplr(img) if self.color: img = np.transpose(img, (2, 0, 1)) else: img = img[ np. newaxis] # 在第一维增加一个新的坐标轴,shape由539 * 410,变为1 * 539 * 410 groundTruth = self.origLandmarks[i] # 取到 if self.initialization == 'rect': bestFit = utils.bestFitRect( groundTruth, self.meanShape) # 返回由平均特征点经过缩放和偏移,生成的初始特征点坐标。 elif self.initialization == 'similarity': bestFit = utils.bestFit(groundTruth, self.meanShape) elif self.initialization == 'box': bestFit = utils.bestFitRect(groundTruth, self.meanShape, box=self.boundingBoxes[i]) self.imgs.append(img) # 把文件夹中的图片依次加入list中 self.initLandmarks.append( bestFit) # 把文件夹中的图片对应的初始landmark坐标依次加入list中 self.gtLandmarks.append(groundTruth) if i % 500 == 0: print("the %dth pic in func LoadImages()" % i) self.initLandmarks = np.array(self.initLandmarks) self.gtLandmarks = np.array(self.gtLandmarks)
def LoadImages(self): self.imgs = [] self.initLandmarks = [] self.gtLandmarks = [] for i in range(len(self.filenames)): # 这段代码写得不好,self.color并没有实际意义 img = ndimage.imread(self.filenames[i]) if self.color: if len(img.shape) == 2: img = np.dstack((img, img, img)) else: # img = ndimage.imread(self.filenames[i], mode='L') if len(img.shape) > 2: img = np.mean(img, axis=2) img = img.astype(np.uint8) if self.mirrors[i]: self.origLandmarks[i] = utils.mirrorShape( self.origLandmarks[i], img.shape) img = np.fliplr(img) if not self.color: # img = np.transpose(img, (2, 0, 1)) # else: img = img[np.newaxis] # img从shape(H,W)变成shape(1,H,W) # img = np.transpose(img, (1, 2, 0)) groundTruth = self.origLandmarks[i] if self.initialization == 'rect': # 仅仅把meanshape适应进入由landmark确定的框中 bestFit = utils.bestFitRect(groundTruth, self.meanShape) elif self.initialization == 'similarity': # 找到meanShape到gt的最优变换,并变换之 bestFit = utils.bestFit(groundTruth, self.meanShape) elif self.initialization == 'box': # 仅仅把meanshape适应进入由检测到的bbx确定的框中 bestFit = utils.bestFitRect(groundTruth, self.meanShape, box=self.boundingBoxes[i]) self.imgs.append(img) self.initLandmarks.append(bestFit) self.gtLandmarks.append(groundTruth) self.initLandmarks = np.array(self.initLandmarks) self.gtLandmarks = np.array(self.gtLandmarks)
def LoadImages(self): self.imgs = [] self.initLandmarks = [] self.gtLandmarks = [] for i in range(len(self.filenames)): img = ndimage.imread(self.filenames[i]) if self.color: if len(img.shape) == 2: img = np.dstack((img, img, img)) else: if len(img.shape) > 2: img = np.mean(img, axis=2) img = img.astype(np.uint8) if self.mirrors[i]: self.origLandmarks[i] = utils.mirrorShape( self.origLandmarks[i], img.shape) img = np.fliplr(img) if self.color: img = np.transpose(img, (2, 0, 1)) else: img = img[np.newaxis] groundTruth = self.origLandmarks[i] if self.initialization == 'rect': bestFit = utils.bestFitRect(groundTruth, self.meanShape) elif self.initialization == 'similarity': bestFit = utils.bestFit(groundTruth, self.meanShape) elif self.initialization == 'box': bestFit = utils.bestFitRect(groundTruth, self.meanShape, box=self.boundingBoxes[i]) self.imgs.append(img) self.initLandmarks.append(bestFit) self.gtLandmarks.append(groundTruth) self.initLandmarks = np.array(self.initLandmarks) self.gtLandmarks = np.array(self.gtLandmarks)
def get_landmarks(bb, img, model): landmarks = None print("bb: ", bb) initLandmarks = utils.bestFitRect(None, model.initLandmarks, [bb[0], bb[1], bb[2], bb[3]]) landmarks, confidence = model.processImg(img[np.newaxis], initLandmarks) print("conf: ", confidence) if confidence < 0.3: landmarks = None return landmarks
def alignment(self, bb, img): landmarks = None initLandmarks = utils.bestFitRect(None, self.model.initLandmarks, [bb[0], bb[1], bb[2], bb[3]]) landmarks = self.model.processImg(img[np.newaxis], initLandmarks) """ landmarks, confidence = self.model.processImg(img[np.newaxis], initLandmarks) print("alignment conf: ", confidence) if confidence < 0.9: landmarks = None """ return landmarks
def faceKeypointsDetector(faceImage, model): img = faceImage.astype(np.uint8) minX = 0 maxX = faceImage.shape[1] minY = 0 maxY = faceImage.shape[0] initLandmarks = utils.bestFitRect(None, model.initLandmarks, [minX, minY, maxX, maxY]) if model.confidenceLayer: landmarks, confidence = model.processImg(img[np.newaxis], initLandmarks) if confidence < 0.1: reset = True else: landmarks = model.processImg(img[np.newaxis], initLandmarks) landmarks = landmarks.astype(np.int32) for i in range(landmarks.shape[0]): cv2.circle(faceImage, (landmarks[i, 0], landmarks[i, 1]), 2, (0, 255, 0)) cv2.imshow("faceKeypoints", faceImage) cv2.waitKey(1)
# reset = True landmarks = None # if reset: rects = cascade.detectMultiScale(gray_img, scaleFactor=1.2, minNeighbors=3, minSize=(50, 50)) for rect in rects: tl_x = rect[0] tl_y = rect[1] br_x = tl_x + rect[2] br_y = tl_y + rect[3] cv2.rectangle(color_img, (tl_x, tl_y), (br_x, br_y), (255, 0, 0)) initLandmarks = utils.bestFitRect(None, model.initLandmarks, [tl_x, tl_y, br_x, br_y]) if model.confidenceLayer: landmarks, confidence = model.processImg(gray_img[np.newaxis], initLandmarks) if confidence < 0.1: reset = True else: landmarks = model.processImg(gray_img[np.newaxis], initLandmarks) landmarks = landmarks.astype(np.int32) for i in range(landmarks.shape[0]): cv2.circle(color_img, (landmarks[i, 0], landmarks[i, 1]), 2, (0, 255, 0)) cv2.imshow("image", color_img) key = cv2.waitKey(0)
img = np.mean(vis, axis=2).astype(np.uint8) else: img = vis.astype(np.uint8) if reset: rects = cascade.detectMultiScale(img, scaleFactor=1.2, minNeighbors=3, minSize=(50, 50)) if len(rects) > 0: minX = rects[0][0] maxX = rects[0][0] + rects[0][2] minY = rects[0][1] maxY = rects[0][1] + rects[0][3] cv2.rectangle(vis, (minX, minY), (maxX, maxY), (255, 0, 0)) initLandmarks = utils.bestFitRect(None, model.initLandmarks, [minX, minY, maxX, maxY]) reset = False if model.confidenceLayer: landmarks, confidence = model.processImg( img[np.newaxis], initLandmarks) if confidence < 0.1: reset = True else: landmarks = model.processImg(img[np.newaxis], initLandmarks) landmarks = landmarks.astype(np.int32) for i in range(landmarks.shape[0]): cv2.circle(vis, (landmarks[i, 0], landmarks[i, 1]), 2, (0, 255, 0)) else: initLandmarks = utils.bestFitRect(landmarks, model.initLandmarks)
img = cv2.imread(filenames[i]) imgColor = np.copy(img[:, :, [2, 1, 0]]) if len(img.shape) > 2: img = np.mean(img, axis=2) faceHeight = img.shape[0] * imageHeightFraction faceWidth = faceHeight center = np.array(img.shape) / 2 box = [ center[1] - faceWidth / 2, center[0] - faceHeight / 2, center[1] + faceWidth / 2, center[0] + faceHeight / 2 ] #first step initLandmarks = utils.bestFitRect([], network.initLandmarks, box) firstStepLandmarks = network.processImg(img[np.newaxis], initLandmarks) #second step normImg, transform = network.CropResizeRotate(img[np.newaxis], firstStepLandmarks) normFirstStepLandmarks = np.dot(firstStepLandmarks, transform[0]) + transform[1] initLandmarks2 = utils.bestFitRect(normFirstStepLandmarks, network.initLandmarks) finalLandmarks = network.processImg(normImg, initLandmarks2) finalLandmarks = np.dot(finalLandmarks - transform[1], np.linalg.inv(transform[0])) baseName = ntpath.basename(filenames[i])[:-4]