Software Archive
Read-only legacy content
17061 Discussions

OpenCV and Galileo Gen1

Anonymous
Not applicable
2,443 Views

I have read many questions over the last few months but I cannot find a tutorial for what I need and I am pretty lost to be honest. Can anyone help out in guiding me to complete my goal for one of my systems. 

I have a system built on an Intel Galileo Gen 1 and I want to be able to provide a feature that from the GUI I have built for the system, users can view a stream from the webcam attached to the board to see realtime what is going on in the location surrounding the unit. I have searched and searched, seen all of the videos on google and still can't find a kind of step by step from installing opencv to getting it running so I can connect to the webcam that is attached to the board.

If anyone can help will be appreciated.

0 Kudos
1 Solution
Anonymous
Not applicable
2,431 Views

Hi matthias-hahn (Intel) / Stewart Christie (Intel) 

BOOM :D Ok got it taking a photo. 

#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(-1);
//check if the file was opened properly
if(!cap.isOpened())
{
cout << "Webcam could not be opened succesfully" << endl;
exit(-1);
}
else
{
cout << "p n" << endl;
}
int w = 960;
int h = 544;
cap.set(CV_CAP_PROP_FRAME_WIDTH, w);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, h);
Mat frame;
cap >>frame;
imwrite("opencv.jpg", frame);
cap.release();
return 0;
}

 Stewart Christie (Intel) Thank you the ebook got me here. So my next stage is to make the node js server trigger this file and save an image that can be accessed via the node server is this possible ? Once I have that sorted I will look into the video streaming but this is definitely a move forward thanks for help so far :D

 

 

View solution in original post

0 Kudos
31 Replies
samontab
Valued Contributor II
1,829 Views

I wrote a very detailed tutorial for installing OpenCV for Ubuntu 14.04. You may be able to use it as a reference to install it on Galileo:

http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/

0 Kudos
Anonymous
Not applicable
1,829 Views

Hi thanks for the reply am I wrong in thinking that it is already part of the IoT dev kit image ? Do you have any info for getting the dev kit installation working and running a script that will allow me to stream the video to the GUI ?

0 Kudos
Matthias_H_Intel
Employee
1,829 Views

AdamMiltonBarker wrote:

Hi thanks for the reply am I wrong in thinking that it is already part of the IoT dev kit image ? Do you have any info for getting the dev kit installation working and running a script that will allow me to stream the video to the GUI ?

No, you are right: if you use the Intel(R) IoT devkit SD card image (eglibc based) OpenCV will be part of. And you should find quite a number of examples online - e.g. http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html.

I'd suggest starting with

  • capture a frame and write to a file - if that works you know you can access the camera fine
0 Kudos
Anonymous
Not applicable
1,829 Views

Hi thanks for the post I have been going for months trying to find anything that works. The closest I have got is:#

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    VideoCapture cap(0); // open the video camera no. 0

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    } else {
        cout << "OPened Cam" << endl;
    }

   double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
   double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    cout << "Frame size : " << dWidth << " x " << dHeight << endl;

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
    //VideoWriter video("capture.avi",CV_FOURCC('M', 'J', 'P', 'G'), 10, cvSize((int)dWidth,(int)dHeight) );

    cout << "GOT HERE" << endl;
    while (1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break;
       }
    }
    return 0;

}

Which returns:

root@quark00a8ee:~# chmod 755 /tmp/TASS;/tmp/TASS ;exit
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
OPened Cam
Frame size : 640 x 480

(MyVideo:353): Gtk-WARNING **: cannot open display: 
logout

I am using Intel Gen 1, IoT dev kit and Logitech HD720p using Eclipse IoT IDE

0 Kudos
Matthias_H_Intel
Employee
1,829 Views

some hints:

  • find out (e.g. debugger) where "VIDIOC_QUERYMENU: Invalid argument" is print out if this message is relevant
  • apparently your camera got openened fine (I suppose 640x480 resolution is fine? possibly you'd have to change the resolution via V4L to allow higher fps on Galileo)
  • I'd recommend to start writing frames to files rather than to use imshow 

 Once you get correct frames start checking your X setup for your attached display. You would at least need to run something like X11 or Wayland - possibly also a display manager. Apparently it's not yet correctly set ("cannot open display").

0 Kudos
Matthias_H_Intel
Employee
1,829 Views

just saw you tried VideoWriter - did that work?

0 Kudos
Anonymous
Not applicable
1,829 Views

Sorry you totally lost me with most of what you said above. I mentioned I have been banging my head with this for a while and I cannot get my head around what is going wrong. 

VideoWriter gives 

VIDIOC_DQBUF: No such device

(MyVideo:380): Gtk-WARNING **: cannot open display: 

So I guess not, how come there is no tutorial for this yet, pretty much every topic I have found on it no one ever provides an answer and its always the same errors. Totally stumped on how to get it working.

Debug just says:


root@quark00a8ee:~# chmod 755 /tmp/TASS;gdbserver :2345 /tmp/TASS;exit
Process /tmp/TASS created; pid = 397
Listening on port 2345
Remote debugging from host PC IP

0 Kudos
Anonymous
Not applicable
1,829 Views

 

#include <vector>
#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

void createAlphaMat(Mat &mat)
{
    for (int i = 0; i < mat.rows; ++i) {
        for (int j = 0; j < mat.cols; ++j) {
            Vec4b& rgba = mat.at<Vec4b>(i, j);
            rgba[0] = UCHAR_MAX;
            rgba[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX);
            rgba[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX);
            rgba[3] = saturate_cast<uchar>(0.5 * (rgba[1] + rgba[2]));
        }
    }
}

int main(int argv, char **argc)
{
    // Create mat with alpha channel
    Mat mat(480, 640, CV_8UC4);
    createAlphaMat(mat);

    vector<int> compression_params;
    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
    compression_params.push_back(9);

        imwrite("alpha.png", mat, compression_params);

    fprintf(stdout, "Saved PNG file with alpha data.\n");
    return 0;
}

Well this worked kind of.

alpha.png

0 Kudos
Anonymous
Not applicable
1,829 Views

So I have managed to write an image but it is blank :S

The camera light is coming on until script ends so making progress, any ideas to push this one forward anyone ?

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int, char**)
{
	VideoCapture cap(-1);
    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    } else {
        cout << "Opened Cam" << endl;
    }
	Mat save_img; cap >> save_img;
	if(save_img.empty())
	{
		cout << "Something is wrong with the webcam, could not get frame." << endl;
	} else {
		cout << "Writing Image." << endl;
		imwrite("test.jpg", save_img); // A JPG FILE IS BEING SAVED
		cout << "Written Image test.jpg" << endl;
	}
	return 0;
}


root@quark00a8ee:~# chmod 755 /tmp/TASS;/tmp/TASS ;exit
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
Opened Cam
select timeout
Writing Image.
Written Image test.jpg
logout

test.jpg

0 Kudos
Anonymous
Not applicable
1,829 Views

OK last update need to sleep lol.

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int, char**){
	VideoCapture cap(-1);
    if (!cap.isOpened()){
        cout << "Cannot open the video cam" << endl;
        return -1;
    } else {
        cout << "Opened Cam" << endl;
    }
	Mat frame; cap >> frame;
	bool bSuccess = cap.read(frame);
	if (!bSuccess){
		cout << "Cannot read a frame from video stream" << endl;
		return -1;
	} else {
		cout << "Read a frame from video stream" << endl;
	}
	cout << "Writing Image" << endl;
	imwrite("bleh.png", frame);
	cout << "Image Written " << endl;
	return 0;
}

Gives me:


root@quark00a8ee:~# chmod 755 /tmp/TASS;/tmp/TASS ;exit
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
Opened Cam
select timeout
select timeout
Read a frame from video stream
Writing Image
Image Written 
logout

And black image still

0 Kudos
Matthias_H_Intel
Employee
1,829 Views

AdamMiltonBarker wrote:

So I have managed to write an image but it is blank :S

The camera light is coming on until script ends so making progress, any ideas to push this one forward anyone ?

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int, char**)
{
	VideoCapture cap(-1);
    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    } else {
        cout << "Opened Cam" << endl;
    }
	Mat save_img; cap >> save_img;
	if(save_img.empty())
	{
		cout << "Something is wrong with the webcam, could not get frame." << endl;
	} else {
		cout << "Writing Image." << endl;
		imwrite("test.jpg", save_img); // A JPG FILE IS BEING SAVED
		cout << "Written Image test.jpg" << endl;
	}
	return 0;
}

