pcDuino有計(jì)算能力來(lái)進(jìn)行計(jì)算機(jī)視覺,這就讓很多在Arduino 上不可能的項(xiàng)目可以在pcDuino上實(shí)現(xiàn)。 我們可以把計(jì)算機(jī)視覺這一非常強(qiáng)大的技術(shù)用在我們的項(xiàng)目中。 在這章,我們來(lái)看怎么安裝和使用SimpleCV和OpenCV。
SimpleCV的安裝和例子
在我們開始動(dòng)手之前,我們要確保手頭有一個(gè) UVC 兼容的USB攝像頭。 下面為安裝SimpleCV的步驟:
- $sudo apt-get install ipython python-opencv python-scipy python-numpy python-setuptools python-pip
- $sudo pip install https://github.com/ingenuitas/SimpleCV/zipball/master
- $sudo apt-get install python-pygame
- $sudo apt-get install python-imaging
復(fù)制代碼 在終端里面輸入 “$simpleCV" 就可以啟動(dòng)simpleCV. simpleCV是一個(gè)交互的命令解釋器。 我們可以輸入命令來(lái)執(zhí)行:
- ubuntu@ubuntu:~$ simplecv
- +-----------------------------------------------------------+
- SimpleCV 1.3.0 [interactive shell] - http://
- +-----------------------------------------------------------+
- Commands:
- "exit()" or press "Ctrl+ D" to exit the shell
- "clear" to clear the shell screen
- "tutorial" to begin the SimpleCV interactive tutorial
- "example" gives a list of examples you can run
- "forums" will launch a web browser for the help forums
- "walkthrough" will launch a web browser with a walkthrough
- Usage:
- dot complete works to show library
- for example: Image().save("/tmp/test.jpg") will dot complete
- just by touching TAB after typing Image().
- Documentation:
- help(Image), ?Image, Image?, or Image()? all do the same
- "docs" will launch webbrowser showing documentation
- SimpleCV:1> cam=Camera()
- VIDIOC_QUERYMENU: Invalid argument
- VIDIOC_QUERYMENU: Invalid argument
- VIDIOC_QUERYMENU: Invalid argument
- VIDIOC_QUERYMENU: Invalid argument
- VIDIOC_QUERYMENU: Invalid argument
- VIDIOC_QUERYMENU: Invalid argument
- VIDIOC_QUERYMENU: Invalid argument
- SimpleCV:2> img=cam.getImage()
- SimpleCV:3> img.show()
- SimpleCV:5:
復(fù)制代碼 以下為命令執(zhí)行的結(jié)果:
OpenCV的安裝和例子
OpenCV 是個(gè)開源的計(jì)算機(jī)視覺軟件包。在這里我們?cè)敿?xì)的介紹如何安裝OpenCV Python版本到pcDuino上面。 并且給出了兩個(gè)例子,一個(gè)例子用來(lái)通過(guò)USB 攝像頭來(lái)獲取圖形,另外一個(gè)例子通過(guò)OpenCV來(lái)做人臉識(shí)別。
安裝步驟:
- $ sudo apt-get -y install build-essential cmake cmake-qt-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools
- $sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils swig libv4l-0 libv4l-dev python-numpy libpython2.6 python2.6-dev libgtk2.0-dev pkg-config
- $sudo apt-get install libopencv-dev python-opencv
- $sudo apt-get install python-dev
- $sudo ln -s /usr/lib/arm-linux-gnueabihf/libjpeg.so /usr/lib
- $sudo ln -s /usr/lib/arm-linux-gnueabihf/libfreetype.so /usr/lib
- $sudo ln -s /usr/lib/arm-linux-gnueabihf/libz.so /usr/lib
- $sudo easy_install PIL
- $sudo pip install -v PIL
復(fù)制代碼 例子1: 用OpenCV通過(guò)USB攝像頭來(lái)抓圖像:
- #!/Users/brent/.virtualenvs/lumber/bin/python
- import cv
- cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
- camera_index = 0
- capture = cv.CaptureFromCAM(camera_index)
- gx = gy = 1
- grayscale = blur = canny = False
- def repeat():
- global capture #declare as globals since we are assigning to them now
- global camera_index
- global gx, gy, grayscale, canny, blur
- frame = cv.QueryFrame(capture)
- # import pdb; pdb.set_trace()
- if grayscale:
- gray = cv.CreateImage(cv.GetSize(frame), frame.depth, 1)
- cv.CvtColor(frame, gray, cv.CV_RGB2GRAY)
- frame = gray
- if blur:
- g = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, frame.channels)
- cv.Smooth(frame, g, cv.CV_GAUSSIAN, gx, gy)
- frame = g
- if grayscale and canny:
- c = cv.CreateImage(cv.GetSize(frame), frame.depth, frame.channels)
- cv.Canny(frame, c, 10, 100, 3)
- frame = c
- cv.ShowImage("w1", frame)
- c = cv.WaitKey(10)
- if c==ord('='): #in "n" key is pressed while the popup window is in focus
- gx += 2
- gy += 2
- elif c == ord('-'):
- gx = max(1, gx-2)
- gy = max(1, gy-2)
- elif c == ord('x'):
- gx += 2
- elif c == ord('X'):
- gx = max(1, gx-2)
- elif c == ord('q'):
- exit(0)
- elif c == ord('b'):
- blur = not blur
- elif c == ord('g'):
- grayscale = not grayscale
- elif c == ord('c'):
- canny = not canny
- while True:
- repeat()
復(fù)制代碼 例子二: 人臉識(shí)別
輸入圖像:
下載上面的圖像,保存為 ”opencv_in.jpg"。
輸出圖像:
- #!/usr/bin/env python
- #coding=utf-8
- import os
- from PIL import Image, ImageDraw
- import cv
- def detect_object(image):
- grayscale = cv.CreateImage((image.width, image.height), 8, 1)
- cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
- cascade = cv.Load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml")
- rect = cv.HaarDetectObjects(grayscale, cascade, cv.CreateMemStorage(), 1.1, 2,
- cv.CV_HAAR_DO_CANNY_PRUNING, (20,20))
- result = []
- for r in rect:
- result.append((r[0][0], r[0][1], r[0][0]+r[0][2], r[0][1]+r[0][3]))
- return result
- def process(infile):
- image = cv.LoadImage(infile);
- if image:
- faces = detect_object(image)
- im = Image.open(infile)
- path = os.path.abspath(infile)
- save_path = os.path.splitext(path)[0]+"_face"
- try:
- os.mkdir(save_path)
- except:
- pass
- if faces:
- draw = ImageDraw.Draw(im)
- count = 0
- for f in faces:
- count += 1
- draw.rectangle(f, outline=(255, 0, 0))
- a = im.crop(f)
- file_name = os.path.join(save_path,str(count)+".jpg")
- # print file_name
- a.save(file_name)
- drow_save_path = os.path.join(save_path,"out.jpg")
- im.save(drow_save_path, "JPEG", quality=80)
- else:
- print "Error: cannot detect faces on %s" % infile
- if __name__ == "__main__":
- process("./opencv_in.jpg")
復(fù)制代碼 保存上面的代碼到文件名為" test_face.py"的文件中。
運(yùn)行“$ python test_face.py" 來(lái)運(yùn)行。
|
|