How to compile/run a cpp file in mac - c++

I downloaded a webcam_face_pose_ex.cpp file from GitHub and now i want to compile and run it on my mac.
#include <dlib/opencv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <X11/Xlib.h>
using namespace dlib;
using namespace std;
int main()
{
try
{
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
return 1;
}
image_window win;
// Load face detection and pose estimation models.
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pose_model;
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
// Grab and process frames until the main window is closed by the user.
while(!win.is_closed())
{
// Grab a frame
cv::Mat temp;
if (!cap.read(temp))
{
break;
}
// Turn OpenCV's Mat into something dlib can deal with. Note that this just
// wraps the Mat object, it doesn't copy anything. So cimg is only valid as
// long as temp is valid. Also don't do anything to temp that would cause it
// to reallocate the memory which stores the image as that will make cimg
// contain dangling pointers. This basically means you shouldn't modify temp
// while using cimg.
cv_image<bgr_pixel> cimg(temp);
// Detect faces
std::vector<rectangle> faces = detector(cimg);
// Find the pose of each face.
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(cimg, faces[i]));
// Display it all on the screen
win.clear_overlay();
win.set_image(cimg);
win.add_overlay(render_face_detections(shapes));
}
}
catch(serialization_error& e)
{
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch(exception& e)
{
cout << e.what() << endl;
}
}
I tried g++ webcam_face_pose_ex.cpp command but I get:
webcam_face_pose_ex.cpp:30:10: fatal error: 'dlib/opencv.h' file not found
#include <dlib/opencv.h>
^~~~~~~~~~~~~~~
1 error generated.
Was Wondering what I could do to fix this?

The Example File Is Not Meant to be Compiled Using g++
Read the following to learn a bit about the -I flag and #include statements:
The webcam_face_pose_ex.cpp is part of a larger project and you won't be able to compile it on its own because it depends on other files. The #include directive specifies that in order to compile this program, code from the file specified by #includemust be compiled first. This means the entire dlib must be downloaded before compiling webcam_face_pose_ex.cpp. This project also requires opencv2 so we can download it and place the opencv2 folder in the dlib project folder.
Now we can open terminal and change directory into the dlib project folder and compile the file using the following command:
g++ -I. examples/webcam_face_pose_ex.cpp
Note we're specifying the directory of where to find the files specified by #include using the -I parameter as -I. this means to search the current working directory for the files. There it will find the dlib folder and dlib/opencv.h.
How ever, this isn't enough. When you execute the command, you'll encounter an error opencv2/opencv_modules.hpp: No such file or directory.
Solution
The dlib project documentation states that the examples should be built using cmake. Make sure to use cmake to compile the examples.

Related

How to copy files to another directory using c++ filesystem library

I'm attempting to automate some image processing, and once I have processed an image I would like to move it to another directory.
I have tried using std::experimental::filesystem::copy and std::experimental::filesystem::copy_file, but I get an error of "Could not copy file: copy_file(p1, p2, options): invalid arguments: operation not permitted"
#include <filesystem>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char* argv[]) {
if (argc < 2) {
cout << "no directory specified" << endl;
return 0;
}
experimental::filesystem::path inputPath(argv[1]);
experimental::filesystem::path
for (auto const & entry : experimental::filesystem::directory_iterator(inputPath)) {
string src = entry.path().string();
experimental::filesystem::path dest("C:\\Users\\Keelan\\Desktop\\MatchTest\\IMG_Copy\\"+ entry.path().filename().string());
cout << entry.path() << endl;
cout << "Copying " << src << " to " << dest << endl;
experimental::filesystem::copy_file(entry.path(), dest, experimental::filesystem::copy_options::none);
}
return 0;
}
I have so far had no success using the either of these copy functions. I have seen other solutions involving redirecting file-streams but I don't think this is appropriate given I will be processing files on demand and need to minimise response time.
EDIT: the folder I'm copying to exists and all users have read/write permissions
EDIT 2: I deleted and recreated the folder and the problem is fixed, not sure of the reason why
There are 2 potential issues here:
Since you use experimental::filesystem::copy_options::none you will get an exception if the file already exists - see the paragraph "Otherwise, if the destination file already exists" here
If the source folder contains subfolders with files, the directory_iterator will iterate them as well, but if you attempt to copy a file nested deeper, you need to create the target directory hierarchy as well.

Why cv::imread return NULL after include tensorflow's header

