1

I have a very simple code that I use for face detection from an image, for example:

from deepface.commons import functions
import numpy as np

random_image = np.random.randint(
    0, 255, size=(360, 360, 3)
)

detected_face = functions.detect_face(
    img=random_image,
    detector_backend="mtcnn",
    enforce_detection=False,
)[0]

This code prints out the following logs (made by MTCNN backend):

1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 13ms/step
1/1 [==============================] - 0s 11ms/step
1/1 [==============================] - 0s 11ms/step
1/1 [==============================] - 0s 11ms/step
1/1 [==============================] - 0s 10ms/step
1/1 [==============================] - 0s 9ms/step
1/1 [==============================] - 0s 10ms/step
5/5 [==============================] - 0s 4ms/step
1/1 [==============================] - 0s 16ms/step

Is there a way how to suppress deepface to print the logs, please?

Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46

2 Answers2

2

Somtimes when you cannot supress printing the messages, you can decoreate function and capture the output.

from functools import wraps
from deepface.commons import functions
import sys
import io


def capture_output(func):
    """Wrapper to capture print output."""

    @wraps(func)
    def wrapper(*args, **kwargs):
        old_stdout = sys.stdout
        new_stdout = io.StringIO()
        sys.stdout = new_stdout
        try:
            return func(*args, **kwargs)
        finally:
            sys.stdout = old_stdout

    return wrapper

w_detect_face = capture_output(functions.detect_face)

detected_face = w_detect_face(
    img=random_image,
    detector_backend="mtcnn",
    enforce_detection=False,
)[0]
Peter Trcka
  • 1,279
  • 1
  • 16
  • 21
2

please reach to mtcnn.py file in your pc. It is perhaps in the path ~Python\Python311\Lib\site-packages\ in windows OS. add verbose=0 in lines: 342, 410 and 466:

     line 342: out = self._pnet.predict(img_y)
     Line 410: out = self._rnet.predict(tempimg1) 
     Line 466: out = self._onet.predict(tempimg1)

like this:

     line 342: out = self._pnet.predict(img_y,verbose=0)
     Line 410: out = self._rnet.predict(tempimg1,verbose=0) 
     Line 466: out = self._onet.predict(tempimg1,verbose=0)

`

MJELVEH
  • 83
  • 5
  • This actually works and seems to be the only working solution. The only problem is it's not ideal as you'll have to patch the dependent libs of deepface. – nonsequiter Jul 16 '23 at 12:49