GithubHelp home page GithubHelp logo

ipazc / mtcnn Goto Github PK

View Code? Open in Web Editor NEW
2.1K 42.0 514.0 2.82 MB

MTCNN face detection implementation for TensorFlow, as a PIP package.

License: MIT License

Python 7.86% Jupyter Notebook 92.14%
mtcnn face detection tensorflow pip package python3 landmark

mtcnn's Introduction

MTCNN

image

image

Implementation of the MTCNN face detector for Keras in Python3.4+. It is written from scratch, using as a reference the implementation of MTCNN from David Sandberg (FaceNet's MTCNN) in Facenet. It is based on the paper Zhang, K et al. (2016) [ZHANG2016].

image

INSTALLATION

Currently it is only supported Python3.4 onwards. It can be installed through pip:

$ pip install mtcnn

This implementation requires OpenCV>=4.1 and Keras>=2.0.0 (any Tensorflow supported by Keras will be supported by this MTCNN package). If this is the first time you use tensorflow, you will probably need to install it in your system:

$ pip install tensorflow

or with conda

$ conda install tensorflow

Note that tensorflow-gpu version can be used instead if a GPU device is available on the system, which will speedup the results.

USAGE

The following example illustrates the ease of use of this package:

>>> from mtcnn import MTCNN
>>> import cv2
>>>
>>> img = cv2.cvtColor(cv2.imread("ivan.jpg"), cv2.COLOR_BGR2RGB)
>>> detector = MTCNN()
>>> detector.detect_faces(img)
[
    {
        'box': [277, 90, 48, 63],
        'keypoints':
        {
            'nose': (303, 131),
            'mouth_right': (313, 141),
            'right_eye': (314, 114),
            'left_eye': (291, 117),
            'mouth_left': (296, 143)
        },
        'confidence': 0.99851983785629272
    }
]

The detector returns a list of JSON objects. Each JSON object contains three main keys: 'box', 'confidence' and 'keypoints':

  • The bounding box is formatted as [x, y, width, height] under the key 'box'.
  • The confidence is the probability for a bounding box to be matching a face.
  • The keypoints are formatted into a JSON object with the keys 'left_eye', 'right_eye', 'nose', 'mouth_left', 'mouth_right'. Each keypoint is identified by a pixel position (x, y).

Another good example of usage can be found in the file "example.py." located in the root of this repository. Also, you can run the Jupyter Notebook "example.ipynb" for another example of usage.

BENCHMARK

The following tables shows the benchmark of this mtcnn implementation running on an Intel i7-3612QM CPU @ 2.10GHz, with a CPU-based Tensorflow 1.4.1.

  • Pictures containing a single frontal face:
Image size Total pixels Process time FPS
460x259 119,140 0.118 seconds 8.5
561x561 314,721 0.227 seconds 4.5
667x1000 667,000 0.456 seconds 2.2
1920x1200 2,304,000 1.093 seconds 0.9
4799x3599 17,271,601 8.798 seconds 0.1
  • Pictures containing 10 frontal faces:
Image size Total pixels Process time FPS
474x224 106,176 0.185 seconds 5.4
736x348 256,128 0.290 seconds 3.4
2100x994 2,087,400 1.286 seconds 0.7

MODEL

By default the MTCNN bundles a face detection weights model.

The model is adapted from the Facenet's MTCNN implementation, merged in a single file located inside the folder 'data' relative to the module's path. It can be overriden by injecting it into the MTCNN() constructor during instantiation.

The model must be numpy-based containing the 3 main keys "pnet", "rnet" and "onet", having each of them the weights of each of the layers of the network.

For more reference about the network definition, take a close look at the paper from Zhang et al. (2016) [ZHANG2016].

LICENSE

MIT License.

REFERENCE

ZHANG2016

Zhang, K., Zhang, Z., Li, Z., and Qiao, Y. (2016). Joint face detection and alignment using multitask cascaded convolutional networks. IEEE Signal Processing Letters, 23(10):1499–1503.

mtcnn's People

Contributors

akofman avatar christian-rncl avatar cyrusofeden avatar elmahyai avatar huberemanuel avatar ipazc avatar justinshenk avatar r-or avatar raviam avatar xanjay avatar xelldart avatar xolott avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mtcnn's Issues

Failed to load weights

When I tried to create an instance of MTCNN detector, something went wrong:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-be16e7eec9f4> in <module>
      3 
      4 img = cv2.imread("ivan.jpg")
----> 5 detector = MTCNN()
      6 
      7 for i in range(len(images)):

.../lib/python3.5/site-packages/mtcnn/mtcnn.py in __init__(self, weights_file, min_face_size, steps_threshold, scale_factor)
    193             self.__session = tf.Session(config=config, graph=self.__graph)
    194 
--> 195             weights = np.load(weights_file).item()
    196             self.__pnet = PNet(self.__session, False)
    197             self.__pnet.set_weights(weights['PNet'])

.../lib/python3.5/site-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
    445             else:
    446                 return format.read_array(fid, allow_pickle=allow_pickle,
--> 447                                          pickle_kwargs=pickle_kwargs)
    448         else:
    449             # Try a pickle

.../lib/python3.5/site-packages/numpy/lib/format.py in read_array(fp, allow_pickle, pickle_kwargs)
    690         # The array contained Python objects. We need to unpickle the data.
    691         if not allow_pickle:
--> 692             raise ValueError("Object arrays cannot be loaded when "
    693                              "allow_pickle=False")
    694         if pickle_kwargs is None:

ValueError: Object arrays cannot be loaded when allow_pickle=False

I googled a bit and now think that problem causes due to new versions of numpy (see this issue: tensorflow/tensorflow#28102).

Passing batch of images for faster processing

Hi, First of all thanks so much for your effort in bringing MTCNN to Python in an easy usable way.

I wanted to know if there is a way to pass a batch of images (instead of just one image) to the GPU to increase the overall speed of face detections for lots of images.

Thanks again

error with my own picture

Excuse me, when I us my own picture to run example.py ,it makes a mistake as follows:

Exception has occurred: IndexError
list index out of range
File "D:\Oneconnnect\mtcnn-master\example.py", line 13, in
bounding_box = result[0]['box']
I find that the result is empty.
Then I use another picture ,it can be successful. I want to know why i use some picture it can not run successfully.

Regarding the MIT License

Hi, thank you for this MTCNN implementation!

Regarding the MIT license of this repo -- if you have used FDDB/WIDERFace dataset for training your models (which are provided only for the research purposes, not commercial usage), how can the weights you provide be on the MIT license?

MIT license allows anyone to use this commercially, which is a derivative work of the images which are not allowed to be used commercially.

Looking forward to your reply. Thanks!

What is the optimal frame resolution and size for detectface method?

Found lagging if frame size goes beyond 100KB. Face detection accuracy affects if resolution comes down. What could be the optimal frame resolution and size for the face detect method?

https://stackoverflow.com/questions/57812858/mtcnn-face-detection-alignment-lagging-in-ip-camera-convention-behind-opencv-cv

https://stackoverflow.com/questions/57852160/how-to-scale-down-physical-size-of-image-in-python-opencv?noredirect=1&lq=1

related questions.

Pytest crashes when using mtcnn package in a project with 'tests' folder

Hi Iván!
Thank you very much for the package. The code here is much more readable than in the original implementation and I like the fact it is on PyPi, which makes it easier to integrate the functionality into different projects.
But, I faced a problem with running my tests. It is caused by the line in mtcnn/setup.py:
packages=setuptools.find_packages()
which includes your 'tests' into the release. After this when referring to 'tests', Python cannot find the local ones. It would be better to write explicitly
packages=setuptools.find_packages(include=['mtcnn.*'])
(which remover tests from the package) or move the tests folder from the repository root.

Generate a new image with bounding box and circles

Hello, thanks for sharing your code

How to generate a new image with bounding box and circles?

I tried to code:
>>> from mtcnn.mtcnn import MTCNN
>>> import cv2
>>>
>>> img = cv2.imread("max.jpg")
>>> detector = MTCNN()
>>> print(detector.detect_faces(img))
>>> image = cv2.imwrite("max_drawn.jpg",img)

(max.jpg is my picture)

thank you

ValueError: Variable pnet/conv1/weights already exists

When I run the code, I get the following error. I would appreciate if someone helped me.
I have:
python 3.6
mtcnn 0.0.7
tensorflow 1.9.0 running on CPU
All installed with pip

File "<ipython-input-1-59d1cc96181f>", line 1, in <module>
    runfile('D:/FaceRecognition/Main.py', wdir='D:/FaceRecognition')

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 678, in runfile
    execfile(filename, namespace)

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 106, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/FaceRecognition/Main.py", line 32, in <module>
    main()

  File "D:/FaceRecognition/Main.py", line 19, in main
    addNewFace()

  File "D:\FaceRecognition\Engine.py", line 30, in addNewFace
    detector = MTCNN()

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\mtcnn\mtcnn.py", line 193, in __init__
    self.__pnet = PNet(self.__session, False)

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\mtcnn\network.py", line 44, in __init__
    self._config()

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\mtcnn\mtcnn.py", line 55, in _config
    padding='VALID', relu=False)

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\mtcnn\layer_factory.py", line 123, in new_conv
    kernel = self.__make_var('weights', shape=[kernel_size[1], kernel_size[0], channels_input // group, channels_output])

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\mtcnn\layer_factory.py", line 78, in __make_var
    return tf.get_variable(name, shape, trainable=self.__network.is_trainable())

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1328, in get_variable
    constraint=constraint)

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1090, in get_variable
    constraint=constraint)

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 435, in get_variable
    constraint=constraint)

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 404, in _true_getter
    use_resource=use_resource, constraint=constraint)

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 743, in _get_single_variable
    name, "".join(traceback.format_list(tb))))

