SunFounder picar-x
(continued from previous page)
img = frame.array
cv2.imshow("video", img) # OpenCV image show
rawCapture.truncate(0) # Release cache
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
print('quit ...')
cv2.destroyAllWindows()
camera.close()
How it works?
Photos are obtained with PiCamera. This package provides a pure Python interface to the Raspberry Pi camera.
• PiCamera Docs
Capturing an image to a file only requires specifying the name of the file to the output of whatever capture()
method was required.
from time import sleep
from picamera import PiCamera
with PiCamera() as camera:
camera.resolution = (640, 480)
camera.start_preview()
# Camera warm-up time
sleep(2)
camera.capture('foo.jpg')
This project uses the capturing timelapse sequences method. This method enables OpenCV to acquire sequential
frames.
With this method, the camera captures images continually until it is told to stop. Images are automatically given unique
names. The sleep(x) function controls the delay between captures.
from time import sleep
from picamera import PiCamera
with PiCamera() as camera:
camera.resolution = (640, 480)
camera.start_preview()
sleep(2)
for filename in camera.capture_continuous('img{counter:03d}.jpg'):
print('Captured %s' % filename)
sleep(10) # capture images with a 10s delay between each shot
In order to capture OpenCV objects, an image will be captured to Python’s in-memory stream class: BytesIO . The
BytesIO will convert the stream to a numpy array, and the program will read the array with OpenCV:
• What is Numpy?
import io
import time
import picamera
import cv2
(continues on next page)
4.7. Computer Vision 61