GTK+ moving window based on face detection coordinates - c++

I have a program here that performs face detection and I would like to use these coordinates to move a window created with GTK+ 3.22 using GTK's
gtk_window_move function. I would like the window to remain open the entire time while it is moving similar to OpenCV's moveWindow function.
I just downloaded the GTK+ packages yesterday so I am not all too familiar.
The program will perform a loop 100 times, tracking a face the entire time. Currently, the face tracking works, but the window does not appear until the loop is complete. Why is this? I believe the gtk_move_window function is working, but the window does not stay open. I have tried reopening the window each time in the loop, or just opening once before the loop. If you are familiar with OpenCV's moveWindow function that is exactly what I am looking for. Here is the sample code.
By the way, if you know how a GTK+ function that will bring the window to the very top layer on top of all the other windows open when called, that is helpful information for me as well.
#include "FlyCapture2.h"
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaobjdetect.hpp>
#include <math.h>
#include <thread>
#include <iostream>
#include <vector>
#include <gtk-3.0/gtk/gtk.h>
using namespace FlyCapture2;
cv::Ptr<cv::cuda::CascadeClassifier> face_detect;
void detect_faces(cv::Mat img, cv::cuda::GpuMat buf,GtkWidget *win)
{
std::vector<cv::Rect>faces;
cv::cuda::GpuMat image_gpu(img);
//Face detection here
...
if (faces.size() > 0)
{
float x = faces[0].x;
float y = faces[0].y;
int new_x = roundf(x*40/51);
int new_y = roundf(y*135/256);
gtk_window_move(GTK_WINDOW (win),new_x,new_y);
gtk_widget_show (win); //Should this go here?
std::cout<<faces[0]<<std::endl;
}
}
int main( int argc, char *argv[])
{
//Camera connect here
...
//face detect variables
face_detect = cv::cuda::CascadeClassifier::create("/home/nvidia/opencv/data/haarcascades_cuda/haarcascade_frontalface_default.xml");
cv::cuda::GpuMat objbuf;
//GTK+ Params
GtkWidget *window;
GdkRGBA *color;
gtk_init (&argc, &argv);
gdk_rgba_parse(color,"(0,0,0)");
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_decorated(GTK_WINDOW (window),FALSE);
gtk_window_set_position(GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_widget_override_background_color(window, GTK_STATE_FLAG_NORMAL, color);
gtk_widget_show (win); //Should this go here?
// capture loop
for (int i=0;i<100;i++)
{
// Get the image
Image rawImage;
camera.RetrieveBuffer( &rawImage );
// convert to rgb
Image rgbImage;
rawImage.Convert( FlyCapture2::PIXEL_FORMAT_MONO8, &rgbImage );
// convert to OpenCV Mat
unsigned int rowBytes = (double)rgbImage.GetReceivedDataSize()/(double)rgbImage.GetRows();
cv::Mat image = cv::Mat(rgbImage.GetRows(), rgbImage.GetCols(), CV_8UC1, rgbImage.GetData(),rowBytes);
//Detect Faces
detect_faces(image,objbuf,window);
}
//Disconnect Camera
camera.StopCapture();
camera.Disconnect();
gtk_main();
return 0;
}

The code in your capture loop should be in an event handler callback.
You first need to call g_timeout_add or g_idle_add to register your callback.
The callback you registered is a GSourceFunc that will be called after gtk_main is run. The return value (G_SOURCE_CONTINUE or G_SOURCE_REMOVE) controls if you want to have the callback be called again.

Related

Problems implementing Intel Realsense and SDL2

as title, I'm having problems trying to make SDL2 display frames from realsesense pipeline. I have no clue how to extract data from the frameset and process it in order to make it works with SDL2. I'm programming in C++. I've read all the documentation on Intel SDK website and tried a lot of different approches, nothing worked so far.
Here's some code I've wrote on a new project:
int main(int argc, char * argv[])
{
window window(1280, 720, "Tecnos Depth Cam Demo");
rs2::colorizer color_map;
rs2::pipeline pipe;
pipe.start();
while (true)
{
rs2::frameset data = pipe.wait_for_frames().apply_filter(color_map);
// Process data to work with SDL2 renderer (my problem)
}
}
At the end I managed to do it. Simply create a texture with SDL_CreateTexture and update it with the pixels data from get_data() function of Realsense library each frame. The tricky part is that you have to define a pixel format with:
SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_PACKED8, SDL_PACKEDORDER_XBGR, SDL_PACKEDLAYOUT_8888, BITS_PER_PIXEL, BYTES_PER_PIXEL)
Here a little code demo that I wrote on porpuse to show you:
#include <iostream>
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <librealsense2/rs.hpp>
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
#define BITS_PER_PIXEL 24
#define BYTES_PER_PIXEL 3
int main(int argc, char* argv[])
{
// Init
SDL_Init(SDL_INIT_EVERYTHING);
// Declarative section
SDL_Window* window = SDL_CreateWindow("Tutorial", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
SDL_Texture* textureCamera = SDL_CreateTexture(renderer, SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_PACKED8, SDL_PACKEDORDER_XBGR, SDL_PACKEDLAYOUT_8888, BITS_PER_PIXEL, BYTES_PER_PIXEL), SDL_TEXTUREACCESS_TARGET, WINDOW_WIDTH, WINDOW_HEIGHT);
rs2::pipeline camera;
rs2::frameset frames;
// Executive section
camera.start();
// Loop
while (true)
{
// Do whatever you like there
SDL_RenderClear(renderer);
// ...
frames = camera.wait_for_frames();
Uint8* pixels = (Uint8*)(frames.get_color_frame().get_data());
SDL_UpdateTexture(textureCamera, NULL, pixels, frames.get_color_frame().get_bytes_per_pixel() * WINDOW_WIDTH);
SDL_RenderCopy(renderer, textureCamera, NULL, NULL);
SDL_RenderPresent(renderer);
}
return EXIT_SUCCESS;
}
I hope I helped someone in trouble like me. For any question just ask me, I'll be more than happy to help! Lastly thanks to the stackoverflow community to always help even when it doesn't!

