0

I am facing this issue, but I couldn't figure out why because I don't know where something changes to str. I am using python 3.6 . This function is supposed to detect a face.

The error is detected in this line

person_name = os.path.basename(os.path.normpath(path(path)))

Code:

    def extract_face(path_to_filename, detector, required_size=(160, 160), save_faces=True):
        image = Image.open(path_to_filename)
        image = image.convert('RGB')
        pixels = asarray(image)
        results = detector.detect_faces(pixels)
        x1, y1, width, height = results[0]['box']
        x1, y1 = abs(x1), abs(y1)
        x2, y2 = x1 + width, y1 + height
        face = pixels[y1:y2, x1:x2]
        image = Image.fromarray(face)
        image = image.resize(required_size)
        if save_faces:
            path = os.path.split(os.path.abspath(path_to_filename))[0]
            file_name = os.path.split(os.path.abspath(path_to_filename))[1]
            person_name = os.path.basename(os.path.normpath(path(path)))
            project_folder = path(path).parent.parent
            print(person_name)
            target_folder = os.path.join(project_folder, 'faces_mini_dataset', person_name)
            if not os.path.exists(target_folder):
                os.makedirs(target_folder)
            target_face_file_path = os.path.join(target_folder, file_name)
            print(target_face_file_path)
            image.save(target_face_file_path)
        face_array = asarray(image)
        return face_array

error:

TypeError: 'str' object is not callable

fulltrace:

WARNING:tensorflow:From /home/mani/Downloads/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

WARNING:tensorflow:From /home/mani/Downloads/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From /home/mani/Downloads/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.

WARNING:tensorflow:From /home/mani/Downloads/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

WARNING:tensorflow:From /home/mani/Downloads/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:174: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.

WARNING:tensorflow:From /home/mani/Downloads/dip_1/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:181: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

2020-12-30 01:02:58.905653: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-12-30 01:02:58.926680: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2299965000 Hz
2020-12-30 01:02:58.927412: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x484d980 executing computations on platform Host. Devices:
2020-12-30 01:02:58.927467: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>
2020-12-30 01:02:59.110157: 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.
Extracting faces from /home/mani/Downloads/dip_1/persons_dataset/zakir/ .....
Traceback (most recent call last):
  File "/home/mani/Downloads/dip_1/face_detector.py", line 85, in <module>
    faces, labels = generate_faces_from_images(persons_dataset_path)
  File "/home/mani/Downloads/dip_1/face_detector.py", line 74, in generate_faces_from_images
    faces1 = extract_faces(path)
  File "/home/mani/Downloads/dip_1/face_detector.py", line 59, in extract_faces
    face = extract_face(path, detector, save_faces=True)
  File "/home/mani/Downloads/dip_1/face_detector.py", line 38, in extract_face
    person_name = os.path.basename(os.path.normpath(path(path)))
TypeError: 'str' object is not callable

Process finished with exit code 1
DapperDuck
  • 2,728
  • 1
  • 9
  • 21
black snow
  • 47
  • 7

2 Answers2

2

The problem is in path(path). It is not clear whether you are trying to call a function and accidentally called a string or you wanted to call a string, but you can't call a string. A string is not the same as a function. Hence the error.

new Q Open Wid
  • 2,225
  • 2
  • 18
  • 34
2

I am learning Python myself. Zixuan is right, but I'd like to add the following about using os.path to get the parent directory of your given one.

You might want to use something like this:

import os

def extract_face(path_to_filename):
  my_path = os.path.split(os.path.abspath(path_to_filename))[0]
  print(my_path)
  parent = os.path.dirname(my_path)
  print(parent)

extract_face("path.py")

Found on SO in how-to-get-the-parent-dir-location and then in the Python os.path Docs.

Another way could be:

from pathlib import Path

def extract_face(path_to_filename):
  path = Path(path_to_filename)
  print(path.parent.parent)

extract_face("path.py")

Found in pathlib Doc - new since 3.4.

I have removed some information I don't have from your method.

Christian
  • 4,902
  • 4
  • 24
  • 42