我的網絡中有三個節點:數據服務器--node1--node2。我的視頻數據"friends.mp4保存在dataServer上。我啟動了dataServer和node2作為rtmp-nginx服務器。我在node1上使用ffmpeg在dataServer上提取數據流,并將轉換后的數據流推送到node2上的應用程序“live”。下面是我對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服務器接收到的數據流,并將其變成openCV可以使用的python對象?
嘗試將文件更改為實時流,然后使用cv2處理流:
對于Node1,可以運行如下命令:
然后得到一個RTMP流,并在Node1上再次處理它:
請注意,RTMP不在節點1上,因此您不應該使用
localhost
或127.0.0.1
作為cv來使用它。