Camera Interfacing with Opencv:

I have Windows 8 on "DELL Inspiron 5537" installed and i create a virtual machine from "Virtual box" and installed Ubuntu 13.04 in it. And then I installed Opencv Version=2.4.9 through commands in Ubuntu's terminal now I want to do camera interfacing in opencv which is very necessary for my project to move forward..my code contains no error still it is not working the way i want.
My code is given below:
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
char key;
int main()
{
cvNamedWindow("Camera_Output", 1); //Create window
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
//Capture using any camera connected to your system
while(1){ //Create infinte loop for live streaming
IplImage* frame = cvQueryFrame(capture); //Create image
frames from capture
cvShowImage("Camera_Output", frame); //Show image
frames on created window
key = cvWaitKey(10); //Capture Keyboard stroke
if (char(key) == 27){
//cvSaveImage("webcam_frame.png",frame);
}
}
cvReleaseCapture(&capture); //Release capture.
cvDestroyWindow("Camera_Output"); //Destroy Window
return 0;
}
when I run this code in my terminal it is not capturing the image rather it shows a blank window. I have attached a photo of output of this code.

Trying to render GTK+ window to image

GTK uses cairo for drawing. So I'm trying to create a hello world app that writes to an image (svg, png, ...) instead of X11. I'm facing 2 problems:
- The image is empty
- When starting without X11 running (which is the actual goal) I get the error "** (a.out:9021): WARNING **: Could not open X display"
The code is draft!
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
#include <cmath>
#include <cairo.h>
#include <cairommconfig.h>
#include <cairomm/context.h>
#include <cairomm/surface.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *button;
// GtkWidget *main_window = gtk_initialize();
window = gtk_offscreen_window_new();
button = gtk_button_new_with_label ("Hello World");
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (window);
GdkWindow *gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
std::cout << "gdk window: " << gdk_window << std::endl;
cairo_surface_t * surfp = gdk_offscreen_window_get_surface(gdk_window);
std::cout << "Created Window will now draw to png" << std::endl;
std::string filename = "image.svg";
double width = 600;
double height = 400;
Cairo::SvgSurface srfobj(surfp);
Cairo::RefPtr<Cairo::SvgSurface> refptr(&srfobj);
Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(refptr);
cr->save(); // save the state of the context
cr->show_page();
std::cout << "Wrote SVG file \"" << filename << "\"" << std::endl;
std::chrono::milliseconds dura( 200 );
std::this_thread::sleep_for(dura);
return 0;
}
Why is this code not working?
Can I run a gtk app without X11 running, or should I just ignore the warning?
Here is my solution based on your example code - keep in mind it is a dirty solution and may not work using newer versions of GTK3. It works to save the UI of a window (only tested with the one button), but still requires (somewhere) a running X-server. It also ignores / don't use your settings for the picture size - you'll have to resize it at your own. I don't know if (and how) it is possible to cut this string (X-Server // X-Framebuffer) too (DirectFB seems not to be really supported anymore), but...
Have fun!
// Default
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
// cairo / cairomm / gtk
#include <cairo.h>
#include <cairomm/context.h> //libcairomm-1.0-dev
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
// Init
gtk_init(&argc, &argv);
// Create window with a button
GtkWidget *window;
GtkWidget *button;
window = gtk_offscreen_window_new();
button = gtk_button_new_with_label("Hello World");
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
// Make a gdk window out of it, prepare cairo and draw it to it
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
cairo_surface_t* surfp = gdk_offscreen_window_get_surface(gdk_window);
cairo_t* context = cairo_create(surfp);
gtk_widget_draw(GTK_WIDGET(window), context);
// Yay - begin the dump!
Cairo::SvgSurface srfobj(surfp);
std::string filename = "image.png";
srfobj.write_to_png(filename);
std::cout << "Done." << std::endl;
// Aaand a little sleep...
std::chrono::milliseconds dura(1000);
std::this_thread::sleep_for(dura);
return 0;
}
Try to use gtk_widget_draw (widget_ptr, cairo_ctx_ptr); to draw a widget (or a hierarchy of widgets) to a cario context?
The answer to both your questions is that you cannot run GTK+ applications without some sort of output. You're using gtk-x11 which requires an XServer. You might have some luck with the DirectFB backend, but I wouldn't hold your breath as I don't know if it's even maintained anymore.
Because Gtk doesn't run without an XServer the resulting image is empty.

SDL loading my image messed up

I'm attemting to load an image that I exported from flash CS3 it's a very cute face but it loads very weird it loads on a blueish way this is the code for the two files:
//main.cpp
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "test.hpp"
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
// Activamos modo de video
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE | SDL_DOUBLEBUF);
image = IMG_Load("face.bmp");
dest.x = 200;
dest.y = 200;
//Main Loop
while(Abierto)
{
//We Draw
Draw();
//Events
while( SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
Abierto = false;
}
}
// We free the image
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}
Now the other one the;
//test.hpp
DL_Surface *image = NULL, *screen = NULL;
SDL_Rect dest;
SDL_Event event;
bool Abierto = true;
float PlaneX = 300, PlaneY = 200;
float velX = 0.1, velY = 0.1;
void Draw()
{
Uint32 color;
// Black Background is created
color = SDL_MapRGB (screen -> format, 0, 0, 0);
SDL_FillRect (screen, NULL, color);
SDL_DisplayFormatAlpha(image);
SDL_BlitSurface(image, NULL, screen, &dest);
// Flip the working image buffer with the screen buffer
SDL_Flip (screen);
}
I need help with this please Im not that experienced on SDL stuff oh and if you want to take a closer look I uplaoded the project here.
Oh my bad I must add the image is 32 pixels with alpha according to flash exporting options
According to docs, SDL_DisplayFormatAlpha returns a new image and keeps the original intact.
So, try in the first part, when you load the image:
SDL_Surface *origImage = IMG_Load("face.bmp");
image = SDL_DisplayFormatAlpha(origImage);
SDL_FreeSurface(origImage)
As there is no need to call SDL_DisplayFormatAlpha each frame.
Then in the second part, just blit image, without calling SDL_DisplayFormatAlpha.
UPDATE
I've just checked your picture, and it looks like it is a weird bmp. I've seen that before: BMP format is such a mess that if you don't keep to the basics chances are that different programs will interpret the data differently.
In your case:
display face.bmp shows correctly.
gthumb face.bmp shows nothing.
eog face.bmp says "bogus header data".
I strongly recommend using PNG files for all your game cartoon-like pictures and JPG for all the photo-like ones.
So run
$ convert face.bmp face.png
And use the PNG file. I'll will work better and you will have a file 20% the size of the original.

Trying to close OpenCV window has no effect

I'm capturing the webcam image with OpenCV. That works fine. But if I want to close the OpenCV when a button is pressed, it does not work (tried both cvDestroyWindow("NameOfWindow")and cvDestroyAllWindows()). The window stays open and the application is still running.
The OpenCV was initialized on separate thread from the main GUI.
I'm using the Juce Framework with C++ on my Mac. But the same problem occurs also on Windows with Qt and Windows Forms, when the OpenCV Window has it's own cvNamedWindow.
Here is the basic code of the VST plugin editor class:
PluginEditor.cpp
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "PluginProcessor.h"
#include "PluginEditor.h"
//
TestAudioProcessorEditor::TestAudioProcessorEditor (TestAudioProcessor* ownerFilter)
: AudioProcessorEditor (ownerFilter)
{
// This is where our plugin's editor size is set.
setSize (500, 500);
// open the tracker
openTracker();
}
// code for opencv handling
TestAudioProcessorEditor::openTracker() {
// KEY LINE: Start the window thread
cvStartWindowThread();
// Create a window in which the captured images will be presented
cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE );
cvWaitKey(0);
cvDestroyWindow( "Webcam" );
// window should disappear!
}
TestAudioProcessorEditor::~TestAudioProcessorEditor()
{
}
// paint stuff on the vst plugin surface
void TestAudioProcessorEditor::paint (Graphics& g) {
}
The piece you may be missing is a call to the cvStartWindowThread function.
On Linux, using the GTK HighGUI, this example reproduced your problem, until I put in the call to cvStartWindowThread.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
// KEY LINE: Start the window thread
cvStartWindowThread();
// Open a window
cvNamedWindow("original", 0);
// Wait until a key gets pressed inside the window
cvWaitKey(0);
// Close the window
cvDestroyWindow("original");
// Verify that the window is closed
cout<<"The window should be closed now. (Press ENTER to continue.)"<<endl;
string line;
getline(cin, line);
cout<<"Exiting..."<<endl;
}
If cvStartWindowThread doesn't help, try doing an extra call to cvWaitKey after your cvDestroy call.
To run the example, compile it with GCC:
g++ destroy_window.cpp -o destroy_window -lopencv_core -lopencv_highgui