# importing libraries
import cv2
import numpy as np
from scipy import stats


drawing = False
point1 = ()
point2 = ()

def mouse_drawing(event, x, y, flags, params):
    global point1, point2, drawing
    if event == cv2.EVENT_LBUTTONDOWN:
        if drawing is False:
            drawing = True
            point1 = (x, y)
        else:
            drawing = False

    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing is True:
            point2 = (x, y)

# Our ROI, defined by two points
p1, p2 = None, None
state = 0

# Called every time a mouse event happen
def on_mouse(event, x, y, flags, userdata):
    global state, point1, point2
    
    # Left click
    if event == cv2.EVENT_LBUTTONUP:
        # Select first point
        if state == 0:
            point1 = (x,y)
            state += 1
        # Select second point
        elif state == 1:
            point2 = (x,y)
            state += 1


#xFine = (848, 187, 225, 21.0)
#yFine = (604, 402, 20.5, 276)

xFine = (848, 187, 848 + 225, 187 + 21.0)
yFine = (604, 402, 604 + 20.5, 402 + 276)

frameCountMod = 0
centroidX = [0, 0]
centroidY = [0, 0]

def track(frame, ROI, centroid, update):
    if(update):
        crop = frame[int(ROI[1]):int(ROI[3]), int(ROI[0]):int(ROI[2])]
        crop = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY)
        crop = cv2.GaussianBlur(crop,(7,7),cv2.BORDER_DEFAULT)

        #ret, thresh = cv2.threshold(crop, 100, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
        ret,thresh = cv2.threshold(crop, 50, 255, 0)
        
        M = cv2.moments(thresh)

        # calculate x,y coordinate of center
        if M["m00"] != 0:
            centroid[0] = int(M["m10"] / M["m00"])
            centroid[1] = int(M["m01"] / M["m00"])
        #else:
        #    cX, cY = 0, 0
        #print(cY)
    cv2.circle(frame, (int(ROI[0]) + centroid[0], int(ROI[1]) + centroid[1]), 3, (0, 255, 0), -1)

cv2.namedWindow("Frame")
cv2.setMouseCallback("Frame", mouse_drawing)
cv2.namedWindow("Process")
  
# Create a VideoCapture object and read from input file
cap = cv2.VideoCapture("/home/mwinter/Portfolio/a_history_of_the_domino_problem/a_history_of_the_domino_problem_source/recs/a_history_of_the_domino_problem_final_documentation_hq.mp4")
cap.set(cv2.CAP_PROP_POS_FRAMES, 10000)

# Check if camera opened successfully
if (cap.isOpened()== False):
    print("Error opening video file")
  
frameCountMod = 0
centroidX = [0, 0]
centroidY = [0, 0]

# Read until video is completed
while(cap.isOpened()):
      
    # Capture frame-by-frame
    ret, frame = cap.read()
    if ret == True:
    # Display the resulting frame

        if point1 and point2:
            px = sorted([point1[0], point2[0]])
            py = sorted([point1[1], point2[1]])
            #cv2.rectangle(frame, point1, point2, (0, 255, 0))
            xFine = (px[0], py[0], px[1], py[1])

        crop = frame[int(xFine[1]):int(xFine[3]), int(xFine[0]):int(xFine[2])]
        gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
        fm = cv2.Laplacian(gray, cv2.CV_64F).var()

        kernel = np.ones((30, 50), np.uint8)

        text = "Not Blurry"
        blur = cv2.GaussianBlur(cv2.bitwise_not(crop),(1001,3), 3, 1) * 3
        dilation = cv2.dilate(gray, kernel, iterations=1)
        ret,dilation = cv2.threshold(dilation,20,255,cv2.THRESH_BINARY_INV)

        mean = pow(dilation.mean(), 3)
        # if the focus measure is less than the supplied threshold,
        # then the image should be considered "blurry"
        if fm < 100.0:
            text = "Blurry"
        # show the image
        cv2.rectangle(frame, (int(xFine[0]), int(xFine[1])), (int(xFine[2]),int(xFine[3])), (0, 255, 0))
        cv2.rectangle(frame, (int(xFine[0]), int(xFine[1])), (int(xFine[2]),int(xFine[3])), (0, 255, 0))
        cv2.putText(frame, "{}: {:.2f}".format(text, fm), (10, 30),
		cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
        cv2.putText(frame, "{}: {:.2f}".format("Brightness", mean), (10, 100),
        cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
            
        cv2.imshow("Frame", frame)
        cv2.imshow("Process", crop)

          
    # Press Q on keyboard to exit
        key = cv2.waitKey(100)

        if key == 32:
            cv2.waitKey()
        elif key == ord('q'):
            break
  
# Break the loop
    else:
        break
  
# When everything done, release
# the video capture object
cap.release()
  
# Closes all the frames
cv2.destroyAllWindows()