# Path: Image_Video/display.py
# def display(infile, nogui=False):
#     image = cv2.imread(infile)
#     if nogui:
#         cv2.imwrite('test_display2.png', image)
#     else:
#         cv2.imshow("Image", image)
#         cv2.waitKey(0)
# 
# Path: Image_Video/blur.py
# def blur_display(infile, nogui=False):
# 
# Path: Image_Video/edge_detect.py
# def edge_detect(infile, nogui=False):
# # The first argument is the image
#     image = cv2.imread(infile)
# 
#     #conver to grayscale
#     gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 
#     #blur it
#     blurred_image = cv2.GaussianBlur(gray_image, (7,7), 0)
#     # Use low thresholds
#     canny = cv2.Canny(blurred_image, 10, 30)
#     # Use high thresholds
#     canny2 = cv2.Canny(blurred_image, 50, 150)
# 
#     if nogui:
#         cv2.imwrite('test_edge.png', canny2)
#     else:
#         cv2.imshow("Orignal Image", image)
#         cv2.imshow("Canny with low thresholds", canny)
#         cv2.imshow("Canny with high thresholds", canny2)
#         cv2.waitKey(0)
# 
# Path: Image_Video/count_cards.py
# def count_cards(infile="cards.jpg", nogui=False):
#     # Read the image
#     image = cv2.imread(infile)
# 
#     #convert to grayscale
#     gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 
#     #blur it
#     blurred_image = cv2.GaussianBlur(gray_image, (7,7), 0)
# 
#     # Run the Canny edge detector
#     canny = cv2.Canny(blurred_image, 30, 100)
#     contours, hierarchy= cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#     image2 = image
#     cv2.drawContours(image2, contours, -1, (0,255,0), 2)
# 
#     print("Number of objects found = ", len(contours))
# 
#     if nogui:
#         cv2.imwrite('test_count_cards.png', image2)
#         return len(contours)
#     else:
#         # Show both our images
#         cv2.imshow("Original image", image)
#         cv2.imshow("Blurred image", blurred_image)
#         cv2.imshow("Canny", canny)
#         cv2.imshow("objects Found", image2)
#         cv2.waitKey(0)
# 
# Path: Image_Video/face_detect.py
# def face_detect(imgpath, nogui = False, cascasdepath = "haarcascade_frontalface_default.xml"):
# 
#     image = cv2.imread(imgpath)
#     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 
#     face_cascade = cv2.CascadeClassifier(cascasdepath)
# 
#     faces = face_cascade.detectMultiScale(
#         gray,
#         scaleFactor = 1.2,
#         minNeighbors = 5,
#         minSize = (30,30)
# 
#         )
# 
#     print("The number of faces found = ", len(faces))
# 
#     for (x,y,w,h) in faces:
#         cv2.rectangle(image, (x,y), (x+h, y+h), (0, 255, 0), 2)
# 
#     if nogui:
#         cv2.imwrite('test_face.png', image)
#         return len(faces)
#     else:
#         cv2.imshow("Faces found", image)
#         cv2.waitKey(0)
# 
# Path: Image_Video/webcam_face_detect.py
# def webcam_face_detect(video_mode, nogui = False, cascasdepath = "haarcascade_frontalface_default.xml"):
# 
#     face_cascade = cv2.CascadeClassifier(cascasdepath)
# 
#     video_capture = cv2.VideoCapture(video_mode)
#     num_faces = 0
# 
# 
#     while True:
#         ret, image = video_capture.read()
# 
#         if not ret:
#             break
# 
#         gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 
#         faces = face_cascade.detectMultiScale(
#             gray,
#             scaleFactor = 1.2,
#             minNeighbors = 5,
#             minSize = (30,30)
# 
#             )
# 
#         print("The number of faces found = ", len(faces))
#         num_faces = len(faces)
# 
#         if not nogui:
#             for (x,y,w,h) in faces:
#                 cv2.rectangle(image, (x,y), (x+h, y+h), (0, 255, 0), 2)
# 
#             cv2.imshow("Faces found", image)
#             if cv2.waitKey(1) & 0xFF == ord('q'):
#                 break
# 
#     video_capture.release()
#     cv2.destroyAllWindows()
#     return num_faces


from pdb import Pdb
from Image_Video import display,blur,edge_detect,count_cards,face_detect,webcam_face_detect
import os
import glob

def test_blur():
    try:
        for f in glob.glob("test*.png"):
            os.remove(f)
    except:
        pass
    image = "Image_Video/ship.jpg"
    blur.blur_display(image, True)
    assert(os.path.exists("test_blurred.png"))

def test_edge():
    try:
        for f in glob.glob("test*.png"):
            os.remove(f)
    except:
        pass
    image = "Image_Video/ship.jpg"
    edge_detect.edge_detect(image, True)
    assert(os.path.exists("test_edge.png"))



def test_count_cards():
    try:
        for f in glob.glob("test*.png"):
            os.remove(f)
    except:
        pass
    image = "Image_Video/cards.jpg"

# Path: Image_Video/display.py
# def display(infile, nogui=False):
#     image = cv2.imread(infile)
#     if nogui:
#         cv2.imwrite('test_display2.png', image)
#     else:
#         cv2.imshow("Image", image)
#         cv2.waitKey(0)
# 
# Path: Image_Video/blur.py
# def blur_display(infile, nogui=False):
# 
# Path: Image_Video/edge_detect.py
# def edge_detect(infile, nogui=False):
# # The first argument is the image
#     image = cv2.imread(infile)
# 
#     #conver to grayscale
#     gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 
#     #blur it
#     blurred_image = cv2.GaussianBlur(gray_image, (7,7), 0)
#     # Use low thresholds
#     canny = cv2.Canny(blurred_image, 10, 30)
#     # Use high thresholds
#     canny2 = cv2.Canny(blurred_image, 50, 150)
# 
#     if nogui:
#         cv2.imwrite('test_edge.png', canny2)
#     else:
#         cv2.imshow("Orignal Image", image)
#         cv2.imshow("Canny with low thresholds", canny)
#         cv2.imshow("Canny with high thresholds", canny2)
#         cv2.waitKey(0)
# 
# Path: Image_Video/count_cards.py
# def count_cards(infile="cards.jpg", nogui=False):
#     # Read the image
#     image = cv2.imread(infile)
# 
#     #convert to grayscale
#     gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 
#     #blur it
#     blurred_image = cv2.GaussianBlur(gray_image, (7,7), 0)
# 
#     # Run the Canny edge detector
#     canny = cv2.Canny(blurred_image, 30, 100)
#     contours, hierarchy= cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#     image2 = image
#     cv2.drawContours(image2, contours, -1, (0,255,0), 2)
# 
#     print("Number of objects found = ", len(contours))
# 
#     if nogui:
#         cv2.imwrite('test_count_cards.png', image2)
#         return len(contours)
#     else:
#         # Show both our images
#         cv2.imshow("Original image", image)
#         cv2.imshow("Blurred image", blurred_image)
#         cv2.imshow("Canny", canny)
#         cv2.imshow("objects Found", image2)
#         cv2.waitKey(0)
# 
# Path: Image_Video/face_detect.py
# def face_detect(imgpath, nogui = False, cascasdepath = "haarcascade_frontalface_default.xml"):
# 
#     image = cv2.imread(imgpath)
#     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 
#     face_cascade = cv2.CascadeClassifier(cascasdepath)
# 
#     faces = face_cascade.detectMultiScale(
#         gray,
#         scaleFactor = 1.2,
#         minNeighbors = 5,
#         minSize = (30,30)
# 
#         )
# 
#     print("The number of faces found = ", len(faces))
# 
#     for (x,y,w,h) in faces:
#         cv2.rectangle(image, (x,y), (x+h, y+h), (0, 255, 0), 2)
# 
#     if nogui:
#         cv2.imwrite('test_face.png', image)
#         return len(faces)
#     else:
#         cv2.imshow("Faces found", image)
#         cv2.waitKey(0)
# 
# Path: Image_Video/webcam_face_detect.py
# def webcam_face_detect(video_mode, nogui = False, cascasdepath = "haarcascade_frontalface_default.xml"):
# 
#     face_cascade = cv2.CascadeClassifier(cascasdepath)
# 
#     video_capture = cv2.VideoCapture(video_mode)
#     num_faces = 0
# 
# 
#     while True:
#         ret, image = video_capture.read()
# 
#         if not ret:
#             break
# 
#         gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 
#         faces = face_cascade.detectMultiScale(
#             gray,
#             scaleFactor = 1.2,
#             minNeighbors = 5,
#             minSize = (30,30)
# 
#             )
# 
#         print("The number of faces found = ", len(faces))
#         num_faces = len(faces)
# 
#         if not nogui:
#             for (x,y,w,h) in faces:
#                 cv2.rectangle(image, (x,y), (x+h, y+h), (0, 255, 0), 2)
# 
#             cv2.imshow("Faces found", image)
#             if cv2.waitKey(1) & 0xFF == ord('q'):
#                 break
# 
#     video_capture.release()
#     cv2.destroyAllWindows()
#     return num_faces


# Path: test_image.py
from pdb import Pdb
from Image_Video import display,blur,edge_detect,count_cards,face_detect,webcam_face_detect
import os
import glob


def test_blur():
    try:
        for f in glob.glob("test*.png"):
            os.remove(f)
    except:
        pass
    image = "Image_Video/ship.jpg"
    blur.blur_display(image, True)
    assert(os.path.exists("test_blurred.png"))

def test_edge():
    try:
        for f in glob.glob("test*.png"):
            os.remove(f)
    except:
        pass
    image = "Image_Video/ship.jpg"
    edge_detect.edge_detect(image, True)
    assert(os.path.exists("test_edge.png"))



def test_count_cards():
    try:
        for f in glob.glob("test*.png"):
            os.remove(f)
    except:
        pass
    image = "Image_Video/cards.jpg"

    cards = count_cards.count_cards(image, True)