def get_mask_data(maskFile, vid, hMatrix=None, check=False):
    """
    Shows user masks overlayed on given image and asks through a dialog box
    if they are acceptable. Returns True for 'yes' and False for 'no'.
    """

    # Parse input parameters
    image = VF.extract_frame(vid, 1, hMatrix=hMatrix)
    try:
        with open(maskFile) as f:
            maskData = pkl.load(f)
    except:
        print 'Mask file not found, please create it now.'
        maskData = IPF.create_mask_data(image, maskFile)

    while check:
        plt.figure('Evaluate accuracy of predrawn masks for your video')
        maskedImage = IPF.mask_image(image, maskData['mask'])
        temp = np.dstack((maskedImage, image, image))
        plt.imshow(temp)
        center = maskData['diskCenter']
        plt.plot(center[0], center[1], 'bx')
        plt.axis('image')

        response = ctypes.windll.user32.MessageBoxA(0, 'Do you wish to keep' + \
                            ' the current mask?','User Input Required', 4)
        plt.close()
        if response == 6:  # 6 means yes
            return maskData

        else:  # 7 means no
            print 'Existing mask rejected, please create new one now.'
            maskData = IPF.create_mask_data(image, maskFile)

    return maskData
def get_homography_matrix(fileName, vid):
    """
    Load the homography matrix from file or create it if it does not exist.
    """

    # Handle homography data file
    if fileName is not None:
        try:
            with open(fileName, 'rb') as f:
                hMatrix = pkl.load(f)
        except:
            image = VF.extract_frame(vid, 0)
    else:
        hMatrix = None

    return hMatrix
def get_intensity_region(fileName, vid):
    """
    Identify a region of the video frames to use for monitoring light 
    intensity.
    """
    try:
        with open(fileName, 'rb') as f:
            mask = pkl.load(f)
    except:
        image = VF.extract_frame(vid, 0)
        plt.gray()
        plt.close()
        points = define_outer_edge(
            image, 'polygon',
            'Define a region for \n' + 'monitoring light intensity.')
        mask, points = IPF.create_polygon_mask(image, points)
        with open(fileName, 'wb') as f:
            pkl.dump(mask, f)

    return mask
t0 = tic()
t1 = tic()
    
# Get list of video files to process
pathToFiles = os.path.join(folder,fileString)
fileList = glob.glob(pathToFiles)
dataList = glob.glob(os.path.join(folder,'*' + suffix))
N = len(fileList)
hMatrix = 0

for i in range(0,N,1):
    # Parse the filename to get video info
    videoPath = fileList[i]
    print os.path.split(videoPath)[1]
    dataFile = fileList[i][:-4] + suffix
    RPM = VF.get_RPM_from_file_name(videoPath)
    fps = VF.get_FPS_from_file_name(videoPath)
    flowRate = VF.get_flowRate_from_file_name(videoPath)
    vid = VF.get_video_object(videoPath)
    
    # Ignore videos for which the reference frame has been rotated
    if '_rot' in videoPath:
        continue
    
    # Load homography matrix then mask data
    if hMatrix is 0:
        # Load homography matrix
        hMatrix = UIF.get_homography_matrix(homographyFile,vid)
        
        # Get mask data for ignoring areas not of interest
        maskData = UIF.get_mask_data(maskFile,vid,hMatrix,checkMasks)
Example #5
0
            # Load original data file and video file
            temp = glob.glob(dataFile)[0]
            with open(temp, 'rb') as f:
                container = pkl.load(f)
            hMatrix = container['hMatrix']
            maskData = container['maskData']
            temp = maskData['diskMask']
            maskData['mask'] = temp
            center = maskData['diskCenter']
            R = maskData['diskRadius']
            pix_per_cm = container['maskData']['diskRadius'] / diskScale
            cropRect = [
                center[0] - R, center[0] + R, center[1] - R, center[1] + R
            ]
            vid = VF.get_video_object(videoPath)

            # Load rCrit and determine corresponding frame number
            rCritQList = rCritData['QList']
            rCritRPMList = rCritData['RPMList']
            i_Q = [
                k for k in range(len(rCritQList)) if QTarget == rCritQList[k]
            ]
            j_RPM = [
                k for k in range(len(rCritRPMList)) if RPM == rCritRPMList[k]
            ]
            rCrit = rCritData[condition][i_Q, j_RPM]
            num = [
                k for k in range(len(aMean))
                if aMean[k + 1] >= rCrit and aMean[k] <= rCrit
            ]