欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Getting started with picamera

程序员文章站 2022-07-11 08:29:33
...

转:https://projects.raspberrypi.org/en/projects/getting-started-with-picamera/4

 

Connect the Camera Module

First of all, with the Pi switched off, you’ll need to connect the Camera Module to the Raspberry Pi’s camera port, then start up the Pi and ensure the software is enabled.

  • Locate the camera port and connect the camera by gently pulling up on the plastic edges, pushing in the camera ribbon and then pushing the plastic connector back into place:
  • Locate the camera port and connect the camera:
    1. Gently pull up on the edges of the plastic clip
    2. Insert the camera ribbon; make sure it is the right way round
    3. Push the plastic clip back into place

    Getting started with picamera

  • Start up the Pi.

  • Open the Raspberry Pi Configuration Tool from the main menu:

    Getting started with picamera

  • Ensure the camera software is enabled:

    Getting started with picamera

    If it’s not enabled, enable it and reboot your Pi to begin.

  • Now your camera is connected and the software is enabled, you can get started by trying out the camera preview.

  • Open Python 3 from the main menu:

    Getting started with picamera

  • Open a new file and save it as camera.py. It’s important that you do not save it as picamera.py.

  • Enter the following code:

    from picamera import PiCamera
    from time import sleep
    
    camera = PiCamera()
    
    camera.start_preview()
    sleep(10)
    camera.stop_preview()
  • Save with Ctrl + S and run with F5. The camera preview should be shown for 10 seconds, and then close. Move the camera around to preview what the camera sees.

    The live camera preview should fill the screen like so:

    Getting started with picamera

    Note that the camera preview only works when a monitor is connected to the Pi, so remote access (such as SSH and VNC) will not allow you to see the camera preview

  • If your preview was upside-down, you can rotate it with the following code:

    camera.rotation = 180
    camera.start_preview()
    sleep(10)
    camera.stop_preview()

    You can rotate the image by 90180, or 270 degrees, or you can set it to 0 to reset.

  • You can alter the transparency of the camera preview by setting an alpha level:

    from picamera import PiCamera
    from time import sleep
    
    camera = PiCamera()
    
    camera.start_preview(alpha=200)
    sleep(10)
    camera.stop_preview()

    alpha can be any value between 0 and 255.

  • The most common use for the Camera Module is taking still pictures.

  • Amend your code to reduce the sleep and add a camera.capture() line:

    camera.start_preview()
    sleep(5)
    camera.capture('/home/pi/Desktop/image.jpg')
    camera.stop_preview()

    It’s important to sleep for at least 2 seconds before capturing, to give the sensor time to set its light levels.

  • Run the code and you’ll see the camera preview open for 5 seconds before capturing a still picture. You’ll see the preview adjust to a different resolution momentarily as the picture is taken.

  • You’ll see your photo on the Desktop. Double-click the file icon to open it:

    Getting started with picamera

  • Now try adding a loop to take five pictures in a row:

    camera.start_preview()
    for i in range(5):
        sleep(5)
        camera.capture('/home/pi/Desktop/image%s.jpg' % i)
    camera.stop_preview()

    The variable i contains the current iteration number, from 0 to 4, so the images will be saved as image0.jpgimage1.jpg, and so on.

  • Run the code again and hold the camera in position. It will take one picture every five seconds.

  • Once the fifth picture is taken, the preview will close. Now look at the images on your Desktop and you’ll see five new pictures.

  • Now you’ve used the camera to take still pictures, you can move on to recording video.

  • Amend your code to replace capture() with start_recording() and stop_recording():

    camera.start_preview()
    camera.start_recording('/home/pi/video.h264')
    sleep(10)
    camera.stop_recording()
    camera.stop_preview()
  • Run the code; it will record 10 seconds of video and then close the preview.

  • To play the video, you’ll need to open a terminal window by clicking the black monitor icon in the taskbar:

    Getting started with picamera

  • Type the following command and press Enter to play the video:

    omxplayer video.h264

    Getting started with picamera

  • The video should play. It may actually play at a faster speed than what has been recorded, due to omxplayer’s fast frame rate.

  • At the beginning, you created a camera object with camera = PiCamera(). You can manipulate this camera object in order to configure its settings. The camera software provides a number of effects and other configurations you can apply. Some only apply to the preview and not the capture, others apply to the capture only, but many affect both.

  • The resolution of the capture is configurable. By default it’s set to the resolution of your monitor, but the maximum resolution is 2592 x 1944 for still photos and 1920 x 1080 for video recording. Try the following example to set the resolution to max. Note that you’ll also need to set the frame rate to 15 to enable this maximum resolution:

    camera.resolution = (2592, 1944)
    camera.framerate = 15
    camera.start_preview()
    sleep(5)
    camera.capture('/home/pi/Desktop/max.jpg')
    camera.stop_preview()
  • The minimum resolution allowed is 64 x 64. Try taking one at that resolution.

  • You can easily add text to your image with annotate_text. Try it:

    camera.start_preview()
    camera.annotate_text = "Hello world!"
    sleep(5)
    camera.capture('/home/pi/Desktop/text.jpg')
    camera.stop_preview()
  • You can alter the brightness setting, which can be set from 0 to 100. The default is 50. Try setting it to another value:

    camera.start_preview()
    camera.brightness = 70
    sleep(5)
    camera.capture('/home/pi/Desktop/bright.jpg')
    camera.stop_preview()
  • Try adjusting the brightness in a loop, and annotating the display with the current brightness level:

    camera.start_preview()
    for i in range(100):
        camera.annotate_text = "Brightness: %s" % i
        camera.brightness = i
        sleep(0.1)
    camera.stop_preview()
  • Similarly, try the same for the contrast:

    camera.start_preview()
    for i in range(100):
        camera.annotate_text = "Contrast: %s" % i
        camera.contrast = i
        sleep(0.1)
    camera.stop_preview()
  • You can set the annotation text size with the following code:

    camera.annotate_text_size = 50

    Valid sizes are 6 to 160. The default is 32.

  • You can also alter the annotation colours. First of all, ensure that Color is imported by amending your import line at the top:

    from picamera import PiCamera, Color

    Then amend the rest of your code as follows:

    camera.start_preview()
    camera.annotate_background = Color('blue')
    camera.annotate_foreground = Color('yellow')
    camera.annotate_text = " Hello world "
    sleep(5)
    camera.stop_preview()
  • You can use camera.image_effect to apply a particular image effect. The options are: nonenegativesolarizesketchdenoiseembossoilpainthatchgpenpastelwatercolorfilmblursaturationcolorswapwashedoutposterisecolorpointcolorbalancecartoondeinterlace1, and deinterlace2. The default is none. Pick one and try it out:

    camera.start_preview()
    camera.image_effect = 'colorswap'
    sleep(5)
    camera.capture('/home/pi/Desktop/colorswap.jpg')
    camera.stop_preview()
  • Try looping over the various image effects in a preview to test them out:

    camera.start_preview()
    for effect in camera.IMAGE_EFFECTS:
        camera.image_effect = effect
        camera.annotate_text = "Effect: %s" % effect
        sleep(5)
    camera.stop_preview()

    Getting started with picamera

  • You can use camera.awb_mode to set the auto white balance to a preset mode to apply a particular effect. The options are: offautosunlightcloudyshadetungstenfluorescentincandescentflash, and horizon. The default is auto. Pick one and try it out:

    camera.start_preview()
    camera.awb_mode = 'sunlight'
    sleep(5)
    camera.capture('/home/pi/Desktop/sunlight.jpg')
    camera.stop_preview()

    You can loop over the available auto white balance modes with camera.AWB_MODES.

  • You can use camera.exposure_mode to set the exposure to a preset mode to apply a particular effect. The options are: offautonightnightpreviewbacklightspotlightsportssnowbeachverylongfixedfpsantishake, and fireworks. The default is auto. Pick one and try it out:

    camera.start_preview()
    camera.exposure_mode = 'beach'
    sleep(5)
    camera.capture('/home/pi/Desktop/beach.jpg')
    camera.stop_preview()

    You can loop over the available exposure modes with camera.EXPOSURE_MODES.

  •  

  •