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

OpenCV+MediaPipe数据采集时np.save报FileNotFoundError求助

问题:M1 Mac上OpenCV+MediaPipe动作采集代码np.save报FileNotFoundError

在M1 Mac上运行基于OpenCV和MediaPipe的动作数据采集代码,已创建根存储文件夹,但执行np.save时出现FileNotFoundError,无法生成存储numpy数组的.npy文件。

代码片段

cap = cv2.VideoCapture(0)

# Set mediapipe model 
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
    
    # NEW LOOP
    # Loop through actions
    for action in actions:
        # Loop through sequences aka videos
        for sequence in range(start_folder, start_folder+no_sequences):
            # Loop through video length aka sequence length
            for frame_num in range(sequence_length):

                # Read feed
                ret, frame = cap.read()
                cv2.startWindowThread()

                # Make detections
                image, results = mediapipe_detection(frame, holistic)

                # Draw landmarks
                draw_styled_landmarks(image, results)
                
                # NEW Apply wait logic
                if frame_num == 0: 
                    cv2.putText(image, 'STARTING COLLECTION', (120,200), 
                               cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255, 0), 4, cv2.LINE_AA)
                    cv2.putText(image, 'Collecting frames for {} Video Number {}'.format(action, sequence), (15,12), 
                               cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
                    # Show to screen
                    cv2.imshow('OpenCV data collection Feed', image)
                    cv2.waitKey(1)

                else: 
                    cv2.putText(image, 'Collecting frames for {} Video Number {}'.format(action, sequence), (15,12), 
                               cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
                    # Show to screen
                    cv2.imshow('OpenCV data collection Feed', image)
                
                # NEW Export keypoints
                keypoints = extract_keypoints(results)
                npy_path = os.path.join(DATA_PATH, action, str(sequence), str(frame_num))
                np.save(npy_path, keypoints)

                # break
                if cv2.waitKey(10) & 0xFF == ord('q'):
                    break
                    
    cap.release()
    cv2.destroyAllWindows()
    cv2.waitKey(1)

报错信息

FileNotFoundError                         Traceback (most recent call last)
Cell In[58], line 43
     41 keypoints = extract_keypoints(results)
     42 npy_path = os.path.join(DATA_PATH, action, str(sequence), str(frame_num))
---> 43 np.save(npy_path, keypoints)
     45 # break
     46 if cv2.waitKey(10) & 0xFF == ord('q'):

File <__array_function__ internals>:180, in save(*args, **kwargs)

File ~/miniconda3/envs/tensorflow/lib/python3.10/site-packages/numpy/lib/npyio.py:518, in save(file, arr, allow_pickle, fix_imports)
    516     if not file.endswith('.npy'):
    517         file = file + '.npy'
---> 518     file_ctx = open(file, "wb")
    520 with file_ctx as fid:
    521     arr = np.asanyarray(arr)

FileNotFoundError: [Errno 2] No such file or directory: '/Users/utkx2/Desktop/python/MLDL/Projects/Human-Action-Recognition/MP_Data/hello/31/0.npy'

解决方案

问题根源

np.save不会自动创建嵌套的子目录。你虽然创建了根文件夹MP_Data,但每个动作(如hello)、每个序列(如31)对应的子目录并未生成,导致保存文件时找不到路径。

修复代码

在调用np.save前,先通过os.makedirs创建所需的目录路径,添加exist_ok=True参数避免重复创建时抛出异常:

# NEW Export keypoints
keypoints = extract_keypoints(results)
npy_path = os.path.join(DATA_PATH, action, str(sequence), str(frame_num))
# 创建文件所在的目录
os.makedirs(os.path.dirname(npy_path), exist_ok=True)
np.save(npy_path, keypoints)

说明

  • os.path.dirname(npy_path)会提取出.npy文件所在的目录路径(比如示例中的/Users/utkx2/Desktop/python/MLDL/Projects/Human-Action-Recognition/MP_Data/hello/31)
  • exist_ok=True确保如果目录已经存在,不会报错,适配循环中创建不同动作、序列目录的场景

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 23:23:18