使用Python+cvzone做手指追踪遇TypeError,摄像头无法启动
手指追踪功能开发报错解决
我正在用Python结合cvzone、OpenCV库开发手指追踪功能,代码如下:
import cvzone import cv2 cap = cv2.VideoCapture(1) detector = cvzone.HandDetector(maxHands=1, detectionCon=0.7) while True: success, img = cap.read() img = detector.findHands(img) lmList, bbox = detector.findPosition(img) cv2.imshow("Image", img) cv2.waitKey(0)
运行代码时出现以下错误:
TypeError: create_int(): incompatible function arguments. The following argument types are supported: 1. (arg0: int) -> mediapipe.python._framework_bindings.packet.Packet Invoked with: 0.7 Process finished with exit code 1
摄像头无法正常启动,解决方法如下:
- 错误根源:
detectionCon参数在当前cvzone版本中要求传入整数类型,你传入的0.7是浮点数,触发了类型不匹配错误。该参数实际接收的是百分比形式的检测置信度(比如70代表70%)。 - 修复操作:
- 将
detectionCon=0.7修改为detectionCon=70 - 把
cv2.waitKey(0)改为cv2.waitKey(1),否则画面会一直卡住无法实时刷新 - 补充摄像头资源释放代码,避免程序退出后摄像头被占用
- 将
修复后的完整代码:
import cvzone import cv2 cap = cv2.VideoCapture(1) detector = cvzone.HandDetector(maxHands=1, detectionCon=70) while True: success, img = cap.read() if not success: break img = detector.findHands(img) lmList, bbox = detector.findPosition(img) cv2.imshow("Image", img) # 按下q键退出程序 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头和窗口资源 cap.release() cv2.destroyAllWindows()
内容的提问来源于stack exchange,提问作者Mohamed Belaid
相关产品推荐
相关产品推荐

