This week, I started to do some things with AI vision because the core of my project is face recognition. I connect the camera, and the holder with the Raspberry Pi, and this is the photo of my devices👇
First, I wanted to connect the camera to my Pi, but I had some problems with this step: I installed a package in the virtual environment, and it did not work. Ultimately, I installed this package in the globule environment and it worked well. So as a result, I can see the real-time video that the camera takes on my laptop.
Here is the code👇
import cv2
from picamera2 import Picamera2
import libcamera
def main():
picamera = Picamera2()
# 配置相机设置
config = picamera.create_preview_configuration(main={"format": 'RGB888', "size": (640, 480)},
raw={"format": "SRGGB12", "size": (1920, 1080)})
config["transform"] = libcamera.Transform(hflip=1, vflip=1) # 水平和垂直翻转
picamera.configure(config)
picamera.start()
while True:
# 捕获帧
frame = picamera.capture_array()
# 转换为 BGR 格式
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# 显示图像
cv2.imshow("Camera Output", frame)
listen_keyboard.listen_keyboard()
if __name__ == "__main__":
main()
But quickly, I realized a serious issue that people’s faces in the camera are blue, and the Chinese flag is blue too! I spent plenty of time to solve the problem and ultimately I realized that the Blue channel and the Red channel are reversed. So I added the following code to solve this issue👇
frame[..., [0, 2]] = frame[..., [2, 0]] # B 和 R 通道交换
As a result, I can see the video with the correct color👇
Then, I started to connect the holder, a machine to let the camera move.
Here is the wiring diagram👇
And here is the code I wrote👇
import cv2
from picamera2 import Picamera2
import listen_keyboard
import libcamera
def main():
picamera = Picamera2()
# 配置相机设置
config = picamera.create_preview_configuration(main={"format": 'RGB888', "size": (640, 480)},
raw={"format": "SRGGB12", "size": (1920, 1080)})
config["transform"] = libcamera.Transform(hflip=1, vflip=1) # 水平和垂直翻转
picamera.configure(config)
picamera.start()
while True:
# 捕获帧
frame = picamera.capture_array()
# 转换为 BGR 格式
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
frame[..., [0, 2]] = frame[..., [2, 0]] # B 和 R 通道交换
# 显示图像
cv2.imshow("Camera Output", frame)
listen_keyboard.listen_keyboard()
if __name__ == "__main__":
main()