ValueError: Variable pnet/conv1/weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\tensorflow\python\framework\ops.py", line 1740, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access
  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\tensorflow\python\framework\ops.py", line 3414, in create_op
    op_def=op_def)
  File "C:\Users\kaany\Anaconda3\envs\facerec\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)

When i use tensorflow-gpu i got error

My opencv version is 4-1 and tensorflow-gpu version is 1-13-1, and when i rundetector = MTCNN() just like what the README said i got a attribute error said ' 'MTCNN' object has no attribute '_MTCNN__session' '. Could anyone tell me how to fix it?

Getting negative image coordinates from 'box' key

Hi,
Thanks for this amazing MTCNN for faces.
I would like to know why negative coordinates in 'box' key (like (55, -5, 49, 79)) are fetching in face detection.In OpenCV 2d image coordinates starts from (0,0). How to assume this negative coordinates.

Tensorflow 1.14.0 API Deprecation Warnings

Tensorflow is deprecating the v1 API, and moving it into tf.compat.v1. In addition, some elements of the API are being replaced by new API.

Example output:

W0703 09:23:51.102508 140059022907200 deprecation.py:323] From site-packages/tensorflow/python/training/queue_runner_impl.py:391: QueueRunner.__init__ (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
W0703 09:23:51.141032 140059022907200 deprecation.py:323] From site-packages/tensorflow/python/training/saver.py:1276: checkpoint_exists (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.
Instructions for updating:
Use standard file APIs to check for files with this prefix.
2019-07-03 09:23:52.959783: W tensorflow/compiler/jit/mark_for_compilation_pass.cc:1412] (One-time warning): Not using XLA:CPU for cluster because envvar TF_XLA_FLAGS=--tf_xla_cpu_global_jit was not set.  If you want XLA:CPU, either set that envvar, or use experimental_jit_scope to enable XLA:CPU.  To confirm that XLA is active, pass --vmodule=xla_compilation_cache=1 (as a proper command-line flag, not via TF_XLA_FLAGS) or set the envvar XLA_FLAGS=--xla_hlo_profile.
W0703 09:23:53.422218 140059022907200 deprecation_wrapper.py:119] From site-packages/mtcnn/mtcnn.py:187: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

