class FaceDetector(object): def __init__(self): self.dataSetFile = '' self.successLogger = Logger( os.path.join(FACE_DETECTION_ROOT, 'faceDetection.success')) self.errorLogger = Logger( os.path.join(FACE_DETECTION_ROOT, 'faceDetection.error')) self.boundingboxFile = Logger( os.path.join(FACE_DETECTION_ROOT, 'boundingbox.list')) self.cc = cv2.CascadeClassifier( os.path.join(FACE_DETECTION_ROOT, 'haarcascade_frontalface_alt.xml')) def setDataSetFile(self, filePath): self.dataSetFile = filePath def run(self): fid = open(self.dataSetFile, 'r').read().splitlines() for src in fid: self._detectFace(src) def _detectFace(self, imgPath): img = cv2.imread(imgPath) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) rects = self.cc.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=2, \ minSize=(30, 30), flags = cv2.CASCADE_SCALE_IMAGE) if not len(rects): self.errorLogger.writeMsg("%s\n" % imgPath) return for rect in rects: rect[2:] += rect[:2] self.successLogger.writeMsg("%s\n" % imgPath) ''' boundingbox format: left right top bottom ''' self.boundingboxFile.writeMsg("%s %s %s %s %s\n" % \ (imgPath, str(rect[0]), str(rect[2]), str(rect[1]), str(rect[3])))
def detectLandmarks(boundingboxList): """ detect landmarks in `src` and store the result in `dst` """ #bboxes = [] #landmarks = [] fl = Landmarker() logger = Logger(os.path.join(FACE_ALIGNMENT_ROOT, 'landmark.list')) # create bbox list fid = open(boundingboxList, 'r'); fLines = fid.read().splitlines() fid.close() for line in fLines: word = line.split() filename = word[0] img = cv2.imread(filename) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) bbox = BBox([int(word[1]), int(word[2]), int(word[3]), int(word[4])])\ .subBBox(0.1, 0.9, 0.2, 1) landmark, status = fl.detectLandmark(gray, bbox) ''' get real landmark position ''' landmark = bbox.reprojectLandmark(landmark) logger.writeMsg("%s" % filename) for x, y in landmark: logger.writeMsg(" %s %s" % (str(x), str(y))) logger.writeMsg('\n') ''' free memory: force the Garbage Collector to release ''' gc.collect()
class FaceDetector(object): def __init__(self): self.dataSetFile = '' self.successLogger = Logger(os.path.join(FACE_DETECTION_ROOT, 'faceDetection.success')) self.errorLogger = Logger(os.path.join(FACE_DETECTION_ROOT, 'faceDetection.error')) self.boundingboxFile = Logger(os.path.join(FACE_DETECTION_ROOT, 'boundingbox.list')) self.cc = cv2.CascadeClassifier(os.path.join(FACE_DETECTION_ROOT, 'haarcascade_frontalface_alt.xml')) def setDataSetFile(self, filePath): self.dataSetFile = filePath def run(self): fid = open(self.dataSetFile, 'r').read().splitlines() for src in fid: self._detectFace(src) def _detectFace(self, imgPath): img = cv2.imread(imgPath) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) rects = self.cc.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=2, \ minSize=(30, 30), flags = cv2.CASCADE_SCALE_IMAGE) if not len(rects): self.errorLogger.writeMsg("%s\n" % imgPath) return for rect in rects: rect[2:] += rect[:2] self.successLogger.writeMsg("%s\n" % imgPath) ''' boundingbox format: left right top bottom ''' self.boundingboxFile.writeMsg("%s %s %s %s %s\n" % \ (imgPath, str(rect[0]), str(rect[2]), str(rect[1]), str(rect[3])))