© Joseph Coburn 2020
J. CoburnBuild Your Own Car Dashboard with a Raspberry Pihttps://doi.org/10.1007/978-1-4842-6080-7_11

11. Reversing Camera

Chapter goal: Configure the Pi Camera to act as a reversing camera. Stream this video feed to the application.
Joseph Coburn1 
(1)
Alford, UK
 

For this project, you’ll connect the Pi Camera to the Pi and stream the video feed to a web page. You’ll learn how to configure the camera and settings and get the best possible image quality. In later chapters, you’ll use this camera in conjunction with several of the other reversing sensors to build a complete reversing module, which automatically kicks in when you engage the reverse gear.

The Pi Camera is unlike other cameras you may have used. There is no button to take a picture, and there’s no memory card to save photos to. There’s no battery, and it’s a fragile looking circuit which won’t survive one trip to the beach. This Pi Camera is a camera module . It connects to the Pi and relays camera data for the Pi to process. The camera module handles reading the sensor stream, streaming data, adjusting light, and more, but the Pi has to tell it what to do and perform some processing of its own, to convert the raw stream into usable image formats.

It’s possible to complete this project with a USB webcam (and many of the steps remain the same), but due to the vast number of different makes, models, styles, and price tags of USB webcams, the number of variations and slight changes needed for every model would not be possible to cover in a sensible number of chapters.
../images/488914_1_En_11_Chapter/488914_1_En_11_Fig1_HTML.jpg
Figure 11-1

The Pi Camera V2.1

For this project, you’ll need to purchase the official Raspberry Pi V2 camera module, shown in Figure 11-1. Introduced in April 2016, this module is developed and built by the Raspberry Pi Foundation. It costs roughly $25 and is available on Amazon, or from any other store which sells Raspberry Pis. Similar clones of the camera module are available, as are the earlier V1 versions, but for the simplest experience following this chapter, you’ll have less issues with the V2 model.

Camera Connection

Always turn off and disconnect the Pi’s power supply when working with electronic circuits.

The Pi Camera connects to the Pi using a ribbon cable and the camera serial interface , or CSI port. This thin cable is included with the camera. Not only does it allow the Pi to communicate with the camera, but it also supplies power to the camera. If you’ve been using an inadequate power supply for your Pi, then the increased current draw required by the camera may mandate the need for a suitable power supply. The 3A supply specified in the Project Overview – Equipment List is sufficient for both the Pi and the camera.
../images/488914_1_En_11_Chapter/488914_1_En_11_Fig2_HTML.jpg
Figure 11-2

Pi Camera ribbon cable installed on the Pi

With the exception of the Pi Zero, every single Pi has the camera connection port. This proprietary connection is cheap to manufacture, although it’s not very durable. It’s not so fragile that you should be afraid of using it, or using the Pi or connecting other peripherals or components to your GPIO ports; just don’t go overboard by stressing the connector.

Begin by powering down the Pi and removing the power supply. The camera module comes with the ribbon cable already connected – you only need to connect the other end to the Pi itself – shown in Figure 11-2. Locate the camera module port, which is a narrow, 1-inch long connector behind the LAN port. Gently pull the connector’s plastic cover up, roughly 1/8th of an inch. It won’t fully detach.
../images/488914_1_En_11_Chapter/488914_1_En_11_Fig3_HTML.jpg
Figure 11-3

Pi Camera ribbon cable installed on the Pi

Now gently insert the ribbon cable, with the exposed “pins” facing away from the Pi’s I/O section, toward the HDMI and power connections – as shown in Figure 11-3. Don’t force it. If you’re struggling, double-check your cable is the correct orientation and that you’re inserting it square on, and not at an angle. Once seated 1/8th of an inch, gently push the plastic cover back down, clamping down on the cable. Visually inspect the cable, and gently tug it, to ensure it’s seated properly. When ready, apply the power and boot up the Pi.

Pi Camera First Time Configuration

Connect to the Pi over SSH. To begin using the camera, you need to tell the Pi it’s connected. You can do this with the raspi-config utility. The output from this command is shown in Figure 11-4:
sudo raspi-config
../images/488914_1_En_11_Chapter/488914_1_En_11_Fig4_HTML.jpg
Figure 11-4

