def loadVideoFrame(filename): assert ":" in filename, filename frameNumber = int(filename.split(':')[-1]) filename = ":".join(filename.split(":")[:-1]) f = open(filename, "rb") proc = pave.PaVE() while True: packet = f.read(10000) if (packet) == 0: break proc.append(packet) header, payload = proc.extract() while payload: if pave.isIFrame(header) and pave.frameNumber( header) == frameNumber: tmpFile = open("tmp.bin", "wb") tmpFile.write(payload) tmpFile.flush() tmpFile.close() cap = cv2.VideoCapture("tmp.bin") ret, frame = cap.read() processFrame(frame, debug=True) # hack!! cv2.imwrite( "test.jpg", frame ) # dirty solution how to get image from OpenCV to Pygame :( return pygame.image.load("test.jpg") header, payload = proc.extract() return None # frame not found
def loadVideoFrame( filename ): assert ":" in filename, filename frameNumber = int(filename.split(':')[-1]) filename = ":".join(filename.split(":")[:-1]) f = open(filename,"rb") proc = pave.PaVE() while True: packet = f.read(10000) if (packet) == 0: break proc.append( packet ) header,payload = proc.extract() while payload: if pave.isIFrame( header ) and pave.frameNumber( header ) == frameNumber: tmpFile = open( "tmp.bin", "wb" ) tmpFile.write( payload ) tmpFile.flush() tmpFile.close() cap = cv2.VideoCapture( "tmp.bin" ) ret, frame = cap.read() processFrame( frame, debug=True ) # hack!! cv2.imwrite( "test.jpg", frame ) # dirty solution how to get image from OpenCV to Pygame :( return pygame.image.load( "test.jpg" ) header,payload = proc.extract() return None # frame not found
def wrapper(packet): global g_pave global g_img if g_pave == None: g_pave = PaVE() cvideo.init() g_img = np.zeros([360, 640, 3], dtype=np.uint8) g_pave.append(packet) header, payload = g_pave.extract() while payload: if isIFrame(header): w, h = frameEncodedWidth(header), frameEncodedHeight(header) if g_img.shape[0] != h or g_img.shape[1] != w: print g_img.shape, (w, h) g_img = np.zeros([h, w, 3], dtype=np.uint8) ret = cvideo.frame(g_img, isIFrame(header) and 1 or 0, payload) frame = g_img assert ret if ret: result = processAvoidGreen(frame, debug=False) return (frameNumber(header), timestamp(header)), result header, payload = g_pave.extract()
def wrapper( packet ): global g_pave global g_img if g_pave == None: g_pave = PaVE() cvideo.init() g_img = np.zeros([360,640,3], dtype=np.uint8) g_pave.append( packet ) header,payload = g_pave.extract() while payload: if isIFrame( header ): w,h = frameEncodedWidth(header), frameEncodedHeight(header) if g_img.shape[0] != h or g_img.shape[1] != w: print g_img.shape, (w,h) g_img = np.zeros([h,w,3], dtype=np.uint8) ret = cvideo.frame( g_img, isIFrame(header) and 1 or 0, payload ) frame = g_img assert ret if ret: result = processAvoidGreen( frame, debug=False ) return (frameNumber( header ), timestamp(header)), result header,payload = g_pave.extract()
def wrapper( packet ): global g_pave if g_pave == None: g_pave = PaVE() g_pave.append( packet ) header,payload = g_pave.extract() while payload: if isIFrame( header ): tmpFile = open( "tmp.bin", "wb" ) tmpFile.write( payload ) tmpFile.flush() tmpFile.close() cap = cv2.VideoCapture( "tmp.bin" ) ret, frame = cap.read() assert ret if ret: return (frameNumber( header ), timestamp(header)), processFrame( frame, debug=False ) header,payload = g_pave.extract()
def wrapper(packet): global g_pave if g_pave == None: g_pave = PaVE() g_pave.append(packet) header, payload = g_pave.extract() while payload: if isIFrame(header): tmpFile = open("tmp.bin", "wb") tmpFile.write(payload) tmpFile.flush() tmpFile.close() cap = cv2.VideoCapture("tmp.bin") ret, frame = cap.read() assert ret if ret: return (frameNumber(header), timestamp(header)), processFrame(frame, debug=False) header, payload = g_pave.extract()
def testPaVEVideo( filename, onlyFrameNumber=None, refLog=None ): f = open( filename, "rb" ) data = f.read(10000) pave = PaVE() cap = None total = 0 while len(data) > 0: pave.append( data ) header,payload = pave.extract() while payload: if isIFrame( header ) and (onlyFrameNumber == None or onlyFrameNumber==frameNumber( header )/FRAMES_PER_INDEX): tmpFile = open( "tmp.bin", "wb" ) tmpFile.write( payload ) tmpFile.flush() tmpFile.close() cap = cv2.VideoCapture( "tmp.bin" ) ret, frame = cap.read() assert ret if ret: global g_filename g_filename = "tmp_%04d.jpg" % (frameNumber( header )/FRAMES_PER_INDEX) result = processFrame( frame, debug=True ) if refLog != None: print refLog.readline().strip() (oldFrameNumber, oldTimestamp), oldResult = eval(refLog.readline().strip()) assert oldFrameNumber == frameNumber(header), (oldFrameNumber, frameNumber(header)) assert oldTimestamp == timestamp(header), (oldTimestamp, timestamp(header)) print ((frameNumber(header), timestamp(header)), result) # assert oldResult == result, oldResult # potential difference linux/windows else: print frameNumber( header )/FRAMES_PER_INDEX, result if onlyFrameNumber: cv2.waitKey(0) return header,payload = pave.extract() if cv2.waitKey(1) & 0xFF == ord('q'): break data = f.read(10000) if refLog != None: # complete termination line print refLog.readline().strip()
import cvideo import cv2 import numpy as np import sys sys.path.append( ".." ) from pave import PaVE, isIFrame, frameEncodedWidth, frameEncodedHeight print cvideo.init() img = np.zeros([720,1280,3], dtype=np.uint8) missingIFrame = True filename = sys.argv[1] pave = PaVE() pave.append( open( filename, "rb" ).read() ) header,payload = pave.extract() while len(header) > 0: w,h = frameEncodedWidth(header), frameEncodedHeight(header) if img.shape[0] != h or img.shape[1] != w: print img.shape, (w,h) img = np.zeros([h,w,3], dtype=np.uint8) missingIFrame = missingIFrame and not isIFrame(header) if not missingIFrame: assert cvideo.frame( img, isIFrame(header) and 1 or 0, payload ) cv2.imshow('image', img) if cv2.waitKey(1) & 0xFF == ord('q'): break header,payload = pave.extract()
import cvideo import cv2 import numpy as np import sys sys.path.append("..") from pave import PaVE, isIFrame, frameEncodedWidth, frameEncodedHeight print cvideo.init() img = np.zeros([720, 1280, 3], dtype=np.uint8) missingIFrame = True filename = sys.argv[1] pave = PaVE() pave.append(open(filename, "rb").read()) header, payload = pave.extract() while len(header) > 0: w, h = frameEncodedWidth(header), frameEncodedHeight(header) if img.shape[0] != h or img.shape[1] != w: print img.shape, (w, h) img = np.zeros([h, w, 3], dtype=np.uint8) missingIFrame = missingIFrame and not isIFrame(header) if not missingIFrame: assert cvideo.frame(img, isIFrame(header) and 1 or 0, payload) cv2.imshow('image', img) if cv2.waitKey(1) & 0xFF == ord('q'): break header, payload = pave.extract()