python opencv载入TensorFlow训练的pb模型

python opencv载入TensorFlow训练的pb模型

感谢https://blog.csdn.net/heiheiya/article/details/88825135文章提供的帮助。

先看一下调用模型的代码。

import cv2
import time
# Pretrained classes in the model
classNames = {0: 'background',
              1: 'blue', 2: 'hn', 3: 'mf', 4: 'red', 5: 'tls', 6: 'whh',
              7: 'wq', 8: 'xh', 9: 'xb'}


def id_class_name(class_id, classes):
    for key, value in classes.items():
        if class_id == key:
            return value

t1=time.time()
# Loading model
model = cv2.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'graph.pbtxt')
image = cv2.imread("images/yld.jpg")

image_height, image_width, _ = image.shape

model.setInput(cv2.dnn.blobFromImage(image, size=(300, 300), swapRB=True))
output = model.forward()
# print(output[0,0,:,:].shape)


for detection in output[0, 0, :, :]:
    confidence = detection[2]
    if confidence > .5:
        class_id = detection[1]
        class_name = id_class_name(class_id, classNames)
        print(str(str(class_id) + " " + str(detection[2]) + " " + class_name))
        box_x = detection[3] * image_width
        box_y = detection[4] * image_height
        box_width = detection[5] * image_width
        box_height = detection[6] * image_height
        print(box_x, box_y, box_width, box_height)
        cv2.rectangle(image, (int(box_x), int(box_y)), (int(box_width), int(box_height)), (23, 230, 210), thickness=1)
        cv2.putText(image, class_name, (int(box_x), int(box_y + .05 * image_height)), cv2.FONT_HERSHEY_SIMPLEX,
                    (.005 * image_width), (0, 0, 255))

cv2.imshow('image', image)
# cv2.imwrite("image_box_text.jpg",image)
t2=time.time()
print(t2-t1)
cv2.waitKey(0)
cv2.destroyAllWindows()

之前尝试其他博客的方式,在调用cv2.dnn.readNetFromTensorflow函数时都会有一个错误,解析graph.pbtxt文件失败。原因就是网上提供的pbtxt文件都已经过时了,因此最好的方式便是用官网提供的pb产生pbtxt文件的方式。为了方便起见,这是通往官网的链接:https://github.com/opencv/opencv/tree/master/samples/dnn
里面有两个文件需要下载,tf_text_graph_common.py与tf_text_graph_ssd.py(这是ssd模型转pbtxt的py文件,其他神经网络链接里也有转化代码文件)。
复制粘贴后,运行命令

 python tf_text_graph_ssd.py \
--input /path/xxx.pb \
--config /path/xxx.config \
--output /path/xxx.pbtxt 

/path可以是绝对路径,也可以是相对路径,自己设置。
再去调用cv2.dnn.readNetFromTensorflow,完美通过。

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
在你提供的引用中,有关于Python中使用OpenCVTensorFlow的信息。根据\[1\]中的代码,你可以使用OpenCV和Matplotlib库来可视化图像数据。而在\[2\]中,提到了TensorFlow中的tf.nn.conv2d函数,该函数用于进行卷积操作,其中包括输入图像、卷积核、步长和填充等参数。另外,在\[3\]中提到了安装Python的版本和一些常用的数据包,包括TensorFlowOpenCV等。所以,如果你想在Python中使用OpenCVTensorFlow,你可以按照\[3\]中提到的版本要求进行安装,并参考\[1\]和\[2\]中的代码示例来进行相应的操作。 #### 引用[.reference_title] - *1* [使用Python+OpenCV+Tensorflow实现图像聚类](https://blog.csdn.net/woshicver/article/details/114650362)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [pythontensorflowopencv的小总结(一)](https://blog.csdn.net/weixin_42267309/article/details/98174267)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [利用pythontensorflowopencv实现人脸识别(包会)!](https://blog.csdn.net/qq_42633819/article/details/81191308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值