W0703 09:23:53.422743 140059022907200 deprecation_wrapper.py:119] From site-packages/mtcnn/mtcnn.py:193: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

W0703 09:23:53.433550 140059022907200 deprecation_wrapper.py:119] From site-packages/mtcnn/network.py:43: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.

W0703 09:23:53.434048 140059022907200 deprecation_wrapper.py:119] From site-packages/mtcnn/layer_factory.py:88: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

W0703 09:23:53.435072 140059022907200 deprecation_wrapper.py:119] From site-packages/mtcnn/layer_factory.py:79: The name tf.get_variable is deprecated. Please use tf.compat.v1.get_variable instead.

W0703 09:23:53.435565 140059022907200 deprecation.py:506] From site-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
W0703 09:23:53.454373 140059022907200 deprecation_wrapper.py:119] From site-packages/mtcnn/layer_factory.py:171: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

W0703 09:23:53.505696 140059022907200 deprecation.py:323] From site-packages/mtcnn/layer_factory.py:221: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Deprecated in favor of operator or tf.math.divide.
W0703 09:23:53.700929 140059022907200 deprecation_wrapper.py:119] From site-packages/mtcnn/layer_factory.py:196: The name tf.nn.xw_plus_b is deprecated. Please use tf.compat.v1.nn.xw_plus_b instead.

