<p dir="auto">hi</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import cv2

cap=cv2.VideoCapture(&quot;set/your/file/here&quot;)
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
cnt = cap.get(cv2.CAP_PROP_FRAME_COUNT)
print(&quot;Opencv width %d frames %d&quot;%(w,cnt))

cap.read()
ts= cap.get(cv2.CAP_PROP_POS_MSEC)
print(&quot;ts @ frame 0 after read %.3f&quot;%ts)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">cv2</span>

<span class="pl-s1">cap</span><span class="pl-c1">=</span><span class="pl-s1">cv2</span>.<span class="pl-v">VideoCapture</span>(<span class="pl-s">"set/your/file/here"</span>)
<span class="pl-s1">w</span> <span class="pl-c1">=</span> <span class="pl-s1">cap</span>.<span class="pl-en">get</span>(<span class="pl-s1">cv2</span>.<span class="pl-v">CAP_PROP_FRAME_WIDTH</span>)
<span class="pl-s1">cnt</span> <span class="pl-c1">=</span> <span class="pl-s1">cap</span>.<span class="pl-en">get</span>(<span class="pl-s1">cv2</span>.<span class="pl-v">CAP_PROP_FRAME_COUNT</span>)
<span class="pl-en">print</span>(<span class="pl-s">"Opencv width %d frames %d"</span><span class="pl-c1">%</span>(<span class="pl-s1">w</span>,<span class="pl-s1">cnt</span>))

<span class="pl-s1">cap</span>.<span class="pl-en">read</span>()
<span class="pl-s1">ts</span><span class="pl-c1">=</span> <span class="pl-s1">cap</span>.<span class="pl-en">get</span>(<span class="pl-s1">cv2</span>.<span class="pl-v">CAP_PROP_POS_MSEC</span>)
<span class="pl-en">print</span>(<span class="pl-s">"ts @ frame 0 after read %.3f"</span><span class="pl-c1">%</span><span class="pl-s1">ts</span>)</pre></div>
<p dir="auto">i have run this on my PC and got different results in different versions:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="after 4.1.2:
ts @ frame 0 after read 0.000

before 4.1.1:
ts @ frame 0 after read 40.000"><pre lang="text" class="notranslate"><code class="notranslate">after 4.1.2:
ts @ frame 0 after read 0.000

before 4.1.1:
ts @ frame 0 after read 40.000
</code></pre></div>
<p dir="auto">so i am a little confused.</p>
<ul dir="auto">
<li>which one is (will be) correct in the future?</li>
<li>what is the actual meaning of these timestamps? (start time ? end time ? )</li>
</ul>
<p dir="auto">thanks</p>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt;  3.4.1</li>
<li>Operating System / Platform =&gt;  windows 64</li>
<li>Compiler =&gt;  visual studio 2017</li>
</ul>
<h5 dir="auto">Detailed description</h5>

<p dir="auto">Trained a model on tensorflow for 2 classes.<br>
I'm using the sample Opencv dnn code to call my .pb file, and it predicts only one single class. Specifically the second class (second line) from labels text file.</p>
<p dir="auto">My C++ code for inference: (note -  for input blob I factored to scale 1/127, swapped channels and also did mean subtraction but to no luck)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

// Copyright (C) 2016, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.

/*
Sample of using OpenCV dnn module with Tensorflow Inception model.
*/

#include &lt;stdafx.h&gt;
using namespace cv;
using namespace cv::dnn;

#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

const String keys =
&quot;{help h    || Sample app for loading Inception TensorFlow model. &quot;
&quot;The model and class names list can be downloaded here: &quot;
&quot;https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip }&quot;
&quot;{model m   |tensorflow_inception_graph.pb| path to TensorFlow .pb model file }&quot;
&quot;{image i   || path to image file }&quot;
&quot;{i_blob    | input | input blob name) }&quot;
&quot;{o_blob    | softmax2 | output blob name) }&quot;
&quot;{c_names c | imagenet_comp_graph_label_strings.txt | path to file with classnames for class id }&quot;
&quot;{result r  || path to save output blob (optional, binary format, NCHW order) }&quot;
;

void getMaxClass(const Mat &amp;probBlob, int *classId, double *classProb);
std::vector&lt;String&gt; readClassNames(const char *filename);

