查看: 1837|回复: 0

角蜂鸟AI视觉套件:(二)人工智能深度体验

[复制链接]
  • TA的每日心情

    2018-11-20 13:41
  • 签到天数: 3 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    发表于 2019-2-28 16:27:57 | 显示全部楼层 |阅读模式
    分享到:
    本篇的硬件环境一样,跟着官方的教程来深度体验下角蜂鸟的人工智能的实力。根据官方介绍,角蜂鸟内置的几种深度神经网络模型如下,包括数字识别、人脸识别和物体识别等。

    MNIST 数字识别模型

    Mobilenet-SSD 人脸检测模型

    Mobilenet-SSD VOC物体检测模型

    SqueezeNet 图像分类模型

    GoogleNet 图像识别模型(特征提取)

    FaceNet 人脸识别模型(特征提取)

    SketchGraph 手绘识别模型

    OCR 中文文字识别模型 (Work in progress)

    https://hornedsungem.github.io/Docs/cn/workflow/

    由于在第一讲中已经体验过入门的MNIST手写数字识别了,这里就不做介绍了,官网也有详细解释。

    1. 人脸检测

    使用角蜂鸟和Python调用内置部署的SSD-Mobilenet人脸检测卷积神经网络。检测器分析图片并找出目标的位置和尺寸。

    1. cd ~/hornedSungemSDK/examples/python/

    2. sudo python3 FaceDetector.py
    复制代码

    1.png

    其中SSD [N]中N为检测人脸个数,下一行为人脸检测框Bounding Box的左上角和右下角坐标以及窗的宽高。比如最后一行的几个数表示检测框的左上角在图片坐标系的(175,28),右下角在(245,127),width为245-175=70个像素,height为127-28=99个像素。图片坐标系的原点在图片窗口左上角,横轴为x,纵轴为y。

    1. #! /usr/bin/env python3

    2. # Copyright(c) 2018 Senscape Corporation.
    3. # License: Apache 2.0

    4. import numpy as np, cv2, sys
    5. sys.path.append('../../api/')
    6. import hsapi as hs

    7. WEBCAM = False # Set to True if use Webcam
    8. net = hs.HS('FaceDetector', zoom = True, verbose = 2, threshSSD=0.55)
    9. if WEBCAM: video_capture = cv2.VideoCapture(0)

    10. try:
    11.         while True:
    12.                 if WEBCAM: _, img = video_capture.read()
    13.                 else: img = None
    14.                 result = net.run(img)
    15.                 img = net.plotSSD(result)
    16.                 cv2.imshow("Face Detector", img)
    17.                 cv2.waitKey(1)
    18. finally:
    19.         net.quit()
    复制代码

    来看代码,加上注释,一共二十多行就实现了人脸识别,实时性也很不错,真的很出乎我的意料。这里要给角蜂鸟的程序猿们奖个大香蕉 O(∩_∩)O哈哈~

    再来细看一下


    • 人脸识别Python:hornedSungemSDK/examples/python/FaceDetector.py
    • 模型文件:hornedSungemSDK/examples/graphs/graph_face_SSD
    • 模型文件名称 Filename: graph_face_SSD
    • 描述 Description: Mobilenet + Single-shot detector 人脸检测模型。
    • 属性 Properties:

    Channel: 3 (RGB图)

    Scale: 0.007843

    Mean: [1.0, 1.0, 1.0]

    Image Size: [300,300]

    Type: SSD检测

    为什么可以这么简洁呢?设置net参数,调用net.run传入图片,然后就是结果了。在hornedSungemSDK/api/hsapi.py可以看到run函数里确实是加载神经网络模型self.graph.LoadTensor

    1. def run(self, img=None, **kwargs):
    2.                 if img is None:
    3.                         image = self.graph.GetImage(self.zoom)
    4.                 else:
    5.                         if self.isGray:
    6.                                 image = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
    7.                         else:
    8.                                 image = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    9.                
    10.                         img2load = cv2.resize(image,self.netSize).astype(float)
    11.                         img2load *= self.scale
    12.                         img2load -= self.mean
    13.                         self.graph.LoadTensor(img2load.astype(numpy.float16), None)

    14.                 self.imgSize = image.shape[:2]
    15.                 output, _ = self.graph.GetResult()
    16.                 #print(output)

    17.                 for k,v in kwargs.items():
    18.                         exec('self.'+k+'=v')
    19.                         
    20.                 if self.type in [1,7] : # Classification
    21.                         output = numpy.argmax(output)
    22.                 elif self.type is 2: # SSD Face
    23.                         output = self.getBoundingBoxFromSSDResult(output, self.imgSize)
    复制代码

    我们一起在hornedSungemSDK/api/hsapi.py中分析下源码,L485行

    1. def plotSSD(self, result, labels=None):
    2.                 if labels is None:
    3.                         labels = self.labels

    4.                 display_image = result[0]
    5.                 boxes = result[1]
    6.                 source_image_width = display_image.shape[1]
    7.                 source_image_height = display_image.shape[0]

    8.                 self.msg_debug('SSD [%d]: Box values' % len(boxes),'*')
    9.                 for box in boxes:
    10.                         class_id = box[0]
    11.                         percentage = int(box[1] * 100)

    12.                         label_text = self.labels[int(class_id)] + " (" + str(percentage) + "%)"
    13.                         box_w = box[4]-box[2]
    14.                         box_h = box[5]-box[3]
    15.                         if (box_w > self.imgSize[0]*0.8) or (box_h > self.imgSize[1]*0.8):
    16.                                 continue        

    17.                         self.msg_debug('Box Name: %s' % self.labels[int(class_id)])
    18.                         self.msg_debug('%d %d %d %d - w:%d h:%d' %(box[2],box[3],box[4],box[5],box_w,box_h))
    19. .
    20. .
    21. .
    复制代码

    打印识别结果。

    2. 物体检测

    介绍如何使用角蜂鸟在Python调用内置部署的SSD-Mobilenet物体检测卷积神经网络。检测器分析图片并找出目标的位置和尺寸。

    1. cd ~/hornedSungemSDK/examples/python/

    2. sudo python3 ObjectDetector.py
    复制代码

    2.png


    • 文件名称 Filename: graph_object_SSD
    • 描述 Description: Mobilenet + Single-shot detector 物体检测模型,VOC数据集训练,共20种物体。
    • 实例 Example:
    • Mobilenet-SSD 物体检测
    • 属性 Properties:

    Channel: 3 (RGB图)
    Scale: 0.007843
    Mean: [1.0, 1.0, 1.0]
    Image Size: [300,300]
    Type: SSD检测

    3. 情景记录器

    简单来说就是利用神经网络进行特征提取,分类器,目前最多支持五个。

    1. cd ~/hornedSungemSDK/examples/python/

    2. sudo python3 SceneRecorder.py
    复制代码

    GoogleNet 图像识别模型(特征提取)
    • 版本 Version: 0.1.0
    • 文件名称 Filename: graph_g
    • 描述 Description: GoogleNet 图像识别模型(输出层用于特征提取)。
    • 实例 Example:
    • 情景记录器
    • 属性 Properties:

    Channel: 3 (RGB图)
    Scale: 0.007843
    Mean: [1.0, 1.0, 1.0]
    Image Size: [224,224]
    Type: 特征提取


    3.png
    4.png

    这里我是设置两个手势,伸出1根手指和伸出两根手指,分别为场景1和场景2。

    ‘r’ 将筛选去除每个存档中的冗余图像特征,并生成模型。

    ’s’ 将存档录入至 /misc/record.dat 文件中。按 ‘l’ 可读档

    按 ‘p’ 将重置删除所有存档。


    路径和文件


    • 情景记录器Python:hornedSungemSDK/examples/python/SceneRecorder.py
    • 模型文件:hornedSungemSDK/examples/graphs/graph_g - GoogleNet
    • 模型文件(备选):hornedSungemSDK/examples/graphs/graph_fn - FaceNet


    4.图像识别器(底层API教程)
    图像识别器,比起检测器,识别器可从图片分析得到较细的分类类别,例如猫、狗的某个品种。
    1. cd ~/hornedSungemSDK/examples/python/

    2. sudo python3 ImageRecognition.py
    复制代码
    • 人脸识别Python:SungemSDK/examples/python/ImageRecognition.py
    • 模型文件:SungemSDK/examples/graphs/graph_sz
    SqueezeNet 图像分类模型

    • 版本 Version: 0.1.0
    • 文件名称 Filename: graph_sz
    • 描述 Description: SqueezeNet 图像分类模型,由ImageNet数据集训练,共1000种物体。
    • 实例 Example:

    SqueezeNet 图像识别器(底层API教程)
    • 属性 Properties:


    Channel: 3 (BGR)
    Scale: 1
    Mean: [104, 117, 123]
    Image Size: [227,227]
    Type: 识别
    5.png
    按W和S可以相应的增大或减小ROI区域,这点官网没有提哦。

    5. 你画我猜
    1. cd ~/hornedSungemSDK/examples/python/

    2. sudo python3 SketchGuess.py
    复制代码
    请见此文章

    本文作者 MS2308
    来源 CSDN

    相关文章阅读
    (一)开箱及软件开发环境配置
    (二)人工智能深度体验
    (三)ROS案例
    (四)ROS下订阅并处理图像

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 07:53 , Processed in 0.111898 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.