Smart driving car | How to connect the camera module to the Raspberry Pi system

I'm Li Slowly.

Continue to fill in the hole.

This Ching Ming Festival was confined at home due to the epidemic, so I had to continue to move out my Raspberry Pi car to continue to overcome difficulties: image recognition. However, I haven’t entered the image recognition process in the past few days. I have been stuck for a long time purely on the hardware (camera) access system (Raspberry Pi). When the system crashes, I have to reinstall the system and configure the environment. I don’t record it. An article, I am really sorry for myself.

This article records how to use the Raspberry Pi system to access the camera module to obtain images. Details below.

Table of contents:

1. Hardware configuration

2. System Configuration

3. Simple test

4. opencv test

1. Hardware configuration

1 Raspberry Pi board: 4B

1 camera module: USB camera

1 wireless keyboard and mouse set

1 x 5" monitor

as follows:

 

After connecting the wires, it looks like this:

The protagonist this time is the following thing (USB camera module).

2. System Configuration

The Raspberry Pi system is installed with the official downloaded image, and the version is as follows.

How to judge whether the camera module is connected to the Raspberry Pi?

Enter the following content on the command line before and after the connection of the camera module to judge whether the system has detected the device. If there is more, it is the USB camera.

lsusb

If it is not detected, you need to go to the system configuration to enable the camera function, and then restart the system.

sudo raspi-config

Which one is the camera shooting program in the system?

If the system can recognize the camera hardware, you can take a picture and try it out, but which camera to use? Similarly, you need to check by plugging and unplugging the camera:

ll /dev/video*

I don’t know why I recognized two extra cameras here, video0 and video1, I don’t care about it, I just use video0.

It needs to be mentioned here that sometimes video0 will disappear after the camera image is acquired by the method described later, which makes it impossible to shoot anymore. Only restarting can solve the problem. Therefore, the following configuration needs to be performed here to solidify it. Solution: Open /etc/modules with root privileges and add a line: bcm2835-v4l2 (note, here is 4l2 not 412), then restart the Raspberry Pi.

3. Effect test - simple test

How to take a photo with a camera?

If it is an official CSI interface camera, use the following command:

raspistill -o image_name.jpg

I use a camera with a USB interface here, and use the following command:

fswebcam /dev/video0 ./img1.jpg

But before using this, the system may not have this program, you need to install it.

sudo apt-get install fswebcam

Here's the happy test:

Find the picture under this path, double-click to open it, as follows:

I can finally take pictures, which is a big step closer to my goal of doing image recognition.

How to record video with camera?

The above is to drive the camera through the program to take pictures and save them. But when actually doing image recognition, the data obtained by the camera should be video? Or in video format for playback convenience. Here, how to get the effect of the video?

Method: You can install a program [luvcview]

sudo apt-get install luvcview

Then, run this program directly, you can open a window on the desktop, and get the input from the camera in real time. Hey, happy.

luvcview

The above content is just a simple camera function test. At this point, there are actually many ways to play in the follow-up, such as using remote access cameras to capture content to achieve monitoring effects. But my purpose here is to study the image recognition function of the self-driving car, so I don't have to worry about it.

4. opencv test

Use python to get camera content:

In order to do image recognition, the traditional way is to use opencv to capture images, and then do further recognition. Based on this, some simple image acquisition tests are done below.

First, you need to install the opencv and numpy packages on the system. (The numpy package generally comes with the system, and the rest is to install the following two packages.)

sudo apt-get install libopencv-devsudo apt-get install python-opencv

After installation, run python on the command line, and import cv2, and check the version of cv2 by the way, if the following interface appears, it means success.

​​​​​​​

pythonimport cv2cv2.__version__

Note: Why is it called cv2 instead of opencv? This is because OpenCV is developed based on C/C++, and there are two versions, the ''cv' version API is developed in C language, and the ''cv2'' version API is developed based on C++ language, in order to maintain backward compatibility Sex is called "cv2", but we all know that cv2 is the original OpenCV.

The next step is to use this package for image capture testing.

Write the following code:​​​​​​​​

#-*- coding:UTF-8 -*-import cv2
img_count = 0
cap = cv2.VideoCapture(0)print("Is the camera opened?",cap.isOpened())
# set the size of image.cap.set(cv2.CAP_PROP_FRAME_WIDTH,500)cap.set(cv2.CAP_PROP_FRAME_HEIGHT,500)# create a window.cv2.namedWindow('image_win',flags=cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO | cv2.WINDOW_GUI_EXPANDED)
while(True):    ret,frame = cap.read()# ret is the status,frame is the image.if not ret:        print("can not take a image, exit...")break    cv2.imshow('image_win',frame)#print("frame: ",frame)    key = cv2.waitKey(1)#get the input from keyboardif key == ord('q'):        print("exit the program normally...")breakelif key == ord('c'):        cv2.imwrite("{}.png".format(img_count),frame)# save the image.        print("a png is saved as: {}.png".format(img_count))        img_count += 1cap.release()cv2.destroyAllWindows()#

Save the above code as [cv2_get_camera_frame.py], and run the script on the command line.

python cv2_get_camera_frame.py

After a while, a window will open to play the real-time footage captured by the camera. As follows:

Press the shortcut "c" on the keyboard to take some pictures to save, as shown below:

Through the settings in the code, you can use c to take pictures, and use q to launch the current program.

By the way, I prepared a test effect video: (published on the WeChat public account [Car Road Slowly])

alright. The system integration test of software and hardware has been completed above, and then the image recognition test can really start. See you next time.

This article is over.

Guess you like

Origin blog.csdn.net/weixin_50262060/article/details/123977015