I am using the tensorflow c++ API in my code. And I found when I load image from files the image is NULL. Then I write a test code to find the reason.
Here is my test code:
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
//#include "tensorflow/core/public/session.h"
//#include "tensorflow/core/protobuf/meta_graph.pb.h"
int main()
{
Mat imgtry = imread("lena.jpg");
printf("%dx%d", imgtry.cols, imgtry.rows );
return 0;
}
When I comment tensorflow's header, the output value is 255x255, but once I uncomment the header, the output value is 0x0. Why ???
The problem seems change slightly after I revised the sequence of link library. At first, I link the tensorflow_cc and tensorflow_frameworok and then the opencv's libriries. Now, I put the tensorflow's libraries after the opencv and the let corresponding include directory as the same sequence. Then I can read image normally even uncomment the code in the above code area. But new porblem occured.
#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs.hpp"
//it's ok.
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/protobuf/meta_graph.pb.h"
using namespace std;
int main()
{
cv::Mat img;
img = cv::imread("lena.jpg");
if(img.empty() == true) {
cout << "Error!" << endl;
exit(1);
}
cout << "ok!" << endl;
//uncomment this, the img is always emtpy!!!
// tensorflow::SessionOptions sessOptions;
// sessOptions.config.mutable_gpu_options()->set_allow_growth(true);
// auto session = tensorflow::NewSession(sessOptions);
// if(session == nullptr) {
// cout << "Could not create Tensorflow session." << endl;
// exit(1);
// }
return 0;
}
It's a known bug
Feel free to update this Community Wiki answer with Workaround / Solution / Bug status / Etc...

Trying to compile OpenCV program but returns "cannot find -lippicv" (Complete linux beginner)

So I have OpenCV 3.1.0 set up on my computer running on Ubuntu 16.04 and I'm trying to run some very simple code to try and test OpenCV and whilst it recognises the opencv #include files, I continue to have compiler errors.
Here is my C++ code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(int argc, char** argv) {
Mat color = imread("lena.jpg");
Mat gray = imread("lena.jpg",0);
imwrite("lenaGray.jpg", gray);
int myRow = color.cols-1;
int myCol = color.rows-1;
Vec3b pixel = color.at<Vec3b>(myRow, myCol);
cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," << (int)pixel[1] << "," << (int)pixel[2] << ")" << endl;
imshow("Lena BGR", color);
imshow("Lena Gray", gray);
waitKey(0);
return 0;
}
And I attempt to compile it on the Linux terminal as such:
g++ `pkg-config opencv --cflags --libs` main.cpp -o main
And this is the error that's returned:
/usr/bin/ld: cannot find -lippicv
collect2: error: ld returned 1 exit status
As the title said, I am a complete beginner at the Linux operating system whilst I am relatively competent at using the OpenCV library. I simply want to be able to use the OpenCV library and I am unable to with this error and I am very frustrated with it.
Thanks in advance for any help!

Raspberry Pi CGI script with OpenCV not saving images

I'm trying to create a CGI script that will take a picture and save it to the location I give it. I'm using the Raspberry Pi and the Pi camera module with the uv4l driver. I have also chosen to use Apache2.
Currently the script runs with no errors given and no errors in the Apache error log, but the image doesn't get saved. The camera does open because the red light appears on it. I also check to see if the image is empty which it isn't.
So far I have tried changing folder permissions so that the user pi owns everything. I have also tried to save over an already existing file but it never gets updated. I have never used Apache2 or CGI scripting before so I feel that the problem lies in there but I'm not entirely sure what to search because I am getting no errors. Any suggestions would be greatly appreciated.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv){
cv::VideoCapture cap(-1);
if (!cap.isOpened()){
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>";
cout << "<h1> Camera didn't open </h1>";
cout << "</html>";
return -1;
}
//cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
//cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
int count = 40;
cv::Mat frame;
bool bSuccess = cap.read(frame);
while (count != 0){
count--;
}
if (!bSuccess){
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>";
cout << "<h1> Photo did't work get read in</h1>";
cout << "</html>";
return 0;
}
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>";
cout << "<h1> Photo Taken + Saved</h1>";
cout << "</html>";
cv::imwrite("/var/www/photos/Current.png", frame);
return 0;
}
I'm using this command to compile:
g++ -lopencv_core -lopencv_highgui -L/usr/lib/uv4l/uv4lext/armv6l -luv4lext -Wl,- rpath,'/usr/lib/uv4l/uv4lext/armv6l' time.cpp -o test_script.cgi
I fixed my own problem. The method imwrite() was saving over an already existing image without write permissions.

OpenCV Eclipse CDT

Hello guys I am developing a simple openCV application with eclipse CDT;
Here is my code
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat M(2,2, CV_8UC3, Scalar(0,0,255));
cout << "M = " << endl << " " << M << endl << endl;
return 0;
}
I have built the project and when I try to run, I am getting this error
The program file specified in the launch configuration does not exist
D:\AndroidKeyStore\ExOpen\Debug\ExOpen.exe not found
Assuming the compilation didn't fail, the executable you're building appears to be different from the one you're trying to run in your configuration.
I'm not familiar with Eclipse CDT but you should
go to the Debug Configurations window for your project
select your launch configuration
change the exe you pasted above to the one your project is configured to build (you can find it in the project properties->C/C++ Build->Build directory)