基于opencv + cnn + PIL的手势识别系统

涉及技术栈:opencv + cnn + PIL

网络训练算法流程(training.py)

  1. 图像读取及预处理

本实验采用PLL库里的open函数完成图片的读取工作,用resize函数将图像的尺寸变为统一值。为减少卷积操作的计算量,将图像做归一化处理,将图像的像素值变为[0,1]之间。

2,编码标签

将训练集和测试集的标签转为独热码。

3.构建卷积神经网络

4.配置和训练网络

搭建完网络后,设置损失函数,优化器和评价指标。配置网络训练参数,包括训练集和测试集,训练轮数等参数,接着开始训练网络。调整网络训练参数,使网络收敛达到比较好的效果。

打印出的模型的具体结构:

训练损失图:

5.评价模型

混淆矩阵是表示精度评价的一种标准格式,用n行n列的矩阵形式来表示。混淆矩阵的每一列代表了预测类别,每一列的总数表示预测为该类别的数据的数目;每一行代表了数据的真实归属类别,每一行的数据总数表示该类别的数据实例的数目。混淆矩阵对角线上的数值越大,说明预测为真的数目越多,分类效果越好。

模型测试(predict.py)测试图片

from tensorflow.keras.models import load_model

# import matplotlib.image as processimage

# import matplotlib as plt

import numpy as np

from PIL import Image

import os

model = load_model('Gesture_2.h5')

Gesturetype = ['666', 'yech', 'stop', 'punch', 'OK']

# Gesturetype = ['666', 'stop', 'yech', 'ok', 'one']

path = 'test\\'

file_count = 0

for file in os.listdir(path):

    list = []

    img = Image.open(path + file)

    test = file.split('_')[0]

    table = file.split('_')[1][0]

    img = np.array(img).reshape(-1, 100, 100, 1) / 255

    prediction = model.predict(img)

    print(prediction)

    final_prediction = [result.argmax() for result in prediction][0]

    print(final_prediction)

    ges_type = Gesturetype[final_prediction]

print(ges_type)

模型测试(录制视频.py)测试视频

import cv2

import numpy as np

from tensorflow.keras.models import load_model

from training import Training

import os

from tensorflow.keras import backend

import time

import random

class Gesture():

    def __init__(self, train_path, predict_path, gesture, train_model):

        self.blurValue = 5

        self.bgSubThreshold = 36

        self.train_path = train_path

        self.predict_path = predict_path

        self.threshold = 60

        self.gesture = gesture

        self.train_model = train_model

        self.skinkernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))

        self.x1 = 380

        self.y1 = 60

        self.x2 = 640

        self.y2 = 350

    def collect_gesture(self, capture, ges, photo_num):

        photo_num = photo_num

        vedeo = False

        predict = False

        count = 0

        # 读取默认摄像头

        cap = cv2.VideoCapture(capture)

        # 设置捕捉模式

        cap.set(10, 200)

        # 背景减法创建及初始化

        bgModel = cv2.createBackgroundSubtractorMOG2(0, self.bgSubThreshold)

        while True:

            # 读取视频帧

            ret, frame = cap.read()

            # 镜像转换

            frame = cv2.flip(frame, 1)

            # cv2.imshow('Original', frame)

            # 双边滤波

            frame = cv2.bilateralFilter(frame, 5, 50,100)

            # 绘制矩形,第一个为左上角坐标(x,y),第二个为右下角坐标

            # rec = cv2.rectangle(frame, (220, 50), (450, 300), (255, 0, 0), 2)

            rec = cv2.rectangle(frame, (self.x1, self.y1), (self.x2, self.y2), (255, 0, 0), 2)

            # 定义roi区域,第一个为y的取值,第2个为x的取值

            # frame = frame[50:300, 220:450]

            frame = frame[self.y1:self.y2, self.x1:self.x2]

            # cv2.imshow('bilateralFilter', frame)

            # 背景减法运动检测

            bg = bgModel.apply(frame, learningRate=0)

            # 显示背景减法的窗口

            # cv2.imshow('bg', bg)

            # 图像边缘处理--腐蚀

            fgmask = cv2.erode(bg, self.skinkernel, iterations=1)

            # 显示边缘处理后的图像

            # cv2.imshow('erode', fgmask)

            # 将原始图像与背景减法+腐蚀处理后的蒙版做"与"操作

            bitwise_and = cv2.bitwise_and(frame, frame, mask=fgmask)

            # 显示与操作后的图像

            # cv2.imshow('bitwise_and', bitwise_and)

            # 灰度处理

            gray = cv2.cvtColor(bitwise_and, cv2.COLOR_BGR2GRAY)

            # 高斯滤波

            blur = cv2.GaussianBlur(gray, (self.blurValue, self.blurValue), 2)

            # cv2.imshow('GaussianBlur', blur)

【计算机毕设之基于CNN+opencv的手势识别系统】 https://www.bilibili.com/video/BV1nB4y1U7m8/?share_source=copy_web&vd_source=3d18b0a7b9486f50fe7f4dea4c24e2a4

http://www.bilibili.com/video/BV1nB4y1U7m8/?share_source=copy_web&vd_source=3d18b0a7b9486f50fe7f4dea4c24e2a4

          

  • 53
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源码空间站11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值