8. Camera use-Opencv¶
The Lubancat RK series boards support the use of common sensors such as cameras. The boards all have 24pin mipi csi interfaces. The camera used in the test is ov5648. This chapter will use the opencv-python library to use the camera and perform simple processing.
提示
If there is no special mention, the tutorials in this book are based on Python 3.8.10 version (the mirror system is Ubuntu20.04) for experiments and explanations. To learn the content of this chapter, you need a camera hardware device, please prepare by yourself.
8.1. Introduction to opencv-python library¶
OpenCV is a classic dedicated library in computer vision. It supports multi-language, cross-platform, and powerful. It was founded by Gary Bradsky in Intel in 1999 and released its first version in 2000. OpenCV now supports numerous algorithms related to computer vision and machine learning and is expanding day by day. OpenCV supports various programming languages, such as C++, Python, Java, etc., and is available on different platforms, including Windows, Linux, OS X (the system has been renamed macOS), Android, and iOS. High-speed GPU operation interfaces based on CUDA and OpenCL are also under active development.
OpenCV-Python is a Python-based library, or OpenCV’s Python application interface, designed to solve computer vision problems, combining the best features of the OpenCV C++ API and the Python language.
8.2. Add camera resource to Lubancat board¶
In this section of the experiment, the LubanCat 2 board is used as an example for demonstration. Before powering on, connect the camera to the mipi csi interface. After the system starts:
# Enter the following command in the terminal to view the camera device resources:
ls /dev/video*
Check the camera device resource example on the board, for reference only:
More camera usage references under linux system “Camera” . This chapter will introduce tools such as v4l-utils, and you can view the relevant information of the camera device.
8.4. Camera to take pictures¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import os
import cv2
# frame width and height
width = 640
height = 480
num = 1
# Create a VideoCapture object and open the system's default camera (you can also open the video or the specified device)
cap = cv2.VideoCapture(0)
# Can't open camera
if not cap.isOpened():
raise RuntimeError('Could not open camera.')
# Set frame width and height
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
while(cap.isOpened()):
# Returns two parameters, ret indicates whether it is opened normally, frame is an image array
ret,frame = cap.read()
# The window displays, named Lubancat_Camera_test
cv2.imshow("Lubancat_Camera_test", frame)
# Delay 1ms, and return the value val according to the keyboard input, which is the keyboard connected to the board
val = cv2.waitKey(1) & 0xFF
# The keyboard input 's' is captured, and the picture is saved to the current directory
if val == ord('s'):
print("=======================================")
# The first parameter is the name of the picture to be saved as, and the second parameter is the image to be saved, in jpeg format
cv2.imwrite("photo" + str(num) + ".jpg", frame)
print("width = ", width)
print("height = ", height)
print("success to save photo: "'photo' + str(num)+".jpg")
print("=======================================")
num += 1
# If the key 'q' is detected, exit, it is the keyboard connected to the board
if val == ord('q'):
break
# Release camera
cap.release()
# Close the window
cv2.destroyAllWindows()
|
Use the mirroring system with desktop, connect the keyboard and screen, run the program on the board system, and use the camera to read the above notes in detail. Run the program with the command:
sudo python3 camera_photo.py
At this time, a pop-up window will appear on the screen. When the keyboard presses “s”, the picture will be saved to the current directory, and the installation of “q” will exit the program. Run example:
8.5. Camera capture, record video¶
Each frame in the video represents an image, and we record the video by capturing a frame of image and compressing and saving it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import os
import cv2
# frame width, height, frame rate
width = 640
height = 480
fps = 25.0
# Create a VideoCapture object and open the system default camera
video = cv2.VideoCapture(0)
# Set frame width and height
video.set(cv2.CAP_PROP_FRAME_WIDTH, width)
video.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Define the writing format of the video file, the uncompressed YUV color coding type (the video file saved in this format is very large, you need to pay attention), some formats may not be supported, the encoder fails, and related libraries need to be installed
# fourcc = cv2.VideoWriter_fourcc(*'MJPG')
fourcc = cv2.VideoWriter_fourcc('I','4','2','0')
# It is used to save multiple images into video files. The first parameter is the name of the video file to be saved, and the second function is the codec code
# The third parameter is the frame rate of the saved video, and the fourth parameter is the size of the saved video file, which must be the same size as the image
# out = cv2.VideoWriter('output.mp4',fourcc, fps, (width,height))
out = cv2.VideoWriter('output.avi',fourcc, fps, (width,height))
while(video.isOpened()):
ret, frame = video.read()
if ret is True:
# window displays, named Lubancat_Camera_video
cv2.imshow('Lubancat_Camera_video',frame)
# Store the captured image, the saved video has no sound
out.write(frame)
# Delay 1ms, if the keyboard presses "q", exit, it is the keyboard connected to the board
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
print("Unable to read camera!")
break
# Release resources
video.release()
out.release()
cv2.destroyAllWindows()
|
Run the program, it will capture and record, and the video will be saved in the current directory. See the note above for the detailed process. Exit when the keyboard presses “q”, or directly interrupt the running of the program.
To play video, you can also use cv2.VideoCapture, refer to the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import cv2
# Create a VideoCapture object and select the video file to play
cap = cv2.VideoCapture('output.avi')
while(cap.isOpened()):
ret, frame = cap.read()
# Window displays, namedLubancat_Camera_video
cv2.imshow('Lubancat_Camera_video',frame)
# Delay 1ms, if the keyboard presses "q", exit, it is the keyboard connected to the board
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()
|