python - Speed up video playback from a networked device with camera or IP camera -
i'm trying stream video raspberry pi using flask api in python. may process individual frames on workstation. working fine far data delivery concerned. on client side process of reading frames introduces lag of 1-3 seconds undesirable in real time application. can view video playback in web browser without latency proves raspberry pi , network innocent. problem method of reading individual frames byte stream. thoughts eliminating latency in such application. below code client side application. complete source sample application can found here: https://github.com/shehzi-khan/video-streaming
import cv2 import urllib import numpy np stream = urllib.urlopen('http://192.168.100.128:5000/video_feed') bytes = '' while true: bytes += stream.read(1024) = bytes.find(b'\xff\xd8') b = bytes.find(b'\xff\xd9') if != -1 , b != -1: jpg = bytes[a:b+2] bytes = bytes[b+2:] img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.imread_color) cv2.imshow('video', img) if cv2.waitkey(1) == 27: exit(0)
main suggestions:
search end-mark , search start-mark
read more data (e.g. 64kb)
drop other frames , show last
i can't test it, here general code:
import cv2 import urllib import numpy np stream = urllib.urlopen('http://192.168.100.128:5000/video_feed') bytes = '' while true: buff = stream.read(64 * 1024) bytes += buff if buff.rfind(b'\xff\xd9') != -1: # buff smaller bytes endmark = bytes.rfind(b'\xff\xd9') + 2 startmark = bytes[:endmark - 2].rfind(b'\xff\xd8') jpg = bytes[startmark:endmark] # please, check indexes! mess them. bytes = bytes[endmark:] img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.imread_color) cv2.imshow('video', img) if cv2.waitkey(1) == 27: exit(0)
i can't find how stream.read behave. if wait until buffer full, need decrease buffer size. if read n bytes or until end of stream, work.
Comments
Post a Comment