1

Varun Chatterji posted how to use requests to stream video from an IP (ethernet) camera that requires a login and password. This is exactly what I needed and the only thing that works for my camera in python 3.4 on windows 7.

However, where is the loop in his code? When I run this code it runs infinitely while showing video in a cv2 window. However, the code lacks a "while True:" statement and I'm not finding any help in my searches. I'd like to move the loop to higher level module, but I don't know where the loop is.

Said another way, can someone refactor this code so there there is a "while True:" line somewhere? That would let me see what is inside the loop and what is not. I'm finding the requests documentation very hard to follow.

Varun's code for reference:

import cv2
import requests
import numpy as np

r = requests.get('http://192.168.1.xx/mjpeg.cgi', auth=('user', 'password'), stream=True)
if(r.status_code == 200):
    bytes = bytes()
    for chunk in r.iter_content(chunk_size=1024):
        bytes += chunk
        a = bytes.find(b'\xff\xd8')
        b = bytes.find(b'\xff\xd9')
        if a != -1 and b != -1:
            jpg = bytes[a:b+2]
            bytes = bytes[b+2:]
            i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
            cv2.imshow('i', i)
            if cv2.waitKey(1) == 27:
                exit(0)
else:
    print("Received unexpected status code {}".format(r.status_code))

The motivation for this is that I want to move the stuff "inside the loop" into a subroutine, call it ProcessOneVideoFrame() and then be able to put into a larger program:

while True:
    ProcessOneVideoFrame()       
    CheckForInput()
    DoOtherStuff()
    ...
Community
  • 1
  • 1
Casey
  • 475
  • 6
  • 19
  • There is a for loop ( lines 8 - 18 ) , why don't you like that ? – t.m.adam May 05 '17 at 04:02
  • @t.m.adam I'm looking for "the while loop." This code runs forever and that's not what for loops do. Unless iter_content is somehow filling the "chunk" variable so fast that the end of the for loop is never reached... making into a while loop, sort of. See, I'm still confused... – Casey May 05 '17 at 04:32
  • There is no while loop in the code you posted . It is possible to have an infinite for loop eg : `l = [1]; for i in l : l += [1]` . In that case use `break` – t.m.adam May 05 '17 at 04:54
  • @t.m.adam: I agree that there is no loop, so why does this code run indefinitely? The window image pops up and shows video until the code is halted at the command line. It looks like the requests module is getting the execution of the for loop to run forever because it's tied to the unending stream of data from the camera... maybe. However, I still haven't figured it out why it is that the code above runs forever? – Casey May 10 '17 at 16:27
  • 1
    "It looks like the requests module is getting the execution of the for loop to run forever because it's tied to the unending stream of data from the camera" . That's correct . You could either break the loop after some point , or run it on a separate thread – t.m.adam May 11 '17 at 06:37
  • @t.m.adam. Thanks. If you put that down as an answer then I'll mark this as solved. Thanks. – Casey May 17 '17 at 02:15

2 Answers2

0

Well, stream=True basically tells us always to run as long as receiving data which is equal to the while True statement. There is also an inner loop which is the for chunk in r.iter_content(chunk_size=1024) that already explained in François answer.

Willy satrio nugroho
  • 908
  • 1
  • 16
  • 27
-1

However, where is the loop in his code?

At this line:

for chunk in r.iter_content(chunk_size=1024):

iter_content is a generator which makes for looping until the stream is empty.

Some doc here: http://docs.python-requests.org/en/latest/api/#requests.Response.iter_content

  • 1
    This only partially answered the question. I recommend you [edit] your answer to refactor the code as mentioned in the OP's question. – Tom Aranda Dec 21 '17 at 16:33