深度学习tensorflow之手势识别摄像头测试

默认上一篇对程序的改造,改造完成并且训练效果很好。可以准备实战测试了。

#callFrame 模块
import sys
import time
# from SaveGesture import *
# from TestGesture import *
import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import Train_inputdata
import Train_model
import os
import tensorflow as tf


def get_one_image(train):
    n = len(train)
    ind = np.random.randint(0, n)
    img_dir = train[ind]

    image = Image.open(img_dir)
    image = image.resize([227, 227])
    image = np.array(image)
    return image


def evaluate_one_image():
    """
    Test one image against the saved models and parameters
    返回字符串1~5
    """
    train_dir = './data/testImage/'
    train, train_label = Train_inputdata.get_files(train_dir)
    image_array = get_one_image(train)

    with tf.Graph().as_default():
        BATCH_SIZE = 1
        N_CLASSES = 10

        image = tf.cast(image_array, tf.float32)
        image = tf.image.per_image_standardization(image)
        image = tf.reshape(image, [1, 227, 227, 3])
        logit = Train_model.cnn_inference(image, BATCH_SIZE, N_CLASSES, keep_prob=1)

        logit = tf.nn.softmax(logit)

        x = tf.placeholder(tf.float32, shape=[227, 227, 3])

        logs_train_dir = '../gesture-recognition-master/log/'

        saver = tf.train.Saver()

        with tf.Session() as sess:

            # print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(logs_train_dir)
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                # print('Loading success, global_step is %s' % global_step)
            else:
                print('No checkpoint file found')

            prediction = sess.run(logit, feed_dict={x: image_array})
            max_index = np.argmax(prediction)
            prediction = np.max(prediction)
            # print('num = %d' %max_index)
            # print('prediction = %s' %prediction)
            return max_index,prediction

def showStatistics(predictedClass, confidence):

    textImage = np.zeros((300,512,3), np.uint8)
    className = ""
    # predictedClass = str(predictedClass)
    if predictedClass == 0:
        className = "up"
    elif predictedClass == 1:
        className = "down"
    elif predictedClass == 2:
        className = "left"
    elif predictedClass == 3:
        className = "right"
    elif predictedClass == 4:
        className = "ok"
    elif predictedClass == 5:
        className = "eight"
    elif predictedClass == 6:
        className = "two"
    elif predictedClass == 7:
        className = "one"
    elif predictedClass == 8:
        className = "zero"
    else:
        className = "bg"
    cv2.putText(textImage,"Pedicted Class : " + className, 
    (30, 30), 
    cv2.FONT_HERSHEY_SIMPLEX, 
    1,
    (255, 255, 255),
    2)

    cv2.putText(textImage,"Confidence : " + str(confidence * 100) + '%', 
    (30, 100), 
    cv2.FONT_HERSHEY_SIMPLEX, 
    1,
    (255, 255, 255),
    2)
    cv2.imshow("Statistics", textImage)


def main():
     cameraCapture = cv2.VideoCapture(0)     
     img_roi_y = 30
     img_roi_x = 200
     img_roi_height = 350  # [2]设置ROI区域的高度
     img_roi_width = 350  # [3]设置ROI区域的宽度
     while(True):
        start = time.time()
        success, frame = cameraCapture.read()
        if success is True:
            cv2.imwrite("./data/testImage/" + "test.jpg", frame)
        testImg = cv2.imread('./data/testImage/test.jpg')
        # print(testImg.shape)  # G7笔记本的摄像头是(480,640,3) 高度,宽度,通道数
        img_roi = testImg[img_roi_y:(img_roi_y + img_roi_height), img_roi_x:(img_roi_x + img_roi_width)]
    
        cv2.imshow("[ROI_Img]", img_roi)
        cv2.imwrite("./data/testImage/roi/" + "img_roi.jpg", img_roi)
        cv2.waitKey(1)        
        end = time.time()
        print('time= %s' %(end-start))
        start = time.time()  
        (predictedClass, confidence) = evaluate_one_image()
        end = time.time()
        print('time= %s' %(end-start))
        showStatistics(predictedClass, confidence)
if __name__ == "__main__":
    main()
    

这一段是摄像头开打,测试手势。

我就不一一贴图了。这里有预测的分组,还有准确率。

如果你在不停的变换手势的时候,准确率不高, 分组错误的话,就重新修改模型去训练。都是工程师。不可能一次就成功的。找到问题继续解决。

有兴趣相关技术的人可以看个人介绍和我毛华望联系。 目前人在深圳宝安区。

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值