Esempio n. 1
0
class PaVEProxy(Thread):
    def __init__(self, readCbk):
        Thread.__init__(self)
        self.setDaemon(True)
        self.lock = Lock()
        self.shouldIRun = Event()
        self.shouldIRun.set()
        self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket, self.addr = None, None
        self.readCbk = readCbk
        self.pave = PaVE()
        self.start()

    def run(self):
        "wait only for the first client - delayed binding"
        self.serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.serverSocket.bind((HOST, PORT))
        self.serverSocket.listen(1)
        self.socket, self.addr = self.serverSocket.accept()
        while self.shouldIRun.isSet():
            self.pave.append(self.readCbk(BUFFER_SIZE))
            header, payload = self.pave.extract()
            if payload:
                self.socket.send(payload)

    def term(self):
        self.shouldIRun.clear()
        if self.socket == None:
            print "KILLING run()"
            tmpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            tmpSocket.connect((HOST, PORT))
            tmpSocket.close()
            print "DONE"
Esempio n. 2
0
class PaVEProxy( Thread ):
  def __init__( self, readCbk ):
    Thread.__init__( self )
    self.setDaemon(True)
    self.lock = Lock()
    self.shouldIRun = Event()
    self.shouldIRun.set()
    self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.socket, self.addr = None, None
    self.readCbk = readCbk
    self.pave = PaVE()
    self.start()

  def run( self ):
    "wait only for the first client - delayed binding"
    self.serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.serverSocket.bind((HOST, PORT))
    self.serverSocket.listen(1)
    self.socket, self.addr = self.serverSocket.accept() 
    while self.shouldIRun.isSet():
      self.pave.append( self.readCbk( BUFFER_SIZE ) )
      header,payload = self.pave.extract()
      if payload:
        self.socket.send( payload )

  def term( self ):
    self.shouldIRun.clear()
    if self.socket==None:
      print "KILLING run()"
      tmpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      tmpSocket.connect((HOST, PORT))
      tmpSocket.close()
      print "DONE"
Esempio n. 3
0
class PacketProcessor(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.setDaemon(True)
        self.lock = Lock()
        self.pave = PaVE()
        self.readyForProcessing = ""
        self._lastResult = None
        self.timestamp = None
        self.frameNum = None
        self.shouldIRun = Event()
        self.shouldIRun.set()
        self.start()

    def process(self, packet):
        self.pave.append(packet)  # re-packing
        header, payload = self.pave.extract()  # TODO multiple packets
        if header != "":
            if len(header) >= 28:
                version, codec, headerSize, payloadSize = struct.unpack_from(
                    "BBHI", header, 4)
                assert version == 3, version
                assert codec == 4, codec
                frameNum, timestamp = struct.unpack_from("II", header, 20)
                if len(payload) == payloadSize:
                    self.lock.acquire()
                    #          if len( self.readyForProcessing ) > 0:
                    #            print "skipping", len(self.readyForProcessing)
                    self.readyForProcessing = payload[:]
                    self.timestamp = timestamp
                    self.frameNum = frameNum
                    self.lock.release()
                else:
                    # this looks like frequent case - PaVE is probably also in the middle of the packets
                    print "BAD PACKET", (len(payload), headerSize, payloadSize)

    def run(self):
        while True:  #self.shouldIRun.isSet():
            if len(self.readyForProcessing) > 0:
                self.lock.acquire()
                tmp = self.readyForProcessing
                timestamp = self.timestamp
                frameNum = self.frameNum
                self.readyForProcessing = ""
                self.lock.release()
                mv = h264.parseFrame(tmp)
                self.lock.acquire()
                self._lastResult = frameNum, timestamp, quadrantMotion(mv)
                self.lock.release()
                print len(mv), self._lastResult

    def lastResult(self):
        self.lock.acquire()
        ret = self._lastResult
        self.lock.release()
        return ret

    def requestStop(self):
        self.shouldIRun.clear()
Esempio n. 4
0
class PacketProcessor( Thread ):
  def __init__( self ):
    Thread.__init__( self )
    self.setDaemon( True )
    self.lock = Lock()
    self.pave = PaVE()
    self.readyForProcessing = ""
    self._lastResult = None
    self.timestamp = None
    self.frameNum = None
    self.shouldIRun = Event()
    self.shouldIRun.set()
    self.start()

  def process( self, packet ):
    self.pave.append( packet ) # re-packing
    header,payload = self.pave.extract() # TODO multiple packets
    if header != "":
      if len(header) >= 28:
        version, codec, headerSize, payloadSize = struct.unpack_from("BBHI", header, 4 )
        assert version == 3, version
        assert codec == 4, codec
        frameNum, timestamp = struct.unpack_from("II", header, 20 )
        if len(payload) == payloadSize:
          self.lock.acquire()
#          if len( self.readyForProcessing ) > 0:
#            print "skipping", len(self.readyForProcessing)
          self.readyForProcessing = payload[:]
          self.timestamp = timestamp
          self.frameNum = frameNum
          self.lock.release()
        else:
          # this looks like frequent case - PaVE is probably also in the middle of the packets
          print "BAD PACKET", (len(payload), headerSize, payloadSize)

  def run(self):
    while True: #self.shouldIRun.isSet():
      if len( self.readyForProcessing) > 0:
        self.lock.acquire()
        tmp = self.readyForProcessing
        timestamp = self.timestamp
        frameNum = self.frameNum
        self.readyForProcessing = ""
        self.lock.release()
        mv = h264.parseFrame( tmp )
        self.lock.acquire()
        self._lastResult = frameNum, timestamp, quadrantMotion( mv )
        self.lock.release()
        print len(mv), self._lastResult

  def lastResult(self):
    self.lock.acquire()
    ret = self._lastResult
    self.lock.release()
    return ret 

  def requestStop(self):
    self.shouldIRun.clear() 
Esempio n. 5
0
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()
Esempio n. 6
0
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()
Esempio n. 7
0
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()
Esempio n. 8
0
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()
Esempio n. 9
0
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()
Esempio n. 10
0
File: test.py Progetto: Vibri/heidi
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()

Esempio n. 11
0
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()