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

使用OpenCV捕获摄像头图像时遇ValueError解包值不足问题求助

问题解决:cv2.findContours 解包 ValueError

问题原因

你遇到的报错是由于OpenCV版本差异导致的:

  • OpenCV 3.x 中,cv2.findContours() 返回3个值:image, contours, hierarchy
  • OpenCV 4.x 及更高版本中,该函数仅返回2个值:contours, hierarchy(移除了冗余的第一个图像返回值)

你的代码基于旧版本OpenCV编写,但当前环境安装的是高版本,解包时就会出现“expected 3, got 2”的错误,进而导致摄像头流程中断。

解决方案

直接修改segment_hand函数中cv2.findContours的调用行:

原代码:

image, contours, hierarchy = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

修改为:

contours, hierarchy = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

如果需要兼容不同OpenCV版本,可以添加版本判断逻辑:

import cv2

# 获取OpenCV主版本号
cv2_major_version = int(cv2.__version__.split('.')[0])

if cv2_major_version == 3:
    _, contours, hierarchy = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else:
    contours, hierarchy = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

(注:用_接收旧版本的第一个返回值,可避免未使用变量的警告)

修改后重新运行代码,摄像头就能正常执行图像捕获和模型评估流程了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 23:25:22