8 Keyboard-controlled camera

 

This chapter covers

  • Capturing images and streaming live videos from the camera
  • Drawing shapes and writing text using the OpenCV library
  • Moving servo motors to specific angles and positions
  • Using keyboard events in OpenCV to make servo movements

In this chapter, we will build a robot with a camera attached to two servo motors. One servo will allow us to pan the camera, and the other will apply a tilt motion. In this way, we can point the camera in many different directions. In addition, we will detect and use keyboard events to move the motors in different directions and capture and save photos from the live camera stream.

We have explored the use of touch screens on mobile devices and joysticks as human interaction devices. Now we will use a keyboard to control our robot movements and camera. Keyboards are one of the oldest and most established input devices. Compared to joysticks, they come with every desktop computer and laptop and have excellent built-in support in most software, not requiring much additional effort. This makes them an excellent alternative to joysticks when we either don’t want to add additional hardware requirements or just want to simplify our device handling.

8.1 Hardware stack

Figure 8.1 shows the hardware stack, with the specific components used in this chapter highlighted. The camera can be directly connected to the Raspberry Pi using the camera connector. The camera used in this chapter is the Raspberry Pi Camera Module 2.

8.2 Software stack

8.3 Capturing images using OpenCV

8.3.1 Exploring the OpenCV library

8.3.2 Drawing shapes and displaying text with OpenCV

8.3.3 Taking snapshots with OpenCV

8.4 Moving a camera with servos

8.4.1 Exploring the servo motors with the CRICKIT library

8.4.2 Performing a pan-and-tilt sweep

8.4.3 Controlling servos and the camera together

Summary