opencv-dnn

复制代码
# utils_words.txt  标签文件
import os


image_types = (".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff")


def list_images(basePath, contains=None):
    # return the set of files that are valid
    return list_files(basePath, validExts=image_types, contains=contains)


def list_files(basePath, validExts=None, contains=None):
    # loop over the directory structure
    for (rootDir, dirNames, filenames) in os.walk(basePath):
        # loop over the filenames in the current directory
        for filename in filenames:
            # if the contains string is not none and the filename does not contain
            # the supplied string, then ignore the file
            if contains is not None and filename.find(contains) == -1:
                continue

            # determine the file extension of the current file
            ext = filename[filename.rfind("."):].lower()

            # check to see if the file is an image and should be processed
            if validExts is None or ext.endswith(validExts):
                # construct the path to the image and yield it
                imagePath = 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)

​
相关推荐
哥只是传说中的小白1 分钟前
无需验证手机Sora2也能用!视频生成,创建角色APi接入教程,开发小白也能轻松接入
数据库·人工智能
lkbhua莱克瓦243 分钟前
参数如何影响着大语言模型
人工智能·llm·大语言模型
saoys5 分钟前
Opencv 学习笔记:图像膨胀 / 腐蚀(附滑块动态调节腐蚀核大小)
笔记·opencv·学习
北京盛世宏博6 分钟前
数据可追溯 + 加密传输:以太网温湿度变送器守护涉密档案安全
大数据·运维·人工智能·档案温湿度
一招定胜负6 分钟前
从基础到进阶:四种经典图像边缘检测算法全解析
图像处理·opencv·计算机视觉
阿星AI工作室6 分钟前
魔改豆包输入法变电脑版,立即拥有千元AI语音输入法typeless平替
前端·人工智能
wenzhangli78 分钟前
SkillFlow:回归本质,重新定义AI时代流程管控
人工智能
m0_603888718 分钟前
Over-Searching in Search-Augmented Large Language Models
人工智能·ai·语言模型·自然语言处理·论文速览
狮子座明仔8 分钟前
GDPO:英伟达提出多奖励强化学习的“解耦归一化“策略,解决GRPO的优势崩溃问题
人工智能·gpt·语言模型·自然语言处理
rgb2gray9 分钟前
论文深度解析:基于大语言模型的城市公园多维度感知解码与公平性提升
大数据·人工智能·机器学习·语言模型·自然语言处理·数据分析·可解释