我的網路中有三個節點:dataServer --- node1 --- node2。我的視頻資料“friends.mp4”保存在 dataServer 上。我將 dataServer 和 node2 都作為 rtmp-nginx 服務器啟動。我在 node1 上使用 ffmpeg 在 dataServer 上提取資料流,并將轉換后的資料流推送到 node2 上的“實時”應用程式。這是我為 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中的面孔:import 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 物件?
uj5u.com熱心網友回復:
嘗試將檔案更改為實時流,然后使用 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 上,因此您不應該使用localhost或127.0.0.1讓 cv 使用它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439572.html
標籤:opencv nginx ffmpeg rtmp pyffmpeg
