SoFunction
Updated on 2024-11-15

Python Eye Closure Length Standard Deviation Script Usage Example Code Explanation

Standard deviation of eye closure duration

Average eye closure length: identify the number of eye closures in a minute and the length of each closure, add up the cumulative length of each closure, and finally take the average value.

Closed-eye standard deviation formula

Sample data format

Text content

coding

Iterate through all txt text files in a folder

# Iterate over all txt files in a folder
def scanner_txt(inputSrc,txt_list):
    file_list = (inputSrc)
    for file in file_list:
        curr_file=(inputSrc,file)
        # Recursive implementation
        if((curr_file)):
            scanner_txt(curr_file,txt_list)
        else:
            curr_file_name=curr_file.split(".")
            curr_file_type=curr_file_name[len(curr_file_name)-1]
            if curr_file_type=="txt":
                txt_list.append(curr_file)
    return txt_list

Read the contents of the text and select the data which is "frame number, state type, state duration".

def readTxtDataProprocessing(filepath,kss7sd,kss8sd,kss9sd):
    resFrame=[]
    print(filepath)
    with open(filepath,encoding='utf-8') as f:
        for line in f:
            frame_info=(',')
            if(len(frame_info)<3):
                continue
            framenum=frame_info[0]
            frametime=frame_info[2].split('\n')[0]
            ([int(framenum),int(frametime)])
    ()
    #Eye closure lengths that are empty within the document are not processed
    if(len(resFrame)==0):
        return
    # Remove duplicate values and consolidate them into a uniform value
    totalSD=deduplicationData(resFrame)
    filename=('\\')[-1]
    kssLevel=('-')[2]
    print("file:{}  The standard deviation of overall eye closure duration was:{}".format(filename,totalSD))
    print()
    if(kssLevel=='KSS7'):
        (totalSD)
    elif(kssLevel=='KSS8'):
        (totalSD)
    elif(kssLevel=='KSS9'):
        (totalSD)

Removal of text data that occurs only once, and merging of data that occurs twice and three times

def deduplicationData(resFrame):
    uniqueRes = []
    framelength = len(resFrame)
    i = 0
    while (i < framelength):
        if (resFrame[i][0] == 17924):
            t = 1;
        count = 1
        if (i + 1 < framelength):
            if (resFrame[i][1] == resFrame[i + 1][1]):
                count += 1
        if (i + 1 >= framelength):
            break
        if (i + 2 < framelength):
            if (resFrame[i][1] == resFrame[i + 2][1]):
                count += 1

        if (count != 1):
            ([resFrame[i][0], resFrame[i][1]])
        i += count
        # Calculate the standard deviation of eye closure duration
    return closeEyeTimeSD(uniqueRes)
    # for i in range(len(uniqueRes)):
    #     print("frameNum:", uniqueRes[i][0], "frameTime:", uniqueRes[i][1], "ms")

Array division intervals by minute

#data The list obtained after de-duplication and frame selection.
def closeEyeTimeSD(data):
    # Segmented by every minute interval: 30 frames per second, 1800 frames per minute
    # Create a segmented array to hold open and closed eye data for the nth minute, divided into 12 segments.
    dividArr=[]
    for i in range(12):
        ([])
    for i in range(len(data)):
        index=int(data[i][0]/1800)
        dividArr[index].append(data[i][1])
    # Get the standard deviation
    sdArr=[]
    for i in range(len(dividArr)):
        avergeTime,sdArray=countTotalSD(dividArr[i])
        if(dividArr[i]!=[]):
            (sdArray)
            print("(prefix indicating ordinal number, e.g. first, number two etc){}The average blink duration in minutes is:{},The standard deviation of eye closure duration was:{}".format(i+1,avergeTime,sdArray))
    totalSD=countTotalSD(sdArr)[1]
    return totalSD

Calculate the standard deviation function

def countTotalSD(sdArray):
    if(len(sdArray)==0):
        return [0,0]
    sumArray = 0
    for i in sdArray:
        sumArray += i
    # Calculate the standard deviation of all
    averageSD = sumArray / len(sdArray)
    sumSD = 0
    for i in sdArray:
        sumSD += pow((i - averageSD), 2)
    totalSD = (sumSD / len(sdArray))
    return [averageSD,totalSD]

To this article on the Python closed-eye length standard deviation script to use the example code to explain the article is introduced to this, more related Python closed-eye length standard deviation content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!