在使用yolov5做物体检测中,需要拉取视频流。分解任务第一步则是需要使用opencv读取rtsp流,只要拿到每一帧图片在进行推理显示即可。
- import cv2
- def read_rtsp():
- cap = cv2.VideoCapture('rtsp://admin:Vrc123456@192.168.2.226:554')
- fourcc = cv2.VideoWriter_fourcc(*'XVID')
- size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
- out = cv2.VideoWriter('./rtsp_video.avi', fourcc,10.0, size)
- while True:
- ret, frame = cap.read()
- out.write(frame)
-
- cv2.imshow('frame', frame)
- cv2.waitKey(1)
-
- cap.release()
- out.release()
- cv2.destroyAllWindows()
- if __name__ == '__main__':
- read_rtsp()
- //读取流
- cap = cv2.VideoCapture('rtsp://...')
- //读取每一帧
- ret, frame = cap.read()
-