From f215db6e84071077082d14f8366ae1cf1aea500f Mon Sep 17 00:00:00 2001 From: adamhrv Date: Thu, 3 Jan 2019 12:51:31 +0100 Subject: fix roi index, clean up pose, roi, records, vector --- megapixels/app/processors/face_detector.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'megapixels/app/processors/face_detector.py') diff --git a/megapixels/app/processors/face_detector.py b/megapixels/app/processors/face_detector.py index 3a90c557..75ba54d4 100644 --- a/megapixels/app/processors/face_detector.py +++ b/megapixels/app/processors/face_detector.py @@ -24,7 +24,7 @@ class DetectorMTCNN: from mtcnn.mtcnn import MTCNN self.detector = MTCNN() - def detect(self, im, size=(400,400), conf_thresh=None, pyramids=None, largest=False): + def detect(self, im, size=(400,400), conf_thresh=None, pyramids=None, largest=False, zone=None): '''Detects face using MTCNN and returns (list) of BBox :param im: (numpy.ndarray) image :returns list of BBox @@ -79,7 +79,7 @@ class DetectorDLIBCNN: self.detector = dlib.cnn_face_detection_model_v1(cfg.DIR_MODELS_DLIB_CNN) os.environ['CUDA_VISIBLE_DEVICES'] = cuda_visible_devices # reset - def detect(self, im, size=None, conf_thresh=None, pyramids=None, largest=False): + def detect(self, im, size=None, conf_thresh=None, pyramids=None, largest=False, zone=None): bboxes = [] conf_thresh = self.conf_thresh if conf_thresh is None else conf_thresh pyramids = self.pyramids if pyramids is None else pyramids @@ -96,6 +96,10 @@ class DetectorDLIBCNN: bbox = BBox.from_dlib_dim(mmod_rect.rect, dim) bboxes.append(bbox) + if zone: + bboxes = [b for b in bboxes if b.cx > zone[0] and b.cx < 1.0 - zone[0] \ + and b.cy > zone[1] and b.cy < 1.0 - zone[1]] + if largest and len(bboxes) > 1: # only keep largest bboxes.sort(key=operator.attrgetter('area'), reverse=True) -- cgit v1.2.3-70-g09d2 From b7aba5109bfdab302b82fe9021f16f73edbeb11d Mon Sep 17 00:00:00 2001 From: Adam Harvey Date: Thu, 3 Jan 2019 19:58:03 +0100 Subject: fix face detectors --- megapixels/app/processors/face_detector.py | 15 +++++-- megapixels/app/processors/face_landmarks_3d.py | 56 ++++++++++++++++++++++++-- megapixels/commands/cv/face_roi.py | 4 +- 3 files changed, 67 insertions(+), 8 deletions(-) (limited to 'megapixels/app/processors/face_detector.py') diff --git a/megapixels/app/processors/face_detector.py b/megapixels/app/processors/face_detector.py index 75ba54d4..a805a474 100644 --- a/megapixels/app/processors/face_detector.py +++ b/megapixels/app/processors/face_detector.py @@ -119,7 +119,7 @@ class DetectorDLIBHOG: self.log = logger_utils.Logger.getLogger() self.detector = dlib.get_frontal_face_detector() - def detect(self, im, size=None, conf_thresh=None, pyramids=0, largest=False): + def detect(self, im, size=None, conf_thresh=None, pyramids=0, largest=False, zone=False): conf_thresh = self.conf_thresh if conf_thresh is None else conf_thresh dnn_size = self.size if size is None else size pyramids = self.pyramids if pyramids is None else pyramids @@ -136,8 +136,13 @@ class DetectorDLIBHOG: bbox = BBox.from_dlib_dim(rect, dim) bboxes.append(bbox) + # filter to keep on faces inside zone + if zone: + bboxes = [b for b in bboxes if b.cx > zone[0] and b.cx < 1.0 - zone[0] \ + and b.cy > zone[1] and b.cy < 1.0 - zone[1]] + + # filter to keep only largest face if largest and len(bboxes) > 1: - # only keep largest bboxes.sort(key=operator.attrgetter('area'), reverse=True) bboxes = [bboxes[0]] @@ -159,7 +164,7 @@ class DetectorCVDNN: self.net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV) self.net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU) - def detect(self, im, size=None, conf_thresh=None, largest=False, pyramids=None): + def detect(self, im, size=None, conf_thresh=None, largest=False, pyramids=None, zone=False): """Detects faces and returns (list) of (BBox)""" conf_thresh = self.conf_thresh if conf_thresh is None else conf_thresh dnn_size = self.size if size is None else size @@ -175,6 +180,10 @@ class DetectorCVDNN: rect_norm = net_outputs[0, 0, i, 3:7] bboxes.append(BBox(*rect_norm)) + if zone: + bboxes = [b for b in bboxes if b.cx > zone[0] and b.cx < 1.0 - zone[0] \ + and b.cy > zone[1] and b.cy < 1.0 - zone[1]] + if largest and len(bboxes) > 1: # only keep largest bboxes.sort(key=operator.attrgetter('area'), reverse=True) diff --git a/megapixels/app/processors/face_landmarks_3d.py b/megapixels/app/processors/face_landmarks_3d.py index 84a423b0..28aff592 100644 --- a/megapixels/app/processors/face_landmarks_3d.py +++ b/megapixels/app/processors/face_landmarks_3d.py @@ -17,11 +17,61 @@ from app.settings import types class FaceLandmarks3D: # Estimates 3D facial landmarks + import face_alignment + from skimage import io def __init__(self): self.log = logger_utils.Logger.getLogger() - pass + self.fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D, flip_input=False) + def landmarks(self, im): + preds = self.fa.get_landmarks(im) + return preds - def landmarks(self): - return [1,2,3,4,100] \ No newline at end of file + def draw(self, im): + '''draws landmarks in 3d scene''' + + ''' + import face_alignment + import numpy as np + from mpl_toolkits.mplot3d import Axes3D + import matplotlib.pyplot as plt + from skimage import io + + # Run the 3D face alignment on a test image, without CUDA. + fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._3D, device='cuda:0', flip_input=True) + + input = io.imread('../test/assets/aflw-test.jpg') + preds = fa.get_landmarks(input)[-1] + + #TODO: Make this nice + fig = plt.figure(figsize=plt.figaspect(.5)) + ax = fig.add_subplot(1, 2, 1) + ax.imshow(input) + ax.plot(preds[0:17,0],preds[0:17,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) + ax.plot(preds[17:22,0],preds[17:22,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) + ax.plot(preds[22:27,0],preds[22:27,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) + ax.plot(preds[27:31,0],preds[27:31,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) + ax.plot(preds[31:36,0],preds[31:36,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) + ax.plot(preds[36:42,0],preds[36:42,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) + ax.plot(preds[42:48,0],preds[42:48,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) + ax.plot(preds[48:60,0],preds[48:60,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) + ax.plot(preds[60:68,0],preds[60:68,1],marker='o',markersize=6,linestyle='-',color='w',lw=2) + ax.axis('off') + + ax = fig.add_subplot(1, 2, 2, projection='3d') + surf = ax.scatter(preds[:,0]*1.2,preds[:,1],preds[:,2],c="cyan", alpha=1.0, edgecolor='b') + ax.plot3D(preds[:17,0]*1.2,preds[:17,1], preds[:17,2], color='blue' ) + ax.plot3D(preds[17:22,0]*1.2,preds[17:22,1],preds[17:22,2], color='blue') + ax.plot3D(preds[22:27,0]*1.2,preds[22:27,1],preds[22:27,2], color='blue') + ax.plot3D(preds[27:31,0]*1.2,preds[27:31,1],preds[27:31,2], color='blue') + ax.plot3D(preds[31:36,0]*1.2,preds[31:36,1],preds[31:36,2], color='blue') + ax.plot3D(preds[36:42,0]*1.2,preds[36:42,1],preds[36:42,2], color='blue') + ax.plot3D(preds[42:48,0]*1.2,preds[42:48,1],preds[42:48,2], color='blue') + ax.plot3D(preds[48:,0]*1.2,preds[48:,1],preds[48:,2], color='blue' ) + + ax.view_init(elev=90., azim=90.) + ax.set_xlim(ax.get_xlim()[::-1]) + plt.show() + ''' + return False \ No newline at end of file diff --git a/megapixels/commands/cv/face_roi.py b/megapixels/commands/cv/face_roi.py index a09a1ce0..c3c2ac05 100644 --- a/megapixels/commands/cv/face_roi.py +++ b/megapixels/commands/cv/face_roi.py @@ -18,7 +18,7 @@ color_filters = {'color': 1, 'gray': 2, 'all': 3} help='Override enum output filename CSV') @click.option('-m', '--media', 'opt_dir_media', default=None, help='Override enum media directory') -@click.option('--data_store', 'opt_data_store', +@click.option('--store', 'opt_data_store', type=cfg.DataStoreVar, default=click_utils.get_default(types.DataStore.HDD), show_default=True, @@ -31,7 +31,7 @@ color_filters = {'color': 1, 'gray': 2, 'all': 3} @click.option('--size', 'opt_size', type=(int, int), default=(300, 300), help='Output image size') -@click.option('-t', '--detector-type', 'opt_detector_type', +@click.option('-d', '--detector', 'opt_detector_type', type=cfg.FaceDetectNetVar, default=click_utils.get_default(types.FaceDetectNet.DLIB_CNN), help=click_utils.show_help(types.FaceDetectNet)) -- cgit v1.2.3-70-g09d2