Ask Your Question
0

.pbtxt work well in python,but show Nothing in c++

asked 2019-03-18 23:55:25 -0600

wjwangz gravatar image

updated 2019-03-19 04:38:48 -0600

first,I download ssd_mobilenet_v2_coco.config at https://github.com/tensorflow/models/... according to my needs modify “num_classes”-->2, “batch_size” -->24,and some“path”。

second,I use myself pics train ,then obtain frozen_inference_graph.pb

third ,I use tf_text_graph_ssd.py generate frozen_inference_graph.pbtxt

Fourth,I test it by cv.dnn.readNetFromTensorflo in python 3.6 ,and pbtxt pb‘s effect is not bad image description

BUT when I use in C++ ,no errors, and show result pics without any marks.

image description

python code

import cv2 as cv

cvNet = 
cv.dnn.readNetFromTensorflow('C:/dl/aaa/frozen_inference_graph.pb','C:/dl/aaa/frozen_inference_graph.pbtxt')
img = cv.imread('C:/dl/aaa/t3.jpg')
rows = img.shape[0]
cols = img.shape[1]
cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
cvOut = cvNet.forward()

for detection in cvOut[0,0,:,:]:
score = float(detection[2])
if score > 0.2:
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)), (23, 230, 210), thickness=2)
cv.putText(img, str(score), (int(right), int(bottom)), cv.FONT_HERSHEY_SIMPLEX, 1, (23, 230, 210), 2)

cv.imshow('img', img)
cv.waitKey()

c++code

#include "pch.h"
#include<opencv2\opencv.hpp>
#include<opencv2\dnn.hpp>
#include

using namespace std;
using namespace cv;
using namespace dnn;
const size_t inWidth = 300;
const size_t inHeight = 300;
const float WHRatio = inWidth / (float)inHeight;
const char* classNames[] = { "background","eye-open","eye-closed" };//

int main() {

String weights = "C:/dl/aaa/frozen_inference_graph.pb";
String prototxt = "C:/dl/aaa/frozen_inference_graph.pbtxt";
dnn::Net net = cv::dnn::readNetFromTensorflow(weights, prototxt);
Mat image = imread("C:/dl/aaa/21.jpg");

float detect_thresh = 0.2;

net.setInput(blobFromImage(image, 1.0, Size(300, 300),true, false));
 //net.setInput(blobFromImage(image, 1.0/256, Size(300, 300),true, false));  //the result is same
Mat cvOut = net.forward();
Mat detectionMat(cvOut.size[2], cvOut.size[3] , CV_32F, cvOut.ptr<float>());
for (int i = 0; i < detectionMat.rows; i++)
{
    int obj_class = detectionMat.at<float>(i, 1);
    float confidence = detectionMat.at<float>(i, 2);

    if (confidence > detect_thresh)
    {
    size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));

    int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * image.cols);
    int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * image.rows);
    int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * image.cols);
    int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * image.rows);

    Rect object((int)xLeftBottom, (int)yLeftBottom,
        (int)(xRightTop - xLeftBottom),
        (int)(yRightTop - yLeftBottom));
        rectangle(image, object, Scalar(0, 0, 255), 2);
        putText(image, classNames[obj_class], Point(xLeftBottom, yLeftBottom - 10), 3, 0.5, Scalar(0, 0, 255), 2);
    }
}

imshow("test", image);
cv::waitKey(0);

return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2019-03-19 03:09:53 -0600

berak gravatar image

updated 2019-03-19 03:13:35 -0600

your Rect parsing is broken, have a look here

  • the network output is: left,top,right bottom, and there can be no thing as int xLeftBottom at all. (looks like you're confusing yourself with the bad naming)
  • opencv's Rect is: (x,y,w,h)
edit flag offensive delete link more

Comments

thanks for your answer,but it seems not the Key reasons.

wjwangz gravatar imagewjwangz ( 2019-03-19 04:37:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-03-18 23:55:25 -0600

Seen: 507 times

Last updated: Mar 19 '19