int main(int argc, char **argv)
{
	cv::CommandLineParser parser(argc, argv, keys);

	if (parser.has(&quot;help&quot;))
	{
		parser.printMessage();
		return 0;
	}

	String modelFile = &quot;OptimizedGraph.pb&quot;;
	String imageFile = &quot;img.JPG&quot;;
	String inBlobName = &quot;input&quot;;
	String outBlobName = &quot;side&quot;;
	if (!parser.check())
	{
		parser.printErrors();
		return 0;
	}

	String classNamesFile = &quot;imagenet_comp_graph_label_strings.txt&quot;;
	String resultFile = &quot;output.txt&quot;;

	//! [Initialize network]
	dnn::Net net = readNetFromTensorflow(modelFile);
	//! [Initialize network]

	if (net.empty())
	{
		std::cerr &lt;&lt; &quot;Can't load network by using the mode file: &quot; &lt;&lt; std::endl;
		std::cerr &lt;&lt; modelFile &lt;&lt; std::endl;
		exit(-1);
	}

	//! [Prepare blob]
	Mat img = imread(imageFile);
	if (img.empty())
	{
		std::cerr &lt;&lt; &quot;Can't read image from the file: &quot; &lt;&lt; imageFile &lt;&lt; std::endl;
		exit(-1);
	}

	Mat inputBlob = blobFromImage(img, 0.00784, Size(160, 160),Scalar(127.5,127.5,127.5), true, false);   //Convert Mat to batch of images
	//! [Prepare blob]
	inputBlob -= 117.0;
	//! [Set input blob]
	net.setInput(inputBlob, inBlobName);        //set the network input
	//! [Set input blob]

	cv::TickMeter tm;
	tm.start();

	//! [Make forward pass]
	Mat result = net.forward(outBlobName);                          //compute output
	//! [Make forward pass]

	tm.stop();

	if (!resultFile.empty()) {
		CV_Assert(result.isContinuous());

		ofstream fout(resultFile.c_str(), ios::out | ios::binary);
		fout.write((char*)result.data, result.total() * sizeof(float));
		fout.close();
	}

	std::cout &lt;&lt; &quot;Output blob shape &quot; &lt;&lt; result.size[0] &lt;&lt; &quot; x &quot; &lt;&lt; result.size[1] &lt;&lt; &quot; x &quot; &lt;&lt; result.size[2] &lt;&lt; &quot; x &quot; &lt;&lt; result.size[3] &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;Inference time, ms: &quot; &lt;&lt; tm.getTimeMilli() &lt;&lt; std::endl;

	if (!classNamesFile.empty()) {
		std::vector&lt;String&gt; classNames = readClassNames(classNamesFile.c_str());

		int classId;
		double classProb;
		getMaxClass(result, &amp;classId, &amp;classProb);//find the best class

		//! [Print results]
		std::cout &lt;&lt; &quot;Best class: #&quot; &lt;&lt; classId &lt;&lt; &quot; '&quot; &lt;&lt; classNames.at(classId) &lt;&lt; &quot;'&quot; &lt;&lt; std::endl;
		std::cout &lt;&lt; &quot;Probability: &quot; &lt;&lt; classProb * 100 &lt;&lt; &quot;%&quot; &lt;&lt; std::endl;
	}
	return 0;
} //main


/* Find best class for the blob (i. e. class with maximal probability) */
void getMaxClass(const Mat &amp;probBlob, int *classId, double *classProb)
{
	Mat probMat = probBlob.reshape(1, 1); //reshape the blob to 1x1000 matrix
	Point classNumber;

	minMaxLoc(probMat, NULL, classProb, NULL, &amp;classNumber);
	*classId = classNumber.x;
}

std::vector&lt;String&gt; readClassNames(const char *filename)
{
	std::vector&lt;String&gt; classNames;

	std::ifstream fp(filename);
	if (!fp.is_open())
	{
		std::cerr &lt;&lt; &quot;File with classes labels not found: &quot; &lt;&lt; filename &lt;&lt; std::endl;
		exit(-1);
	}

	std::string name;
	while (!fp.eof())
	{
		std::getline(fp, name);
		if (name.length())
			classNames.push_back(name);
	}

	fp.close();
	return classNames;
}"><pre lang="//" class="notranslate"><code class="notranslate">// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

// Copyright (C) 2016, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.

/*
Sample of using OpenCV dnn module with Tensorflow Inception model.
*/

#include &lt;stdafx.h&gt;
using namespace cv;
using namespace cv::dnn;

#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

const String keys =
"{help h    || Sample app for loading Inception TensorFlow model. "
"The model and class names list can be downloaded here: "
"https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip }"
"{model m   |tensorflow_inception_graph.pb| path to TensorFlow .pb model file }"
"{image i   || path to image file }"
"{i_blob    | input | input blob name) }"
"{o_blob    | softmax2 | output blob name) }"
"{c_names c | imagenet_comp_graph_label_strings.txt | path to file with classnames for class id }"
"{result r  || path to save output blob (optional, binary format, NCHW order) }"
;

void getMaxClass(const Mat &amp;probBlob, int *classId, double *classProb);
std::vector&lt;String&gt; readClassNames(const char *filename);