This issue is distinct from #3, which refers to the keep_dims deprecation.

I understand that I can filter warnings to suppress them, but sometimes they contain useful notifications that are relevant elsewhere in my codebase, so I'd prefer not to if I can help it!

Is there a roadmap to migrate to non-deprecated Tensorflow APIs?

Predict multiple faces in image

When run predict on an image, for example:

>>> from mtcnn.mtcnn import MTCNN
>>> import cv2
>>>
>>> img = cv2.imread("ivan.jpg")
>>> detector = MTCNN()
>>> print(detector.detect_faces(img))

Is it possible to predict multiple faces in image? Return each face in array element, something like :

[
    {'box': [277, 90, 48, 63], ...},
    {'box': [277, 90, 48, 63], ...},
    {'box': [277, 90, 48, 63], ...}
]

result is not same as facenet?

Hi

I use this image as input

2007_007763

The result is

2007_007763_drawn

I use mtcnn in facenet with the same image, it can catch all faces

i install mtcnn use
pip3 install mtcnn

Why flip dx1, dy1, dx2, dy2 in generateBoundingBox when y.shape[0]==1? Please help me~

    def __generate_bounding_box(imap, reg, scale, t):

        # use heatmap to generate bounding boxes
        stride = 2
        cellsize = 12

        imap = np.transpose(imap)
        dx1 = np.transpose(reg[:, :, 0])
        dy1 = np.transpose(reg[:, :, 1])
        dx2 = np.transpose(reg[:, :, 2])
        dy2 = np.transpose(reg[:, :, 3])

        y, x = np.where(imap >= t)

        if y.shape[0] == 1:
            dx1 = np.flipud(dx1)
            dy1 = np.flipud(dy1)
            dx2 = np.flipud(dx2)
            dy2 = np.flipud(dy2)

        score = imap[(y, x)]
        reg = np.transpose(np.vstack([dx1[(y, x)], dy1[(y, x)], dx2[(y, x)], dy2[(y, x)]]))

        if reg.size == 0:
            reg = np.empty(shape=(0, 3))

        bb = np.transpose(np.vstack([y, x]))

        q1 = np.fix((stride * bb + 1)/scale)
        q2 = np.fix((stride * bb + cellsize)/scale)
        boundingbox = np.hstack([q1, q2, np.expand_dims(score, 1), reg])

        return boundingbox, reg

Understand min_face_size param in Mtcnn

Hi, I am try to use different min_face_size in Mtcnn, but I wonder what is the exactly meaning of this param? It does not seem like with this meaning: 'minimum size of the face to detect' in the comment there.

No such file or directory; torchvision-0.2.1-py3.6.egg

I have included the error traceback and directory listing.

import cv2
import dlib
import argparse
import time
import dlib.cuda as cuda
from mtcnn.mtcnn import MTCNN

here is the directory listing

