[Python] 纯文本查看 复制代码 import cv2
import mediapipe as mp
import time
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1270)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
mpHands = mp.solutions.hands
hands = mpHands.Hands() #使用meidapipe的手部追踪模型
mpDraw = mp.solutions.drawing_utils #将点坐标画在手上的函数
handLmsStyle = mpDraw.DrawingSpec(color=(0,0,255), thickness=5)#调整点的样式
handConStyle = mpDraw.DrawingSpec(color=(0,255,0), thickness=10)#线的样式
pTime = 0
cTime = 0
while True:
ret, img1 = cap.read()
img = cv2.flip(img1, 1)
if ret:
imgRGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #把bgr的图片转化成rgb
result = hands.process(imgRGB)
#print(result.multi_hand_landmarks)#侦测手并输出手的坐标
imgHeight = img.shape[0]
imgWidth = img.shape[1]
if result.multi_hand_landmarks:
for handLms in result.multi_hand_landmarks:
mpDraw.draw_landmarks(img,handLms,mpHands.HAND_CONNECTIONS,handLmsStyle,handConStyle)
for i, lm in enumerate(handLms.landmark):
xPos =int(lm.x * imgWidth)
yPos =int(lm.y * imgHeight)
#cv2.putText(img , str(i),(xPos-25,yPos+5),cv2.FONT_HERSHEY_SIMPLEX,0.4,(0,0,255),2)#给点编号
print(i, xPos, yPos)
#算手部追踪的帧率
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(img, f'fps : {int(fps)}',(30,50),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,0),3)
cv2.imshow('img',img)
if cv2.waitKey(1) == ord('q'):#点q关闭运行
Break
|