LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2017, 05:53 AM   #1
faruk.c
LQ Newbie
 
Registered: Feb 2017
Posts: 14

Rep: Reputation: Disabled
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
 
Old 04-16-2017, 08:21 AM   #2
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
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; 
}
 
Old 04-16-2017, 11:04 AM   #3
faruk.c
LQ Newbie
 
Registered: Feb 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Xeratul View Post
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".
 
Old 04-16-2017, 11:11 AM   #4
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
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.

Last edited by Xeratul; 04-16-2017 at 11:14 AM.
 
Old 04-16-2017, 11:26 AM   #5
faruk.c
LQ Newbie
 
Registered: Feb 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Xeratul View Post
//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.
 
Old 04-16-2017, 11:48 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,783

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by Xeratul View Post
//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.
 
Old 04-16-2017, 12:52 PM   #7
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by faruk.c View Post
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
 
Old 04-16-2017, 02:10 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Mind you, it has nothing to do with enabling and disabling the camera-device.
 
Old 04-16-2017, 03:19 PM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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.
 
Old 04-16-2017, 05:12 PM   #10
faruk.c
LQ Newbie
 
Registered: Feb 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Xeratul View Post
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".
 
Old 04-16-2017, 05:13 PM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,231

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
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

Last edited by dugan; 04-16-2017 at 05:22 PM.
 
2 members found this post helpful.
Old 04-16-2017, 05:41 PM   #12
faruk.c
LQ Newbie
 
Registered: Feb 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
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

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)
 
Old 04-17-2017, 01:11 AM   #13
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
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
 
Old 04-17-2017, 01:09 PM   #14
faruk.c
LQ Newbie
 
Registered: Feb 2017
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Xeratul View Post
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?
 
Old 04-17-2017, 01:14 PM   #15
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by faruk.c View Post
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://' "
 
  


Reply

Tags
bash, python, webcam



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to use /dev/uinput to turn on integrated webcam vomv1988 Linux - Software 1 04-07-2013 10:33 PM
[SOLVED] Python TypeError when calling Python program from a Bash script noppeli Programming 2 01-15-2013 08:06 AM
LXer: Turn your Android Phone into a Wireless Camera using IP Webcam for Free LXer Syndicated Linux News 0 09-23-2011 07:41 PM
LXer: Howto Turn your old webcam into a motion-detecting security camera in Linux LXer Syndicated Linux News 0 12-27-2010 06:00 PM
turn off auto exposure on a uvc webcam zoran119 Slackware 7 10-22-2009 06:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration