我正在写一些物体检测代码。所以,我做了一次培训,从tensorflow获得了.pb和graph.pbtxt文件。接下来我要做的是python代码,它使用opencv for python基于这两个文件执行对象检测。 下面是我的python脚本,它运行良好:
import cv2 as cv
cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'graph.pbtxt')
img = cv.imread('75.png')
rows = img.shape[0]
cols = img.shape[1]
cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
cvOut = cvNet.forward()
print(rows)
print(cols)
for detection in cvOut[0,0,:,:]:
print(type(cvOut[0,0,:,:]))
score = float(detection[2])
if score > 0.1:
left = detection[3] * cols
top = detection[4] * rows
right = detection[5] * cols
bottom = detection[6] * rows
cv.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (0, 0, 255), thickness=2)
print('true')
print(score)
cv.imshow('img', cv.resize(img, None, fx=0.3, fy=0.3))
cv.waitKey()
但是,我需要使用传统OpenCV的包装器EmguCV库使用.NET(C#)完成相同的代码
下面是我设法编写的代码的一部分:
private bool RecognizeCO(string fileName)
{
Image<Bgr, byte> img = new Image<Bgr, byte>(fileName);
int cols = img.Width;
int rows = img.Height;
imageBox2.Image = img;
Net netcfg = DnnInvoke.ReadNetFromTensorflow("CO.pb", "graph.pbtxt");
netcfg.SetInput(DnnInvoke.BlobFromImage(img));
Mat mat = netcfg.Forward();
return false;
}
不幸的是,我不知道在那之后该怎么办。。。。实际上,我在C代码中得到了相同的结果,就像在Python代码中一样。我知道,我可以从C#调用python脚本,但我真的需要用C#和EmguCV来完成这段代码。 请帮帮我! 提前感谢您的帮助
所以,我终于成功地结束了那段代码。。。 解决方案相当简单: 在获取
mat
变量后,我们可以从Mat
中获取Data
作为浮点[,,]数组:float[,,,] flt = (float[,,,])mat.GetData();
或者只使用一维数组:float[] flt = (float[])mat.GetData(jagged:false)
(但我更喜欢上一个)然后,在该数组中创建一个循环:
最后,我们可以保存该图像:
因此,结果代码如下所示:
相关问题 更多 >
编程相关推荐