You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jupyter Notebook运行代码遇PermissionError权限错误求助

解决人脸情绪检测代码中的PermissionError问题

错误原因

你执行train_features = extract_features(['images'])时,直接将目录路径传给了load_img函数,但load_img仅支持加载单个图片文件,无法直接读取目录,因此触发权限错误(系统禁止将目录当作文件打开)。

解决方案

1. 修改函数遍历目录内的图片文件

更新extract_features函数,让它自动遍历目标目录下的所有图片,而不是直接处理目录名。示例代码如下:

import os
from tqdm import tqdm
from keras_preprocessing.image import load_img
import numpy as np

def extract_features(image_dirs):
    features = []
    for dir_path in image_dirs:
        # 遍历目录下的所有文件
        for filename in tqdm(os.listdir(dir_path)):
            # 筛选图片格式(根据数据集实际格式调整后缀)
            if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
                img_path = os.path.join(dir_path, filename)
                img = load_img(img_path, grayscale=True)
                img = np.array(img)
                features.append(img)
    return features

# 调用修改后的函数
train_features = extract_features(['images'])

2. 检查并修复目录权限

  • 确保当前用户对images目录有读取权限:Windows系统可右键目录→属性→安全,确认当前用户拥有读取权限;
  • 若数据集是从Kaggle解压而来,避免将目录放在C盘根目录、Program Files等系统保护文件夹,建议解压到普通用户目录(如D:/Face Emotion Detection/data)。

3. 确认目录路径正确性

先检查Jupyter Notebook的当前工作目录,确保images目录在该路径下:

import os
# 打印当前工作目录
print(os.getcwd())

如果images不在当前工作目录,传入绝对路径即可:

train_features = extract_features(['D:/Face Emotion Detection/images'])

内容的提问来源于stack exchange,提问作者Esan Raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 05:48:11