用cvzone识别手臂上下左右四个方向
cvzone是一个计算机视觉工具包,可方便的图像处理和实现视觉AI功能。核心是使用OpenCV和Mediapipe库。安装使用都十分简单方便。
项目地址
安装:
pip install cvzone
from cvzone.PoseModule import PoseDetector
import cv2
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
detector = PoseDetector()
while True:
success, img = cap.read()
img = detector.findPose(img)
lmList, bboxInfo = detector.findPosition(img, bboxWithHands=False)
if bboxInfo:
center = bboxInfo["center"]
cv2.circle(img, center, 5, (255, 0, 255), cv2.FILLED)
h,w,c=img.shape
#角度cvzone里面原来是0到360
#右手与左肩的夹角
angleLR=detector.findAngle(img, 16, 12, 11,draw=True)
if angleLR > 180:
angleLR = 360 - angleLR
#
angleUD=detector.findAngle(img, 16, 12, 24,draw=True)
if angleUD > 180:
angleUD = 360 - angleUD
if angleLR >45 and angleLR<145:
if angleUD >145:
dir=1
elif angleUD <45:
dir=2
if angleUD >45 and angleUD<145:
if angleLR<45:
dir=3
elif angleLR>145:
dir=4
cv2.putText(img, str(dir), (w // 2, h // 2), cv2.FONT_HERSHEY_SIMPLEX, 10, (255, 255, 255), 20, cv2.LINE_AA)
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()