Reader small image

You're reading from  OpenCV for Secret Agents

Product typeBook
Published inJan 2015
Reading LevelIntermediate
PublisherPackt
ISBN-139781783287376
Edition1st Edition
Languages
Right arrow
Author (1)
Joseph Howse
Joseph Howse
author image
Joseph Howse

Joseph Howse lives in a Canadian fishing village, where he chats with his cats, crafts his books, and nurtures an orchard of hardy fruit trees. He is President of Nummist Media Corporation, which exists to support his books and to provide mentoring and consulting services, with a specialty in computer vision. On average, in 2015-2022, Joseph has written 1.4 new books or new editions per year for Packt. He also writes fiction, including an upcoming novel about the lives of a group of young people in the last days of the Soviet Union.
Read more about Joseph Howse

Right arrow

Chapter 5. Equipping Your Car with a Rearview Camera and Hazard Detection

 

Comes the morning and the headlights fade away.

 
 --The Living Daylights (1987)

James Bond is a car thief. The movies show that he has stolen many automobiles, often from innocent bystanders. We do not know whether these unfortunate people ever recovered their property, but even if they did, the damages from collisions, submersions, bullets, and rockets would have had a lasting impact on their insurance premiums. Bond has also stolen a propeller plane, a tank, and a moon buggy.

The man has been driving since the 1950s, and perhaps it is time that he stopped.

Be that as it may, we can break away from the bad, old Cold War days of indifference to collateral damage. With modern technology, we can provide a driver with timely information about others who are sharing the road. This information might make it easier to avoid collisions and to properly aim the vehicle's rocket launchers such that a chase scene can be conducted...

Planning The Living Headlights app


For this app, we will return to the cross-platform wxPython framework. Optionally, we can develop and test our wxPython application on a Windows, Mac, or Linux desktop or laptop before deploying it to our Raspberry Pi computer in the car. With the Raspbian operating system, Raspberry Pi can run wxPython just as any Linux desktop could.

The GUI for The Living Headlights includes a live video feed, a set of controls where the user can enter the true distance to the currently imaged headlights, and a label that initially displays a set of instructions, as seen in the following screenshot:

When a pair of headlights is detected, the user must perform a one-time calibration step. This consists of entering the true distance between the camera and headlights (specifically, the midpoint between the headlights) and then clicking on the Calibrate button. Thereafter, the app continuously updates and displays an estimate of the headlights' distance and color, as seen...

Detecting lights as blobs


To the human eye, light can appear both very bright and very colorful. Imagine a sunny landscape or a storefront lit by a neon sign; they are bright and colorful! However, a camera captures a range of contrast that is much narrower and not as intelligently selected such that the sunny landscape or neon-lit storefront can look washed-out. This problem of poorly controlled contrast is especially bad in cheap cameras and cameras that have small sensors, as webcams do. As a result, bright light sources tend to be imaged as big white blobs with thin rims of color. Moreover, these blobs tend to take the shape of the lens's iris, typically a polygon approximating a circle.

The thought of all lights becoming white and circular makes the world seem like a poorer place, if you ask me. Nonetheless, in computer vision, we can take advantage of such a predictable pattern. We can look for white blobs that are nearly circular, and we can infer their human-perceptible color from...

Estimating distances (a cheap approach)


Suppose we have an object sitting in front of a pinhole camera. Regardless of the distance between the camera and object, the following equation holds true:

objectSizeInImage / focalLength = objectSizeInReality / distance

We might use any unit (such as pixels) in the equation's left-hand side and any unit (such as meters) in its right-hand side. (On each side of the equation, the division cancels the unit.) Moreover, we can define the object's size based on anything linear that we can detect in the image, such as the diameter of a detected blob or the width of a detected face rectangle.

Let's rearrange the equation to illustrate that the distance to the object is inversely proportional to the object's size in the image:

distance = focalLength * objectSizeInReality / objectSizeInImage

