1.
打开终端,更新树莓派,将软件源更新一遍
sudo apt-get update
更新系统,将已安装的软件更新到最新版
sudo apt-get upgrade
这是带摄像头的树莓派。
开启摄像头
sudo raspi-config
选择Interfacing option,enter后进入
将camera设为enable。退出后Reboot树莓派。
2.
安装我们要用的包库。
sudo apt-get install python-imaging
sudo apt-get install python-zbar
sudo apt-get install python-opencv
sudo apt-get install libcv-dev
sudo apt-get install build-essential
3.
树莓派专用CSI摄像头插到树莓派的CSI口上并在在raspi-config中打开后就可以使用raspistill命令直接使用,但如果在OpenCV中调用CSI摄像头会出现无数据的现象(cv2.VideoCapture(0)这时不会报错)。 这是因为树莓派中的camera module是放在/boot/目录中以固件形式加载的,不是一个标准的V4L2的摄像头驱动,所以加载起来之后会找不到/dev/video0的设备节点。我们在/etc/modules里面添加一行bcm2835-v4l2(小写的L)就能解决问题。
sudo nano /etc/modules
之后进入这个界面
加一行bcm2835-v4l2,ctrl+O 然后回车保存 ,ctrl+X退出。Reboot一下。
用下面命令行查看一下有没有video0的设备。
ls /dev/
4.
运行python文件——camera.py
python /home/pi/camera.py
其中详细代码为
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
# import the necessary packages
import cv2
import numpy as np
import zbar
from PIL import Image
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
font=cv2.FONT_HERSHEY_SIMPLEX
camera=cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
grabbed, frame = camera.read()
if not grabbed:
break
pil= Image.fromarray(frame).convert('L')
width, height = pil.size
raw = pil.tobytes()
zarimage = zbar.Image(width, height, 'Y800', raw)
scanner.scan(zarimage)
for symbol in zarimage:
if not symbol.count:
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
cv2.putText(frame,symbol.data,(20,100),font,1,(0,255,0),4)
# do something useful with results...
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
camera.release()
cv2.destroyAllWindows()
5.
结果,正确识别。
参考:
https://blog.youkuaiyun.com/x115104/article/details/78878599