How do you access runfiles with Bazel 5.2.0 in c++ - bazel

Im trying to access runfiles within c++. Im using Bazel 5.2.0. I tried to access like this:
std::string error;
std::unique_ptr<Runfiles> runfiles(Runfiles::Create(argv[0], &error));
if (!runfiles) {
std::cerr << error << std::endl;
return 1;
}
std::string path = runfiles->Rlocation("Test/Example.tx");
std::cout << "Example.tx: " << path << std::endl;
std::ifstream in(path);
if (!in.is_open())
{
std::cout << "Example.tx not found" << std::endl;
return -1;
}
(Example.tx is right, just to lazy to change)
The program is finding a path but the path starts from the bazelisk directory and doesn't point to the binary dir.
Example.tx: C:\users\nikla\_bazel_nikla\d47dtf2d\execroot\__main__\bazel-out\x64_windows-fastbuild\bin\Test\Test.exe.runfiles/Test/Example.tx
Example.tx not found
Im getting this as an result.
Maybe there is a new way to access runfiles but Im not finding it.

The answer is you have to include the workspace name in the path, if it isn't set you have to use __main__.
Closed.

Related

What are the rules for file references in Jetbrains IDEs?

I am attempting to automate the creation of reference links within a Jetbrains IDE's output for a logging utility, but cannot find proper documentation of the types of links that are supported. While this will automatically work for references in the form of FILENAME:LINE_NUMBER, I'm wondering if other reference types are permitted (e.g. FILENAME#METHOD).
When working within a Jetbrains IDE, it will automatically resolve string outputs to files within the project or local filesystem. For example (with C++):
#include <iostream>
int main() {
std::cout << __FILE__ << ':' << __LINE__ << std::endl;
return 0;
}
The above code will generate a reference link when ran within the IDE:
This works from relative paths as well (e.g. the source root or project root):
int main() {
std::cout << __FILE__ << ':' << __LINE__ << std::endl; //LINKS
std::cout << "main.cpp" << ':' << __LINE__ << std::endl; //LINKS
std::cout << "src/main.cpp" << ':' << __LINE__ << std::endl; //LINKS
std::cout << __FILE__ << std::endl; //NO LINK
std::cout << __FILE__ << '#' << "main" << std::endl; //NO LINK
std::cout << __FILE__ << "::" << "main" << std::endl; //NO LINK
std::cout << __FILE__ << ':' << "main" << std::endl; //NO LINK
return 0;
}
I've found some reference to Copy Path/Reference within Jetbrain's documentation, which seems to offer insight into a way to generate a reference link from a line of code, but copying a reference to a function foo simply generates the string "foo" as the reference string. According to a comment on a YouTrack issue, the "Copy Path/Reference" option will work for a reference link only when not pointing to a symbol/reference/declaration (i.e. a blank line), which generates a link such as src/main.cpp:2. The line-number style references also work within the search tools (double-shift). For instance, searching main.cpp:2 will take you to the second line of the main.cpp file.
In short, what are the actual parsing rules surrounding these links? Is FILE_LOCATION:LINE_NUMBER the only permittable reference?

opencv read image “Premature end of JPEG file”

I was using OpenCV to read the images from a folder. A lot of messages like this show up:
Corrupt JPEG data: premature end of data segment
Premature end of JPEG file
Premature end of JPEG file
Premature end of JPEG file
How to catch this exception and remove these image files?
Since you said you are reading 'images' (multiple images), you would be looping through files in the folder that you are reading them from.
In that case, if you check if the image is valid or not by using the following :
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
you can then proceed to deleting files which are corrupt/bad.
I've been struggling to find a solution too. Read tens of articles, most of which just state that openCV does not throw errors and only outputs the error on stderr.
Some suggest to use PIL, but that does not detect most of the image corruptions. Usually only premature end of file.
However the same errors that OpenCV warns about can be detected via imagemagick.
Install imagemagick (https://imagemagick.org/)
Make sure you have it in the path.
Put the following sub into your code and call it to verify a file from wherever you need to. It also outputs errors to stderr, however it raises an error (thanks to "-regard-warnings")
import subprocess
def checkFile(imageFile):
try:
subprocess.run(["identify", "-regard-warnings", imageFile]).check_returncode()
return true
except (subprocess.CalledProcessError) as e:
return false
If you don't want the check to spam your outputs, add stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL params to the run function call.
On windows if you have not installed the legacy commands use the new syntax:
subprocess.run(["magick", "identify", "-regard-warnings", imageFile]).check_returncode()

writing a video of depth images using opencv

I am trying to write a video with some sequence of depth map.
I have converted the depth image from cv::Mat_ to cv::Mat with single channel.
But no codec I use is able to open the avi file I want to write. The VideoCapture.open(...) doesn't seem to be able to either create the file or open it.
I think its the problem choosing the right codec. I might be wrong. I have posted a small code snippet.
cv::VideoWriter outputVideo_;
source_ = "~/Hello.avi";
cv::Size S(480, 640);
outputVideo_.open(source_, CV_FOURCC('D','I','B', ' '), 20, S, false);
if (!outputVideo_.isOpened())
{
std::cout << "Could not open the output video for write: " << source_ << std::endl;
return;
}
How do I get opencv to work correctly in this case. I am using linux 12.04, ROS (Robot Operating System) and OpenCV 2.4.2
Try to use open() function with only file name. because here with me it works well.
VideoWriter outputVideo_;
source_ = "~/Hello.avi";
// cv::Size S(480, 640);
outputVideo_.open(source_);
if (!outputVideo_.isOpened())
{
std::cout << "Could not open the output video for write: " << source_ << std::endl;
return;

OpenCV exception while reading a yaml file compressed with gz using filestorage

The output file when I save some variable using FileStorage is a yaml file of 9MB. I saw we can just add ".gz" to file name in order to compress the file. The resulting file is 3.5MB. That is ok. But when I try to open the myfile.yaml.gz file to APPEND using filestorage i am having an unhandled exception. Do you know if OpenCV supports this? If yes, how should I read the file? If not, any suggestion to compress/decompress the file easily?
Here is a sample code:
std::vector<int> myArray;
myArray.push_back(0); myArray.push_back(1);
FileStorage fs("myFile.yaml.gz", FileStorage::WRITE);
fs << "myArrayName" << myArray;
fs.release();
myArray.clear();
fs = FileStorage("myFile.yaml.gz", FileStorage::APPEND);
fs << "intValue" << 3;
fs.release();
fs = FileStorage("myFile.yaml.gz", FileStorage::READ);
fs["myArrayName"] >> myArray;
fs.release();

Using mingw with opencv -- init error

I use Eclipse CDT for developing C with mingw. I also add opencv libary. Everything compiled without problems. But if I start the compiled application (using a opencv-function) there is an init error. If I only include the .h-files without using a function it works.
The code:
#include <opencv2/opencv.hpp>
using namespace std;
int main() {
cout << "!!!Streaming!!!" << endl; // prints !!!Streaming!!!
// Nothing but create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);
cvWaitKey(0);
return 0;
}
Error-Image: http://i.stack.imgur.com/zdmT7.png
If I do not use a cv.. - function there will be no init error. Even if I include opencv2/opencv.hpp
I do not have an idea how it works.
Hope you can help.
I found the solution. The opencv-dll-files for mingw are damaged. I rename the visualstudio dlls to the names of the mingw-dlls and put it directly to in the exe folder and it works.

Resources