Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

ztk-webcam snapshot-grabber

by zentara (Archbishop)
on Jul 18, 2011 at 15:10 UTC ( [id://915202]=CUFP: print w/replies, xml ) Need Help??

I can't believe how hard it is to find a simple digital webcam snapshot program. Here is one. :-) It is a Tk frontend to mplayer, run in slave mode. screenshot

Remember, for best results, look at the camera. :-)

#!/usr/bin/perl -w use strict; use Tk; use Proc::Killfam; use Tk::Spinbox; $|++; # by zentara of perlmonks, July 18, 2011 # this is free to use and distribute # I wrote this because I wanted just a simple # webcam grab for my built-in digital camera # each snap shot will be deposited in the pwd as shot0001.png, shot000 +2.png, etc # you can run mplayer by itself to do the same thing, # but remembering all the key options and commandline stuff # is a mind blower :-) #put your preferred settings here #my($width,$height) = (640,480); my($width,$height) = (800,600); # defaults are normally good my $device = '/dev/video0'; my $brightness = 0; # range -100 to 100 my $contrast = 0; my $fps = 30; #default frames per second my $snaps_taken = 0; #counts number of snapshots taken $SIG{INT} = sub { &close_it_up }; $SIG{PIPE} = 'IGNORE'; my $mw = MainWindow->new(-background =>'black'); $mw->Tk::bind("<q>", sub{&close_it_up}); $mw->Tk::bind("<Escape>", sub{&close_it_up}); $mw->protocol('WM_DELETE_WINDOW' => sub {&close_it_up}); $mw->fontCreate('big', -family=>'courier', -weight=>'bold', -size=>int(-18*18/14)); my $cframe1 = $mw->Frame(-background =>'black') ->pack( -fill =>'x'); my $cframe2 = $mw->Frame(-background =>'black') ->pack( -fill =>'x'); my $canv = $mw->Scrolled('Canvas', -bg => 'black', -borderwidth => 0, -highlightthickness => 0, -relief => 'sunken', -width => $width, -height => $height, -scrollregion=>[0,0,$width,$height], + -scrollbars=>'osoe', )->pack(); ## this Frame is needed for including the window in Tk::Canvas my $Container = $canv->Frame(-container => 1); my $xtid = $Container->id(); # converting the id from HEX to decimal as xterm requires a decimal Id my ($xtId) = sprintf hex $xtid; my $dcontitem = $canv->createWindow(10,10, -anchor=>'nw', -window => $Container, -width => $width, -height => $height, -state => 'hidden', -tags => ['viewport'], ); my $pid; my @options = ( '-slave','-loop 0', '-zoom', "-x $width", "-y $height", '-really-quiet', "-wid $xtId", "-vf screenshot", "tv:// -tv driver=v4l2:device=/dev/video0:input=1:brightness=$brightn +ess:amode=1:fps=$fps:width=$width:height=$height", ); my $loadl_but = $cframe1->Button(-text => 'Snapshot', -background => 'black', -foreground => 'hotpink', -activebackground => 'lightyellow', -padx => 0, -command => \&grab1, -font => 'big' )->pack(-side =>'left',-padx=>10 ); $cframe1->Button(-text => "Exit", -background => 'black', -foreground => 'hotpink', -activebackground => 'lightyellow', -padx => 0, -command => [sub{&close_it_up}], -font => 'big' )->pack(-side=>'right', -padx =>10 ); $cframe1->Label(-text=>" Brightness->", -background => 'black', -foreground => 'lightgreen' )->pack (-side=>'left'); my $sp1 = $cframe1->Spinbox(-width => 4, -font => 'big', -background => 'black', -buttonbackground => 'black', -foreground => 'lightgreen', -command => sub{ &set_brightness }, -from => -100, -to => 100, -textvariable => \$brightness, )->pack (-side=>'left'); $cframe1->Label(-text=>" Contrast->", -background => 'black', -foreground => 'lightgreen' )->pack (-side=>'left'); my $sp2 = $cframe1->Spinbox(-width => 4, -font => 'big', -background => 'black', -foreground => 'lightgreen', -buttonbackground => 'black', -command => sub{ &set_contrast }, -from => -100, -to => 100, -textvariable => \$contrast, )->pack (-side=>'left'); $cframe1->Label(-text=>" Snapshots taken->", -background => 'black', -foreground => 'lightgreen' )->pack (-side=>'left'); $cframe1->Entry(-textvariable => \$snaps_taken, -background => 'lightyellow', -font => 'big', -width => 2 )->pack( -side => 'left'); $cframe2->Label(-text => 'Hit SpaceBar to take snapshot q o +r esc to exit ', -background => 'black', -foreground => 'lightgreen', -font => 'big' )->pack(-expand=>1 ); $mw->Tk::bind("<space>", \&grab1); &start_player; MainLoop(); ######################################################### ############################################################## sub start_player{ $pid = open(MP, "| mplayer @options >/dev/null 2>&1 "); $canv->itemconfigure($dcontitem,-state => 'normal'); } ############################################################## sub set_brightness{ #print "$brightness\n"; syswrite(MP, "brightness $brightness 1\n"); } ############################################################## sub set_contrast{ # print "$contrast\n"; syswrite(MP, "contrast $contrast 1\n"); } ############################################################## sub grab1{ syswrite(MP, "screenshot 0\n"); $snaps_taken++; } ############################################################# sub stop{ syswrite(MP, "quit\n"); close MP; $canv->itemconfigure($dcontitem,-state => 'hidden'); killfam 9, $pid; } ################################################################# sub close_it_up{ &stop; exit; } ################################################################### __END__

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re: ztk-webcam snapshot-grabber
by ambrus (Abbot) on Jul 18, 2011 at 20:41 UTC

      Would you explain what "$|++" meaning is in the code? Thanks

        Would you explain what "$|++" meaning is in the code? Thanks

        Its like $foo++ except working on the variable $| , a reall shell-ish (or hack-ish) way to turn autoflush on, as in

        use IO::Handle; print 1; # doesn't print, puts 1 in the buffer STDOUT->autoflush(1) print 1; # prints 1 immediately (and the previously buffered 1) print "\n2\n"; # \n forces a flush
        Tutorials: Input and Output: Suffering from Buffering?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://915202]
Approved by luis.roca
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-20 07:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found