ubuntu@cuda:~$ ls anaconda3/lib/python3.6/site-packages/torch*
anaconda3/lib/python3.6/site-packages/torch:
_C.cpython-36m-x86_64-linux-gnu.so _utils.py multiprocessing
init.py _utils_internal.py nn
pycache autograd onnx
_dl.cpython-36m-x86_64-linux-gnu.so backends optim
_jit_internal.py contrib random.py
_nvrtc.cpython-36m-x86_64-linux-gnu.so cuda serialization.py
_ops.py distributed share
_six.py distributions sparse
_storage_docs.py for_onnx storage.py
_tensor_docs.py functional.py tensor.py
_tensor_str.py hub.py testing
_thnn jit utils
_torch_docs.py lib version.py

anaconda3/lib/python3.6/site-packages/torch-1.0.0-py3.6.egg-info:
PKG-INFO SOURCES.txt dependency_links.txt entry_points.txt top_level.txt

anaconda3/lib/python3.6/site-packages/torchvision:
init.py pycache datasets models transforms utils.py

anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py2.7.egg-info:
PKG-INFO dependency_links.txt top_level.txt
SOURCES.txt requires.txt zip-safe

error message


FileNotFoundError Traceback (most recent call last)
in ()
5 import time
6 import dlib.cuda as cuda
----> 7 from mtcnn.mtcnn import MTCNN
8 #from mtcnn.mtcnn import MTCNN
9

~/anaconda3/lib/python3.6/site-packages/mtcnn/mtcnn.py in ()
35 import cv2
36 import numpy as np
---> 37 import pkg_resources
38 import tensorflow as tf
39 from mtcnn.layer_factory import LayerFactory

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in ()
3084
3085
-> 3086 @_call_aside
3087 def _initialize_master_working_set():
3088 """

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in _call_aside(f, *args, **kwargs)
3068 # from jaraco.functools 1.3
3069 def _call_aside(f, *args, **kwargs):
-> 3070 f(*args, **kwargs)
3071 return f
3072

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in _initialize_master_working_set()
3097 at their own risk.
3098 """
-> 3099 working_set = WorkingSet._build_master()
3100 _declare_state('object', working_set=working_set)
3101

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in _build_master(cls)
563 Prepare the master working set.
564 """
--> 565 ws = cls()
566 try:
567 from main import requires

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in init(self, entries)
556
557 for entry in entries:
--> 558 self.add_entry(entry)
559
560 @classmethod

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in add_entry(self, entry)
612 self.entry_keys.setdefault(entry, [])
613 self.entries.append(entry)
--> 614 for dist in find_distributions(entry, True):
615 self.add(dist, entry, False)
616

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in find_eggs_in_zip(importer, path_item, only)
1878 return
1879 metadata = EggMetadata(importer)
-> 1880 if metadata.has_metadata('PKG-INFO'):
1881 yield Distribution.from_filename(path_item, metadata=metadata)
1882 if only:

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in has_metadata(self, name)
1397
1398 def has_metadata(self, name):
-> 1399 return self.egg_info and self._has(self._fn(self.egg_info, name))
1400
1401 def get_metadata(self, name):

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in _has(self, fspath)
1752 def _has(self, fspath):
1753 zip_path = self._zipinfo_name(fspath)
-> 1754 return zip_path in self.zipinfo or zip_path in self._index()
1755
1756 def _isdir(self, fspath):

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in zipinfo(self)
1629 @Property
1630 def zipinfo(self):
-> 1631 return self._zip_manifests.load(self.loader.archive)
1632
1633 def get_resource_filename(self, manager, resource_name):

~/anaconda3/lib/python3.6/site-packages/pkg_resources/init.py in load(self, path)
1586 """
1587 path = os.path.normpath(path)
-> 1588 mtime = os.stat(path).st_mtime
1589
1590 if path not in self or self[path].mtime != mtime:

FileNotFoundError: [Errno 2] No such file or directory: '/home/ubuntu/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg'

Images without faces not handled correctly by MTCNN

Images that do not contain faces raise the following error when invoking detect_faces:

>>> mtcnn.detect_faces(cv2.imread("1.jpg"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ivan/facerec/lib/python3.4/site-packages/mtcnn/mtcnn.py", line 405, in detect_faces
    for bounding_box, keypoints in zip(total_boxes, points.T):
AttributeError: 'list' object has no attribute 'T'

Ubuntu 14.04
Python 3.4
MTCNN 0.0.3

MTCNN instance with preloaded weights doesn't work when used to detect faces after that

I'm using MTCNN module as one of the final layers to detect faces. In my API, I'm pre-loading the MTCNN instance by executing the following code before spanning workers using gunicorn.
**mtcnn_detector = MTCNN()**
But when I'm trying to use it on the image coming to MTCNN layer, the detect_faces method gets stuck and the worker times out all the time.
**detector_object = mtcnn_detector.detect_faces(image_rgb)**
But surprisingly. when I initialize it just before calling detect_faces method, it works. I also noticed that the object created while preloading is the one getting used inside too when the image comes, but just that it gets stuck.
And, even when I try to use it as a singleton inside, it gets created for all the workers since it is not preloaded. Each object takes to around 200 MB approximately and I'm not in the position to afford 5 instances of it across the workers.

Any help would be of great help.

Thanks in advance.

how to increase worker cpu

Hi,

I modified the code for camera ( videocapture(0) ) for realtime test. I have mac book pro 2017 8 cores. What I saw only 3 cores is utilized.

Also I have c++ mtcnn implemantation and utilizimg more cores.

How can I increase the worker? any way ?

thx

Can't create more than one instance of MTCNN class

Creating another instance of MTCNN causes a tensorflow error. Seems like they are trying to share the same tf session:

from mtcnn.mtcnn import MTCNN

detector_1 = MTCNN(steps_threshold=(.2, .7, .7))  # ok
detector_2 = MTCNN(steps_threshold=(.1, .7, .5))  # error
ValueError                                Traceback (most recent call last)
<ipython-input-3-62d11dc62821> in <module>()
----> 1 detector_2 = MTCNN(steps_threshold=(.1, .7, .5))

~/py/ocean/test/env/lib/python3.6/site-packages/mtcnn/mtcnn.py in __init__(self, weights_file, min_face_size, steps_threshold, scale_factor)
    191
    192         weights = np.load(weights_file).item()
--> 193         self.__pnet = PNet(self.__session, False)
    194         self.__pnet.set_weights(weights['PNet'])
    195

~/py/ocean/test/env/lib/python3.6/site-packages/mtcnn/network.py in __init__(self, session, trainable)
     42
     43         with tf.variable_scope(self.__class__.__name__.lower()):
---> 44             self._config()
     45
     46     def _config(self):

~/py/ocean/test/env/lib/python3.6/site-packages/mtcnn/mtcnn.py in _config(self)
     53         layer_factory.new_feed(name='data', layer_shape=(None, None, None, 3))
     54         layer_factory.new_conv(name='conv1', kernel_size=(3, 3), channels_output=10, stride_size=(1, 1),
---> 55                                padding='VALID', relu=False)
     56         layer_factory.new_prelu(name='prelu1')
     57         layer_factory.new_max_pool(name='pool1', kernel_size=(2, 2), stride_size=(2, 2))

~/py/ocean/test/env/lib/python3.6/site-packages/mtcnn/layer_factory.py in new_conv(self, name, kernel_size, channels_output, stride_size, padding, group, biased, relu, input_layer_name)
    121
    122         with tf.variable_scope(name) as scope:
--> 123             kernel = self.__make_var('weights', shape=[kernel_size[1], kernel_size[0], channels_input // group, channels_output])
    124
    125             output = convolve(input_layer, kernel)

~/py/ocean/test/env/lib/python3.6/site-packages/mtcnn/layer_factory.py in __make_var(self, name, shape)
     76         :return: created TF variable.
     77         """
---> 78         return tf.get_variable(name, shape, trainable=self.__network.is_trainable())
     79
     80     def new_feed(self, name: str, layer_shape: tuple):

~/py/ocean/test/env/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in get_variable(name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint)
   1315       partitioner=partitioner, validate_shape=validate_shape,
   1316       use_resource=use_resource, custom_getter=custom_getter,
-> 1317       constraint=constraint)
   1318 get_variable_or_local_docstring = (
   1319     """%s

~/py/ocean/test/env/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint)
   1077           partitioner=partitioner, validate_shape=validate_shape,
   1078           use_resource=use_resource, custom_getter=custom_getter,
-> 1079           constraint=constraint)
   1080
   1081   def _get_partitioned_variable(self,

~/py/ocean/test/env/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in get_variable(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint)
    423           caching_device=caching_device, partitioner=partitioner,
    424           validate_shape=validate_shape, use_resource=use_resource,
--> 425           constraint=constraint)
    426
    427   def _get_partitioned_variable(

~/py/ocean/test/env/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in _true_getter(name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, constraint)
    392           trainable=trainable, collections=collections,
    393           caching_device=caching_device, validate_shape=validate_shape,
--> 394           use_resource=use_resource, constraint=constraint)
    395
    396     if custom_getter is not None:

~/py/ocean/test/env/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in _get_single_variable(self, name, shape, dtype, initializer, regularizer, partition_info, reuse, trainable, collections, caching_device, validate_shape, use_resource, constraint)
    731                          "reuse=tf.AUTO_REUSE in VarScope? "
    732                          "Originally defined at:\n\n%s" % (
--> 733                              name, "".join(traceback.format_list(tb))))
    734       found_var = self._vars[name]
    735       if not shape.is_compatible_with(found_var.get_shape()):

ValueError: Variable pnet/conv1/weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

  File "/Users/erizo/py/ocean/test/env/lib/python3.6/site-packages/mtcnn/layer_factory.py", line 78, in __make_var
    return tf.get_variable(name, shape, trainable=self.__network.is_trainable())
  File "/Users/erizo/py/ocean/test/env/lib/python3.6/site-packages/mtcnn/layer_factory.py", line 123, in new_conv
    kernel = self.__make_var('weights', shape=[kernel_size[1], kernel_size[0], channels_input // group, channels_output])
  File "/Users/erizo/py/ocean/test/env/lib/python3.6/site-packages/mtcnn/mtcnn.py", line 55, in _config
    padding='VALID', relu=False)

RGB or BGR input?

The readme.md has a code example of how to use the code:

img = cv2.imread("ivan.jpg")
detector = MTCNN()
print(detector.detect_faces(img))

In this example, the img is a BGR image (opencv format). But, I think the correct is to convert the image to RGB.

I did some tests, and the model has better results with RGB images.

Need face alignment feature

Currently repo doesn't include face alignment which was part of the original work of joint face detection and alignment , does any body has an idea how it can be achieved ?

Benchmark information?

@ipazc , could provide more details about the benchmark setup? In particular:

  • What minFaceSize, scaleFactor and stepsThreshold where used?
  • Could you please share the benchmark test images, so i can compare it to my benchmarks?

Thank you!

Tensorflow Errored

Traceback (most recent call last):
……
File "……", line 16, in
detector_ = MTCNN()

File "E:\Programs\Anaconda3\lib\site-packages\mtcnn\mtcnn.py", line 193, in init
self.__pnet = PNet(self.__session, False)

File "E:\Programs\Anaconda3\lib\site-packages\mtcnn\network.py", line 44, in init
self._config()

File "E:\Programs\Anaconda3\lib\site-packages\mtcnn\mtcnn.py", line 55, in _config
padding='VALID', relu=False)

File "E:\Programs\Anaconda3\lib\site-packages\mtcnn\layer_factory.py", line 123, in new_conv
kernel = self.__make_var('weights', shape=[kernel_size[1], kernel_size[0], channels_input // group, channels_output])

File "E:\Programs\Anaconda3\lib\site-packages\mtcnn\layer_factory.py", line 78, in __make_var
return tf.get_variable(name, shape, trainable=self.__network.is_trainable())

File "E:\Programs\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1065, in get_variable
use_resource=use_resource, custom_getter=custom_getter)

File "E:\Programs\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 962, in get_variable
use_resource=use_resource, custom_getter=custom_getter)

File "E:\Programs\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 367, in get_variable
validate_shape=validate_shape, use_resource=use_resource)

