OPENCV: Fail to use dnn module in c++ - c++

I'm using dnn module in opencv.
Previously, it work well in python.
But when I turn to C++ version.readNetFromDarknet function report error that I don't know how to fix. Error is below:
Unhandled exception at 0x00007FFF37BE4F69 in untitled.exe: Microsoft C++ exception: std::out_of_range at memory location 0x000000BF2A53F090
My full code is :
#include <QCoreApplication>
#include <opencv2/opencv.hpp>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
string classesFile, modelConfig, modelWeights;
classesFile = "D:/work/rs/model/guoxing_mac9_3535_20220224.names";
modelConfig = "D:/work/rs/model/guoxing_mac9_3535_20220224.cfg";
modelWeights = "D:/work/rs/model/guoxing_mac9_3535_20220224_last.weights";
dnn::Net m_model;
vector<string> m_classes;
ifstream ifs(classesFile.c_str());
string line;
while (getline(ifs, line))
m_classes.push_back(line);
try
{
cout << "trying" << endl;
m_model = dnn::readNetFromDarknet(modelConfig, modelWeights);
}
catch (Exception& e)
{
cout << e.msg << endl;
}
m_model.setPreferableBackend(dnn::DNN_BACKEND_OPENCV);
m_model.setPreferableTarget(dnn::DNN_TARGET_OPENCL);
return a.exec();
}
Test in Opencv3.4.2 and VS2017.
Thanks for all reply!

After update Opencv from 3.4.2 to 3.4.16, it solved. low version dnn module is not work well for some network architecture.

Related

XercesDOMParser scoped instantiation causes segfault

I have just come across a strange behavior of the Xerces-C library which I do not understand. The following code, which has already been seen in loads of examples, works for me:
#include <iostream>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
using namespace std;
using namespace xercesc;
int main (int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize ();
XercesDOMParser* parser = new XercesDOMParser ();
// here one might want to add some useful operations
delete parser;
XMLPlatformUtils::Terminate ();
}
catch (...) {
cout << "caught some exception" << endl;
}
return 0;
}
Surely, this code does not do many meaningful things. But it runs and in particular terminates cleanly.
Now, I was trying to avoid the new/delete and switch to a scoped object, like so:
#include <iostream>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
using namespace std;
using namespace xercesc;
int main (int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize ();
XercesDOMParser parser;
// here one might want to add some useful operations
XMLPlatformUtils::Terminate ();
}
catch (XMLException& exc) {
cout << "caught an XMLException" << endl;
}
catch (...) {
cout << "caught an exception" << endl;
}
return 0;
}
This or similar code has also been seen many times. However, when I run it, it creates a segfault after (?) XMLPlatformUtils::Terminate() (that's at least, what my debugger is suggesting). Still, I have successfully worked with a parser object created in that way. And if I omit the call to Terminate(), I see my process terminate cleanly.
Does anyone have a clue what I am doing wrong here?
Paul's comment already gave the correct answer and his explanation why does make sense. So for completeness, here is the code that actually works (note the explicitly created inner scope):
#include <iostream>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
using namespace std;
using namespace xercesc;
int main (int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize ();
{
XercesDOMParser parser;
// here one might want to add some useful operations
}
XMLPlatformUtils::Terminate ();
}
catch (XMLException& exc) {
cout << "caught an XMLException" << endl;
}
catch (...) {
cout << "caught an exception" << endl;
}
return 0;
}
Thank you Paul!

not able to open a file in write mode in cpp, strerro says permission denied

I am trying to do a simple file operation by opening it and writing something.
Here is the code:
// image_read.cpp : Defines the entry point for the console application.
#include<cerrno>
#include<cstring>
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
ofstream myfile;
myfile.open("C:/file_example.txt");
cout << myfile.is_open();
if (!(myfile.is_open()))
{
cout << "cannot open the file, error number" << strerror(errno);
}
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
getting the error from cout<<strerror as permission denied
How can I resolve this?
Thanks in advance
Please try to set 'UAC Execution Level' = 'requireAdministrator'.
https://learn.microsoft.com/en-us/cpp/build/reference/manifestuac-embeds-uac-information-in-manifest

Encryption of an public rsa key file with Poco fails with "file access error"

