系统框图
第一部分:
系统选择
选择树莓派5的官方64位版本
配置虚拟环境
使用virtualenv或者venv
python3 -m venv --system-site-packages env1
安装OPENCV库
pip3 install opencv-python==4.7.0.72
pip3 install opencv-contrib-python==4.7.0.72
安装opencv要注意与python版本相对应。目前树莓派5官方64位系统自带的是
python 3.11版本。
piwheels - opencv-python参考这个网站查看二者对应版本
安装 mediapipe库
pip3 install mediapipe
编写简单测试代码
from picamera2 import Picamera2
import cv2
import time
import mediapipe as mp
import numpy as np
if __name__ == "__main__":
camera = Picamera2()
camera.start()
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
while True:
camera.resolution =(640,480)
im = camera.capture_array("main")
image_height, image_width, _ =np.shape(im)
#raw_capture = PiRGBArray(camera,size=(640,480))
#time.sleep(2)
#camera.capture(raw_capture, format='bgr')
#image = raw_capture.array
imgRGB= cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)
if results.multi_hand_landmarks:
for hand in results.multi_hand_landmarks:
print("\r%.2f %.2f %.2f %.2f %.2f %.2f "%(hand.landmark[0].z,hand.landmark[4].z,hand.landmark[8].z,hand.landmark[12].z,hand.landmark[16].z,hand.landmark[20].z),end="")
#mpDraw.draw_landmarks(im,hand,mpHands.HAND_CONNECTIONS)
for i in range(21):
pos_x = hand.landmark[i].x*image_width
pos_y = hand.landmark[i].y*image_height
cv2.circle(im, (int(pos_x),int(pos_y)), 3, (0,255,255),-1)
cv2.imshow("hands",im)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
camera.release()
第二部分:
手势识别现象:
第三部分:
以下视频演示: