【问题标题】:TypeError: 'str' object is not callable with path [closed]TypeError:'str'对象不能用路径调用[关闭]
【发布时间】:2023-03-06 00:10:02
【问题描述】:

我遇到了这个问题,但我不知道为什么,因为我不知道 str.我正在使用 python 3.6 。这个函数应该是检测人脸。

在这一行检测到错误

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

代码:

    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

错误

TypeError: 'str' 对象不可调用

完整跟踪:

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

【问题讨论】:

  • 帮助我们帮助您 - 分享完整的追溯
  • 打电话给path(path))到底想做什么?
  • path 是一个字符串,你不能像 path() 这样的字符串 call 像你试图用 path(path) 做的那样
  • 如果我的回答帮助您解决了问题,您可以点击旁边的灰色复选标记接受。
  • (以“问题出在...”开头的答案

标签: python tensorflow


【解决方案1】:

问题出在path(path)。目前尚不清楚您是在尝试调用函数并意外调用了字符串,还是您想调用字符串,但您无法调用字符串。字符串与函数不同。因此出现错误。

【讨论】:

    【解决方案2】:

    我自己也在学习 Python。子轩是对的,但是我想补充一下关于使用os.path获取你给定的父目录的内容。

    你可能想使用这样的东西:

    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")
    

    how-to-get-the-parent-dir-location 中的 SO 上找到,然后在 Python os.path Docs 中找到。

    另一种方式可能是:

    from pathlib import Path
    
    def extract_face(path_to_filename):
      path = Path(path_to_filename)
      print(path.parent.parent)
    
    extract_face("path.py")
    

    发现于pathlib Doc - new since 3.4

    我已经从你的方法中删除了一些我没有的信息。

    【讨论】:

      猜你喜欢
      • 2018-03-27
      • 1970-01-01
      • 2015-03-07
      • 2019-03-05
      • 1970-01-01
      • 2019-01-28
      • 2014-03-06
      • 2020-09-16
      相关资源
      最近更新 更多