最近抖音上看到opencv 识别 手势的.自己写了个机器学习的效果如下
机器学习.和 深度学习 不一样..不一样..不一样..重要事情说3遍.....
这里开发原理 主要是通过mediapipe库识别手部21个坐标点 利用sklearn学习框架训练 识别先放代码
import cv2
import mediapipe as mp
import numpy as np
import json
import joblib
import numpy as np
from sklearn.svm import SVC
clf = joblib.load('number.pkl')
pd =False
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
static_image_mode=False,
max_num_hands=2,
min_detection_confidence=0.75,
min_tracking_confidence=0.75)
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
ret,frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame= cv2.flip(frame,1)
results = hands.process(frame)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_idx in range(len(results.multi_hand_landmarks)):
hand_landmarks = results.multi_hand_landmarks[hand_idx]
handarr =[]
for i in range(21):
handarr.append(hand_landmarks.landmark[i].x - hand_landmarks.landmark[i].y)
a = clf.predict([handarr])
#print(a)
mp_drawing.draw_landmarks(
frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
cv2.putText(frame, str(a[0]), (10, 100), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 1.5, (100, 200, 200), 2)
cv2.imshow('hello', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
开发很快.主要遇到几个问题.记录整理一下
1.windows 上cap = cv2.VideoCapture(0) 有时候无法调用摄像头需要 加入cv2.CAP_DSHOW 参数
2.cv2.putText 不支持中文...
,