def main(testName, dataPath, imagePath='images/white_background.jpg'): # Call loadData() and save results pos, fixations, saccade = loadData( dataPath) # Eventually this could use inputs to specify data leftEyeFrame = fixations.loc[fixations['eye'] == 'L'] # Filter only the rows for the left eye durations = leftEyeFrame['duration'].values xPositions = leftEyeFrame['x_pos'].values yPositions = leftEyeFrame['y_pos'].values fixDurations = leftEyeFrame['duration'].values """ fix - a dict with three keys: 'x', 'y', and 'dur' (each contain a numpy array) for the x and y coordinates and duration of each fixation """ fix = {'x': xPositions, 'y': yPositions, 'dur': fixDurations} try: fig = gazeplotter.draw_heatmap( fix, [GetSystemMetrics(0), GetSystemMetrics(1)], imagefile=imagePath) except ValueError as e: fig = gazeplotter.draw_heatmap( fix, [GetSystemMetrics(1), GetSystemMetrics(0)], imagefile=imagePath) plt.show() analyze(testName, pos, fixations, saccade)
def process_images(imglist, eventfile, ppname, imgdir, plotdir): # check if the image directory exists if not os.path.isdir(imgdir): raise Exception("ERROR: no image directory found; path '%s' does not exist!" % imgdir) # check if output directorie exist; if not, create it if not os.path.isdir(plotdir): os.mkdir(plotdir) # EXPERIMENT SPECS DISPSIZE = (1920,1080) # (px,px) SCREENSIZE = (39.9,29.9) # (cm,cm) SCREENDIST = 61.0 # cm PXPERCM = numpy.mean([DISPSIZE[0]/SCREENSIZE[0],DISPSIZE[1]/SCREENSIZE[1]]) # px/cm # read the file smidata = read_smioutput(eventfile, None, ag_mode=SMIModes.LEFT_ONLY, stop=None, debug=False) nokey = 0 processed = 0 # loop through trials for trialnr in range(len(imglist)): # load image name, saccades, and fixations imgname = imglist[int(trialnr)] try: saccades = smidata[trialnr]['events']['Esac'] # [starttime, endtime, duration, startx, starty, endx, endy] fixations = smidata[trialnr]['events']['Efix'] # [starttime, endtime, duration, endx, endy] except KeyError: print "No eyetracking data for trial %s (image: %s)" % (trialnr + 1, imgname) nokey += 1 continue except IndexError: print "No eyetracking data for trial %s (image: %s)" % (trialnr + 1, imgname) nokey += 1 continue processed += 1 # paths imagefile = os.path.join(imgdir, imgname) # rawplotfile = os.path.join(plotdir, "raw_data_%s_%d" % (ppname,trialnr)) scatterfile = os.path.join(plotdir, "fixations_%s_%d" % (ppname,trialnr)) scanpathfile = os.path.join(plotdir, "scanpath_%s_%d" % (ppname,trialnr)) heatmapfile = os.path.join(plotdir, "heatmap_%s_%d" % (ppname,trialnr)) # raw data points # draw_raw(smidata[trialnr]['x'], smidata[trialnr]['y'], DISPSIZE, imagefile=imagefile, savefilename=rawplotfile) # fixations draw_fixations(fixations, DISPSIZE, imagefile=imagefile, durationsize=True, durationcolour=False, alpha=0.5, savefilename=scatterfile) # scanpath draw_scanpath(fixations, saccades, DISPSIZE, imagefile=imagefile, alpha=0.5, savefilename=scanpathfile) # heatmap draw_heatmap(fixations, DISPSIZE, imagefile=imagefile, durationweight=True, alpha=0.75, savefilename=heatmapfile) print "%s images processed. %s sections without eyetracker data." % (processed, nokey)
def generate_plots(IMGDIR, PLOTDIR, filename, fixations, saccades, data, DISPSIZE, trialnr): # cognitive load if int(filename[4:5]) == 1: cognitive_load = "LOW" else: cognitive_load = "HIGH" #imgname = "screenshot.jpg" #imagefile = os.path.join(IMGDIR,imgname) # paths rawplotfile = os.path.join( PLOTDIR, "raw_data_%s_%s" % (filename[:3], cognitive_load)) scatterfile = os.path.join( PLOTDIR, "fixations_%s_%s" % (filename[:3], cognitive_load)) scanpathfile = os.path.join( PLOTDIR, "scanpath_%s_%s" % (filename[:3], cognitive_load)) heatmapfile = os.path.join( PLOTDIR, "heatmap_%s_%s" % (filename[:3], cognitive_load)) # raw data points draw_raw(data[trialnr]['x'], data[trialnr]['y'], DISPSIZE, imagefile=None, savefilename=rawplotfile) # fixations draw_fixations(fixations, DISPSIZE, imagefile=None, durationsize=True, durationcolour=False, alpha=0.5, savefilename=scatterfile) # scanpath draw_scanpath(fixations, saccades, DISPSIZE, imagefile=None, alpha=0.5, savefilename=scanpathfile) # heatmap draw_heatmap(fixations, DISPSIZE, imagefile=None, durationweight=True, alpha=0.5, savefilename=heatmapfile)
# plot fixations fig = gazeplotter.draw_fixations( fixations, DISPSIZE, imagefile=os.path.join(IMGDIR, "%s%s" % (imgname, ext)), durationsize=True, durationcolour=False, alpha=0.5, savefilename=os.path.join( OUTDIR, "fixations_%s_%s.png" % (trackertype, imgname))) pyplot.close(fig) # create heatplot fig = gazeplotter.draw_heatmap( fixations, DISPSIZE, imagefile=os.path.join(IMGDIR, "%s%s" % (imgname, ext)), durationweight=True, alpha=0.5, savefilename=os.path.join( OUTDIR, "heatmap_%s_%s.png" % (trackertype, imgname))) pyplot.close(fig) # PRECISION AND ACCURACY AVERAGES # empty dicts alldata = {} for ppname in PPS: # read data file datafile = open( os.path.join(IOUTDIR, ppname, trackertype, "ROI_analysis.txt"), 'r') data = datafile.readlines() datafile.close()
heatmapfile = os.path.join(pplotdir, "heatmap_%s_%d" % (ppname, trialnr)) ##raw data points #draw_raw(edfdata[trialnr]['x'], edfdata[trialnr]['y'],edfdata[trialnr]['size'], DISPSIZE, imagefile=imagefile, savefilename=rawplotfile) #fixations draw_fixations(fixations, DISPSIZE, imagefile=imagefile, durationsize=True, durationcolour=True, alpha=0.5, savefilename=scatterfile) ##scanpath draw_scanpath(fixations, saccades, DISPSIZE, imagefile=imagefile, alpha=0.5, savefilename=scanpathfile) # heatmap draw_heatmap(fixations, DISPSIZE, imagefile=imagefile, durationweight=True, alpha=0.5, savefilename=heatmapfile)
# PLOT DATA for i in range(len(imagedata)): # image name imgname = os.path.splitext(os.path.basename(IMAGES[i]))[0] # pickle fixation data fixfile = open(os.path.join(outputdir, "fixations_%s.dat" % imgname), 'w') pickle.dump(imagedata[i]['events']['Efix'], fixfile) fixfile.close() # plot raw data fig = gazeplotter.draw_raw(imagedata[i]['x'], imagedata[i]['y'], DISPSIZE, imagefile=IMAGES[i], savefilename=os.path.join(outputdir, "image_%s_raw.png" % imgname)) pyplot.close(fig) # plot fixations fig = gazeplotter.draw_fixations(imagedata[i]['events']['Efix'], DISPSIZE, imagefile=IMAGES[i], durationsize=True, durationcolour=False, alpha=0.5, savefilename=os.path.join(outputdir, "image_%s_fixations.png" % imgname)) pyplot.close(fig) # draw heatmap fig = gazeplotter.draw_heatmap(imagedata[i]['events']['Efix'], DISPSIZE, imagefile=IMAGES[i], durationweight=True, alpha=0.5, savefilename=os.path.join(outputdir, "image_%s_heatmap.png" % imgname)) pyplot.close(fig) # ROI ANALYSIS if "01F_HA_O" in IMAGES[i]: # empty dicts Nfix = {} fixdur = {} dwelltime = {} # go through all ROI names for ROIname in ROILIST: # starting values Nfix[ROIname] = 0 fixdur[ROIname] = 0 dwelltime[ROIname] = 0 # go through all fixations
# loop through all images for imgname in IMAGES: # get rid of image path and extension imgname, ext = os.path.splitext(os.path.basename(imgname)) # empty lists fixations = [] # loop through all participants for ppname in PPS: fixfile = open(os.path.join(IOUTDIR, ppname, trackertype, "fixations_%s.dat" % imgname), 'r') fixations.extend(pickle.load(fixfile)) fixfile.close() # plot fixations fig = gazeplotter.draw_fixations(fixations, DISPSIZE, imagefile=os.path.join(IMGDIR, "%s%s" % (imgname,ext)), durationsize=True, durationcolour=False, alpha=0.5, savefilename=os.path.join(OUTDIR, "fixations_%s_%s.png" % (trackertype,imgname))) pyplot.close(fig) # create heatplot fig = gazeplotter.draw_heatmap(fixations, DISPSIZE, imagefile=os.path.join(IMGDIR, "%s%s" % (imgname,ext)), durationweight=True, alpha=0.5, savefilename=os.path.join(OUTDIR, "heatmap_%s_%s.png" % (trackertype,imgname))) pyplot.close(fig) # PRECISION AND ACCURACY AVERAGES # empty dicts alldata = {} for ppname in PPS: # read data file datafile = open(os.path.join(IOUTDIR, ppname, trackertype, "ROI_analysis.txt"), 'r') data = datafile.readlines() datafile.close() # parse data for i in range(len(data)): data[i] = data[i].replace('\n','').replace('\r','').replace('"','').replace("'","").split('\t') header = data.pop(0) # loop through all fields in the header
if '.jpg' in msg[1]: imgname = os.path.splitext(msg[1])[0] imgfile = os.path.join(IMGDIR, msg[1]) # get the fixations fixations = gazedata[trialnr]['events']['Efix'] print "outdir:", outdir # plot the samples gazeplotter.draw_raw(gazedata[trialnr]['x'], gazedata[trialnr]['y'], RESOLUTION, imagefile=imgfile, savefilename=os.path.join(outdir, '%s_samples.png' % imgname)) # plot the fixations gazeplotter.draw_fixations(fixations, RESOLUTION, imagefile=imgfile, durationsize=True, durationcolour=False, alpha=0.5, savefilename=os.path.join( outdir, '%s_fixations.png' % imgname)) # plot a heatmap gazeplotter.draw_heatmap(fixations, RESOLUTION, imagefile=imgfile, durationweight=True, alpha=0.5, savefilename=os.path.join( outdir, '%s_heatmap.png' % imgname))
# PLOTS print("plotting gaze data") # loop through trials for trialnr in range(len(data)): # load image name, saccades, and fixations imgname = data[trialnr][header.index("image")] saccades = edfdata[trialnr]['events']['Esac'] # [starttime, endtime, duration, startx, starty, endx, endy] fixations = edfdata[trialnr]['events']['Efix'] # [starttime, endtime, duration, endx, endy] # paths imagefile = os.path.join(IMGDIR,imgname) rawplotfile = os.path.join(pplotdir, "raw_data_%s_%d" % (ppname,trialnr)) scatterfile = os.path.join(pplotdir, "fixations_%s_%d" % (ppname,trialnr)) scanpathfile = os.path.join(pplotdir, "scanpath_%s_%d" % (ppname,trialnr)) heatmapfile = os.path.join(pplotdir, "heatmap_%s_%d" % (ppname,trialnr)) # raw data points draw_raw(edfdata[trialnr]['x'], edfdata[trialnr]['y'], DISPSIZE, imagefile=imagefile, savefilename=rawplotfile) # fixations draw_fixations(fixations, DISPSIZE, imagefile=imagefile, durationsize=True, durationcolour=False, alpha=0.5, savefilename=scatterfile) # scanpath draw_scanpath(fixations, saccades, DISPSIZE, imagefile=imagefile, alpha=0.5, savefilename=scanpathfile) # heatmap draw_heatmap(fixations, DISPSIZE, imagefile=imagefile, durationweight=True, alpha=0.5, savefilename=heatmapfile)
def heatMap(startTime, endTime, totalTime): #(in seconds) (later converted to millis) #start time of video segment #end time of video segment #total time of video #saveLocation: location where output heatmap is to be saved #fixationFilePath: location of fixation csv #videoLocation: location of video #imageWidth and IimageHeight: dimension of a frame in a video saveLocation = '/Users/jeffhe/Desktop/commitments/urap/resources/heatmap_processing/0308_1528_manual_03/heatmat.png' fixationFilePath = "/Users/jeffhe/Desktop/commitments/urap/resources/heatmap_processing/0308_1528_manual_03/fixations.csv" gazeFilePath = "/Users/jeffhe/Desktop/commitments/urap/resources/heatmap_processing/0308_1528_manual_03/gaze_positions.csv" videoLocation = "/Users/jeffhe/Desktop/commitments/urap/resources/heatmap_processing/0308_1528_manual_03/worldwithoutgaze.mp4" imageWidth = 1280 imageHeight = 720 def changeStartTimeMillis(): with open(gazeFilePath) as f: reader = csv.reader(f) reader = list(reader) startTimeSeconds = float(reader[1][0]) return startTimeSeconds startTimeMillis = changeStartTimeMillis() * 1000 vidcap = cv2.VideoCapture(videoLocation) frames = [] success, image = vidcap.read() frames += [image] while success: success, image = vidcap.read() frames += [image] startFrame = (startTime / totalTime) * len(frames) endFrame = (endTime / totalTime) * len(frames) image = frames[int((startFrame + endFrame) / 2)] startTime = startTime * 1000 endTime = endTime * 1000 with open(fixationFilePath) as f: reader = csv.reader(f) line_num = 0 Efix = [] for row in reader: if (line_num != 0): start = float(row[1]) * 1000 - startTimeMillis Efix += [[ start, start + float(row[2]), float(row[2]), int(float(row[5]) * imageWidth), imageHeight - int(float(row[6]) * imageHeight) ]] line_num += 1 i = 0 while (i < len(Efix)): start = Efix[i][0] end = Efix[i][1] print(startTime, end) if (startTime > end): Efix.remove(Efix[i]) continue if (endTime < start): Efix.remove(Efix[i]) continue i += 1 draw_heatmap(Efix, (imageWidth, imageHeight), image, savefilename=saveLocation) print(Efix)
def read_files(reduced, csvonly): # Loop through all files for ppname in PPS: print("loading gaze data") # path fp = os.path.join(DATADIR, '%s.tsv' % ppname) # check if the path exist if not os.path.isfile(fp): raise Exception( "No eye data file file found for participant '%s'" % (ppname)) # read the file tobiidata = read_tobii(fp, reduced, missing=0.0, debug=True) #save_csv(tobiidata) pplotdir = os.path.join(PLOTDIR, ppname[:3]) if not csvonly: print("plotting gaze data") # Loop through trials for trialnr in range(len(tobiidata)): # Load image name, saccades, and fixations imgname = "screenshot.jpg" imagefile = os.path.join(IMGDIR, imgname) print(imagefile) # [starttime, endtime, duration, startx, starty, endx, endy] saccades = tobiidata[trialnr]['events']['Esac'] # [starttime, endtime, duration, endx, endy] fixations = tobiidata[trialnr]['events']['Efix'] # cognitive load if int(ppname[4:5]) == 1: cognitive_load = "LOW" else: cognitive_load = "HIGH" if reduced: suffix = "_reduced" else: suffix = "" # Save the fixation and saccades in csv format for MATLAB save_csv(fixations, saccades, ppname, suffix) if not csvonly: # paths rawplotfile = os.path.join( pplotdir, "raw_data_%s_%s%s" % (ppname[:3], cognitive_load, suffix)) scatterfile = os.path.join( pplotdir, "fixations_%s_%s%s" % (ppname[:3], cognitive_load, suffix)) scanpathfile = os.path.join( pplotdir, "scanpath_%s_%s%s" % (ppname[:3], cognitive_load, suffix)) heatmapfile = os.path.join( pplotdir, "heatmap_%s_%s%s" % (ppname[:3], cognitive_load, suffix)) # raw data points draw_raw(tobiidata[trialnr]['x'], tobiidata[trialnr]['y'], DISPSIZE, imagefile=None, savefilename=rawplotfile) # fixations draw_fixations(fixations, DISPSIZE, imagefile=None, durationsize=True, durationcolour=False, alpha=0.5, savefilename=scatterfile) # scanpath draw_scanpath(fixations, saccades, DISPSIZE, imagefile=None, alpha=0.5, savefilename=scanpathfile) # heatmap draw_heatmap(fixations, DISPSIZE, imagefile=None, durationweight=True, alpha=0.5, savefilename=heatmapfile)
def heatMap(startTime, endTime, totalTime): #(in seconds) #start time of video segment you want to extract #end time of video segment you want to extract #total time of video #saveLocation: location where output heatmap is to be saved #gazeFilePath: location of gaze csv #videoLocation: location of video #imageWidth and IimageHeight: dimension of a frame in a video saveLocation = '/Users/jeffhe/Desktop/commitments/urap/resources/heatmap_processing/0308_1630_text_02/heatmap.png' gazeFilePath = "/Users/jeffhe/Desktop/commitments/urap/resources/heatmap_processing/0308_1630_text_02/gaze_positions.csv" videoLocation = "/Users/jeffhe/Desktop/commitments/urap/resources/heatmap_processing/0308_1630_text_02/world.mp4" imageWidth = 1280 imageHeight = 720 def StartTimeSeconds(): with open(gazeFilePath) as f: reader = csv.reader(f) reader = list(reader) startTimeSeconds = float(reader[1][0]) return startTimeSeconds startTimeSeconds = StartTimeSeconds() + startTime endTimeSeconds = StartTimeSeconds() + endTime vidcap = cv2.VideoCapture(videoLocation) frames = [] success, image = vidcap.read() frames += [image] while success: success, image = vidcap.read() frames += [image] startFrame = (startTime / totalTime) * len(frames) endFrame = (endTime / totalTime) * len(frames) image = frames[int((startFrame + endFrame) / 2)] with open(gazeFilePath) as f: reader = csv.reader(f) line_num = 0 norm_pos_x = [] norm_pos_y = [] timeStamps = [] for row in reader: if (line_num != 0): if not (float(row[2]) < 0.5 or float(row[3]) > 1 or float(row[3]) < 0 or float(row[4]) > 1 or float(row[4]) < 0): if (float(row[0]) > startTimeSeconds and float(row[0]) < endTimeSeconds): norm_pos_x += [int(float(row[3]) * 1280)] norm_pos_y += [imageHeight - int(float(row[4]) * 720)] timeStamps += [float(row[0])] line_num += 1 count = 0 while (count < len(timeStamps)): timeStamps[count] *= 1000 count += 1 Sfix, Efix = fixation_detection(norm_pos_x, norm_pos_y, timeStamps) draw_heatmap(Efix, (imageWidth, imageHeight), image, savefilename=saveLocation) #the above function call isn't necessary in Shaantam's case. The fixation data is stored in Efix #in the form of [[start time, end time, duration, x-coordinates, y-coordinates][][]...] #here, the y-coordinate is calculated from top (meaning that top pixel is 0, bottom pixel is 720) return Efix
print i + 4 print gazedata[i]['events']['msg'] # imgname = gazedata[i]['events']['msg'][71][1].split()[-1] imgname = imgs[i + 4] imgname = imgname.split('.')[0] + '.jpg' imgfile = os.path.join(IMGDIR, imgname) print imgname, imgfile # get the fixations fixations = gazedata[i]['events']['Efix'] # get the saccades saccades = gazedata[i]['events']['Esac'] # plot a heatmap gazeplotter.draw_heatmap(fixations, RESOLUTION, imagefile=imgfile, alpha=0.5, invert_x=True, invert_y=False, savefilename=os.path.join( outdir, '%s_fix.png' % imgname)) gazeplotter.draw_fixations(fixations, RESOLUTION, imagefile=imgfile, alpha=0.5, invert_x=True, invert_y=False, savefilename=os.path.join( outdir, '%s_heatmap.png' % imgname)) # gazeplotter.draw_scanpath(fixations, saccades, RESOLUTION, imagefile=imgfile, # alpha=0.5, invert_x=True, invert_y=False,