MacBook M1运行OpenCV采集关键点后Jupyter内核崩溃如何解决
问题背景
使用设备为MacBook Air M1,开发行为检测程序时,为训练模型采集关键点运行了如下代码:
关键点采集代码
cap = cv2.VideoCapture(0) # Set mediapipe model with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic: for action in actions: # Loop through sequences aka videos for sequence in range(no_sequences): # Loop through video length aka sequence length, each frame, 30 frames of keypoints for each video for frame_num in range(sequence_length): # Read feed ret, frame = cap.read() # Make detections image, results = mediapipe_detection(frame, holistic) # print the results # 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 Feed', image) cv2.waitKey(1200) 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 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 gracefully if cv2.waitKey(10) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
报错现象
- 运行上述代码完成数据录制后,OpenCV feed窗口持续加载无响应,Mac出现彩虹转圈光标
- 运行如下模型训练代码时Python意外退出,Jupyter Notebook内核崩溃,提示Python quit unexpectedly. The kernel appears to have died. It will restart automatically.
# Training Model # Preprocess Data from sklearn.model_selection import train_test_split from tensorflow.keras.utils import to_categorical
尝试操作及报错
尝试通过新建conda环境解决OpenCV相关问题,执行命令conda create --name myenv opencv-env python=3.6后运行失败,终端返回如下内容:
Collecting package metadata (current_repodata.json): done Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source. Collecting package metadata (repodata.json): done Solving environment: failed PackagesNotFoundError: The following packages are not available from current channels: - opencv-env Current channels: - https://repo.anaconda.com/pkgs/main/osx-64 - https://repo.anaconda.com/pkgs/main/noarch - https://repo.anaconda.com/pkgs/r/osx-64 - https://repo.anaconda.com/pkgs/r/noarch To search for alternate channels that may provide the conda package you're looking for, navigate to https://anaconda.org and use the search bar at the top of the page.
解决方案
- 修正conda创建环境的错误命令:之前的命令混淆了虚拟环境名和依赖包名,
opencv-env是要创建的环境名,不是包名,且M1芯片不支持Python3.6,最低适配3.7及以上版本,推荐用兼容性更好的3.9版本,正确的创建命令为:
conda create --name opencv-env python=3.9
- 环境创建完成后执行激活命令:
conda activate opencv-env
- 安装适配M1芯片的依赖包,优先选用conda-forge源的arm64适配版本,避免默认源的x86架构包不兼容问题:
conda install -c conda-forge opencv-python mediapipe tensorflow-macos tensorflow-metal scikit-learn jupyter
- 修复采集代码的逻辑bug:原有代码中按q触发的break只会跳出最内层的帧循环,
cap.release()和cv2.destroyAllWindows()只有所有数据采集完成才会执行,中途退出会导致窗口进程残留无响应,修改退出逻辑:
# Break gracefully if cv2.waitKey(10) & 0xFF == ord('q'): cap.release() cv2.destroyAllWindows() exit()
- 后续启动Jupyter Notebook前需先激活对应的conda环境,再执行
jupyter notebook启动服务,保证内核调用的是适配环境的依赖包。
内容的提问来源于stack exchange,提问作者teeara
相关产品推荐
相关产品推荐