I'm trying to encrypt an rsa public key that I have generated using libary poco crypto. The idea came from these answers (C++ Encrypt a text file, allow use of decrypt via ifstream). The pubkey is in the form of
"MIIBIDANBgkqhkiG.........==
I'm using ubuntu 16.04 and poco library version 1.7.8p2 which has been built with OpenSSL 1.0.2g.
The code I use is the following:
#include <iostream>
#include <fstream>
#include "Poco/Crypto/CipherFactory.h"
#include "Poco/Crypto/Cipher.h"
#include "Poco/Crypto/RSADigestEngine.h"
using namespace std;
using namespace Poco::Crypto;
int main(int argc, char** argv)
{
try {
Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey("pubkey.txt"));
}
catch (const exception& exc)
{
cout << exc.what() << endl;
}
}
When I run the above code I get the exception "file access error"
The txt file is given all the permissions to read write and execute.
Afterwards I tried with the istream constructor that RSAKey class provides:
int main(int argc, char** argv)
{
try {
ifstream myfile("pubkey.txt");
Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey(&myfile));
catch (const exception& exc)
{
cout << exc.what() << endl;
}
}
But I got the same error.
It worked when I replaced the above code with the following line:
Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey(RSAKey::KL_1024, RSAKey::EXP_SMALL));
But this is not what I want.
I also print the text file into a string by using
ifstream myfile("pubkey.txt");
string file;
myfile >> file;
file;
and the file is correctly written into the string.
What am I doing wrong in this case?
I have faced the same problem and it turned out to be format error.
A friend of mine saw that the file format was wrong and fixed it and it worked.
Make sure that the file format is right, check every line ending.
Here is a screenshot from VIM for the file. Also, the file extension is .pub or .pem. I'm not sure though if the file extension affect it.

Opening image with ImageMagick via c++

Creating a simple project for an open image with ImageMagick.
Using simple code:
#include "Magick++.h"
#include <iostream>
using namespace std;
using namespace Magick;
int main(int argc, char **argv)
{
const char *c = "test.png";
cout << c << endl;
try {
InitializeMagick(*argv);
Magick::Image img;
img.read(c);
img.write("logo.png");
}
catch (Exception &error_)
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
But while opening any image(img.read(c)) I get an error:
Caught exception: MyProject.exe: unable to open image `≡█I': No such file or directory # error/blob.c/OpenBlob/2695
Work with: ImageMagick-7.0.3-Q8, Windows 7 x64

Exception handling doesn't work with Qt on Windows

I'm facing strange problem. Namely, Qt somehow turns off exception handling in my program. I can't catch any exception, and when I throw an exception application crashes.
I'm using Qt 4.7.0 (32 bit) from Qt SDK v2010.05 on Windows 7 (64 bit), g++ (GCC) 4.5.1 from MinGW, NetBeans 6.9.1.
But I also cheked this with g++ 3.4.5 (also from MinGW) and Qt Creator 2.0.1 - same strange behavior.
For example (simplest case):
#include <Qt/QApplication.h>
#include <iostream>
#include <stdexcept>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
try {
cout << "Before exception" << endl;
throw runtime_error("Exception occured");
cout << "After exception" << endl;
} catch (runtime_error& exc) {
cout << exc.what() << endl;
exit(1);
}
return 0;
}
When I execute above program I've got this output:
Before exception
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
I've tried to add flag "-fexceptions" to g++ but it hasn't changed anything.
When I don't use Qt, everything is OK:
#include <Qt/QApplication.h> // It is not caused only by including Qt header
// so it doesn't matter if I comment this out or not
#include <iostream>
#include <stdexcept>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
// QApplication app(argc, argv);
try {
cout << "Before exception" << endl;
throw runtime_error("Exception occured");
cout << "After exception" << endl;
} catch (runtime_error& exc) {
cout << exc.what() << endl;
exit(1);
}
return 0;
}
The output:
Before exception
Exception occured
Does anybody know why is this happen that way and how to fix this? Has it something to do with type of exception handling method (SJLJ or Dwarf-2) used when Qt was build?
I've reconfigured and recompiled Qt with flag -exceptions:
D:\Qt\2010.05\qt>mingw32-make confclean && configure -exceptions && mingw32-make
and now everything is ok!
Thanks all for help, especially to Nick D!
Anyway, it's very strange that I had Qt build without this flag. I had downloaded Qt SDK in binary form from official site.
It's no longer necessary to use the -exceptions flag with Qt. In Qt Creator 4 it's the default, and my Windows Qt app happily uses vast and extensive exception handling with no problems. Qt MSVC builds use the /EHsc compiler option, which turns normal exception handling on.