手势识别项目(个人demo)

来源:手势检测 |项目介绍_哔哩哔哩_bilibili

目标:识别6、7、8 手势

 

步骤:

1.制作数据集

2.在线训练/自己训练

3.测试

# 环境依赖
pip install opencv
pip install tensorflow
pip install cvzone
pip install mediaflow

数据集制作:

通过opencv打开摄像头并且截取根据cvzone检测出来的手部关节点以及边界框数据得到的手部截图。

# make datasets
import math
import os.path

import cv2
import numpy as np


from cvzone.HandTrackingModule import HandDetector


# 调用摄像头,并展示窗口
cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1)

folder6 = "./images/eight"
imgsize = 300
counter = 0

while True:
    success, img = cap.read()
    hands, img = detector.findHands(img)

    if hands:
        hand = hands[0]
        x, y, w, h = hand['bbox']
        img_white = np.ones((imgsize, imgsize, 3), np.uint8) * 255


        a_y = 0.09*y
        a_x = 0.09*x
        imgcrop = img[y - int(a_y):y + h + int(a_y), x - int(a_x):x + w + int(a_x)]

        aspectRatio = h / w

        if aspectRatio > 1:
            k = imgsize / h
            wcal = math.ceil(k * w)
            resized_img = cv2.resize(imgcrop, (wcal, imgsize))

            w_offset = math.ceil((imgsize - wcal) / 2)
            img_white[:, w_offset:wcal + w_offset] = resized_img
        else:
            k = imgsize/w
            hcal = math.ceil(k*h)
            resized_img = cv2.resize(imgcrop, (imgsize, hcal))
            h_offset = math.ceil((imgsize - hcal) / 2)
            img_white[h_offset:hcal+h_offset, :] =resized_img

        cv2.imshow('imgcrop', img_white)
    cv2.imshow('image', img)
    key = cv2.waitKey(1)

    if key == ord("s"):
        counter += 1
        cv2.imwrite(os.path.join(folder6,f'{counter}.jpg'),img_white)
        print(counter)

通过在线训练网站,进行在线训练。

也可自选分类模型训练,只有6,7,8三个数字分类标签比较容易制作。

在线训练网址:https://teachablemachine.withgoogle.com/

通过在线训练网址得到训练结果的h5文件和label标签

测试并用opencv可视化调用摄像头:

import math
import os.path

import cv2
import numpy as np


from cvzone.HandTrackingModule import HandDetector
from cvzone.ClassificationModule import Classifier

# 调用摄像头,并展示窗口
cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1)
classifier = Classifier('model/keras_model.h5', 'model/labels.txt')

offset = 20
imgsize = 300
labels = ["6", "7", "8"]

while True:
    success, img = cap.read()
    hands, img = detector.findHands(img)

    if hands:
        hand = hands[0]
        x, y, w, h = hand['bbox']
        img_white = np.ones((imgsize, imgsize, 3), np.uint8) * 255


        a_y = 0.09*y
        a_x = 0.09*x
        imgcrop = img[y - offset:y + h + offset, x - offset:x + w + offset]

        aspectRatio = h / w

        if aspectRatio > 1:
            k = imgsize / h
            wcal = math.ceil(k * w)
            resized_img = cv2.resize(imgcrop, (wcal, imgsize))

            w_offset = math.ceil((imgsize - wcal) / 2)
            img_white[:, w_offset:wcal + w_offset] = resized_img
            prediction, index = classifier.getPrediction(img_white)
            print(prediction)
        else:
            k = imgsize/w
            hcal = math.ceil(k*h)
            resized_img = cv2.resize(imgcrop, (imgsize, hcal))
            h_offset = math.ceil((imgsize - hcal) / 2)
            img_white[h_offset:hcal+h_offset, :] =resized_img
            prediction, index = classifier.getPrediction(img_white)
            print(prediction)

        cv2.rectangle(img, (x - offset, y - offset - 50), (x + w + offset, y - offset), (255, 0, 255), thickness=-1)
        cv2.putText(img, labels[index], (x, y - 26), cv2.FONT_HERSHEY_COMPLEX, 1.5, (255, 255, 255), 2)
        cv2.rectangle(img, (x - offset, y - offset), (x + w + offset, y + h + offset), (255, 0, 255), 2)
        # cv2.imshow('imgcrop', img_white)
    cv2.imshow('image', img)
    cv2.waitKey(1)

遇到问题:

1.offset可能会导致x和y的值小于零以致imshow和rectangle不工作,尝试使用x/10和y/10作为偏移量,取得较好效果。

2.数据集制作可以做多一点防止过拟合。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值