from __future__ import print_function
from imutils.video.pivideostream import PiVideoStream
from imutils.video import FPS
import imutils
import time
import cv2
#from multiprocessing.dummy import Pool

##def findsharpness(src):
##    src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
##    src=cv2.bilateralFilter(src,3,6,1.5)
##    dx = cv2.convertScaleAbs(cv2.Sobel(src, cv2.CV_16S,1,0, ksize=3))
##    dy = cv2.convertScaleAbs(cv2.Sobel(src, cv2.CV_16S,0,1, ksize=3))
##    accu=cv2.addWeighted(dx,0.5,dy,0.5,0,-1)
##    return cv2.sumElems(accu)[0]

##def slicer(src):
##    size=src.shape[0:2:1]
##    return (src[0:size[0]/2,0:size[1]/2],
##            src[0:size[0]/2,size[1]/2:size[1]],
##            src[size[0]/2:size[0],0:size[1]/2],
##            src[size[0]/2:size[0],size[1]/2:size[1]])

#p=Pool(processes=4)


def diff(frame1,frame2):
    return(cv2.sumElems(frame1)[0]-cv2.sumElems(frame2)[0])

vs = PiVideoStream(resolution=(720,720),framerate=40).start()
time.sleep(2.0)
fps = FPS().start()
diffs=[]
frame2 = vs.read()
while fps._numFrames < 50:
    frame1 = frame2
    time.sleep(0.025)
    frame2 = vs.read()
    diffs.append(diff(frame1,frame2)) 

##    frame = vs.read()
##    s1=sum([item for item in p.map(findsharpness,slicer(frame),1) if item is not None])
##    frame = vs.read()
##    s2=sum([item for item in p.map(findsharpness,slicer(frame),1) if item is not None])
    
       

    fps.update()
 

fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
print(diffs) 

cv2.destroyAllWindows()
vs.stop()
#p.close()
#p.join()

