我得到一个错误,而运行此代码片段,我看代码好像无法创建numpy数组,我想它正在运行一个无限循环

wwwo4jvm  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(104)
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)

使用OpenCV和MediaPipe库进行数据收集的代码单元。它使用cv2.VideoCapture(0)从默认摄像机(索引0)设置视频捕获,并初始化MediaPipe整体模型。
然后,代码进入一个循环,循环遍历一系列操作。在每个动作中,它在一系列序列上循环,在每个序列中,它在帧上循环。对于每一帧,它使用www.example.com()读取视频源cap.read,并使用mediapipe_detection()函数执行检测。然后使用draw_styled_landmarks()在帧上绘制地标。
我得到的错误是

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'

我的文件夹用于存储数据已经创建,但我不知道为什么它无法创建numpy文件用于存储numpy数组值。亲切的帮助。我使用的是M1 Mac(仅供参考,以防出现操作系统问题或其他问题)

xlpyo6sf

xlpyo6sf1#

我认为你需要创建保存numpy数组的完整路径,更具体地说,你需要在定义的路径中创建所有子文件夹:

npy_path = os.path.join(DATA_PATH, action, str(sequence), str(frame_num))
np.save(npy_path, keypoints)

因此,您可以在调用www.example.com之前调用os.makedirs(npy_path,exit_ok=True)np.save,如果文件夹已经创建,则参数exit_ok=true将不会创建它们。

npy_path = os.path.join(DATA_PATH, action, str(sequence), str(frame_num))
os.makedirs(npy_path, exit_ok=True)
np.save(npy_path, keypoints)

相关问题