I successfully compiled opencv_example (which is VLC video filter plugin from vlc/modules/video_filter). But it doesn't want to work on me.
I run vlc as the following:
XXX#XXX:/opt/vlc-2.0.1-td/bin$ vlc --video-filter opencv_example
VLC runs, I open the video file, it opens well (I see frames). But then the debug always answered me with:
[0x7f3bec792b78] main filter error: corrupt module: /usr/local/lib/vlc/plugins/video_filter/libopencv_example_plugin.so
[0x7f3bec1728f8] main video output error: Failed to create video filter2 'opencv_example'
[0x7f3bec1728f8] main video output error: Failed to add filter 'opencv_example'
[0x7f3bee4b1a88] main filter error: corrupt module: /usr/local/lib/vlc/plugins/video_filter/libopencv_example_plugin.so
[0x7f3bec1728f8] main video output error: Failed to create video filter2 'opencv_example'
[0x7f3bec1728f8] main video output error: Failed to add filter 'opencv_example'
I found out that it may be linking error, since when I run ldd util, I can't see the reference to any opencv library among the libraries, used by libopencv_example_plugin.so:
XXX#XXX:/usr/local/lib/vlc/plugins/video_filter$ ldd libopencv_example_plugin.so
linux-vdso.so.1 => (0x00007fff947ff000)
libvlccore.so.5 => /usr/local/lib/libvlccore.so.5 (0x00007f9c1b92c000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9c1b56c000)
libdbus-1.so.3 => /lib/x86_64-linux-gnu/libdbus-1.so.3 (0x00007f9c1b327000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f9c1b11f000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f9c1af02000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9c1acfd000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f9c1aa01000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9c1be31000)
So. The question is listed above: how can I make opencv_example work?
I'm working on Ubuntu 12.04 with VLC 2.0.1, OpenCV 2.4.9
Thanks in advance for everyone!
Finally dealed with that die hard. The problem of running opencv_example is complex, so there are two steps to solve it:
1 Link necessary OpenCV libraries
My initial thought was true: failure in creating VLC video filter is caused by unlinked OpenCV libraries.
To link them open configure.ac for writing. Then find the following line:
PKG_ENABLE_MODULES_VLC([OPENCV], [opencv_example opencv_wrapper], [opencv], (OpenCV (computer vision) filter), [off])
This exact line tells VLC NOT to link OpenCV to opencv_example and opencv_wrapper. So change it and add some more lines to check the availability of CV-functions:
PKG_ENABLE_MODULES_VLC([OPENCV], [opencv_example opencv_wrapper], [opencv], (OpenCV (computer vision) filter), [on])
AC_MSG_CHECKING([opencv libs for opencv_example & opencv_wrapper])
AC_CHECK_LIB(opencv_objdetect, cvHaarDetectObjects,
[
VLC_ADD_PLUGIN([opencv_wrapper opencv_example])
VLC_ADD_CFLAGS([opencv_wrapper opencv_example], [$OPENCV_CFLAGS])
VLC_ADD_LIBS([opencv_wrapper opencv_example],[-lopencv_core -lopencv_objdetect])
AC_MSG_RESULT([opencv libs added, cvHaarDetectObjects is ok])
],
AC_MSG_ERROR([opencv libs for opencv_example & opencv_wrapper cannot be added!]))
Save this file, go back to VLC directory and do the following operations:
autoconf
./configure
make
make install
When running ./configure, among the other information your terminal should show:
checking opencv libs for opencv_example & opencv_wrapper...
checking for cvHaarDetectObjects in -lopencv_objdetect... yes
This is a good sing means that OpenCV is installed and VLC is ready to link it to our plugins.
After installing everything (make install) check linkage by running ldd utility in /usr/local/lib/vlc/plugins/video_filter directory (or whereever your VLC-plugins are installed):
XXX#XXX:/usr/local/lib/vlc/plugins/video_filter# ldd libopencv_example_plugin.so
linux-vdso.so.1 => (0x00007fff549cd000)
libvlccore.so.5 => /usr/local/lib/libvlccore.so.5 (0x00007f036aa2a000)
libopencv_core.so.2.4 => /usr/local/lib/libopencv_core.so.2.4 (0x00007f036a5e8000)
libopencv_objdetect.so.2.4 => /usr/local/lib/libopencv_objdetect.so.2.4 (0x00007f036a367000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0369fa7000)
libdbus-1.so.3 => /lib/x86_64-linux-gnu/libdbus-1.so.3 (0x00007f0369d63000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f0369b5a000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f036993d000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f0369739000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f036943c000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f0369225000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f0368f25000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f0368d0e000)
libopencv_imgproc.so.2.4 => /usr/local/lib/libopencv_imgproc.so.2.4 (0x00007f036884c000)
libopencv_highgui.so.2.4 => /usr/local/lib/libopencv_highgui.so.2.4 (0x00007f03684ae000)
/lib64/ld-linux-x86-64.so.2 (0x00007f036af2f000)
libjpeg.so.8 => /usr/lib/x86_64-linux-gnu/libjpeg.so.8 (0x00007f036825d000)
libpng12.so.0 => /lib/x86_64-linux-gnu/libpng12.so.0 (0x00007f0368035000)
libtiff.so.4 => /usr/lib/x86_64-linux-gnu/libtiff.so.4 (0x00007f0367dd0000)
libgtk-x11-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0 (0x00007f0367796000)
libgdk-x11-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgdk-x11-2.0.so.0 (0x00007f03674e4000)
libgobject-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007f0367294000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f0366f9f000)
libdc1394.so.22 => /usr/lib/x86_64-linux-gnu/libdc1394.so.22 (0x00007f0366d2c000)
libv4l1.so.0 => /usr/lib/x86_64-linux-gnu/libv4l1.so.0 (0x00007f0366b25000)
libavcodec.so.53 => /usr/lib/x86_64-linux-gnu/libavcodec.so.53 (0x00007f0365d15000)
libavformat.so.53 => /usr/lib/x86_64-linux-gnu/libavformat.so.53 (0x00007f0365a15000)
libavutil.so.51 => /usr/lib/x86_64-linux-gnu/libavutil.so.51 (0x00007f03657f4000)
libswscale.so.2 => /usr/lib/x86_64-linux-gnu/libswscale.so.2 (0x00007f03655ae000)
libpangocairo-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangocairo-1.0.so.0 (0x00007f03653a1000)
libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f036506c000)
libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f0364e66000)
libatk-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libatk-1.0.so.0 (0x00007f0364c43000)
libcairo.so.2 => /usr/lib/x86_64-linux-gnu/libcairo.so.2 (0x00007f0364985000)
libgdk_pixbuf-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0 (0x00007f0364765000)
libgio-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0 (0x00007f0364415000)
libpangoft2-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0 (0x00007f03641eb000)
libpango-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpango-1.0.so.0 (0x00007f0363fa2000)
libfontconfig.so.1 => /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007f0363d6b000)
libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f0363b5a000)
libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f0363950000)
libXinerama.so.1 => /usr/lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f036374c000)
libXi.so.6 => /usr/lib/x86_64-linux-gnu/libXi.so.6 (0x00007f036353d000)
libXrandr.so.2 => /usr/lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f0363335000)
libXcursor.so.1 => /usr/lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f036312a000)
libXcomposite.so.1 => /usr/lib/x86_64-linux-gnu/libXcomposite.so.1 (0x00007f0362f27000)
libXdamage.so.1 => /usr/lib/x86_64-linux-gnu/libXdamage.so.1 (0x00007f0362d24000)
libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007f0362b1b000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f03628de000)
libraw1394.so.11 => /usr/lib/x86_64-linux-gnu/libraw1394.so.11 (0x00007f03626ce000)
libusb-1.0.so.0 => /lib/x86_64-linux-gnu/libusb-1.0.so.0 (0x00007f03624bf000)
libv4l2.so.0 => /usr/lib/x86_64-linux-gnu/libv4l2.so.0 (0x00007f03622b3000)
libvpx.so.1 => /usr/lib/libvpx.so.1 (0x00007f036200d000)
libvorbisenc.so.2 => /usr/lib/x86_64-linux-gnu/libvorbisenc.so.2 (0x00007f0361b3e000)
libvorbis.so.0 => /usr/lib/x86_64-linux-gnu/libvorbis.so.0 (0x00007f0361912000)
libtheoraenc.so.1 => /usr/lib/x86_64-linux-gnu/libtheoraenc.so.1 (0x00007f03616d4000)
libtheoradec.so.1 => /usr/lib/x86_64-linux-gnu/libtheoradec.so.1 (0x00007f03614b9000)
libspeex.so.1 => /usr/lib/x86_64-linux-gnu/libspeex.so.1 (0x00007f03612a0000)
libschroedinger-1.0.so.0 => /usr/lib/libschroedinger-1.0.so.0 (0x00007f0360fec000)
libgsm.so.1 => /usr/lib/libgsm.so.1 (0x00007f0360dde000)
libva.so.1 => /usr/lib/x86_64-linux-gnu/libva.so.1 (0x00007f0360bc8000)
libbz2.so.1.0 => /lib/x86_64-linux-gnu/libbz2.so.1.0 (0x00007f03609b7000)
libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f036071b000)
libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f03604fc000)
libpixman-1.so.0 => /usr/lib/x86_64-linux-gnu/libpixman-1.so.0 (0x00007f0360275000)
libxcb-shm.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-shm.so.0 (0x00007f0360072000)
libxcb-render.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-render.so.0 (0x00007f035fe67000)
libgmodule-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 (0x00007f035fc63000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f035fa43000)
libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f035f827000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f035f5fc000)
libv4lconvert.so.0 => /usr/lib/x86_64-linux-gnu/libv4lconvert.so.0 (0x00007f035f387000)
libogg.so.0 => /usr/lib/x86_64-linux-gnu/libogg.so.0 (0x00007f035f180000)
liborc-0.4.so.0 => /usr/lib/x86_64-linux-gnu/liborc-0.4.so.0 (0x00007f035ef04000)
libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f035ed01000)
libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f035eafb000)
See, the amount of linked libraries increased severalfold.
Step one is done. You can check the loading of opencv_example by running your VLC. But rightly then you may face another issue:
...
[0x7f2d741728f8] main video output debug: Adding 'opencv_example' as interactive
[0x7f2d747f7a68] main filter debug: looking for video filter2 module: 1 candidate
[0x7f2d747f7a68] main filter debug: using video filter2 module "opencv_example"
[0x7f2d747f7a68] main filter debug: TIMER module_need() : 149.351 ms - Total 149.351 ms / 1 intvls (Avg 149.351 ms)
[0x7f2d741728f8] main video output debug: Filter 'opencv_example' (0x7f2d747f7a68) appended to chain
[0x7f2d74173cb8] main spu text debug: removing module "freetype"
[0x7f2d74173cb8] main spu text debug: looking for text renderer module: 3 candidates
[0x7f2d74173cb8] freetype spu text debug: Building font databases.
[0x7f2d74173cb8] freetype spu text debug: Took 33 microseconds
[0x7f2d74173cb8] freetype spu text debug: Using Serif Bold as font from file /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf
[0x7f2d74173cb8] freetype spu text debug: using fontsize: 2
[0x7f2d74173cb8] main spu text debug: using text renderer module "freetype"
[0x7f2d74173cb8] main spu text debug: TIMER module_need() : 5.776 ms - Total 5.776 ms / 1 intvls (Avg 5.776 ms)
[0x7f2d8c00d018] main decoder debug: End of video preroll
[0x7f2d8c00d018] main decoder debug: Received first picture
Segmentation fault (core dumped)
2 Change the opencv_example source code
So, as I found out, the error is hidden in converting image from picture_t (VLC structure for storing image) to IplImage (OpenCV type for image):
//(hack) cast the picture_t to array of IplImage*
p_img = (IplImage**) p_pic->p[0].p_pixels;
Just change this convertion a little bit to make everything work well. Here is the full code for Filter method from my modified opencv_example.c:
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
IplImage* p_img;
int i_planes = 0;
CvPoint pt1, pt2;
int i, scale = 1;
filter_sys_t *p_sys = p_filter->p_sys;
if ((!p_pic))
{
msg_Err( p_filter, "no image array" );
return NULL;
}
i_planes = p_pic->i_planes;
if (i_planes<1)
{
msg_Err( p_filter, "no image planes" );
return NULL;
}
if ((p_pic->format.i_chroma != VLC_CODEC_I420))
{
msg_Err( p_filter, "wrong chroma - use I420" );
return NULL;
}
//picture_t to IplImage without segmentation fault
p_img = cvCreateImageHeader( cvSize( p_pic->p[0].i_pitch, p_pic->p[0].i_visible_lines ),
IPL_DEPTH_8U, 1 );
cvSetData( p_img, p_pic->p[0].p_pixels, p_pic->p[0].i_pitch );
//perform face detection
cvClearMemStorage(p_sys->p_storage);
if( p_sys->p_cascade )
{
//we should make some of these params config variables
CvSeq *faces = cvHaarDetectObjects( p_img, p_sys->p_cascade, p_sys->p_storage,
1.15, 5, CV_HAAR_DO_CANNY_PRUNING,
cvSize(0, 0), cvSize(20, 20) );
//create the video_filter_region_info_t struct
if (faces && (faces->total > 0))
{
msg_Dbg( p_filter, "Found %d face(s)", faces->total );
free( p_sys->event_info.p_region );
p_sys->event_info.p_region = (video_filter_region_info_t*)
calloc( faces->total, sizeof(video_filter_region_info_t));
if( !p_sys->event_info.p_region )
return NULL;
p_sys->event_info.i_region_size = faces->total;
}
//populate the video_filter_region_info_t struct
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect *r = (CvRect*)cvGetSeqElem( faces, i );
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
cvRectangle( p_img, pt1, pt2, CV_RGB(0,0,0), 3, 8, 0 );
*(CvRect*)(&(p_sys->event_info.p_region[i])) = *r;
p_sys->event_info.p_region[i].i_id = p_sys->i_id++;
p_sys->event_info.p_region[i].p_description = "Face Detected";
}
if (faces && (faces->total > 0)) //raise the video filter event
var_TriggerCallback( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE );
}
else
msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" );
//IplImage to picture_t without segmentation fault
cvGetRawData( p_img, (uchar**)&p_pic->p[0].p_pixels, NULL, NULL );
return p_pic;
}
VLC should work well, as well as opencv_example filter. Please, let me know if you'll have any other issues connected to the topic of VLC+OpenCV
#Anton good solution, what about timing i.e if the function in the filter need quite a time to finish > 500ms will that effect the framerate of the playbck !!
Related
As I know,
ldd output normally looks like SHARED_LIBRARY => location.
But liblz.so.1 and libzstd.so.1 below don't seem to be so.
ldd librdkafka.so
linux-vdso.so.1 => (0x00007ffde7484000)
liblz4.so.1 (0x00007f97c8953000)
libzstd.so.1 (0x00007f97c8420000)
libsasl2.so.2 => /usr/lib64/libsasl2.so.2 (0x00007f97c8206000)
libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007f97c7f99000)
libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007f97c7bb4000)
libz.so.1 => /lib64/libz.so.1 (0x00007f97c799e000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f97c7799000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f97c757c000)
librt.so.1 => /lib64/librt.so.1 (0x00007f97c7374000)
libc.so.6 => /lib64/libc.so.6 (0x00007f97c6fdf000)
What is the meaning of this type of output?
Psychic debugging: data and packet are the parameters of MPU6050::dmpGetAccel, and you are trying to combine four bytes into a word.
You need to do your arithmetic in uint32_ts, not uint8_ts, otherwise you always overflow the values.
Change all the lines of the form
data[n] = ((packet[m] << 24) + (packet[m+1] << 16) + (packet[m+2] << 8) + packet[m+3]);
to
data[n] = ((uint32_t{ packet[m] } << 24) + (uint32_t{ packet[m+1] } << 16) + (uint32_t{ packet[m+2] } << 8) + uint32_t{ packet[m+3] });
I have a file that is read and split into %ojects, the %objects are populated as shown below.
$VAR1 = 'cars';
$VAR2 = {
'car1' => {
'info1' => '"fast"',
'info2' => 'boring'
},
'car2' => {
'info1' => '"slow"',
'info2' => 'boring info'
},
'car3' => {
'info1' => '"unique"',
'info2' => 'useless info'
}
};
$VAR3 = 'age';
$VAR4 = {
'new' => {
'info3' => 'rust',
'info4' => '"car1"'
},
'old' => {
'info3' => 'shiny',
'info4' => '"car2" "car3"'
}
}
};
My goal is to insert data like "car1 fast rust, car2 slow shiny, car3 unique shiny" in a DB but I can't get e.g. "rust to match based on info4 in age" ..
my $key = cars;
my $key2 = age;
foreach my $obj (keys %{$objects{$key}}) { # for every car
#info1s = $objects{$type}{$obj}{'info1'} =~ m/"(.*?)"/g; # added to clean up all info1
foreach my $infos ($info1s) {
dbh execute insert $obj $infos # this gives me "car1 fast, car2 slow, car3 unique"
}
...
Can somebody please point me in the right direction to fetch and store info4 with related info1/info2?
Thanks!
I take the objective to be as follows.
Get values for (info4) keys in $VAR4, at the deepest-level hashref, and find them as top-level keys in $VAR2 hashref. Then associate with them both a value from a (info3) key, their "sibling" in their own $VAR4's deepest level hashref, as well as the value of a key (info1) from $VAR2.
One can traverse the structure by hand for this purpose, specially if it's always with the same two levels as shown, but it's easier and better with libraries. I use Data::Leaf::Walker to get leaves (deepest values) and key-paths to them, and Data::Diver to get values for known paths.
use warnings;
use strict;
use feature 'say';
use Data::Dump;
use Data::Leaf::Walker;
use Data::Diver qw(Dive);
my $hr1 = {
'car1' => { 'info1' => 'fast', 'info2' => 'boring' },
'car2' => { 'info1' => 'slow', 'info2' => 'boring info' },
'car3' => { 'info1' => 'unique', 'info2' => 'useless info' }
};
my $hr2 = {
'new' => { 'info3' => 'rust', 'info4' => 'car1' },
'old' => { 'info3' => 'shiny', 'info4' => 'car2 car3' }
};
my $walker = Data::Leaf::Walker->new($hr2);
my %res;
while ( my ($path, $value) = $walker->each ) {
next if $path->[-1] ne 'info4';
# Some "values" have multiple needed values separated by space
for my $val (split ' ', $value) {
# Get from 'info4' path the one to its sibling, 'info3'
my #sibling_path = ( #{$path}[0..$#$path-1], 'info3' );
# Collect results: values of `info3` and `info1`
push #{$res{$val}},
Dive( $hr2, #sibling_path ),
Dive( $hr1, ($val, 'info1') );
}
}
dd \%res;
This assumes a few things and takes some shortcuts, for simplicity.
For one, I use explicit infoN keys from the questions, and the two-level structure. If data is, or can be different, this shouldn't be hard to adjust.
Next, this assumes that a value like car1 always exists as a key in the other hashref. Add an exists check for that key if it is possible that it doesn't exist as a key.
I've removed some extra quotes from data. (If that's for database entry do that when constructing the statement. If data comes in with such extra quotes it should be easy to adjust the code to take them into account.)
The above program prints
{
car1 => ["rust", "fast"],
car2 => ["shiny", "slow"],
car3 => ["shiny", "unique"],
}
(I use Data::Dump to display complex data structure, for its simplicity and default compact output.)
I'm trying to write a simple program that just opens a window with GLEW 2.0 and GLFW 3, but am being met with a segfault...
Here is my source:
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(int argc, char **argv) {
const int height = 100;
const int width = 100;
if (!glfwInit()) {
printf("Glfw failed to init\n");
return -1;
}
// 4x antialiasing
glfwWindowHint(GLFW_SAMPLES, 4);
// We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// We don't want the old OpenGL
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(width, height, "Tutorial 01", NULL, NULL);
if (window == NULL) {
printf("GLFW Failed to open a window. "
"Intel GPUs don't support 3.3\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = 1;
if (glewInit() != GLEW_OK) {
printf("GLEW Failed to initialize.\n");
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do {
glfwSwapBuffers(window);
glfwPollEvents();
} while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
}
I compile this with
gcc test.c -g -lGLEW -lglfw -o test
And receive a segfault upon running the program. Using gdb, a backtrace on the segfault gives
0 0x0000000000000000 in ?? ()
1 0x00007ffff7ba02a7 in glxewInit () from /usr/lib64/libGLEW.so.2.0
2 0x00007ffff7ba87a3 in glewInit () from /usr/lib64/libGLEW.so.2.0
3 0x0000555555554bd4 in main (argc=1, argv=0x7fffffffdf18) at test.c:39
I am on Ubuntu 17.04, and I installed glew/glfw with Ubuntus pacakge manager with
sudo apt-get install libglew2.0 libglew-dev libglfw3 libglfw3-dev
I was following this tutorial here. If it makes any difference, I don't have a desktop manager or a compositor running. I'm using i3wm. However, the segfault still occurs when running alongside the compton compositor.
Lastly, just so I can provide some extra info, the ldd shows the linked libraries of test as
linux-vdso.so.1 => (0x00007ffc44ce9000)
libGLEW.so.2.0 => /usr/lib64/libGLEW.so.2.0 (0x00007f8e53dc9000)
libglfw.so.3 => /usr/lib/x86_64-linux-gnu/libglfw.so.3 (0x00007f8e53b86000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8e537bf000)
libGL.so.1 => /usr/lib/nvidia-375/libGL.so.1 (0x00007f8e5351b000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f8e53313000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8e53008000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8e52e04000)
libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f8e52acb000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f8e528ad000)
libXrandr.so.2 => /usr/lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f8e526a2000)
libXinerama.so.1 => /usr/lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f8e5249f000)
libXxf86vm.so.1 => /usr/lib/x86_64-linux-gnu/libXxf86vm.so.1 (0x00007f8e52297000)
libXcursor.so.1 => /usr/lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f8e5208d000)
/lib64/ld-linux-x86-64.so.2 (0x000055e0ef28b000)
libGLX.so.0 => /usr/lib/nvidia-375/libGLX.so.0 (0x00007f8e51e5d000)
libGLdispatch.so.0 => /usr/lib/nvidia-375/libGLdispatch.so.0 (0x00007f8e51b8f000)
libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f8e5196d000)
libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f8e5175b000)
libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f8e5154f000)
libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f8e51349000)
libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f8e51145000)
libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f8e50f3f000)
Thanks for any help.
There is a bug report saying that core profile was broken in GLEW until 2.0.0.
Thus, you should update the GLEW. If that won't work, switch to compatibility profile by replacing GLFW_OPENGL_CORE_PROFILE flag with GLFW_OPENGL_COMPAT_PROFILE.
I am using mime_content_type() in PHP 5.5 to get a MIME type, but it throws fatal: error function not found.
How can I achieve this on PHP 5.5?
Make use of the finfo() functions.
A simple illustration:
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, "path/to/image_dir/image.gif");
finfo_close($finfo);
OUTPUT :
image/gif
Note : Windows users must include the bundled php_fileinfo.dll DLL file in php.ini to enable this extension.
I've spent too much time trying to get the finfo functions to work, properly. I finally just ended up creating my own function to match the file extension to any array of mime types. It's not a full-proof way of assuring that the files are truly what the extension denotes them to be, but that problem can be mitigated by how you process I/O of said files on your server(s).
function mime_type($file) {
// there's a bug that doesn't properly detect
// the mime type of css files
// https://bugs.php.net/bug.php?id=53035
// so the following is used, instead
// src: http://www.freeformatter.com/mime-types-list.html#mime-types-list
$mime_type = array(
"3dml" => "text/vnd.in3d.3dml",
"3g2" => "video/3gpp2",
"3gp" => "video/3gpp",
"7z" => "application/x-7z-compressed",
"aab" => "application/x-authorware-bin",
"aac" => "audio/x-aac",
"aam" => "application/x-authorware-map",
"aas" => "application/x-authorware-seg",
"abw" => "application/x-abiword",
"ac" => "application/pkix-attr-cert",
"acc" => "application/vnd.americandynamics.acc",
"ace" => "application/x-ace-compressed",
"acu" => "application/vnd.acucobol",
"adp" => "audio/adpcm",
"aep" => "application/vnd.audiograph",
"afp" => "application/vnd.ibm.modcap",
"ahead" => "application/vnd.ahead.space",
"ai" => "application/postscript",
"aif" => "audio/x-aiff",
"air" => "application/vnd.adobe.air-application-installer-package+zip",
"ait" => "application/vnd.dvb.ait",
"ami" => "application/vnd.amiga.ami",
"apk" => "application/vnd.android.package-archive",
"application" => "application/x-ms-application",
// etc...
// truncated due to Stack Overflow's character limit in posts
);
$extension = \strtolower(\pathinfo($file, \PATHINFO_EXTENSION));
if (isset($mime_type[$extension])) {
return $mime_type[$extension];
} else {
throw new \Exception("Unknown file type");
}
}
Edit:
I'd like to address Davuz's comment (since it keeps getting up-voted) and remind everyone that I put in the pseudo disclaimer at the top that this isn't "full-proof." So, please keep that in mind when considering the approach I've offered in my answer.
mime_content_type() is not deprecated and works fine.
Why is mime_content_type() deprecated in PHP?
http://php.net/manual/en/function.mime-content-type.php
As of PHP 5.3, it's even built-in.
$finfo = finfo_open(FILEINFO_MIME_TYPE); should do it.
Taken from the php.net docs. Your function is deprecated and probably already removed.
http://www.php.net/manual/en/function.finfo-file.php
You should understand that file_get_contents will upload whole file to the memory, it is not good way to get only mime type. You don't need to use buffer method and file_get_contents function in this case.
To prevent any errors and warnings, better do like this.
$filename = 'path to your file';
if (class_exists('finfo')) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (is_object($finfo)) {
echo $finfo->file($filename);
}
} else {
echo 'fileinfo did not installed';
}
Also you should know $finfo->file will throw PHP Warning if it fail.
If fileinfo is not installed properly, and you have a fresh version of PHP, you can get mime type from headers.
You can use cURL to get mime type from headers.
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 1,
CURLOPT_URL => $link)
);
$headers = curl_exec($ch);
curl_close($ch);
if (preg_match('/Content-Type:\s(.*)/i', $headers, $matches)) {
echo trim($matches[1], "\t\n\r");
}else {
echo 'There is no content type in the headers!';
}
Also you can use get_headers function, but it more slow than cURL request.
$url = 'http://www.example.com';
$headers = get_headers($url, 1);
echo $headers['Content-Type'];
Get the image size using:
$infFil=getimagesize($the_file_name);
and
echo $infFil["mime"]
The getimagesize returns an associative array which have a MIME key and obviously the image size too
I used it and it works
I use the MimeTypeTool from Bat (https://github.com/lingtalfi/Bat)
It uses fileinfo if available, and defaults back to an "extension => mime type" mapping otherwise.
This is the best solution I found by combining two very good posts
// Thanks to http://php.net/manual/en/function.mime-content-type.php#87856
function getMimeContentType($filename, $ext)
{
if(!function_exists('mime_content_type'))
{
if($mime_types = getMimeTypes())
{
if (array_key_exists($ext, $mime_types))
{
return $mime_types[$ext];
}
elseif (function_exists('finfo_open'))
{
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
}
return 'application/octet-stream';
}
return mime_content_type($filename);
}
// Thanks to http://php.net/manual/en/function.mime-content-type.php#107798
function getMimeTypes()
{
$url = 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types';
$mimes = array();
foreach(#explode("\n",#file_get_contents($url)) as $x)
{
if(isset($x[0]) && $x[0]!=='#' && preg_match_all('#([^\s]+)#', $x, $out) && isset($out[1]) && ($c = count($out[1])) > 1)
{
for($i=1; $i < $c; $i++)
{
$mimes[$out[1][$i]] = $out[1][0];
}
}
}
return (#sort($mimes)) ? $mimes : false;
}
Use it link this:
$filename = '/path/to/the/file.pdf';
$ext = strtolower(array_pop(explode('.',$filename)));
$content_type = getMimeContentType($filename, $ext);
Will continue to work even if the mime_content_type function is no longer supported in php.
How to auto download the excel files from the browser in one click on the link, without going through the "save as" and other windows in watir. I am trying to keep it OS independent, so would not be interested in using win32ole gem.
for this task I tweaking my profile preferences
my code looks like this:
chrome driver:
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.default_directory'] = download_directory
profile['download.prompt_for_download'] = false
browser = Watir::Browser.new :chrome, :profile => profile
chrome driver 2:
prefs = {
'download' => {
'default_directory' => download_directory,
'prompt_for_download' => false,
'directory_upgrade' => true,
'extensions_to_open' => '',
},
'profile' => {
'default_content_settings' => {'multiple-automatic-downloads' => 1}, #for chrome version olde ~42
'default_content_setting_values' => {'automatic_downloads' => 1}, #for chrome newer 46
'password_manager_enabled' => false,
'gaia_info_picture_url' => true,
}
}
caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['chromeOptions'] = {'prefs' => prefs}
browser = Watir::Browser.new :chrome, :desired_capabilities => caps
firefox:
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.lastDir'] = download_directory
profile['browser.download.folderList'] = 2
profile['browser.download.dir'] = download_directory
profile['browser.download.manager.showWhenStarting'] = false
profile['browser.helperApps.alwaysAsk.force'] = false
profile['browser.helperApps.neverAsk.openFile'] = "text/csv,application/pdf"
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"
profile['pdfjs.disabled'] = true
browser = Watir::Browser.new :firefox, :profile => profile
(firefox example for me working only for pdf files)
but selenium browsers download has many bugs
some problem in chrome or firefox webdriver (like this http://code.google.com/p/chromedriver/issues/detail?id=130)
do not allow write good tests for file downloads
I wrote the following ruby script for download files
require ‘rubygems’
require “net/http”
require “uri”
def download(_url, _download_path = ”)
url = URI.parse _url
http_object = Net::HTTP.new(url.host, url.port)
http_object.use_ssl = true if (url.scheme == ‘https’ || url.port == 443)
http_object.start.request_get(url.path) do |response|
start_time = Time.now
response["Content-Disposition"] =~ /^.+?filename=”(.+?)”$/
file_name = $1
file = open(_download_path + file_name, ‘wb’)
length = response['Content-Length'].to_i
response.read_body do |fragment|
file.write(fragment)
end
file.close
file_size = File.size(_download_path + file_name)/1024.0/1024.0
puts “-“*80
puts “Download time – #{Time.now – start_time}”
puts “Download speed – #{file_size/(Time.now – start_time)} MB/s”
puts “-“*80
end
end
download(‘http://storagemadeeasy.com/files/1cf064a30aba6d1b8fbc0fba8ac8be5b.jpg’)
I hope this code will be useful for those who need test file download (not browser file download dialog window)
It appears to be unique to each browser. Alister Scott wrote this << try that.