example.py
· 577 B · Python
Исходник
import fcntl, sys, os
from v4l2 import *
import time
import scipy.misc as misc
import numpy as np
import cv2
import signal
import sys
import streamer
st = streamer.Streamer('/dev/video1')
def signal_handler(signal, frame):
print('You pressed Ctrl+C!')
st.stop()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
imageBuffer = []
for i in range(1,9,1):
im = misc.imread("test/%s.JPG" %i)
im = misc.imresize(im, (480, 640))
imageBuffer.append(im)
st.start()
i = 0
while True:
st.updateFrame(imageBuffer[i])
time.sleep(1./5.)
i+=1
i%=8
st.stop()
| 1 | import fcntl, sys, os |
| 2 | from v4l2 import * |
| 3 | import time |
| 4 | import scipy.misc as misc |
| 5 | import numpy as np |
| 6 | import cv2 |
| 7 | import signal |
| 8 | import sys |
| 9 | |
| 10 | import streamer |
| 11 | |
| 12 | st = streamer.Streamer('/dev/video1') |
| 13 | |
| 14 | def signal_handler(signal, frame): |
| 15 | print('You pressed Ctrl+C!') |
| 16 | st.stop() |
| 17 | sys.exit(0) |
| 18 | |
| 19 | signal.signal(signal.SIGINT, signal_handler) |
| 20 | |
| 21 | imageBuffer = [] |
| 22 | for i in range(1,9,1): |
| 23 | im = misc.imread("test/%s.JPG" %i) |
| 24 | im = misc.imresize(im, (480, 640)) |
| 25 | imageBuffer.append(im) |
| 26 | |
| 27 | st.start() |
| 28 | |
| 29 | i = 0 |
| 30 | while True: |
| 31 | st.updateFrame(imageBuffer[i]) |
| 32 | time.sleep(1./5.) |
| 33 | i+=1 |
| 34 | i%=8 |
| 35 | |
| 36 | st.stop() |
streamer.py
· 1.9 KiB · Python
Исходник
#!/usr/bin/env python
import fcntl, sys, os
from v4l2 import *
from threading import Thread, Lock
import time
import numpy as np
import scipy.misc as misc
import cv2
import tools
class Streamer(Thread):
_width = 640
_height = 480
_frameRate = 30
_fmt = V4L2_PIX_FMT_YUYV
_curFrame = None
_running = False
_mutex = Lock()
def __init__(self, devName):
Thread.__init__(self)
if not os.path.exists(devName):
print "Warning: device does not exist",devName
self._device = open(devName, 'wr', 0)
capability = v4l2_capability()
print "Get capabilities result: %s" % (fcntl.ioctl(self._device, VIDIOC_QUERYCAP, capability))
print "Capabilities: %s" % hex(capability.capabilities)
print "V4l2 driver: %s" % capability.driver
format = v4l2_format()
format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT
format.fmt.pix.pixelformat = self._fmt
format.fmt.pix.width = self._width
format.fmt.pix.height = self._height
format.fmt.pix.field = V4L2_FIELD_NONE
format.fmt.pix.bytesperline = self._width * 2
format.fmt.pix.sizeimage = self._width * self._height * 2
format.fmt.pix.colorspace = V4L2_COLORSPACE_JPEG
print "Set format result: %s" % (fcntl.ioctl(self._device, VIDIOC_S_FMT, format))
self._curFrame = np.zeros((self._height, self._width, 2), dtype=np.uint8)
def updateFrame(self, frame):
frame = misc.imresize(frame, (self._height, self._width))
buff = tools.ConvertToYUYV(frame)
self._mutex.acquire()
self._curFrame = buff
self._mutex.release()
def start(self):
print "Starting Streamer on %s" % self._device
self._running = True
Thread.start(self)
def stop(self):
self._running = False
self.join()
def run(self):
while self._running:
self._mutex.acquire()
self._device.write(self._curFrame)
self._mutex.release()
time.sleep(1./self._frameRate)
| 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import fcntl, sys, os |
| 4 | from v4l2 import * |
| 5 | from threading import Thread, Lock |
| 6 | import time |
| 7 | import numpy as np |
| 8 | import scipy.misc as misc |
| 9 | import cv2 |
| 10 | import tools |
| 11 | |
| 12 | |
| 13 | class Streamer(Thread): |
| 14 | |
| 15 | _width = 640 |
| 16 | _height = 480 |
| 17 | _frameRate = 30 |
| 18 | _fmt = V4L2_PIX_FMT_YUYV |
| 19 | _curFrame = None |
| 20 | _running = False |
| 21 | _mutex = Lock() |
| 22 | |
| 23 | def __init__(self, devName): |
| 24 | Thread.__init__(self) |
| 25 | if not os.path.exists(devName): |
| 26 | print "Warning: device does not exist",devName |
| 27 | self._device = open(devName, 'wr', 0) |
| 28 | capability = v4l2_capability() |
| 29 | print "Get capabilities result: %s" % (fcntl.ioctl(self._device, VIDIOC_QUERYCAP, capability)) |
| 30 | print "Capabilities: %s" % hex(capability.capabilities) |
| 31 | print "V4l2 driver: %s" % capability.driver |
| 32 | |
| 33 | format = v4l2_format() |
| 34 | format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT |
| 35 | format.fmt.pix.pixelformat = self._fmt |
| 36 | format.fmt.pix.width = self._width |
| 37 | format.fmt.pix.height = self._height |
| 38 | format.fmt.pix.field = V4L2_FIELD_NONE |
| 39 | format.fmt.pix.bytesperline = self._width * 2 |
| 40 | format.fmt.pix.sizeimage = self._width * self._height * 2 |
| 41 | format.fmt.pix.colorspace = V4L2_COLORSPACE_JPEG |
| 42 | |
| 43 | print "Set format result: %s" % (fcntl.ioctl(self._device, VIDIOC_S_FMT, format)) |
| 44 | self._curFrame = np.zeros((self._height, self._width, 2), dtype=np.uint8) |
| 45 | |
| 46 | def updateFrame(self, frame): |
| 47 | frame = misc.imresize(frame, (self._height, self._width)) |
| 48 | buff = tools.ConvertToYUYV(frame) |
| 49 | self._mutex.acquire() |
| 50 | self._curFrame = buff |
| 51 | self._mutex.release() |
| 52 | |
| 53 | def start(self): |
| 54 | print "Starting Streamer on %s" % self._device |
| 55 | self._running = True |
| 56 | Thread.start(self) |
| 57 | |
| 58 | def stop(self): |
| 59 | self._running = False |
| 60 | self.join() |
| 61 | |
| 62 | def run(self): |
| 63 | while self._running: |
| 64 | self._mutex.acquire() |
| 65 | self._device.write(self._curFrame) |
| 66 | self._mutex.release() |
| 67 | time.sleep(1./self._frameRate) |