我试图使用gesturedetection.py (4.4.0.4版)运行cvzone OpenCV代码。当我运行代码,我看到我的摄像头图像2秒,然后它立即关闭。然后,我得到一个错误,说明捕获地标数据存在问题。是代码错误,还是包不兼容?我正在运行以下代码:
from cvzone.PoseModule import PoseDetector
import cv2
detector = PoseDetector()#upBody=True
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
img = detector.findPose(img, draw=False)
lmList, bboxInfo = detector.findPosition(img, draw=False)
gesture = ''
if bboxInfo:
angArmL = detector.findAngle(img, 13, 11, 23, draw=False)
angArmR = detector.findAngle(img, 14, 12, 24, draw=False)
crossDistL, img, _ = detector.findDistance(15, 12, img)
crossDistR, img, _ = detector.findDistance(16, 11, img)
if detector.angleCheck(angArmL, 90) and detector.angleCheck(angArmR, 270):
gesture = 'T Pose'
elif detector.angleCheck(angArmL, 170) and detector.angleCheck(angArmR, 180):
gesture = 'UP'
elif crossDistL:
if crossDistL < 70 and crossDistR < 70:
gesture = "Cross"
cv2.putText(img, gesture, (20, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
cv2.imshow("Image", img)
cv2.waitKey(1)我得到以下错误:
Traceback (most recent call last):
File "Part2/GestureDetection.py", line 15, in <module>
angArmL = detector.findAngle(img, 13, 11, 23, draw=False)
File ".../cvzone/PoseModule.py", line 98, in findAngle
x1, y1 = self.lmList[p1][1:]findAngle的代码是
def findAngle(self, img, p1, p2, p3, draw=True):
"""
Finds angle between three points. Inputs index values of landmarks
instead of the actual points.
:param img: Image to draw output on.
:param p1: Point1 - Index of Landmark 1.
:param p2: Point2 - Index of Landmark 2.
:param p3: Point3 - Index of Landmark 3.
:param draw: Flag to draw the output on the image.
:return:
"""
# Get the landmarks
x1, y1 = self.lmList[p1][1:]
x2, y2 = self.lmList[p2][1:]
x3, y3 = self.lmList[p3][1:]
# Calculate the Angle
angle = math.degrees(math.atan2(y3 - y2, x3 - x2) -
math.atan2(y1 - y2, x1 - x2))
if angle < 0:
angle += 360
# Draw
if draw:
cv2.line(img, (x1, y1), (x2, y2), (255, 255, 255), 3)
cv2.line(img, (x3, y3), (x2, y2), (255, 255, 255), 3)
cv2.circle(img, (x1, y1), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (x1, y1), 15, (0, 0, 255), 2)
cv2.circle(img, (x2, y2), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (x2, y2), 15, (0, 0, 255), 2)
cv2.circle(img, (x3, y3), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (x3, y3), 15, (0, 0, 255), 2)
cv2.putText(img, str(int(angle)), (x2 - 50, y2 + 50),
cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
return angle发布于 2022-04-30 12:07:12
我看到lmlist返回了4个值,而不是原始代码中预期的3个值。为了减少值的数量,我指定了x1,y1的地标值的数量,方法是:
#original:
x1, y1 = self.lmList[p1][1]
#change:
x1, y1 = self.lmList[p1][1:3]下面是我在PoseModule.py中对以下两个函数所做的更改
def findAngle(self, img, p1, p2, p3, draw=True):
"""
Finds angle between three points. Inputs index values of landmarks
instead of the actual points.
:param img: Image to draw output on.
:param p1: Point1 - Index of Landmark 1.
:param p2: Point2 - Index of Landmark 2.
:param p3: Point3 - Index of Landmark 3.
:param draw: Flag to draw the output on the image.
:return:
"""
# Get the landmarks
x1, y1 = self.lmList[p1][1:3]
x2, y2 = self.lmList[p2][1:3]
x3, y3 = self.lmList[p3][1:3]
# Calculate the Angle
angle = math.degrees(math.atan2(y3 - y2, x3 - x2) -
math.atan2(y1 - y2, x1 - x2))
if angle < 0:
angle += 360
# Draw
if draw:
cv2.line(img, (x1, y1), (x2, y2), (255, 255, 255), 3)
cv2.line(img, (x3, y3), (x2, y2), (255, 255, 255), 3)
cv2.circle(img, (x1, y1), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (x1, y1), 15, (0, 0, 255), 2)
cv2.circle(img, (x2, y2), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (x2, y2), 15, (0, 0, 255), 2)
cv2.circle(img, (x3, y3), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (x3, y3), 15, (0, 0, 255), 2)
cv2.putText(img, str(int(angle)), (x2 - 50, y2 + 50),
cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
return angle
def findDistance(self, p1, p2, img, draw=True, r=15, t=3):
x1, y1 = self.lmList[p1][1:3]
x2, y2 = self.lmList[p2][1:3]
cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
if draw:
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), t)
cv2.circle(img, (x1, y1), r, (255, 0, 255), cv2.FILLED)
cv2.circle(img, (x2, y2), r, (255, 0, 255), cv2.FILLED)
cv2.circle(img, (cx, cy), r, (0, 0, 255), cv2.FILLED)
length = math.hypot(x2 - x1, y2 - y1)
return length, img, [x1, y1, x2, y2, cx, cy]https://stackoverflow.com/questions/72068258
复制相似问题