手势识别也是逆天了孩子

import os
import cv2
import numpy as np
from sklearn import svm

# 读取文件夹中的图片,并将其转换为灰度图像
def load_images_from_folder(folder, target_size=(100, 100)):
    images = []
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder, filename), cv2.IMREAD_GRAYSCALE)
        if img is not None:
            img = cv2.resize(img, target_size)  # 调整图像大小
            images.append(img)
    return images

# 加载训练集和测试集数据
ok_train_images = load_images_from_folder(r"C:\Users\ly\Desktop\handpose_x_gesture_v1\ok\ok-train")
ok_test_images = load_images_from_folder(r"C:\Users\ly\Desktop\handpose_x_gesture_v1\ok\ok-test")
paper_train_images = load_images_from_folder(r"C:\Users\ly\Desktop\handpose_x_gesture_v1\paper\paper-train")
paper_test_images = load_images_from_folder(r"C:\Users\ly\Desktop\handpose_x_gesture_v1\paper\paper-test")
rock_train_images = load_images_from_folder(r"C:\Users\ly\Desktop\handpose_x_gesture_v1\rock\rock-train")
rock_test_images = load_images_from_folder(r"C:\Users\ly\Desktop\handpose_x_gesture_v1\rock\rock-test")

# 提取训练集特征和标签
X_train = np.concatenate((ok_train_images, paper_train_images, rock_train_images))
y_train = np.array([0]*len(ok_train_images) + [1]*len(paper_train_images) + [2]*len(rock_train_images))

# 将图像数据拉平成一维向量
X_train = [np.ravel(x) for x in X_train]

# 训练SVM模型
clf = svm.SVC(kernel='linear')
clf.fit(X_train, y_train)

# 提取测试集特征和标签
X_test = np.concatenate((ok_test_images, paper_test_images, rock_test_images))
y_test = np.array([0]*len(ok_test_images) + [1]*len(paper_test_images) + [2]*len(rock_test_images))

# 将图像数据拉平成一维向量
X_test = [np.ravel(x) for x in X_test]

# 读取并预处理图像
def read_and_preprocess_image(image_path, target_size=(100, 100)):
    img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    img = cv2.resize(img, target_size)
    img_flat = np.ravel(img)
    return img, img_flat

# 指定要识别的图片路径
image_path = r"C:\Users\ly\Desktop\handpose_x_gesture_v1\OK\ok-train\gesture-ok-2021-03-07_23-07-50-131_25569.jpg"

# 读取并预测该图片的类别
img, img_flat = read_and_preprocess_image(image_path)
prediction = clf.predict([img_flat])[0]

# 根据预测结果输出类别
if prediction == 0:
    print(f'The image is predicted as "ok" gesture')
elif prediction == 1:
    print(f'The image is predicted as "paper" gesture')
else:
    print(f'The image is predicted as "rock" gesture')

# 显示原始图片
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值