from picamera import PiCamera
from time import sleep

from sys import exit
import cv2
import numpy as np

def nothing(x):
    pass

a=np.zeros((360,640,3),dtype=np.uint8)
a[180,:,:]=255
a[:,320,:]=255
camera=PiCamera()
#camera.vflip=True
#camera.sensor_mode=2
camera.resolution=(320,240)
#camera.sensor_mode=3

#p=camera.start_preview(fullscreen=False,window=(100,50,640,360))
#camera.start_preview()
camera.video_stabilization=True
camera.video_denoise=True
camera.start_preview()
##o= camera.add_overlay(np.getbuffer(a),fullscreen=False,layer=3,alpha=128,window=(100,100,640,360))

cv2.namedWindow("detection", cv2.WINDOW_AUTOSIZE)
cv2.createTrackbar('x','detection',100,100,nothing)
cv2.createTrackbar('y','detection',100,100,nothing)
cv2.createTrackbar('w','detection',13,100,nothing)
cv2.createTrackbar('h','detection',42,100,nothing)
while cv2.waitKey(1) != ord("q"):
##      #print camera.exposure_speed
##      x=cv2.getTrackbarPos('x','detection')/100.0
##      y=cv2.getTrackbarPos('y','detection')/100.0
##      w=cv2.getTrackbarPos('w','detection')/100.0
##      h=cv2.getTrackbarPos('h','detection')/100.0
##      a=np.zeros((360,640,3),dtype=np.uint8)
##      a[180,:,:]=255
##      a[:,320,:]=255
##      o.update(np.getbuffer(a))
##      p.window=(int(h*1000),int(w*1000),640,360)
      sleep(0.1)
      

     

cv2.destroyAllWindows()
camera.stop_preview()
#camera.remove_overlay(o)
#camera.capture('foo.jpg',resize=(800,600))
print "exit"
exit()
      



