Im working with some legacy code that I need to use, as well as do some work in Python. I am able to read from file in Python but not in C++. Here is the C++ code
int main(int argc, char** argv) {
CvCapture *capture = NULL;
capture = cvCaptureFromFile("path/to/foo.mov");
if (!capture) {
std::cerr << "Cannot open " << "path/to/foo.mov" << std::endl;
return 1;
}
return 0;
}
Related
I am creating a background music player and I wanted to use the MPV C Plugin to do so, but my problem arrives when I disable displaying the video (with check_error(mpv_set_option_string(ctx, "vid", "no"));, this does the job of disabling the video, but then I can't use keys (like q (quit) or > (skip)) anymore... How do I allow them to be used in the terminal without the video GUI?
My Code:
#include <iostream>
#include <mpv/client.h>
static inline void check_error(int status)
{
if (status < 0)
{
std::cout << "mpv API error: " << mpv_error_string(status) << std::endl;
exit(1);
}
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cout << "pass a single media file as argument" << std::endl;
return 1;
}
mpv_handle *ctx = mpv_create();
if (!ctx)
{
std::cout << "failed creating context" << std::endl;
return 1;
}
check_error(mpv_set_option_string(ctx, "input-default-bindings", "yes"));
mpv_set_option_string(ctx, "input-vo-keyboard", "yes");
int val = 1;
check_error(mpv_set_option(ctx, "osc", MPV_FORMAT_FLAG, &val));
check_error(mpv_initialize(ctx));
const char *cmd[] = {"loadfile", argv[1], NULL};
check_error(mpv_command(ctx, cmd));
// THIS IS WHAT I USE TO DISABLE THE VIDEO
// check_error(mpv_set_option_string(ctx, "vid", "no"));
// Let it play, and wait until the user quits.
while (1)
{
mpv_event *event = mpv_wait_event(ctx, 10000);
std::cout << "event: " << mpv_event_name(event->event_id) << std::endl;
if (event->event_id == MPV_EVENT_SHUTDOWN)
break;
}
mpv_terminate_destroy(ctx);
return 0;
}
As you can see with mpv_set_option_string(ctx, "input-default-bindings", "yes") I allow it to use keybinding, but how do I make the keybinding works with just the terminal, since it only works when the GUI is visible? If you ran: mpv path/to/video.mp3 --no-video then the key bindings would still work fine, even without the video GUI.
I wrote a simple QrCode detection and decode code with OpenCV.
But the problem I'm facing is that the QR code gets detected but can't be decoded with the following image (see bottom).
The code I wrote looks like this:
int main(int argc, char* argv[])
{
cv::Mat src = imread("scaled.png");
if(src.empty())
{
cout << "can not open " << "Picture" << endl;
return -1;
}
QRCodeDetector qrDecoder = QRCodeDetector();
std::string data;
data = qrDecoder.detectAndDecode(src);
if(data.length()>0)
{
cout << "data: " << data; //data should be STOP
}
return 0;
}
Does somebody know why the QR code can be detected but not decoded ?
Here the image I used:
Edit:
I've searched a little more about QR code detection with OpenCv and found these to code snippets from: https://docs.opencv.org/3.4.9/de/dc3/classcv_1_1QRCodeDetector.html
setEpsX(double epsX)
setEpsY(double epsY)
unfortunately the documentation is very bad so those somebody know what these 2 parameters are and if they can fix my problem ?
I think I found the problem:
The image I use has a Size of 2400x1600 which is to big to decode. Therefore I resized the image before I decode the image so my code looks like this:
int main(int argc, char* argv[])
{
cv::Mat src = imread("scaled.png");
if(src.empty())
{
cout << "can not open " << "Picture" << endl;
return -1;
}
std::string data;
cv::resize(src,src, cv::Size(1600,1200));
QRCodeDetector qrDecoder = QRCodeDetector();
data = qrDecoder.detectAndDecode(src);
if(data.length()>0)
{
cout << "data: " << data; //data should be STOP
}
return 0;
}
Questions:
1) Where do files go that are created by a C++ Google unit test?
2) Is there a way to write a persistent data file in a C++ Google unit test, such that the file is accessible after the test runs?
Code and desired behavior
I'm running the unit test on Ubuntu 14.04 with catkin_make. I would like the code to write a file somewhere that I can find it after the test runs. The following code writes a file, but I don't know where it goes, or if it persists after the unit tests complete.
TEST(GUnitTestFileIo, Test_One)
{
std::ofstream csvFile;
csvFile.open("helloWorldTestFile.csv");
if (csvFile.is_open()) {
csvFile << "Hello, World, !" << std::endl;
csvFile.close();
} else {
std::cout << "Failed to open the file!" << std::endl;
}
}
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
One solution is to simply write to an absolute file path. The following code writes a file to the user's home directory from inside of a google unit test:
TEST(GUnitTestFileIo, Test_One)
{
char const* tmp = getenv( "HOME" );
if ( tmp == NULL ) {
std::cout << "$(HOME) environment variable is not defined!";
} else {
std::string home( tmp ); // string for the home directory
std::ofstream csvFile; // write the file
csvFile.open(home + "/helloWorldTestFile.csv");
if (csvFile.is_open()) {
csvFile << "Hello, World, !" << std::endl;
csvFile.close();
} else {
std::cout << "Failed to open the file!" << std::endl;
}
}
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
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
I built OpenCV 3.0.0 for VS 2015 in Windows 10 Pro x64.
Then I compiled the following code:
#include "opencv2\opencv.hpp"
#include <iostream>
int main(int argc, char** argv){
cv::VideoCapture capture("Vid_A_ball.avi");
if (! capture.isOpened()){
std::cout << "Video Not Opened" << std::endl;
return -1;
}
cv::Mat frameReference;
while (true){
capture >> frameReference;
if (frameReference.empty()){
std::cout << "Capture Finished" << std::endl;
break;
} else {
cv::imshow("video", frameReference);
cv::waitKey(10);
}
}
return 0;
}
Video opened correctly, but frameReference.empty() is always true.
TIA,
horothesun.