OpenCV调用objectdetectionAPI模型

突然发现OpenCV可以调用很多模型,我竟然不知道!用很多时间想去安装caffe ssd简直是粑粑吃多了,用不用caffemodel又有什么关系

“OpenCV 3.4.1之后的版本中contrib扩展库中的dnn模块支持TensorFlow、Caffe、Pytorch三种深度学习框架的模型,能够修改或者读取网络结构,并且也能够读取已经训练好的模型”

对于我用object detection API训练好的模型
找到两个个文件分别是
frozen_inference_graph.pb模型文件
ssd_mobilenet_v1_coco.config配置文件
去https://github.com/opencv/opencv下载下来
在\opencv-master\samples\dnn运行

python tf_text_graph_ssd.py --input E:\models\dataset\frozen_inference_graph.pb\frozen_inference_graph.pb --output E:\models\dataset\graph.pbtxt --config E:\models\dataset\ssd_mobilenet_v1_coco.config

这时我们得到了一个graph.ptxt文件,再加上frozen_inference_graph.pb
就可以完成检测任务了
当然cv.dnn.readNetFromTensorflow()
还可以改为cv2.dnn.readNetFromCaffe()
cv2.dnn.readNetFromDarknet()
不论什么框架训练的,只要有模型文件和配置文件,加上以下代码实现检测任务,和框架、训练过程分开,用cv2.dnn调用就行了

import numpy as np
import cv2

# 随机生成box的的颜色,CLASSES的第一个必须是background,第二个开始是标签的第一个,因为标签值1,在数列从0开始
CLASSES = ["background","apple", "banana", "orange"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
# 加载模型文件
pb_file = r'E:\models\dataset\frozen_inference_graph.pb/frozen_inference_graph.pb'
pbtxt_file = r'E:\models\dataset\graph.pbtxt'
print("[INFO] loading model...")
net = cv2.dnn.readNetFromTensorflow(pb_file, pbtxt_file)
# 设定识别概率阈值,大于0.3识别概率可以显示
score_threshold = 0.3
img_file = r'E:\models/research/object_detection/tesi_images/image1.jpg'
image = cv2.imread(img_file)
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(image,size=(300, 300),swapRB=True,crop=False)
# pass the blob through the network and obtain the detections and
# predictions
print("[INFO] computing object detections...")
net.setInput(blob)
detections = net.forward()
print(detections)
# loop over the detections
for i in np.arange(0, detections.shape[2]):
	# extract the confidence (i.e., probability) associated with the
	# prediction
	confidence = detections[0, 0, i, 2]
	# filter out weak detections by ensuring the `confidence` is
	# greater than the minimum confidence
	if confidence > score_threshold:
		# extract the index of the class label from the `detections`,
		# then compute the (x, y)-coordinates of the bounding box for
		# the object
		idx = int(detections[0, 0, i, 1])
		#detections[0, 0, i, 3:7]=detections[0,0,i,3]-detections[0,0,i,6]
		box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
		(startX, startY, endX, endY) = box.astype("int")

		# display the prediction
		label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)
		print("[INFO] {}".format(label))
		cv2.rectangle(image, (startX, startY), (endX, endY),
					  COLORS[idx], 2)
		y = startY - 15 if startY - 15 > 15 else startY + 15
		x = (endX + startX) * 0.5
		cv2.putText(image, label, (int(x), y),
					cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)

# show the output image
cv2.imshow("Output", image)
cv2.waitKey(0)

看起来不准是我没训练好
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值