将 gstreamer 的输出重定向到 OpenCV
Redirect the output of gstreamer to OpenCV
如何将 gstreamer 的输出重定向到 OpenCV(在 linux 上使用 python),以便进行图像处理?
编辑:
这是我当前的代码:
./receive_video.py | gst-launch-1.0 fdsrc fd=0 ! h264parse ! avdec_h264 ! xvimagesink sync=false
并且我希望将制作的视频重定向到 openCV。
编辑 2:
我的代码 receive_video.py
:
#!/usr/bin/env python3
import socket
import sys
import time
def convert_2_bytes_array(x):
data=x.split(':')
my_bytes = bytearray()
for elem in data:
my_bytes.append(int(elem,16))
return my_bytes
TCP_IP="172.16.10.1"
TCP_PORT=8888
BUFFER_SIZE = 4096
try:
TCP_sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
TCP_sock.settimeout(5.0)
TCP_sock.connect((TCP_IP,TCP_PORT))
magic_word=convert_2_bytes_array("00:01:02:03:04:05:06:07:08:09:28:28")
TCP_sock.send(magic_word)
except:
exit(-1)
t0=time.time()
while(1):
try:
data = TCP_sock.recv(BUFFER_SIZE)
# sys.stdout.write(data) # python 2.7
sys.stdout.buffer.write(data) # python 3
except ValueError:
print(ValueError)
break
if time.time()-t0>1:
t0=time.time()
try:
TCP_sock.send(magic_word)
except:
exit(-1)
TCP_sock.close()
我终于明白了,这是我想出的解决方案:
pipe = sp.Popen(["receive_video.py"], shell=True, stdout=sp.PIPE, bufsize=10 ** 8)
pipe2 = sp.Popen(["gst-launch-1.0 fdsrc ! h264parse ! avdec_h264 ! filesink location=/dev/stdout"], shell=True, stdout=sp.PIPE, stdin=pipe.stdout, bufsize=10 ** 8)
然后:
while True:
raw_image = pipe2.stdout.read(720 * 1280 * 3)
image = numpy.fromstring(raw_image, dtype='uint8')
image = image.reshape(720, 1280,3)
cv2.imshow('Video', image)
说明:
来自 receive_video.py
的标准输出作为标准输入发送到 pipe2。通过应用命令 filesink location=/dev/stdout
,gstreamer 将其输出发送到 dev/stdout。此输出稍后被命令 pipe2.stdout.read
捕获,该命令转到 opencv imshow
图片有问题,如下所示:
odd image using cv2 and numpy,但是它可以工作并且可以在 opencv 中接收视频。
如何将 gstreamer 的输出重定向到 OpenCV(在 linux 上使用 python),以便进行图像处理?
编辑: 这是我当前的代码:
./receive_video.py | gst-launch-1.0 fdsrc fd=0 ! h264parse ! avdec_h264 ! xvimagesink sync=false
并且我希望将制作的视频重定向到 openCV。
编辑 2:
我的代码 receive_video.py
:
#!/usr/bin/env python3
import socket
import sys
import time
def convert_2_bytes_array(x):
data=x.split(':')
my_bytes = bytearray()
for elem in data:
my_bytes.append(int(elem,16))
return my_bytes
TCP_IP="172.16.10.1"
TCP_PORT=8888
BUFFER_SIZE = 4096
try:
TCP_sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
TCP_sock.settimeout(5.0)
TCP_sock.connect((TCP_IP,TCP_PORT))
magic_word=convert_2_bytes_array("00:01:02:03:04:05:06:07:08:09:28:28")
TCP_sock.send(magic_word)
except:
exit(-1)
t0=time.time()
while(1):
try:
data = TCP_sock.recv(BUFFER_SIZE)
# sys.stdout.write(data) # python 2.7
sys.stdout.buffer.write(data) # python 3
except ValueError:
print(ValueError)
break
if time.time()-t0>1:
t0=time.time()
try:
TCP_sock.send(magic_word)
except:
exit(-1)
TCP_sock.close()
我终于明白了,这是我想出的解决方案:
pipe = sp.Popen(["receive_video.py"], shell=True, stdout=sp.PIPE, bufsize=10 ** 8)
pipe2 = sp.Popen(["gst-launch-1.0 fdsrc ! h264parse ! avdec_h264 ! filesink location=/dev/stdout"], shell=True, stdout=sp.PIPE, stdin=pipe.stdout, bufsize=10 ** 8)
然后:
while True:
raw_image = pipe2.stdout.read(720 * 1280 * 3)
image = numpy.fromstring(raw_image, dtype='uint8')
image = image.reshape(720, 1280,3)
cv2.imshow('Video', image)
说明:
来自 receive_video.py
的标准输出作为标准输入发送到 pipe2。通过应用命令 filesink location=/dev/stdout
,gstreamer 将其输出发送到 dev/stdout。此输出稍后被命令 pipe2.stdout.read
捕获,该命令转到 opencv imshow
图片有问题,如下所示: odd image using cv2 and numpy,但是它可以工作并且可以在 opencv 中接收视频。