查看: 2817|回复: 0

角蜂鸟实例教程 > Python > 图像识别器

[复制链接]

该用户从未签到

发表于 2018-8-3 13:58:57 | 显示全部楼层 |阅读模式
分享到:
本帖最后由 cll826 于 2018-8-3 13:58 编辑

图像识别器(底层API教程)

SqueezeNet 1000类图像识别器

本章为您介绍如何使用角蜂鸟在Python调用底层API实现基于SqueezeNet的图像识别器。
比起检测器,识别器可从图片分析得到较细的分类类别,例如猫、狗的某个品种。

  • 本教程基于Ubuntu 16.04系统

路径和文件

  • 人脸识别Python:SungemSDK/examples/python/ImageRecognition.py
  • 模型文件:SungemSDK/examples/graphs/graph_sz

图像识别 Image Recognition

python目录下执行以下命令来启动 图像识别 范例。

  1. ~/SungemSDK/examples/python$ python3 ImageRecognition.py
复制代码

识别目标图像为/SungemSDK/中区域(ROI)的图像部分。通过按 ‘w’ 与 ’s’ 可调整 ROI 的大小。
得到 Top5 结果例如: 属于计算机键盘概率98.39%、空格1.40%、打字机键盘0.22%、笔记本0.03%、仓鼠0.00%。

图像.png

因为调用全部函数为底层API,终端不显示任何提示。

参数设置
初始化
  1. # Load device (载入设备)
  2. devices = hs.EnumerateDevices()
  3. dev = hs.Device(devices[0])
  4. dev.OpenDevice()

  5. # Load CNN model (载入模型)
  6. with open('../graphs/graph_sz', mode='rb') as f:
  7.         b = f.read()
  8. graph = dev.AllocateGraph(b)
  9. dim = (227,227)

  10. # Load classes (载入分类标签)
  11. classes=np.loadtxt('../misc/image_category.txt',str,delimiter='\t')

  12. # Set camera mode (摄像头模式)
  13. if WEBCAM: video_capture = cv2.VideoCapture(0)
复制代码

两种模式(WEBCAM):

  • True时使用外接USB摄像头
  • False时使用角蜂鸟内置摄像头

图像识别
  1. # 裁切出ROI区域
  2. sz = image_raw.shape
  3. cx = int(sz[0]/2)
  4. cy = int(sz[1]/2)
  5. ROI = int(sz[0]*ROI_ratio)
  6. cropped = image_raw[cx-ROI:cx+ROI,cy-ROI:cy+ROI,:]

  7. # 前处理
  8. cropped = cropped.astype(np.float32)
  9. cropped[:,:,0] = (cropped[:,:,0] - 104)
  10. cropped[:,:,1] = (cropped[:,:,1] - 117)
  11. cropped[:,:,2] = (cropped[:,:,2] - 123)

  12. # 图像数据传入角蜂鸟并排序分类结果
  13. graph.LoadTensor(cv2.resize(cropped,dim).astype(np.float16), 'user object')
  14. output, userobj = graph.GetResult()
  15. output_label = output.argsort()[::-1][:5]
复制代码

可视化

  1. # 输出文字
  2. for i in range(5):
  3.         label = re.search("n[0-9]+\s([^,]+)", classes[output_label[i]]).groups(1)[0]
  4.         cv2.putText(image_raw, "%s %0.2f %%" % (label, output[output_label[i]]*100), (20, 50+i*30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)

  5. # 画框
  6. cv2.rectangle(image_raw, (cy-ROI, cx-ROI), (cy+ROI, cx+ROI),(255,255,0), 5)

  7. # 显示图像
  8. cv2.imshow('Result',image_raw)

  9. # 检测键盘输入
  10. key = cv2.waitKey(1)
  11. if key == ord('w'):
  12.         ROI_ratio += 0.1
  13. elif key == ord('s'):
  14.         ROI_ratio -= 0.1
  15. if ROI_ratio < 0.1:
  16.         ROI_ratio = 0.1
复制代码

输入result,输出画框之后的图像。之后通过OpenCV函数来显示图像。


产品购买链接:https://www.cirmall.com/shop/?c=products&a=view&id=5113









回复

使用道具 举报

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

本版积分规则

关闭

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

手机版|小黑屋|与非网

GMT+8, 2024-4-16 15:03 , Processed in 0.126707 second(s), 16 queries , MemCache On.

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

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.