HTTP stream from camera delayed by a long time

asked 2017-02-24 07:14:26 -0500

Hi all,

I have a raspberry pi camera with "Motion" operating at localhost/8081 which I have got access to through my browser by simply including this html element in a page: <iframe src="my.pi.IP.add:8081" *params*></iframe>, and this works absolutely great.

After the fact, I decide I needed to get the "profile" across a frame for data processing reasons and, since I already have other data coming from a python server running tornado, I decided to include python-opencv and use that to analyse the frames.

I initialise the video with:

def init_video():
    print("Opening video port")
    # Set up video stream
    video_port = "8081"
    stream = "localhost:%s/frame.mjpeg" % video_port
    capture = cv2.VideoCapture(stream)
    return capture

Within the tornado ioloop I have a scheduled function which sends data to the client every 50ms or so, to update numeric indicators on a display, and again this works flawlessly. I also have the following function as part of the scheduler (which takes capture as an argument):

def intensity_profiles(capture):
    # capture frame from stream and take profiles across it
    ret, frame =
    grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    x_profile = grey[grey.shape[1]/2, :]
    return x_profile

And this works, but there is a BIG delay between the stream shown in the iframe (which updates close to real time) and the x_profile data which is coming out of this function. If I include a line in intensity_profiles() which prints the value of one pixel, covering the camera with my hand, I counted the seconds until I saw a difference in the output and it was between 10 and 30 seconds delay.

I don't know where this delay comes from, since both the iframe and the cv2 capture are pointed at the exact same URI for the motion server and so should be receiving frames at roughly the same time, right?

In any case, this is really annoying for a ~real time application viewing the profiles (maximum desired lag ~2 seconds or less) so does anyone know what I should do differently?

edit retag flag offensive close merge delete