The Pi configuration utility menu

Use the arrow and enter keys to navigate around this tool. Scroll down and choose Interfacing Options . Figure 11-5 shows the Interfacing Options menu. Choose P1 Camera, and select yes when asked to enable the Pi Camera. Choose OK when the Pi confirms the camera is enabled. Now use your right arrow key to select finish, and choose yes when the Pi asks to restart.
../images/488914_1_En_11_Chapter/488914_1_En_11_Fig5_HTML.jpg
Figure 11-5

Interfacing Options menu of the Pi configuration utility

Once restarted, you can test your camera. The raspistill utility is a command-line tool to work with the Pi Camera, and it’s included with your Raspbian install. Use it to save a test photo to your Pi’s desktop, called “test_photo.jpg”:
raspistill -o Desktop/test_photo.jpg
You can inspect the contents of this photo file using the cat command:
cat Desktop/test_photo.jpg
This command produces nonsense. It dumps the contents of the file to your terminal. As it’s a photo, the contents are not very readable. To actually see the photo, use SCP to pull the photo from the Pi to your computer over SSH:
scp [email protected]:Desktop/test_photo.jpg /Users/coburn/Desktop/
SCP stands for secure copy . It lets you copy files from one remote location to another. This command is split into three sections. Begin with the scp keyword. This invokes the secure copy tool. The next section is the remote address and file location – separated by a colon. This looks for the file in location Desktop/test_photo.jpg on the remote [email protected] – your IP address will vary, or you could use your pi-car alias here. Finally, specify the location on your computer to put the file, which is /Users/coburn/Desktop/ on my computer – put yours wherever you like.
../images/488914_1_En_11_Chapter/488914_1_En_11_Fig6_HTML.jpg
Figure 11-6

Early, uncorrected Pi Camera photo

Open your photo and look at it. It may not be perfect – my photo is blurry and upside down and visible in Figure 11-6. For now, providing that you have something, then that will do. You’ll improve the quality and make it usable later on, but right now, this test is about ensuring the module works properly. If you can’t copy the file, double-check your scp command. Does the file exist at all on the Pi? Was there a problem raspistill raised saving the photo at all? Double-check your work and, if necessary, your wiring.

Camera Live Stream Configuration

To work with this camera feed, the Pi will read in the camera data, interpret it, process it, and stream it to the network over its TCP port. This section isn’t configured in Python. There are many Python libraries available, but a solution which is both simple to configure and that works reliably (or can be defensively programmed around) is difficult to achieve in Python.

For this section, you’ll use the Motion (https://motion-project.github.io/) library to handle the stream. This is a Debian package for working with the Pi Camera. It’s mainly designed around motion detection and video recording (which would be an interesting extension project), yet it has excellent streaming capabilities, and is configured with a config file. It can run as a daemon, so the Pi can configure the stream itself when it boots.

Update the Pi’s package list, and install Motion onto the Pi itself as a package:
sudo apt-get update
sudo apt-get install motion
Configure the Motion daemon – this allows motion to start when the Pi boots. Edit the motion file:
sudo nano /etc/default/motion
Change the start_motion_daemon option to yes, such that the whole file looks like this:
# Set to 'yes' to enable the motion daemon
start_motion_daemon=yes
Save and exit this file. Modify the file so it’s executable by all users, and restart the Pi:
sudo chmod 777 /etc/default/motion
sudo reboot
When the Pi restarts, Motion will automatically start and begin streaming. Use the service command to check on the status:
sudo service motion status
You can also use this command to stop, start, or restart services such as motion, which you’ll do shortly:
sudo service motion start
sudo service motion stop
sudo service motion restart
Motion is now running, but you can’t see it. You can see evidence in the motion logs:
cat /var/log/motion/motion.log
This is good, but application logs are no substitute for a camera feed – they won’t stop you reversing into a tree! To configure motion further, edit the motion.conf file, which defines all the possible settings for motion:
sudo nano /etc/motion/motion.conf
There are lots of options here, the vast majority of which you can safely ignore and leave as is. Table 11-1 covers the core features you need to change and what they do.
Table 11-1

Motion config changes required

Option

Value

Description

Full config line

width

800

Width of the video in pixels.

width 800

height

480

Height of the video in pixels.

height 480

framerate

30

Number of frames (individual images) to stream per second.

framerate 30

stream_quality

100

Percentage quality of video stream.

stream_quality 100

stream_localhost

off

Restrict the video stream to the Pi only, or allow access over the network.

stream_localhost off

ffmpeg_output_movies

off

Record videos to the Pi’s memory card in addition to streaming.

ffmpeg_output_movies off

stream_maxrate

30

The maximum framerate to stream.

stream_maxrate 30

rotate

180

Rotate the video feed in degrees, only works in increments of 90.

rotate 180

When configured, save and exit the config file, and restart the motion service:
sudo service motion restart

Now load the Pi in your web browser, and use Motion’s default streaming port of 8081:

http://pi-car:8081/

Verify that you see the feed from the camera. Move around and observe as it changes. Note the timestamp in the bottom-right corner of the video. The stream is now ready to pull into your Flask application. You may need to revisit these configuration settings as the project progresses. Perhaps when you install the project into your car, the camera is rotated upside down, or you need to change the quality. Figure 11-7 shows the rotated image stream, complete with timestamp in the lower-right corner.
../images/488914_1_En_11_Chapter/488914_1_En_11_Fig7_HTML.jpg
Figure 11-7

Rotated and refined Pi Camera photo

Pi Camera Focus Adjustment

Like any camera, the Pi Camera V2 has a lens with adjustable focus. There’s no autofocus, it’s purely manual – a mechanical lens adjustment. If your video feed is blurry, and providing you haven’t increased the output dimensions to huge numbers (which will reduce the quality), then you may need to adjust the lens focus.

All cameras use a plane of focus (PoF). This imaginary line defines what is and is not in focus at a given time. Adjusting the focus on a lens moves this plane forward or backward. Everything parallel to the lens at a given distance is in focus, and moving nearer or further from or to the lens gets increasingly out of focus. An object 1M away could be in focus, and something right next to the lens would be out of focus. For these reasons, you won’t be able to get everything the camera sees in focus – it will be a compromise, which may need adjusting once the camera is mounted on your car.

To adjust the Pi’s focus, identify the sensor and lens module on the front of the Pi Camera module. The small black square in the middle of the module houses the lens. Within this square, the lens features a hexagonal casing, known as the lens barrel . By rotating this barrel, you can adjust the plane of focus.
../images/488914_1_En_11_Chapter/488914_1_En_11_Fig8_HTML.jpg
Figure 11-8

Tools needed for Pi Camera lens focus adjustment

Use a small pair of pliers to grip the outer lens housing – shown in Figure 11-8. Use a small screwdriver to nudge the lens barrel, and use the indentation to grip your tool. Be very careful not to apply too much pressure. Slipping here could damage the lens itself or risk pulling the entire lens assembly away from the module itself. Treat this lens and this maintenance step as you would a pair of glasses, or when putting contact lenses in.

Remember, this focus will need adjusting when the camera is installed in your car.

Camera Flask Code

For this code section, you’ll create a new Flask route to serve the image. Later on, you’ll expand this route to make use of the reversing sensors, but for now, it simply serves the camera feed. This is a simple code change – the Pi is already handling the hard work of the video.

Create the files needed for this section:
touch Pi_Car/reverse.py
mkdir Pi_Car/templates
touch Pi_Car/templates/reverse.html

The reverse.py file is the new route for this reversing screen. The template file reverse.html is the markup to render this stream. It lives inside the templates folder, which is a Flask pattern for storing markup files.

Here’s the code you need for reverse.py:
from flask import Blueprint, render_template
from flask import current_app as app
reverse_blueprint = Blueprint("reverse", __name__)
@reverse_blueprint.route("/reverse/")
def reverse():
    app.logger.info("Starting to load reverse route")
    stream_port = app.config.get("STREAM_PORT", 8081)
    host = "pi-car"
    stream_url = f"http://{host}:{stream_port}"
    app.logger.debug(f"Camera streaming url: {stream_url}")
    app.logger.info("Finished loading reverse route")
    return render_template(template_name_or_list="reverse.html", base_url=stream_url)

This is a new blueprint file. It defines a new route /reverse/ and handles the logic to connect to the stream. There’s lots of logging (as is standard across this project). This file uses the render_template function from Flask. This function renders the contents of a template to the browser – reverse.html. Notice how you don’t need to specify the templates folder, as Flask already knows this is where to look for templates. By using a render template, you can keep your code neater, as all the HTML and markup-specific logic lives in the template itself, ready for reuse by any of your application logic.

You can also pass data to templates. This route passes the stream_url to the template. This stream URL is where the browser can find the video feed. It’s a Fully Qualified Domain Name , or FQDN. It consists of the protocol (http), host (pi-car), and stream port. Notice how the stream port is pulled from the config even though it does not exist. This defensive logic falls back to the 8081 stream port if the config is not set. This lets you easily change the port in the future, just by modifying the config file. You don’t have to dig through the code, which could use this port in numerous locations in a bigger code base.

Here’s the code you need for the reverse.html template:
<img src="{{ base_url }}">
This isn’t valid HTML, but I cover that in detail in the tidying up chapter toward the end of this book. For now, it’s sufficient. This tag tells the browser to render an image. It points the browser toward the live stream URL you built in the reverse.py file. Notice the dual curly braces around the variable:
{{ base_url }}

This is Jinja, the templating language included with Flask. This renders the value “http://pi-car:8081”, as specified in your back-end logic.

Finally, inside app.py, import your new blueprint:
from .reverse import reverse_blueprint
and then register the route:
app.register_blueprint(reverse_blueprint)
Your whole app.py file now looks like this:
import logging
from flask import Flask
from logging.handlers import RotatingFileHandler
from .data import data_blueprint
from .reverse import reverse_blueprint
def create_app(config_file="config/local_config.py"):
    app = Flask(__name__)  # Initialize app
    app.config.from_pyfile(config_file, silent=False)  # Read in config from file
    if app.config.get("FILE_LOGGING"):
        # Configure file-based log handler
        log_file_handler = RotatingFileHandler(
            filename=app.config.get("LOG_FILE_NAME", "config/pi-car.log"),
            maxBytes=10000000,
            backupCount=4,
        )
        log_file_handler.setFormatter(
            logging.Formatter("[%(asctime)s] %(levelname)s in %(module)s: %(message)s")
        )
        app.logger.addHandler(log_file_handler)
    app.logger.setLevel(app.config.get("LOGGER_LEVEL", "ERROR"))
    app.logger.info("----- STARTING APP ------")
    app.register_blueprint(data_blueprint)
    app.register_blueprint(reverse_blueprint)
    app.logger.info("----- FINISHED STARTING APP -----")
    return app
Start Flask on your computer, and visit the new URL route at http://127.0.0.1:5000/reverse/. Providing the Pi is on and the Motion service is streaming the video feed, you’ll see the real-time video. Commit your changes, and perform a build on the Pi. Visit the Pi’s URL and verify the route also works there at http://pi-car:5000/reverse/ (shown in Figure 11-9).
../images/488914_1_En_11_Chapter/488914_1_En_11_Fig9_HTML.jpg
Figure 11-9

Flask application streaming the live video feed

Notice how you’re using port 5000 to access the application, but if you look at the code rendered by the reverse route (by right-clicking and choosing inspect to bring up your console developer tools), you’ll see the stream is coming from http://pi-car:8081. This is due to ports, as discussed in earlier chapters. The application runs on port 5000, but the stream runs on port 8081. You don’t need to merge or combine the two – they are independent data feeds coming out of the Pi.

Chapter Summary

This chapter introduced to you the Pi Camera module. You learned how to connect it to the Pi and fine-tune the settings to achieve the best possible quality. You configured the Pi to stream the camera feed over the network and created a basic template to pull this stream into your application. This is the basis for your entire reversing module, which you’ll continue to expand upon later on.

The next chapter shows you how to develop a reverse beeper, to indicate the presence of a rear obstacle.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.5.239