Beruflich Dokumente
Kultur Dokumente
Ads by Google
The flexibility of the Raspberry Pi knows no bounds, and just when
you think youve achieved everything possible, something else
comes along. This might be thanks to a great idea you or someone
else had, or inspired by a newly released piece of expansion
hardware for the device.
One of the first expansions you should buy for the Raspberry Pi is
the camera module. With a dedicated connector, the camera can
be used for a variety of tasks. Lets take a look at them.
Take A Photo
When your Pi restarts, login again, and at the prompt enter
raspistill o image.jpg
This will capture your first image, which you will be able to view in
the GUI. If youre not already using Terminal from the GUI, you
should switch to this, by using the command
startx
Subsequent commands can be run in Terminal, and the results
checked in the Raspbian file manager. You can take as many
photos as you like with this command, although note that the
filename, image.jpg, will need to be changed with each iteration of
the command, to avoid overwriting the previous image.
import picamera
camera.start_preview()
time.sleep(0)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()
Use File > Save to save your work, naming it something like
timedsnap.py. When youre ready to run the script, go to Run >
Run Module, or just tap F5.
We can use this same script with some modifications to use the
Raspberry Pi camera module for other projects.
time.sleep(0)
to
time.sleep(5)
When youre done, remember to save and press F5 to begin the
countdown. Say Cheese!
import time
import picamera
camera.start_preview()
camera.start_recording('/home/pi/Desktop/video.h264')
time.sleep(30)
camera.stop_recording()
camera.stop_preview()
Youll notice Ive set the time.sleep() value to 30, meaning the script
will start recording, wait for 30 seconds, then stop. Save this script
as videocapture.py, and press F5 to run.
Notice the use of the camera.start_recording() function. This saves
the footage as a file called video.h264, a high definition video clip
that you can open from the Raspbian desktop. The best way to do
this is to browse to the Desktop folder (or whatever your chosen
file path in the above script is), press F4 to open the terminal and
enter
omxplayer video.h264
Add a suitable battery for the Raspberry Pi and a display, and
youve got yourself a compact camcorder!
Time-Lapse Photography
Time-lapse photography has increased in popularity with the
explosion of smartphone cameras in the past few years, making
what was once the province of specialist photographers accessible
by almost everybody.
The downside of using a smartphone for that sort of photography
is obvious; it is time consuming, and hogs a resource that you
might need for, well, making and receiving phone calls. The
Raspberry Pi with its attached camera makes a good alternative,
and with a battery attached can prove just as portable and
versatile as an Android or iPhone app, and makes more sense than
just using your Pi as a time-lapse trigger for a DSLR.
Before proceeding, install ffmpeg:
import picamera
VIDEO_DAYS = 1
FRAMES_PER_HOUR = 60
def capture_frame(frame):
cam.capture('/home/pi/Desktop/frame%03d.jpg' % frame)
start = time.time()
capture_frame(frame)
time.sleep(
)
Youve created a collection of images recorded over a 60 minute
period with this script. To view the images as a film, compile the
images as follows:
omxplayer timelapse.mp4
The video will then be played full screen. It might look something
like this
Ads by Google
Of the many projects that you can build with the Raspberry Pi, one
of the most interesting and permanently useful is the motion
capture security system.
With this setup, you can keep an eye on your property from a
remote location, receiving alerts if anything is captured by the
webcam and a link to the footage if necessary.
Ads by Google
cd /etc/init.d
You can then run emacs:
#! /bin/sh
# /etc/init.d/tightvncserver
#
exit 0
Right-click to paste it into emacs. A few moments later, it should
auto-save. Exit by pressing Ctrl+X and agreeing to save the file
upon exiting.
You should then modify the permissions for the VNC Server
directory, making the new script executable:
dmesg | tail
This will output the details about your camera model, as below.
We then need to set the motion capture, so run:
emacs /etc/default/motion
and set start_motion_daemon to yes.
If you have problems with permissions here, use:
emacs /etc/motion/motion.conf
The script below (taken from a Raspberry Pi forum thread
Installation Guide for motion detection with webcam) should be
added to motion.conf:
set "daemon on"
set "minimum_frame_time 5" (this can be modified, depending how
often you want to take picture)
set "pre_capture 2"
set "post_capture 2"
set "output_normal on"
set "quality 100"
set "ffmpeg_cap_new on"
set "ffmpeg_timelapse 30"
set "ffmpeg_variable_bitrate 2"
set "get_dir /media/webcam/motion"
set "webcam_port 8080"
set "control_port 8081" (important)
set "webcam_localhost off"
set "width 320" (important)
set "height 240" (important)
Note that these settings can be configured as necessary. Those
labelled (important) should be left as they are.
tail -f /var/log/syslog
The output should confirm that the device is live and receiving
data. The final step here is to boot the VNC client on your desktop
computer and connect to your Raspberry Pi (run ipconfig to check
the IP address) using the :1 suffix, which indicates X session
number one.
Post-Positioning Checks
With the RPi in position, you should head back to your computer
and check everything is as it should be.
Begin by checking that the network connection is up and running.
Although this should have been checked earlier, your wireless
dongle may have trouble connecting in windy weather or if you
have some unusual architectural structures or materials.
With the connection established, you should also head outside and
run through several motion capture scenarios to ensure that your
movement is detected and alerted.
Finally, for the first day of testing, keep an eye on the battery use
if you have chosen a portable power source, as you will need to
know how much recharging is required. Be aware that if you have
the camera positioned indoors or relatively close to a powered
out-building that using mains power is a better solution.