# - Diffs are gray-scale, obtained from RGB by conversion to YUV and keeping Y channel

# Name of video file in tests/
inFileName="HALLWAY_A.mpg"

# Input video file directory (HAS TO BE ABSOLUTE FOR cv2.VideoCapture TO WORK ON MAC OS X!)
inDir="/Users/sorenvind/Documents/work/research/MotionDetection/HistogramCompression/tests"

# The number of frames that should be between any diff-image. I.e. if 25fps and diffInterval=5, 5 diffs are created per second.
diffInterval=10


###############################
# Do the magic!
outputPath=os.path.join(inDir, "diff-"+inFileName+"_{}".format(diffInterval))
fileops.ensureDir(outputPath)

inputPath=os.path.join(inDir, inFileName)
capture = cv2.VideoCapture(inputPath)

# Detect length of video to stop it looping. This is a workaround from normal because OS X can't detect the end of file (so it loops the video)
frames = capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
fps = capture.get(cv2.cv.CV_CAP_PROP_FPS)

totalSize = 0
count = 1
success, first = capture.read()

# Converts to YUV while only keeping Y channel
grayFirst = cv2.cvtColor(first, cv2.COLOR_BGR2GRAY)
while count <= frames:
    data = fileops.dataFromFile(current_file, vidWidth, vidHeight)

    # Iterate over different numbers of regions.
    for regSize in regSizes:
        timings = allTimings[regSize]

        # Calculate histograms from data
        linearHist = fileops.regionsFromData(data, regSize, cols)

        # Output and compress a number of files simultaneously
        for i in histograms.keys():
            histograms[i][regSize].append(linearHist)

            # Write the regions in a file for this regionsize and number of histograms
            cp = os.path.join(pp, "frames_" + str(i), str(regSize))
            fileops.ensureDir(cp)

            if filecount % i == 0 or ff == dirlist[-1]:
                if debug:
                    print "Processing {}, i {}, length {}, first {}, linear {}, file {}, regsize {}".format(
                        filecount,
                        i,
                        len(histograms[i][regSize]),
                        len(histograms[i][regSize][0]),
                        len(linearHist),
                        cp,
                        regSize,
                    )

                    # Output the histograms to disk with different layouts, and compress them
                for layout in layouts: