LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Turn on Webcam from Bash or Python (https://www.linuxquestions.org/questions/programming-9/turn-on-webcam-from-bash-or-python-4175603935/)

faruk.c 04-16-2017 05:53 AM

Turn on Webcam from Bash or Python
 
Hi everyone,

I have a problem about using webcam from terminal. I hope, you can help me. My notebook is MSI GE60 2PC Apache, and my OS is Debian 8 Jessie.

When I press FN+F6, my webcam turns on and I can use webcam with cheese application. If I press FN+F6 again, webcam turns off and I can't use webcam, there is a message "No device found". I want to control webcam with python code or bash code, I don't want to press FN+F6. How can I do? Syslog has "udev", "evdev", "mtp-probe" .., I guess I will use these but which? Or I can use keypress function in python but how?

That's my syslog output, when I press FN+F6:

Code:

Apr 16 01:24:28 debian kernel: [34628.327642] usb 1-1.4: new high-speed USB device number 8 using ehci-pci
Apr 16 01:24:28 debian kernel: [34628.462468] usb 1-1.4: New USB device found, idVendor=5986, idProduct=0248
Apr 16 01:24:28 debian kernel: [34628.462471] usb 1-1.4: New USB device strings: Mfr=6, Product=2, SerialNumber=0
Apr 16 01:24:28 debian kernel: [34628.462472] usb 1-1.4: Product: BisonCam, NB Pro
Apr 16 01:24:28 debian kernel: [34628.464910] uvcvideo: Found UVC 1.00 device BisonCam, NB Pro (5986:0248)
Apr 16 01:24:28 debian mtp-probe: checking bus 1, device 8: "/sys/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4"
Apr 16 01:24:28 debian mtp-probe: bus: 1, device: 8 was not an MTP device
Apr 16 01:24:28 debian kernel: [34628.468993] input: BisonCam, NB Pro as /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4:1.0/input/input24
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (II) config/udev: Adding input device BisonCam, NB Pro (/dev/input/event17)
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (**) BisonCam, NB Pro: Applying InputClpass "evdev keyboard catchall"
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (II) Using input driver 'evdev' for 'BisonCam, NB Pro'
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (**) BisonCam, NB Pro: always reports core events
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (**) evdev: BisonCam, NB Pro: Device: "/dev/input/event17"
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (--) evdev: BisonCam, NB Pro: Vendor 0x5986 Product 0x248
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (--) evdev: BisonCam, NB Pro: Found keys
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (II) evdev: BisonCam, NB Pro: Configuring as keyboard
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (**) Option "config_info" "udev:/sys/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4:1.0/input/input24/event17"
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (II) XINPUT: Adding extended input device "BisonCam, NB Pro" (type: KEYBOARD, id 15)
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (**) Option "xkb_rules" "evdev"
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (**) Option "xkb_model" "pc105"
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: (**) Option "xkb_layout" "tr"
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: The XKEYBOARD keymap compiler (xkbcomp) reports:
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: > Warning:          Type "ONE_LEVEL" has 1 levels, but <RALT> has 2 symbols
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: >                  Ignoring extra symbols
Apr 16 01:24:28 debian gdm-Xorg-:0[1092]: Errors from xkbcomp are not fatal to the X server

Please, help me. Thanks :)

Xeratul 04-16-2017 08:21 AM

You may try with ncurses:

Code:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h> 
#include <ctype.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h> 
// time
#include <time.h>
#include <ncurses.h>
#include "../libc/libc-tui.c"

 

int main()
{
    int ch, posy , foo;
    char cwd[PATH_MAX];

    printf( "CAM\n" );
    //nruncmd(  " pkill  mplayer  ; mplayer -fs -zoom -tv device=/dev/video1 tv://          " );
    nstartncurses(  );
    int rows, cols ;
    getmaxyx( stdscr, rows , cols);

    nc_color_default( );
    ncerase();
    mvprintw( 0, 0, "CAM " );
    for( foo = 0 ; foo <= cols -1  ; foo++)
      mvaddch( 1 , foo , ACS_HLINE );
    posy = 4;
    mvprintw( posy++, 0, " Press Key \"0\" to View Webcam VIDEO-0 (embedded notebook) " );
    mvprintw( posy++, 0, " Press Key \"1\" to View Webcam VIDEO-1 (external webcam) " );

    ch = getch();
    switch( ch ) {
        case '0':
            runcmd(  " pkill  mplayer  ; mplayer -fs -zoom -tv device=/dev/video0 tv://          " );
            break;
        case '1':
            runcmd(  " pkill  mplayer  ; mplayer -fs -zoom -tv device=/dev/video1 tv://          " );
            break;
    }

    curs_set( 1 ) ;
    attroff( A_BOLD );
    attroff( A_REVERSE );
    curs_set( 1 );
    endwin();       
    return 0;
}


faruk.c 04-16-2017 11:04 AM

Quote:

Originally Posted by Xeratul (Post 5697636)
You may try with ncurses:

Code:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h> 
#include <ctype.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h> 
// time
#include <time.h>
#include <ncurses.h>
#include "../libc/libc-tui.c"

 

int main()
{
    int ch, posy , foo;
    char cwd[PATH_MAX];

    printf( "CAM\n" );
    //nruncmd(  " pkill  mplayer  ; mplayer -fs -zoom -tv device=/dev/video1 tv://          " );
    nstartncurses(  );
    int rows, cols ;
    getmaxyx( stdscr, rows , cols);

    nc_color_default( );
    ncerase();
    mvprintw( 0, 0, "CAM " );
    for( foo = 0 ; foo <= cols -1  ; foo++)
      mvaddch( 1 , foo , ACS_HLINE );
    posy = 4;
    mvprintw( posy++, 0, " Press Key \"0\" to View Webcam VIDEO-0 (embedded notebook) " );
    mvprintw( posy++, 0, " Press Key \"1\" to View Webcam VIDEO-1 (external webcam) " );

    ch = getch();
    switch( ch ) {
        case '0':
            runcmd(  " pkill  mplayer  ; mplayer -fs -zoom -tv device=/dev/video0 tv://          " );
            break;
        case '1':
            runcmd(  " pkill  mplayer  ; mplayer -fs -zoom -tv device=/dev/video1 tv://          " );
            break;
    }

    curs_set( 1 ) ;
    attroff( A_BOLD );
    attroff( A_REVERSE );
    curs_set( 1 );
    endwin();       
    return 0;
}


I can't compile this code. Because of '#include "../libc/libc-tui.c"' line. If I remove this line, there is a message "collect2: error: ld returned 1 exit status".

Xeratul 04-16-2017 11:11 AM

Code:

// spartan spartrekus
// compile&run with gcc -lncurses file.c ; ./a.out
// visit https://github.com/spartrekus?tab=repositories


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h> 
#include <ctype.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h> 
// time
#include <time.h>
#include <ncurses.h>
//#include "../libc/libc-tui.c"


void runcmd( char *thefile ){
      char cmdi[PATH_MAX];
      def_prog_mode();
      endwin();
      strncpy( cmdi , "  " , PATH_MAX );
      strncat( cmdi , thefile , PATH_MAX - strlen( cmdi ) -1 );
      strncat( cmdi , " " , PATH_MAX - strlen( cmdi ) -1 );
      system( cmdi );
      reset_prog_mode();
}

 

int main()
{
    int ch, posy , foo;
    char cwd[PATH_MAX];

    printf( "CAM\n" );
    //nruncmd(  " pkill  mplayer  ; mplayer -fs -zoom -tv device=/dev/video1 tv://          " );
    //nstartncurses(  );
    initscr();       
    curs_set( 0 );
    int rows, cols ;
    getmaxyx( stdscr, rows , cols);

    //nc_color_default( );
    erase();
    mvprintw( 0, 0, "CAM " );
    for( foo = 0 ; foo <= cols -1  ; foo++)
      mvaddch( 1 , foo , ACS_HLINE );
    posy = 4;
    mvprintw( posy++, 0, " Press Key \"0\" to View Webcam VIDEO-0 (embedded notebook) " );
    mvprintw( posy++, 0, " Press Key \"1\" to View Webcam VIDEO-1 (external webcam) " );

    ch = getch();
    switch( ch ) {
        case '0':
            runcmd(  " pkill  mplayer  ; mplayer -fs -zoom -tv device=/dev/video0 tv://          " );
            break;
        case '1':
            runcmd(  " pkill  mplayer  ; mplayer -fs -zoom -tv device=/dev/video1 tv://          " );
            break;
    }

    curs_set( 1 ) ;
    attroff( A_BOLD );
    attroff( A_REVERSE );
    curs_set( 1 );
    endwin();       
    return 0;
}

//edit: please do not use python, unless you have too. Get learning C or C++, this is useful. I am too a beginner in C.

faruk.c 04-16-2017 11:26 AM

Quote:

Originally Posted by Xeratul (Post 5697679)
//edit: please do not use python, unless you have too. Get learning C or C++, this is useful. I am too a beginner in C.

I used C language, already. When I try running "gcc webcam.c -o webcam", there is an error.

ntubski 04-16-2017 11:48 AM

Quote:

Originally Posted by Xeratul (Post 5697679)
//edit: please do not use python, unless you have too. Get learning C or C++, this is useful. I am too a beginner in C.

Please answer the OP's question. Please don't suggest using <X> language instead of giving an answer.

Xeratul 04-16-2017 12:52 PM

Quote:

Originally Posted by faruk.c (Post 5697682)
I used C language, already. When I try running "gcc webcam.c -o webcam", there is an error.

gcc -lncurses webcam.c -o webcam

apt-get install ncurses-dev

NevemTeve 04-16-2017 02:10 PM

Mind you, it has nothing to do with enabling and disabling the camera-device.

ondoho 04-16-2017 03:19 PM

you could try to emulate the keypresses with xdotool.
that is surely possible.
with a little additional research, i'm sure there's ways to address the camera (driver) programmatically.

faruk.c 04-16-2017 05:12 PM

Quote:

Originally Posted by Xeratul (Post 5697714)
gcc -lncurses webcam.c -o webcam

apt-get install ncurses-dev

Ok, I could run script but the script said "not found" when I press "0" or "1".

dugan 04-16-2017 05:13 PM

Xeratul's wasting your time.

All his C code does is run one of two commands:

Code:

pkill mplayer  ; mplayer -fs -zoom -tv device=/dev/video0 tv://
Code:

pkill  mplayer  ; mplayer -fs -zoom -tv device=/dev/video1 tv://
Neither of which is what you want.

I think it's likely that what you're thinking of isn't possible. But here's someone who looked into it and didn't succeed:

How to turn the laptop built-in webcam on/off from shell

faruk.c 04-16-2017 05:41 PM

Quote:

Originally Posted by ondoho (Post 5697773)
you could try to emulate the keypresses with xdotool.
that is surely possible.
with a little additional research, i'm sure there's ways to address the camera (driver) programmatically.

When I run
Code:

xdotool key XF86AudioPause
, there is a notifaction :thumbsup:

But, when I run
Code:

xdotool key XF86WebCam
, there isn't a notifaction and WebCam isn't working.

Can I call
Code:

state 0x10, keycode 220 (keysym 0x1008ff8f, XF86WebCam)

Xeratul 04-17-2017 01:11 AM

xdotool is pretty good. are you sure that XF86WebCam is really seen?

you made a test with xev, I see.

xbindkeys is pretty good!you can xbindkeys -k >> .xbindkeys
and then edit it with leafpad eg., leafpad ~/.xbindkeys

" xterm -e 'pkill mplayer ; mplayer -fs -zoom -tv device=/dev/video0 tv://' "
m:0x10 + c:127
Mod2 + Pause

faruk.c 04-17-2017 01:09 PM

Quote:

Originally Posted by Xeratul (Post 5697929)
xdotool is pretty good. are you sure that XF86WebCam is really seen?

you made a test with xev, I see.

xbindkeys is pretty good!you can xbindkeys -k >> .xbindkeys
and then edit it with leafpad eg., leafpad ~/.xbindkeys

" xterm -e 'pkill mplayer ; mplayer -fs -zoom -tv device=/dev/video0 tv://' "
m:0x10 + c:127
Mod2 + Pause

Thanks for reply. I made a test with xev and my "FN+F6" key is "XF86WebCam". Now, I tried xbindkeys and here results:

When I press "j" key:
Code:

"(Scheme function)"
    m:0x10 + c:44
    Mod2 + j

When I press "d" key:
Code:

"(Scheme function)"
    m:0x10 + c:40
    Mod2 + d

And when I press FN+F6:
Code:

"(Scheme function)"
    m:0x10 + c:220
    Mod2 + XF86WebCam

Now, I can change keys and commands but, which command can I use for XF86WebCam key?

Xeratul 04-17-2017 01:14 PM

Quote:

Originally Posted by faruk.c (Post 5698168)
Thanks for reply. I made a test with xev and my "FN+F6" key is "XF86WebCam". Now, I tried xbindkeys and here results:

When I press "j" key:
Code:

"(Scheme function)"
    m:0x10 + c:44
    Mod2 + j

When I press "d" key:
Code:

"(Scheme function)"
    m:0x10 + c:40
    Mod2 + d

And when I press FN+F6:
Code:

"(Scheme function)"
    m:0x10 + c:220
    Mod2 + XF86WebCam

Now, I can change keys and commands but, which command can I use for XF86WebCam key?

"(Scheme function)" shall be replaced by " xterm -e 'pkill mplayer ; mplayer -fs -zoom -tv device=/dev/video0 tv://' "


All times are GMT -5. The time now is 07:32 AM.