ipazc / mtcnn

MTCNN face detection implementation for TensorFlow, as a PIP package.
MIT License
2.24k stars 528 forks source link

Issue while using mtcnn with Multiprocessing lib "multiprocessing" and "concurrent.futures" - cv2.resize() error #97

Closed torta24x closed 1 month ago

torta24x commented 4 years ago

@ipazc While I was using mtcnn with multiprocessing lib to do parallel inferences, the code gave an ERROR with opencv-python version 4.2.0.32 and above

Error

File "miniconda3/envs/dbai/lib/python3.7/concurrent/futures/process.py", line 476, in _chain_from_iterable_of_lists
    for element in iterable:
  File "miniconda3/envs/dbai/lib/python3.7/concurrent/futures/_base.py", line 586, in result_iterator
    yield fs.pop().result()
  File "miniconda3/envs/dbai/lib/python3.7/concurrent/futures/_base.py", line 425, in result
    return self.__get_result()
  File "miniconda3/envs/dbai/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

Implementation I have tried two ways of multiprocessing on which I received similar error:

 with concurrent.futures.ProcessPoolExecutor() as ex:
        result = ex.map(extract_face, file_list)

#------------------AND -------------------

p1= multiprocessing.Process(target=extract_face,args=(file_list[0],))
p2= multiprocessing.Process(target=extract_face,args=(file_list[1],))
p1.start()
p2.start()

p1.join()
p2.join()

Solution Eventually, I was able to figure out the reason, though I am not sure why is this problem occured. I will be glad if someone could give an explanation for this.

Error occurred due to cv2.resize operation on line:124 in mtcnn.py file Older Code

    def __scale_image(image, scale: float):

        height, width, _ = image.shape

        width_scaled = int(np.ceil(width * scale))
        height_scaled = int(np.ceil(height * scale))

        im_data = cv2.resize(image, (width_scaled, height_scaled), interpolation=cv2.INTER_AREA)

        # Normalize the image's pixels
        im_data_normalized = (im_data - 127.5) * 0.0078125

        return im_data_normalized

Modified Code

    def __scale_image(image, scale: float):

        height, width, _ = image.shape

        width_scaled = int(np.ceil(width * scale))
        height_scaled = int(np.ceil(height * scale))

        # ---------Modification: Resizing via PIL-----------
        i_m = Image.fromarray(image)
        im = im.resize((width_scaled,height_scaled))
        im_data = np.asarray(im)

        #----------Remove cv2.resize()-----------
        # im_data = cv2.resize(image, (width_scaled, height_scaled), interpolation=cv2.INTER_AREA)

        # Normalize the image's pixels
        im_data_normalized = (im_data - 127.5) * 0.0078125

        return im_data_normalized

Is this the correct way to do it, or am I missing something? Thanks

BONUS A similar error occurs with mtcnn implementation by PFLD and others (tf and pytorch) implementations on Github PFLD: https://github.com/guoqiangqi/PFLD.

ipazc commented 1 month ago

This issue should be fixed now that cv2 dependency is removed from the release version v1.0.0, in #133