# Path: airrace.py
# def main( argv, imgFce=None ):
#   if imgFce != None:
#     global processFrame
#     processFrame = imgFce
#   if len(argv) < 2:
#     print __doc__
#     sys.exit(2)
#   filename = argv[1]
#   if filename.endswith(".jpg"):
#     testFrame( filename )
#   else:
#     if isParrotVideo( filename ):
#       if len(argv) > 2:
#         if "src_cv2_" in argv[2]:
#           testPaVEVideo( filename, refLog=open(argv[2]) )
#         else:
#           testPaVEVideo( filename, onlyFrameNumber=int(eval(argv[2])) )
#       else:
#         testPaVEVideo( filename )
#     else:
#       testVideo( filename )
#   cv2.destroyAllWindows()
# 
# Path: airrace.py
# def saveIndexedImage( img ):
#   if g_filename:
#     cv2.imwrite( g_filename, img )


import sys
import cv2
import cimg
from airrace import main as imgmain # image debugging TODO move to launcher
from airrace import saveIndexedImage
      else:
        frame[y][x] = (255, 255, 255)
    if count > limit:
      break
  return x

def avoidGreen( frame, fromX, toX, fromY, toY, limit ):
  "numpy implementation"
  return cimg.avoidGreen( frame, fromX, toX, fromY, toY, limit, 1.1 )

def stripLeftRight( frame, fromY, toY, limit ):
  width = frame.shape[1]
  xL = avoidGreen( frame, width/2, 0, fromY, toY, limit=limit )
  xR = avoidGreen( frame, width/2, width, fromY, toY, limit=limit )
  return xL,xR


def processAvoidGreen( frame, debug=False ):
  height, width, colors = frame.shape
  offset = 0
  stripWidth = 70
  limit = 120
  topLR = stripLeftRight( frame, offset, offset+stripWidth, limit=limit )
  bottomLR = stripLeftRight( frame, height-offset-stripWidth, height-offset, limit=limit )
  if debug:
    cv2.imshow('image', frame)
    saveIndexedImage( frame )
  return topLR, bottomLR

if __name__ == "__main__":

# Path: airrace.py
# def main( argv, imgFce=None ):
#   if imgFce != None:
#     global processFrame
#     processFrame = imgFce
#   if len(argv) < 2:
#     print __doc__
#     sys.exit(2)
#   filename = argv[1]
#   if filename.endswith(".jpg"):
#     testFrame( filename )
#   else:
#     if isParrotVideo( filename ):
#       if len(argv) > 2:
#         if "src_cv2_" in argv[2]:
#           testPaVEVideo( filename, refLog=open(argv[2]) )
#         else:
#           testPaVEVideo( filename, onlyFrameNumber=int(eval(argv[2])) )
#       else:
#         testPaVEVideo( filename )
#     else:
#       testVideo( filename )
#   cv2.destroyAllWindows()
# 
# Path: airrace.py
# def saveIndexedImage( img ):
#   if g_filename:
#     cv2.imwrite( g_filename, img )


# Path: green.py
import sys
import cv2
import cimg
from airrace import main as imgmain # image debugging TODO move to launcher
from airrace import saveIndexedImage

      else:
        frame[y][x] = (255, 255, 255)
    if count > limit:
      break
  return x

def avoidGreen( frame, fromX, toX, fromY, toY, limit ):
  "numpy implementation"
  return cimg.avoidGreen( frame, fromX, toX, fromY, toY, limit, 1.1 )

def stripLeftRight( frame, fromY, toY, limit ):
  width = frame.shape[1]
  xL = avoidGreen( frame, width/2, 0, fromY, toY, limit=limit )
  xR = avoidGreen( frame, width/2, width, fromY, toY, limit=limit )
  return xL,xR


def processAvoidGreen( frame, debug=False ):
  height, width, colors = frame.shape
  offset = 0
  stripWidth = 70
  limit = 120
  topLR = stripLeftRight( frame, offset, offset+stripWidth, limit=limit )
  bottomLR = stripLeftRight( frame, height-offset-stripWidth, height-offset, limit=limit )
  if debug:
    cv2.imshow('image', frame)
    saveIndexedImage( frame )
  return topLR, bottomLR

if __name__ == "__main__":

  imgmain( sys.argv, processAvoidGreen ) #processGreen )