不廢話直接看兩支程式吧 (imutils_camera.py)
#!/usr/bin/python from imutils.video import VideoStream import imutils import time import cv2 vs = VideoStream(usePiCamera=1).start() time.sleep(1) width = int(640) height = int(480) while True: start = time.time() # picamera videostream read frame = vs.read() frame = imutils.resize(frame, width=width) cv2.imshow("preview", frame) end = time.time() seconds = end - start print "time : {0} sec".format(round(seconds, 3)) fps = 1 / seconds; print "fps : {0}".format(round(fps, 3)); if cv2.waitKey(1) & 0xFF == ord("q"): break cv2.destroyAllWindows()使用前要先 sudo pip install imutils
另外是使用 V4L2, 程式碼(v4l2_camera.py)
#!/usr/bin/python import cv2 import time cap = cv2.VideoCapture(0) cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640) cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480) while True: start = time.time() # v4l2 read ret, frame = cap.read() cv2.imshow("preview", frame) end = time.time() seconds = end - start print "time : {0} sec".format(round(seconds, 3)) fps = 1 / seconds; print "fps : {0}".format(round(fps, 3)); if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows()使用前要先 sudo modprobe bcm2835-v4l2
這樣寫看起來是使用 picamera 反而比較慢, 如果用原型的話就可以看出還是 V4L2 比較慢
import picamera from picamera.array import PiRGBArray import time import cv2 time.sleep(1) resolution = (640, 480) with picamera.PiCamera() as camera: camera.resolution = resolution rawCapture = PiRGBArray(camera, size=resolution) stream = camera.capture_continuous(rawCapture, format="bgr", use_video_port=True) for f in stream: start = time.time() frame = f.array rawCapture.truncate(0) cv2.imshow("preview", frame) end = time.time() seconds = end - start print "time : {0} sec".format(round(seconds, 3)) fps = 1 / seconds; print "fps : {0}".format(round(fps, 3)); if cv2.waitKey(1) & 0xFF == ord("q"): break
沒有留言:
張貼留言