【问题标题】:PermissionError: [Errno 13] Permission denied error when trying to read a directoryPermissionError: [Errno 13] 尝试读取目录时出现权限被拒绝错误
【发布时间】:2021-01-15 20:44:58
【问题描述】:

我有以下代码从照片中提取人脸。现在我想从一个目录中提取它,即一次提取多个文件。 这应该非常简单,就像只是将“MultiPeople/Faces/IMG100”更改为“MultiPeople/Faces”,即整个目录。但我一直在代码下方出现错误。我已经完成了所有常规检查,例如确保帐户是管理员,确保文件或目录没有同时打开等等。

from PIL import Image
import face_recognition

path = r"C:\Users\Julio\Desktop\Face Extraction\Output\face"
image = face_recognition.load_image_file('MultiPeople/Faces')

face_locations = face_recognition.face_locations(image)

print("I found {} face(s) in this photograph.".format(len(face_locations)))

face_counter = 0
for face_location in face_locations:
    top, right, bottom, left = face_location
    print("I found a face in image location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.save(str(path) + str(face_counter) + ".jpg")
    face_counter += 1


Traceback (most recent call last):
  File "C:\Users\Julio\PycharmProjects\test\Extract faces from photo.py", line 5, in <module>
    image = face_recognition.load_image_file('MultiPeople/Faces')
  File "C:\Users\Julio\PycharmProjects\test\venv\lib\site-packages\face_recognition\api.py", line 86, in load_image_file
    im = PIL.Image.open(file)
  File "C:\Users\Julio\PycharmProjects\test\venv\lib\site-packages\PIL\Image.py", line 2878, in open
    fp = builtins.open(filename, "rb")
PermissionError: [Errno 13] Permission denied: 'MultiPeople/Faces'

【问题讨论】:

    标签: python-3.x face-recognition


    【解决方案1】:

    我不确定这是否能解决您的问题,但face_recognition.load_image_file() 仅用于加载一张图片而不是目录/多个文件。检查问题是否存在!

    也可能是this example可以帮到你,看看吧。

    【讨论】:

    • 嗯好的 - 有谁知道如何加载一个目录或多个文件?
    • 您可以像这样加载列表中目录中的所有文件:from os import walkmypath='/home/name/.../'f = []for (_, _, filenames) in walk(mypath):f.extend(filenames)break 然后使用上面链接中的方法 - 遍历它们/
    • 唯一的问题是有时可能有 500 多个文件。在这种情况下,这行得通吗?
    • 为什么不应该这样做?长度为 500 的列表并没有那么大,而且您存储的不是对象,而是字符串。在这种情况下,循环 500 个元素可能会有点慢,但你可以在让它工作后进行优化 :)
    • 如果您的问题将(或已经)解决,请关闭问题
    猜你喜欢
    • 2015-07-17
    • 2016-07-25
    • 2018-11-18
    • 2020-02-07
    • 1970-01-01
    • 2020-07-01
    • 2016-11-12
    • 2019-11-09
    • 2020-06-06
    相关资源
    最近更新 更多