最近抖音上看到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 不支持中文...
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!