File "E:\Programs\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 352, in _true_getter
use_resource=use_resource)

File "E:\Programs\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 664, in _get_single_variable
name, "".join(traceback.format_list(tb))))

ValueError: Variable pnet/conv1/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

File "E:\Programs\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1204, in init
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
File "E:\Programs\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2630, in create_op
original_op=self._default_original_op, op_def=op_def)
File "E:\Programs\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
op_def=op_def)

How to skip this error? bounding_box = result[0]['box'] IndexError: list index out of range

I'm trying to process a live video feed and detect faces in every frame, but as soon as I take my face out of the camera's view, I receive an error and the code stops.

Here is a code snippet:

cap = cv2.VideoCapture(0)
while(True):
    ret, frame = cap.read()
    detector = MTCNN()
    result = detector.detect_faces(frame)

and so on...

This is the error message that I receive when the frame I'm trying to process doesn't have a face.
bounding_box = result[0]['box']
IndexError: list index out of range

I wish to skip those frames where there are no faces, and continue the processing (face detection) for further frames.
But the code just stops as soon as it encounters a frame where it cannot detect a face.
Any way to go around this issue? That is - any way to just ignore those frames where no faces are detected, and skip to the next frame?

I'll appreciate soeme help!

Taking too long to process images. How to leverage GPU properly?

I am using TensorFlow 1.8.0. with a Nvidia 960M GPU. I have obtained the following results on two sets of 5 images with different resolutions:
No. of images; Resolution; Runtime; GPU Memory used:
5; 3280x2460; 220secs; 3176Mib
5; 167x215; 61secs; 380Mib

Can anyone suggest why is it taking so long to process these images. The inference time seems to be way off from the benchmark results given in the repo.
Also for the second set of low res. images why isn't more GPU memory being utilised to process faster?
Am I unable to leverage the GPU properly? If yes, please help me out with how to go about this?
Has anyone else experienced such results?

My entire code executes two times?

Somehow when i use the mtcnn module my code executes two times.
2019-08-31 03:07:29,548 - INFO - there was a face detected
I0831 03:07:29.548512 140184487691904 test.py:66] there was a face detected

Not working for Tensorflow 1.8 on my Mac

Hello,

I was running the mtcnn on my mac. It was showing a error at this line:
detector = MTCNN()
AttributeError: module 'tensorflow' has no attribute ConfigProto

But when I downgraded my tensorflow from 1.8 to 1.4 it ran.

Also it is not working with tensorflow-gpu. It was giving an Importerror. I had to uninstall it.

Can you please look into the issue ?

Thanks in advance.

issue: image transpose in stage 1

I have a question about the transpose of image during the first stage before the scaled image feed to the pnet,
why do we need to transpose the image here?
would you help to explain this?
Thank you.

437    for scale in scales:
438        scaled_image = self.__scale_image(image, scale)
439
440        img_x = np.expand_dims(scaled_image, 0)
441        img_y = np.transpose(img_x, (0, 2, 1, 3))
442    
443        out = self.__pnet.feed(img_y)

I do not see any code the transpose operation in original code implementation line 27 original matlat code

nipples being detected as eyes

I am using it on beach volleyball photos and videos and it mistakens nipples for eyes and belly button for a mouth I believe. The landmarks are not exact but fairly close. Do I need a separate nipple/belly button detector so I can write something like if detect_nipple then it's not an eye?

Tensorflow warning of deprecation.

WARNING:tensorflow:From /usr/local/miniconda3/envs/mtcnn_py35/lib/python3.5/site-packages/mtcnn/layer_factory.py:211: calling reduce_max (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead

won't detect frontal faces lying sideways

This model will not detect the frontal face of someone lying down sideways on the screen (top of head pointing to left or right of frame). So I think it needs to be rotated or flipped.

threshold

i am using this in images, what is the point of the threshold value? my results are the same with 0.1 and 0.9?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.