【目的】驱动摄像头完成拍照
【实验器材】
1、鲁班猫0+wifi开发板
2、野火MIPI CSI摄像头
【软件环境】
1、ubuntu20.4.0
2、python3.10
3、opencv 4.5.0
【实现步骤】
1、安装摄像头:

连接好后,在系统里查看是否安装成功:
ls /dev/video*

2、相关工具及库安装
sudo apt update
sudo apt -y install python3-pil python3-numpy python3-pip git wget
sudo apt install libavcodec-dev libavformat-dev libswscale-dev
sudo apt install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
sudo apt-get install python3-opencv
cat@lubancat:~$ python3
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.5.4
>>>
到这里软件硬件都准备完毕了。
编写照相软件,可以在win上面用软件编写好,再上传到鲁班猫上,或者直接在鲁班猫上用vim编写。我这里先用pycharm编写好了,代码如下:
import os
import cv2
width = 640
height = 480
num = 1
cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise RuntimeError("Could not open camera.")
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow("Lubancat_Camera_test", frame)
val = cv2.waitKey(1) & 0xFF
if val == ord('s'):
print("==========================================")
cv2.imwrite("photo" + str(num) + ".jpg", frame)
print("width = ", width)
print("height = ", height)
print("success to save photo: 'pthoto'" + str(num) + ".jpg")
print("==========================================")
num += 1
if val == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
获然后上传到开发板的/home/cat/Documents/目录下:

然后打开桌面,用python3 camera_photo.py运行:

然后按下键盘上的S键就可以拍出美美照了:


拍好的照片存放在/home/cat/Documents/目录下面:

【小结】
鲁班猫非的强大呀,可以驱动4K的显示器。是我用过的最好的卡片式的电脑。
|