问题描述
我正在尝试使用opencv捕获rtsp流,但是它抱怨cv2.COLOR_BGR2RGB。 我已经尝试过流式处理其他网络摄像头,但效果不佳,但不能与rtsp一起使用。它自己的rtsp流正在运行,并已通过vlc正常测试。本身的rtsp流是h264格式。
但是,如果我在if语句的行中将其注释为false,因为该框架为空!
import os
import cv2
import time
import argparse
import multiprocessing
import numpy as np
import tensorflow as tf
from utils.app_utils import FPS,WebcamVideoStream,HLSVideoStream
from multiprocessing import Queue,Pool
CWD_PATH = os.getcwd()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-str','--stream',dest="stream",action='store',type=str,default=None)
parser.add_argument('-src','--source',dest='video_source',type=int,default=0,help='Device index of the camera.')
parser.add_argument('-wd','--width',dest='width',default=640,help='Width of the frames in the video stream.')
parser.add_argument('-ht','--height',dest='height',default=480,help='Height of the frames in the video stream.')
parser.add_argument('-num-w','--num-workers',dest='num_workers',default=2,help='Number of workers.')
parser.add_argument('-q-size','--queue-size',dest='queue_size',default=5,help='Size of the queue.')
args = parser.parse_args()
logger = multiprocessing.log_to_stderr()
logger.setLevel(multiprocessing.SUBDEBUG)
input_q = Queue(maxsize=args.queue_size)
output_q = Queue(maxsize=args.queue_size)
print('Reading from webcam.')
#video_capture = WebcamVideoStream(src="rtsp://ip:port/stream",# width=args.width,# height=args.height).start()
frame_width = args.width
frame_height = args.height
print('Width:',frame_width,'Height:',frame_height)
out = cv2.VideoWriter('output_test.avi',cv2.VideoWriter_fourcc('m','p','4','v'),10,(frame_width,frame_height))
vcap = cv2.VideoCapture("rtsp://ip:port/stream",cv2.CAP_FFMPEG)
while True:
ret,frame = vcap.read()
#if ret == False:
# break
# Our operations on the frame come here
frame_rgb = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
output_q.put(frame_rgb)
# Convert and store to out file
#output_rgb = cv2.cvtColor(output_q.get(),cv2.COLOR_RGB2BGR)
#out.write(output_rgb)
#cv2.imshow('VIDEO',output_rgb)
# display the resulting frame
cv2.imshow('frame',frame_rgb)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#video_capture.stop()
out.release()
cv2.destroyAllWindows()
哪个给了我一个问题(没有if语句):
[DEBUG/MainProcess] Queue._after_fork()
Reading from webcam.
Width: 640 Height: 480
Traceback (most recent call last):
File "testRTSP_try.py",line 52,in <module>
frame_rgb = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion Failed) !_src.empty() in function 'cv::cvtColor'
[INFO/MainProcess] process shutting down
更多信息:
- 相机镜头来自树莓派 通过以下方式创建的
- RTSP流:
raspivid -o - -t 0 -rot 180 -w 640 -h 480 -fps 25 -b 2000000 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:<port>/stream}' :demux=h264
- 通过Windows vlc监视的RTSP流
我通过使用运动采取了另一种方法,而不是参见How to Make Raspberry Pi Webcam Server and Stream Live Video || Motion + Webcam + Raspberry Pi
这将设置http流而不是rtsp!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)