添加代码所需要的模块
import cv2 as cv
import mediapipe as mp
import numpy as np
把cv2改为cv是本人习惯一般都是用cv2
构建基本的结构
cap=cv.VideoCapture(0)
mpHands=mp.solutions.hands
hands=mpHands.Hands()
while True:
ret,img=cap.read()
if ret:
lst=[]
imgRGB=cv.cvtColor(img,cv.COLOR_BGR2RGB)
result=hands.process(imgRGB)
print(result.multi_hand_landmarks)
cv.imshow("img",img)
if cv.waitKey(1)==ord('q'):
break
第1行,指定摄像头,一般笔记本自带摄像头给0,而自己安装的摄像头给1
第2行 给一个mp的手部模型
第3行 主要为了可以同时检测到多只手
ret,img=cap.read()读取每一帧
因为我们做处理需要RGB的图像所以将BGR转换为RGB
result.multi_hand_landmarks手的坐标
做完这一步可以先调试一下,调试成功,那么当你的手没有出现在画面里将打印None
手出现则将打印坐标
进行画线处理,以及在手上显示关键点序号,并且处理坐标细节
mpDraw=mp.solutions.drawing_utils #画线
while True:
ret,img=cap.read()
if ret:
lst=[]
imgRGB=cv.cvtColor(img,cv.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)#连线手上点
for i,lm in enumerate(handlms.landmark):
xpos=int(lm.x*imgWidth)
ypos=int(lm.y*imgHeight)
cv.putText(img,str(i),(xpos-25,ypos+5),cv.FONT_HERSHEY_SIMPLEX,0.4,(0,0,255),2)
print(i,xpos,ypos)
cv.imshow("img",img)
if cv.waitKey(1)==ord('q'):
break
先画线
一个if语句,如果可以检测到有手则进行下面操作
先循环遍历手上每一个点
再将手上点相连
所呈现的样子
因为这时的坐标为小数,代表我们的手占整个屏幕的比例,为了看的方便所以我对数据进行了一系列操作使其变为了整数
整体代码展示
import cv2 as cv
import mediapipe as mp
import numpy as np
cap=cv.VideoCapture(0) #放进摄像头
mpHands=mp.solutions.hands #使用mp的手部模型
hands=mpHands.Hands() #可以检测多只手
mpDraw=mp.solutions.drawing_utils #画线
while True: # 一个死循环
ret,img=cap.read() #读出一个图像
if ret:
lst=[]
imgRGB=cv.cvtColor(img,cv.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)#连线手上点
for i,lm in enumerate(handlms.landmark):
xpos=int(lm.x*imgWidth)
ypos=int(lm.y*imgHeight)
cv.putText(img,str(i),(xpos-25,ypos+5),cv.FONT_HERSHEY_SIMPLEX,0.4,(0,0,255),2)
print(i,xpos,ypos)
cv.imshow("img",img)
if cv.waitKey(1)==ord('q'):
break