前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >体感游戏 | 手势识别玩飞机大战游戏(四) 使用深度学习实现手势识别玩飞机大战游戏

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

作者头像
Color Space
发布2021-01-22 10:25:13
1.5K0
发布2021-01-22 10:25:13
举报
文章被收录于专栏:OpenCV与AI深度学习
后面将分四篇文章来介绍实现手势识别控制飞机大战游戏的功能,它们分别是:

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

代码语言:javascript
复制
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()

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

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

OpenCV手势识别优缺点:

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

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

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

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

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

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

觉得有用,麻烦给个赞和在看~

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 OpenCV与AI深度学习 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图像识别
腾讯云图像识别基于深度学习等人工智能技术,提供车辆,物体及场景等检测和识别服务, 已上线产品子功能包含车辆识别,商品识别,宠物识别,文件封识别等,更多功能接口敬请期待。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档