Search code examples
pythonopencvvideo-streamingh.264ip-camera

How to get http stream from wifi camera compressed format: h.264 on python?


Hy! I am working on a project for school using a HD-eyewear Wifi Camera that uses H.264 compressed format. I have read a lot of documentation about how to get the frames from the camera, but I shouldn't manage my problem. My code looks like this:

import cv2

while True:
    cap = cv2.VideoCapture('http://admin:@192.168.10.1/videostream.asf?user=admin&pwd=') 
    ret, frame = cap.read()
    print(frame) 

I only want to see that it gets correctly the frames, but it drops errors like this:

[h264 @ 0x1e0a100] non-existing PPS 0 referenced

[h264 @ 0x1e0a100] non-existing PPS 0 referenced

[h264 @ 0x1e0a100] decode_slice_header error

[h264 @ 0x1e0a100] no frame!

I really apreciate the help! Thanks! :D


Solution

  • With a little help from comments, I could solve my problem, and works, with some initial frame loss.

    import cv2
    from threading import Thread
    import time
    
    
    
    
    url = ('http://admin:@192.168.10.1/videostream.asf?user=admin&pwd=')
    
    
    class VideoStream(object):
        def __init__(self,url = ('http://admin:@192.168.10.1/videostream.asf?user=admin&pwd=')):
                self.capture = cv2.VideoCapture(url)
                self.thread = Thread(target=self.update, args=())
                self.thread.daemon = True
                self.thread.start()
    
        def update(self):
                while True:
                        if self.capture.isOpened():
                                (self.status, self.frame) = self.capture.read()
                        time.sleep(.01)
    
        def show_frame(self):
                cv2.imshow('frame', self.frame)
                key = cv2.waitKey(1)
                if key == ord('q'):
                        self.capture.release()
                        cv2.destroyAllWindows()
                        exit(1)
    
    if __name__ == '__main__':
        video_stream = VideoStream()
        while True:
                try:
                        video_stream.show_frame()
                except AttributeError:
                        pass
    

    Copied from this link: Video Streaming from IP Camera in Python Using OpenCV cv2.VideoCapture