首页 > 其他分享 >体感游戏 | 手势识别玩飞机大战游戏(四) 使用深度学习实现手势识别玩飞机大战游戏

体感游戏 | 手势识别玩飞机大战游戏(四) 使用深度学习实现手势识别玩飞机大战游戏

时间:2022-10-18 13:37:00浏览次数:82  
标签:游戏 graph image cv2 label detection 识别 手势


    后面将分四篇文章来介绍实现手势识别控制飞机大战游戏的功能,它们分别是:


实现改为了深度学习实现,这里识别手势主要用到的是目标检测,当然不止TensorFlow可以实现,其他能够做到实时的目标检测网络也很多,比如最近比较火的YoloV4/V5。这里演示我为了方便,就用我以前训练好的SSD手势识别模型,你只需要使用下面的代码,加载模型就可以测试:

import os
import sys
import tarfile
import cv2
import tensorflow as tf
import numpy as np

from utils import label_map_util
from utils import visualization_utils as vis_util

# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_FROZEN_GRAPH = './export/frozen_inference_graph_ssd.pb'

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('./data', 'hands_label_map.pbtxt')

NUM_CLASSES = 3

detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')

label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categorys = label_map_util.convert_label_map_to_categories(label_map,max_num_classes=NUM_CLASSES,use_display_name=True)
categorys_index = label_map_util.create_category_index(categorys)

config = tf.ConfigProto()
config.gpu_options.allow_growth = True

cap = cv2.VideoCapture(0)
#cap = cv2.VideoCapture("xxx.mp4")

with detection_graph.as_default():
with tf.Session(graph=detection_graph,config=config) as sess:
#image = cv2.imread("./VOC2012/JPEGImages/700.jpg")
while True:
start = cv2.getTickCount()
ret,image = cap.read()
image_np_expanded = np.expand_dims(image,axis = 0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
(boxes,scores,classes,num_detections) = sess.run([boxes,scores,classes,\
num_detections],feed_dict={image_tensor:image_np_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
categorys_index,
min_score_thresh = 0.5,
use_normalized_coordinates = True,
line_thickness = 5
)

end = cv2.getTickCount()
use_time = (end - start) / cv2.getTickFrequency()
print("use_time:%0.3fs" % use_time)
strText = ("SSD one frame use_time: %0.3fs" % use_time)
cv2.putText(image,strText,(5,30),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,0),2)
cv2.imshow("object_detection", image)
if cv2.waitKey(1)&0xFF ==27:
break
cv2.destroyAllWindows()

体感游戏 | 手势识别玩飞机大战游戏(四) 使用深度学习实现手势识别玩飞机大战游戏_手势识别

体感游戏 | 手势识别玩飞机大战游戏(四) 使用深度学习实现手势识别玩飞机大战游戏_飞机大战_02

体感游戏 | 手势识别玩飞机大战游戏(四) 使用深度学习实现手势识别玩飞机大战游戏_手势识别_03

    上面是用TensorFlow Object Detection API训练的SSD手势识别模型,训练步骤网上很多参考文章,当然这里可以替换为YoloV4或者YoloV5,识别的时候可以使用GPU加速或者OpenVINO CPU加速。

    用深度学习目标检测的方法和OpenCV传统方法识别的优缺点这里做个简单说明:

    OpenCV手势识别优缺点:

  • 优点 :灵活、简单、省时。在允许情况下做简单算法设计即可达到较好的效果,不用花长时间标注样本和训练;
  • 缺点:对环境(光照,背景等)适应性差。

    深度学习目标检测手势识别优缺点:

  • 优点:对环境(光照,背景等)适应性好---前提是样本足够丰富足够多
    可以使用硬件加速;
  • 缺点:耗费时间长(包括标注时间、训练时间和调参优化等)

    所以总体来说,各有优缺点,根据实际情况来使用,我们工作中一般能使用传统算法解决的尽量不用深度学习,看大家自己的应用场景决定吧。

    剩余的步骤就和上篇文章一样了,讲手势识别部分用目标检测的方法代替即可,源码前面的文章都有,大家自己组合整理一下就可以用,有兴趣可以自行优化。

    如果您看到这,相比也算是本公众号的真爱粉了,下面做一个简单调研活动,我想了解下大家比较希望看到在公众号更新哪些内容呢?可以投票或留言回复,我会根据投票和留言点赞数尽量多更新相关文章,谢谢。

标签:游戏,graph,image,cv2,label,detection,识别,手势
From: https://blog.51cto.com/stq054188/5765881

相关文章