Let's assume that the object's real size and the camera's focal length are constant. Consider the following arrangement, which isolates the pair of constants on the right...

Implementing The Living Headlights app


The Living Headlights app will use the following files:

  • LivingHeadlights.py: This contains our application class and its main function. This is a new file.

  • ColorUtils.py: This contains the utility functions to convert colors to different representations. This is a new file.

  • GeomUtils.py: This contains the utility functions for geometric calculations. Create a copy of or link to the version that we used in Chapter 3, Training a Smart Alarm to Recognize the Villain and His Cat.

  • PyInstallerUtils.py: This contains the utility functions for accessing resources in a PyInstaller application bundle. Create a copy of or link to the version that we used in Chapter 3, Training a Smart Alarm to Recognize the Villain and His Cat.

  • ResizeUtils.py: This contains the utility functions for resizing images, including camera capture dimensions. Create a copy of or link to the version that we used in Chapter 3, Training a Smart Alarm to Recognize the Villain and His Cat...

Testing The Living Headlights app at home


Do not run out onto the highway at night to point your laptop's webcam into the headlights! We can devise more convenient and safer ways to test The Living Headlights, even if you have no car or do not drive.

A pair of LED flashlights is a good proxy for a pair of headlights. A flashlight with many LEDs (for example, 19) is preferable because it creates a denser circle of light that is more likely to be detected as exactly one blob. To ensure that the distance between the two flashlights remains constant, we can attach them to a rigid object, such as a board, using brackets, clamps, or tape. Here is an image of my flashlight holder, seen from the side:

The next image shows a frontal view of the flashlight holder, including a decorative grill:

Set up the lights in front of the webcam (parallel to the webcam's lens), run the app, and make sure that the lights are being detected. Then, using a tape measure, find the distance between the webcam and the...

Testing The Living Headlights app in a car


While choosing the hardware for our setup in a car, we must consider two questions:

  • Can the car's outlets power the hardware?

  • Can the hardware fit conveniently in the car?

A Raspberry Pi draws power via its micro USB port. It needs a 5V, 700mA power source. We can satisfy this power requirement by plugging a USB adapter into the car's cigarette lighter receptacle and then connecting it to the Raspberry Pi via a USB to micro USB cable, as seen in the following image:

Note

Normally, the cigarette lighter receptacle is a 12V power source, so it can power a variety of devices via an adapter. You could even power a chain of devices, and Raspberry Pi need not be the first device in the chain. Later in this section, we will discuss the example of Raspberry Pi drawing power from a 5V outlet on an HDMIPi display, which is in turn drawing power from a cigarette lighter receptacle via an adapter.

Standard USB peripherals, such as a webcam, mouse, and keyboard, can...

Summary


This chapter has provided you an opportunity to scale down the complexity of our algorithms in order to support low-powered hardware. We have also played with colorful lights, a homemade toy car, a puzzle of adapters, and a real car!

There is much room to extend the functionality of The Living Headlights. We could take an average of multiple reference measurements or store different reference measurements for different colors of lights. Across multiple frames, we could analyze patterns of flashing colored lights to judge whether the vehicle behind us is a police car or a road maintenance truck, or is signaling to turn. We could try to detect the flash of rocket launchers, though testing it might be problematic.

The next chapter's project is not something a driver should use! We are going to amplify our perception of motion so that we can even check a person's pulse in real time!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
OpenCV for Secret Agents
Published in: Jan 2015Publisher: PacktISBN-13: 9781783287376
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Joseph Howse

Joseph Howse lives in a Canadian fishing village, where he chats with his cats, crafts his books, and nurtures an orchard of hardy fruit trees. He is President of Nummist Media Corporation, which exists to support his books and to provide mentoring and consulting services, with a specialty in computer vision. On average, in 2015-2022, Joseph has written 1.4 new books or new editions per year for Packt. He also writes fiction, including an upcoming novel about the lives of a group of young people in the last days of the Soviet Union.
Read more about Joseph Howse