root@quark00a8ee:~# chmod 755 /tmp/TASS;/tmp/TASS ;exit
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
Opened Cam
select timeout
Writing Image.
Written Image test.jpg
logout

false

code looks ok - maybe try specifying the camera in VideoCapture (e.g. set to '0' = default, or to '1', ..., '99'). Maybe you're on a "/dev/zero" kind of camera? 

You could write a quick OpenCV program to scan through the cameras to get a list of existing ones

0 Kudos
Matthias_H_Intel
Employee
1,829 Views

at this stage you could also work on e.g. a Windows, or a Linux host (I am pretty sure OSX would work the same but I never tried OpenCV there). and compare results with what you get on Galileo

0 Kudos
Stewart_C_Intel
Employee
1,829 Views

There's a great book published by Apress, and the ebook is free http://www.apress.com/9781430268390 and it has gen 1 and gen 2 OpenCV examples for both C++ and Python, even includes the different frame rates. Example code and a tutorial is difficult to beat.

The Kindle version is free too.  

 

0 Kudos
Anonymous
Not applicable
1,829 Views

Hi thanks I have downloaded it will check it out, have you any idea why the image is black ?

0 Kudos
Anonymous
Not applicable
2,432 Views

Hi matthias-hahn (Intel) / Stewart Christie (Intel) 

BOOM :D Ok got it taking a photo. 

#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(-1);
//check if the file was opened properly
if(!cap.isOpened())
{
cout << "Webcam could not be opened succesfully" << endl;
exit(-1);
}
else
{
cout << "p n" << endl;
}
int w = 960;
int h = 544;
cap.set(CV_CAP_PROP_FRAME_WIDTH, w);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, h);
Mat frame;
cap >>frame;
imwrite("opencv.jpg", frame);
cap.release();
return 0;
}

 Stewart Christie (Intel) Thank you the ebook got me here. So my next stage is to make the node js server trigger this file and save an image that can be accessed via the node server is this possible ? Once I have that sorted I will look into the video streaming but this is definitely a move forward thanks for help so far :D

 

 

0 Kudos
Anonymous
Not applicable
1,829 Views

Now that is sorted I think I will leave the c++ for when  I integrate my AI, looking at https://github.com/peterbraden/node-opencv now as I think it will be better suited for what I want to do. For anyone else that was worried about the following errors:

VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument

I found that these are actually standard and not an error that effects the code, I am not 100% sure but I think the reason the image was black for me was because I was creating a PNG, looking at my previous code and the functioning code it is pretty much all I can see different so maybe this was the reason.

Also Stewart Christie (Intel) thanks again for the eBook its pretty cool and will check it out in more detail soon.

0 Kudos
Matthias_H_Intel
Employee
1,829 Views

good you were able to proceed - btw: I already asked you earlier whether the image dimensions are correct ;-) "I suppose 640x480 resolution is fine"

0 Kudos
Anonymous
Not applicable
1,829 Views

;) 

Could you give me some feedback on my project ? This is the project link on my website, I have to update some info and the videos but you can see the GUI I have built etc. If you like would be great if you could use the social buttons on the page :D

https://www.techbubble.info/internet-of-things/tass-techbubble-assisted-security-system

0 Kudos
Anonymous
Not applicable
1,829 Views

Well issues with node-opencv now lol won't install :S What ways are there to have to the node server trigger the c++ function on demand, the effect I am trying to get is:

if motion, light or sound detected > trigger the camera recording > send file name with the request that updates the database and then at a later date the user can view those images. The only other thing I can think of at the moment is just have it permanently running in a loop but this will fill up the storage in no time. Any suggestions.

0 Kudos
Matthias_H_Intel
Employee
1,694 Views

AdamMiltonBarker wrote:

Well issues with node-opencv now lol won't install :S What ways are there to have to the node server trigger the c++ function on demand, the effect I am trying to get is:

what are the issues with "install"?

AdamMiltonBarker wrote:

if motion, light or sound detected > trigger the camera recording > send file name with the request that updates the database and then at a later date the user can view those images. The only other thing I can think of at the moment is just have it permanently running in a loop but this will fill up the storage in no time. Any suggestions.

 

circular buffer?

0 Kudos
Reply