首先安装必要的包python-zbar,
Shell
sudo apt-get install python-zbar
1
sudoapt-getinstallpython-zbar
然后安装SimpleCV:(参考这里的)
Monkey
sudo apt-get install ipython python-opencv python-scipy python-numpy python-setuptools python-pip
1
sudoapt-getinstallipythonpython-opencvpython-scipypython-numpypython-setuptoolspython-pip
Shell
sudo pip install https://github.com/sightmachine/SimpleCV/zipball/master
1
sudopipinstallhttps://github.com/sightmachine/SimpleCV/zipball/master
还有一个包svgwrite:(不安装的话,运行会出错:No module named svgwrite),同时一起安装了一个pyparsing
Shell
sudo pip install svgwrite
1
sudopipinstallsvgwrite
还要安装一个lsof,(不安装的话,运行cam = Camera()会报错:/bin/sh: 1: lsof: not found),参考:https://www.raspberrypi.org/forums/viewtopic.php?t=56103&p=460672
Shell
sudo aptitude install lsof
1
sudoaptitudeinstalllsof
上程序:
Python
import time
t0 = time.time()
from SimpleCV import *
def readFromCamera(cam):
ok = False
n=0
max_try = 50
while(ok == False and n
img = cam.getImage()
barcodes = img.findBarcode()
if type(barcodes) == FeatureSet and len(barcodes) >=1:
ok = True
img.save("1.jpg")
return barcodes[0].data, n
else:
ok = False
n = n +1
return "",n
if __name__ == "__main__":
t1 = time.time()
print t1-t0, "[firom SimpleCV import * time used]"
cam = Camera()
t2 = time.time()
print t2-t1, "[cam = Camera() time used]"
print readFromCamera(cam)
t3 = time.time()
print t3-t2, "[get image and findBarcode time used]"
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
importtime
t0=time.time()
fromSimpleCVimport*
defreadFromCamera(cam):
ok=False
n=0
max_try=50
while(ok==Falseandn
img=cam.getImage()
barcodes=img.findBarcode()
iftype(barcodes)==FeatureSetandlen(barcodes)>=1:
ok=True
img.save("1.jpg")
returnbarcodes[0].data,n
else:
ok=False
n=n+1
return"",n
if__name__=="__main__":
t1=time.time()
printt1-t0,"[firom SimpleCV import * time used]"
cam=Camera()
t2=time.time()
printt2-t1,"[cam = Camera() time used]"
printreadFromCamera(cam)
t3=time.time()
printt3-t2,"[get image and findBarcode time used]"
运行结果(手动对焦摄头):
Shell
pi@raspberrypi:~/jlink/jlink/readSN $ python sn.py
2.31966090202 [firom SimpleCV import * time used]
0.699511051178 [cam = Camera() time used]
('640522710850', 0)
0.160804033279 [get image and findBarcode time used]
1
2
3
4
5
pi@raspberrypi:~/jlink/jlink/readSN$pythonsn.py
2.31966090202[firomSimpleCVimport*timeused]
0.699511051178[cam=Camera()timeused]
('640522710850',0)
0.160804033279[getimageandfindBarcodetimeused]
运行结果(自动对焦摄头):
Shell
pi@raspberrypi:~/jlink/jlink/readSN $ python sn.py
2.35182714462 [firom SimpleCV import * time used]
1.07666397095 [cam = Camera() time used]
('640522710850', 13)
2.31718182564 [get image and findBarcode time used]
1
2
3
4
5
pi@raspberrypi:~/jlink/jlink/readSN$pythonsn.py
2.35182714462[firomSimpleCVimport*timeused]
1.07666397095[cam=Camera()timeused]
('640522710850',13)
2.31718182564[getimageandfindBarcodetimeused]
可以看到
导入SimpleCV包用掉了2.3秒,
获取手动对焦摄像头用掉0.7秒,获取自动对焦摄像头用掉1秒,
获取手动对焦图像及分析条码加起来只用了0.16秒,获取自动对焦图像及分析条码加起来用了2.3秒(循环了解14次),
所以在正常循环使用时,可以先import包,获取到摄像头,然后当需要读sn时就比较快了。
结果输出里有显示循环了几次才正确读到条码,这个0表示只用一次就读到了。这款手动对焦摄像头其实很普通,是好多年前电脑CPU还是奔腾4时代买来玩的。对好焦后一次过读取到SN。 相反一款罗技带自动对焦功能的摄像头反而要用多次才能读到,时间上就慢了一些。
反复试验焦点可以在5-14cm之间
这是焦点设在12cm时获取到的图像,条码长度占图片宽度约1/3.
参考:
使用usb webcam获取图片:
打赏
微信扫一扫,打赏作者吧~
Post Views:
5,173