基于OpenCV的手势跟踪与识别项目代码
时间: 2023-08-09 13:12:26 浏览: 117
以下是一个基于OpenCV的手势跟踪与识别项目的代码示例,供参考:
```
import cv2
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
# 初始化摄像头
cap = cv2.VideoCapture(0)
# 定义分类器
knn = KNeighborsClassifier(n_neighbors=3)
# 训练手势数据
gestures = ['fist', 'palm', 'peace', 'thumbs_up']
train_data = []
train_labels = []
for gesture in gestures:
for i in range(1, 6):
img = cv2.imread('train_data/' + gesture + '/' + str(i) + '.jpg', 0)
_, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
img = cv2.resize(img, (50, 50))
img_vec = np.reshape(img, (1, 2500))
train_data.append(img_vec)
train_labels.append(gestures.index(gesture))
knn.fit(train_data, train_labels)
# 定义颜色范围
lower_range = np.array([0, 20, 70], dtype=np.uint8)
upper_range = np.array([20, 255, 255], dtype=np.uint8)
while True:
# 读取摄像头数据
ret, frame = cap.read()
# 镜像翻转
frame = cv2.flip(frame, 1)
# 转换为HSV颜色空间
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# 提取手部区域
mask = cv2.inRange(hsv, lower_range, upper_range)
# 形态学操作
kernel = np.ones((5, 5), np.uint8)
mask = cv2.erode(mask, kernel, iterations=1)
mask = cv2.dilate(mask, kernel, iterations=1)
# 轮廓检测
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 处理轮廓
if contours:
max_contour = max(contours, key=cv2.contourArea)
if cv2.contourArea(max_contour) > 500:
moments = cv2.moments(max_contour)
if moments['m00'] != 0:
cx = int(moments['m10'] / moments['m00'])
cy = int(moments['m01'] / moments['m00'])
# 绘制中心点
cv2.circle(frame, (cx, cy), 5, (0, 0, 255), -1)
# 裁剪手部区域
x, y, w, h = cv2.boundingRect(max_contour)
hand_roi = mask[y:y+h, x:x+w]
hand_roi = cv2.resize(hand_roi, (50, 50))
hand_vec = np.reshape(hand_roi, (1, 2500))
# 预测手势
pred = knn.predict(hand_vec)
cv2.putText(frame, gestures[pred[0]], (cx-50, cy-50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# 显示结果
cv2.imshow('frame', frame)
# 按q键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头并关闭所有窗口
cap.release()
cv2.destroyAllWindows()
```
在上述代码中,我们使用KNN算法来训练手势数据,并在摄像头画面中提取手部区域,预测手势并在画面中显示结果。需要注意的是,这只是一个简单的示例,实际应用中需要根据具体情况进行优化和改进。
阅读全文