Skip to content

Video streaming using OpenCV and ONVIF is too slow #123

@kolyan288

Description

@kolyan288
import cv2
from onvif import ONVIFCamera


ip = 'ip'
username = 'username'
password = 'password'

port_rtsp = 'port_rtsp'

cam = ONVIFCamera(ip, port, username, password, wsdl_dir = r'python-onvif-zeep/wsdl')

media = cam.create_media_service()
ptz = cam.create_ptz_service()

media_profile = media.GetProfiles()[0]

def make_rtsp(ip, port, username, password):
    return 'rtsp_link'

rtsp = make_rtsp(ip, port_rtsp, username, password)

# Open the video file
cap = cv2.VideoCapture(rtsp)

request = ptz.create_type("GetConfigurationOptions")
request.ConfigurationToken = media_profile.PTZConfiguration.token

ptz_configuration_options = ptz.GetConfigurationOptions(request)

relative_move_request = ptz.create_type("RelativeMove")
relative_move_request.ProfileToken = media_profile.token
relative_move_request.Speed = {'PanTilt': {'x': 0, 'y': 0}}

# Loop through the video frames
while cap.isOpened():
    # Read a frame from the video
    success, frame = cap.read()

    if success:
              
        cv2.imshow('camera', frame)
        pan_offset = 0.001
        tilt_offset = 0.001
        zoom_offset = 0

        relative_move_request.Translation = {"PanTilt": {"x": pan_offset, # 0.01 * abs_zoom
                                            "y": tilt_offset}, # 0.01 * abs_zoom
                                "Zoom": zoom_offset}

        relative_move_request.Speed = {'PanTilt': {'x': 0, 'y': 0}}

        ptz.RelativeMove(relative_move_request)

        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        # Break the loop if the end of the video is reached
        break

# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()

The frames displayed on the screen via OpenCV are transmitted with a delay relative to the actual frames from the PTZ camera

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions