当前位置: 首页 > news >正文

opencv-dnn

# utils_words.txt  标签文件
import osimage_types = (".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff")def list_images(basePath, contains=None):# return the set of files that are validreturn list_files(basePath, validExts=image_types, contains=contains)def list_files(basePath, validExts=None, contains=None):# loop over the directory structurefor (rootDir, dirNames, filenames) in os.walk(basePath):# loop over the filenames in the current directoryfor filename in filenames:# if the contains string is not none and the filename does not contain# the supplied string, then ignore the fileif contains is not None and filename.find(contains) == -1:continue# determine the file extension of the current fileext = filename[filename.rfind("."):].lower()# check to see if the file is an image and should be processedif validExts is None or ext.endswith(validExts):# construct the path to the image and yield itimagePath = os.path.join(rootDir, filename)yield imagePath

​
# 导入工具包
import utils_paths
import numpy as np
import cv2# 标签文件处理
rows = open("synset_words.txt").read().strip().split("\n")
classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows]# Caffe所需配置文件
net = cv2.dnn.readNetFromCaffe("bvlc_googlenet.prototxt","bvlc_googlenet.caffemodel")# 图像路径
imagePaths = sorted(list(utils_paths.list_images("images/")))# 图像数据预处理
image = cv2.imread(imagePaths[0])
resized = cv2.resize(image, (224, 224))
# image scalefactor size mean swapRB 
blob = cv2.dnn.blobFromImage(resized, 1, (224, 224), (104, 117, 123))
print("First Blob: {}".format(blob.shape))# 得到预测结果
net.setInput(blob)
preds = net.forward()# 排序,取分类可能性最大的
idx = np.argsort(preds[0])[::-1][0]
text = "Label: {}, {:.2f}%".format(classes[idx],preds[0][idx] * 100)
cv2.putText(image, text, (5, 25),  cv2.FONT_HERSHEY_SIMPLEX,0.7, (0, 0, 255), 2)# 显示
cv2.imshow("Image", image)
cv2.waitKey(0)# Batch数据制作
images = []# 方法一样,数据是一个batch
for p in imagePaths[1:]:image = cv2.imread(p)image = cv2.resize(image, (224, 224))images.append(image)# blobFromImages函数,注意有s
blob = cv2.dnn.blobFromImages(images, 1, (224, 224), (104, 117, 123))
print("Second Blob: {}".format(blob.shape))# 获取预测结果
net.setInput(blob)
preds = net.forward()
for (i, p) in enumerate(imagePaths[1:]):image = cv2.imread(p)idx = np.argsort(preds[i])[::-1][0]text = "Label: {}, {:.2f}%".format(classes[idx],preds[i][idx] * 100)cv2.putText(image, text, (5, 25),  cv2.FONT_HERSHEY_SIMPLEX,0.7, (0, 0, 255), 2)cv2.imshow("Image", image)cv2.waitKey(0)​

  

 

 

 

 

http://www.lryc.cn/news/138800.html

相关文章:

  • 如何选择合适的开源许可证?
  • 【前端】深入解析CSS:选择器、显示模式、背景属性和特征剖析
  • 算法训练营第三十四天(8.23)| 动态规划Part04:01背包
  • 【python】tkinter使用多进程打包成exe后multiprocessing无法关闭对应进程
  • Redis工具类(缓存操作,Object转换成JSON数据)
  • Linux 下 Java Socket 编程报 java.net.Exception:Permission denied (权限不足)
  • IDEA项目实践——VUE介绍与案例分析
  • vue-canvas基本使用和注意事项-动画闪烁效果-自适应适配不同分辨率问题
  • Jmeter 如何才能做好接口测试?
  • 电商平台京东平台获得京东商品描述API接口演示案例
  • 《算法竞赛·快冲300题》每日一题:“单位转换”
  • R语言13-R语言中的数据导入导出和批量导入
  • 【Java】对象与类
  • 视频尺寸缩小,一键批量剪辑,轻松制作精简版
  • leetcode做题笔记94. 二叉树的中序遍历
  • UWB高精度人员定位系统源码,微服务+java+ spring boot+ vue+ mysql技术开发
  • 企业党建杂志企业党建杂志社企业党建编辑部2023年第4期目录
  • ChatGPT + Flutter快速开发多端聊天机器人App
  • ubuntu18.04复现yolo v8之最终章,realsenseD435i+yolo v8完美运行
  • Python统计中文词频的四种方法
  • sql server 快速安装
  • 机器学习之损失函数
  • nacos适配SqlServer、Oracle
  • 力扣:74. 搜索二维矩阵(Python3)
  • CPU、MCU、MPU、SOC、SOCPC、概念解释之在嵌入式领域常听到的名词含义
  • 每日两题 111二叉树的最小深度 112路径总和(递归)
  • 实训笔记8.24
  • Linux下的系统编程——系统调用(五)
  • 动物体外受精手术VR模拟仿真培训系统保证学生及标本的安全
  • 微信小程序|步骤条