int main(int argc, char **argv)
{
	cv::CommandLineParser parser(argc, argv, keys);

	if (parser.has("help"))
	{
		parser.printMessage();
		return 0;
	}

	String modelFile = "OptimizedGraph.pb";
	String imageFile = "img.JPG";
	String inBlobName = "input";
	String outBlobName = "side";
	if (!parser.check())
	{
		parser.printErrors();
		return 0;
	}

	String classNamesFile = "imagenet_comp_graph_label_strings.txt";
	String resultFile = "output.txt";

	//! [Initialize network]
	dnn::Net net = readNetFromTensorflow(modelFile);
	//! [Initialize network]

	if (net.empty())
	{
		std::cerr &lt;&lt; "Can't load network by using the mode file: " &lt;&lt; std::endl;
		std::cerr &lt;&lt; modelFile &lt;&lt; std::endl;
		exit(-1);
	}

	//! [Prepare blob]
	Mat img = imread(imageFile);
	if (img.empty())
	{
		std::cerr &lt;&lt; "Can't read image from the file: " &lt;&lt; imageFile &lt;&lt; std::endl;
		exit(-1);
	}

	Mat inputBlob = blobFromImage(img, 0.00784, Size(160, 160),Scalar(127.5,127.5,127.5), true, false);   //Convert Mat to batch of images
	//! [Prepare blob]
	inputBlob -= 117.0;
	//! [Set input blob]
	net.setInput(inputBlob, inBlobName);        //set the network input
	//! [Set input blob]

	cv::TickMeter tm;
	tm.start();

	//! [Make forward pass]
	Mat result = net.forward(outBlobName);                          //compute output
	//! [Make forward pass]

	tm.stop();

	if (!resultFile.empty()) {
		CV_Assert(result.isContinuous());

		ofstream fout(resultFile.c_str(), ios::out | ios::binary);
		fout.write((char*)result.data, result.total() * sizeof(float));
		fout.close();
	}

	std::cout &lt;&lt; "Output blob shape " &lt;&lt; result.size[0] &lt;&lt; " x " &lt;&lt; result.size[1] &lt;&lt; " x " &lt;&lt; result.size[2] &lt;&lt; " x " &lt;&lt; result.size[3] &lt;&lt; std::endl;
	std::cout &lt;&lt; "Inference time, ms: " &lt;&lt; tm.getTimeMilli() &lt;&lt; std::endl;

	if (!classNamesFile.empty()) {
		std::vector&lt;String&gt; classNames = readClassNames(classNamesFile.c_str());

		int classId;
		double classProb;
		getMaxClass(result, &amp;classId, &amp;classProb);//find the best class

		//! [Print results]
		std::cout &lt;&lt; "Best class: #" &lt;&lt; classId &lt;&lt; " '" &lt;&lt; classNames.at(classId) &lt;&lt; "'" &lt;&lt; std::endl;
		std::cout &lt;&lt; "Probability: " &lt;&lt; classProb * 100 &lt;&lt; "%" &lt;&lt; std::endl;
	}
	return 0;
} //main


/* Find best class for the blob (i. e. class with maximal probability) */
void getMaxClass(const Mat &amp;probBlob, int *classId, double *classProb)
{
	Mat probMat = probBlob.reshape(1, 1); //reshape the blob to 1x1000 matrix
	Point classNumber;

	minMaxLoc(probMat, NULL, classProb, NULL, &amp;classNumber);
	*classId = classNumber.x;
}

std::vector&lt;String&gt; readClassNames(const char *filename)
{
	std::vector&lt;String&gt; classNames;

	std::ifstream fp(filename);
	if (!fp.is_open())
	{
		std::cerr &lt;&lt; "File with classes labels not found: " &lt;&lt; filename &lt;&lt; std::endl;
		exit(-1);
	}

	std::string name;
	while (!fp.eof())
	{
		std::getline(fp, name);
		if (name.length())
			classNames.push_back(name);
	}

	fp.close();
	return classNames;
}
</code></pre></div>
<p dir="auto">Exact output is as following:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[ INFO:0] Initialize OpenCL runtime...

Net Outputs(1):
side
Output blob shape 1 x 2 x 1090226688 x 427
Inference time, ms: 1178.1
Best class: #1 'class2'
Probability: 765.036% &lt;-------- look at the bad prob score"><pre class="notranslate"><code class="notranslate">[ INFO:0] Initialize OpenCL runtime...

Net Outputs(1):
side
Output blob shape 1 x 2 x 1090226688 x 427
Inference time, ms: 1178.1
Best class: #1 'class2'
Probability: 765.036% &lt;-------- look at the bad prob score
</code></pre></div>
<p dir="auto">My possible thoughts on why this is happening:</p>
<p dir="auto">In TF code, I get logits from tf.slim.fully_connected. I saved this by using<br>
<code class="notranslate">logits = tf.multiply(logits,1,name='output')</code>. Maybe this isn't the right way of saving output node?</p>
<p dir="auto">I'm not at liberty to share the .pb file, so any comments on how to solve this?</p>