如何使用我从nginx服务器获得的视频数据流



我的网络中有三个节点:数据服务器--node1-node2。我的视频数据";friends.mp4";保存在dataServer上。我将dataServer和node2都作为rtmp-nginx服务器启动。我在node1上使用ffmpeg来拉取dataServer上的数据流,并将转换后的数据流推送到应用程序";活的";在节点2上。以下是我为node2配置的nginx.conf。

worker_processes  1;
events {
worker_connections  1024;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
application play {
play /usr/local/nginx/html/play;
}
application hls {
live on;
hls on;
hls_path /usr/local/nginx/html/hls;
hls_fragment 1s;
hls_playlist_length 4s;
}
application live  
{
live on; 
allow play all;
}
}
}

我想运行这个python代码来识别friends.mp4中的人脸:进口cv2

vid_capture=cv2.VideoCapture("rtmp://127.0.0.1:1935/live")
face_detect = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
if (vid_capture.isOpened() == False):
print("Error opening the video file")
else:
fps = vid_capture.get(5)
print("Frames per second : ", fps,'FPS')
frame_count = vid_capture.get(7)
print('Frame count : ', frame_count)
while(vid_capture.isOpened()):
ret, frame = vid_capture.read()
if ret == True:
gray = cv2.cvtColor(frame, code=cv2.COLOR_BGR2GRAY)
face_zone = face_detect.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
for x, y, w, h in face_zone:
cv2.rectangle(frame, pt1 = (x, y), pt2 = (x+w, y+h), color = [0,0,255], thickness=2)
cv2.circle(frame, center = (x + w//2, y + h//2), radius = w//2, color = [0,255,0], thickness = 2)
cv2.imshow('Frame', frame)
key = cv2.waitKey(50)
if key == ord('q'):
break
else:
break
vid_capture.release()
cv2.destoryAllWindows()

但我做不到,因为cv2.VideoCapture无法从";rtmp://127.0.0.1:1935/live"。也许是因为这个路径不是一个文件。如何获取nginx服务器接收到的视频流,并将其放入我的openCV模型中?有没有一种方法可以让我访问niginx服务器接收到的dataStreaming,并使其成为openCV可以使用的python对象?

尝试将文件更改为实时流,然后使用cv2处理流:

DataServer --> Node1(FFmpeg MP4 to RTMP) --> Node2(Media Server)
Node2 ---> Node1(cv2 process RTMP)

对于Node1,您可以运行以下命令:

ffmpeg -re -i friends.mp4 -c copy -f flv rtmp://node2/live/livestream

然后你得到一个RTMP流,并在Node1上再次处理它:

cv2.VideoCapture("rtmp://node2:1935/live/livestream")

请注意RTMP不在node1上,因此您永远不应该使用localhost127.0.0.1来消耗cv。

最新更新