import cv2
import numpy as np

camera = cv2.VideoCapture(0)
# camera.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
# camera.set(cv2.CAP_PROP_EXPOSURE, -5)

cv2.namedWindow('Image', cv2.WINDOW_GUI_NORMAL)
cv2.namedWindow('Debug', cv2.WINDOW_GUI_NORMAL)

lower = 100
upper = 200


def lower_update(value):
    global lower
    lower = value


def upper_update(value):
    global upper
    upper = value


cv2.createTrackbar("Lower", "Debug", lower, 255, lower_update)
cv2.createTrackbar("Upper", "Debug", upper, 255, upper_update)

while camera.isOpened():
    ret, image = camera.read()
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (7,7), 0)
    mask = cv2.Canny(gray, lower, upper)
    cnts, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(image, cnts, -1, (0,255,0), 10)
    print(mask.shape)
    print(image.shape)
    cv2.imshow('Image', image)
    cv2.imshow('Debug', mask)
    key = cv2.waitKey(10)
    if key == ord('q'):
        break