def script_init(sbj_id, day, disconnect_file): #Colors green = (0,200,0) labels, ratios, fig, pixel_points = rc.main(sbj_id, day) all_markers = {} start_time, end_time, start, end = get_disconnected_times(disconnect_file) for i in xrange(len(labels)): marker_cur = Marker(labels[i], ratios[i], pixel_points[i], start_time + timedelta(seconds=i)) if int(pixel_points[i][0]) not in all_markers: all_markers[int(pixel_points[i][0])] = {} if int(pixel_points[i][1]) not in all_markers[int(pixel_points[i][0])]: all_markers[int(pixel_points[i][0])][int(pixel_points[i][1])] = [] all_markers[int(pixel_points[i][0])][int(pixel_points[i][1])].append(marker_cur) canvas = agg.FigureCanvasAgg(fig) canvas.draw() renderer = canvas.get_renderer() raw_data = renderer.tostring_rgb() window = pygame.display.set_mode((1280, 480), pygame.locals.DOUBLEBUF) screen = pygame.display.get_surface() size = canvas.get_width_height() surf = pygame.image.fromstring(raw_data, size, "RGB") screen.blit(surf, (0,0)) pygame.draw.rect(screen, green,(0,0,80,40)) TextSurf, TextRect = text_objects("Restart", pygame.font.Font('freesansbold.ttf',20)) TextRect.center = ((40),(20)) screen.blit(TextSurf, TextRect) pygame.display.flip() return all_markers, screen
def display_image(selected, video_loc, vid_start_end, disconnect_file): start_time, end_time, start, end = get_disconnected_times(disconnect_file) video_starts = pickle.load(open("%s\%s_%i.p" % (vid_start_end, sbj_id, day), "rb")) if not disconnected(start, end, selected.time): vid_counter = len(video_starts) for i, video_start in enumerate(video_starts["start"]): if selected.time<=video_start: vid_counter=i-1 break vid_time = (selected.time-video_starts["start"][vid_counter]).seconds subprocess.call("ffmpeg -ss %i -i %s/%s_%i_%04i.avi -t 1 -f image2 -update 1 tmp.png" % (vid_time, video_loc, sbj_id, day, vid_counter), shell=True) #window = pygame.display.set_mode((1400, 600), pygame.locals.DOUBLEBUF) screen = pygame.display.get_surface() try: surf = pygame.image.load("tmp.png") os.remove("tmp.png") screen.blit(surf, (640,0)) pygame.display.update() except: print "image not found"
def main(): root_folder = "D:\\NancyStudyData\\ecog\\raw\\" sbj_ids = ['a86a4375', 'be66b17c', 'cb46fd46', 'da3971ee', 'fcb01f7a'] days = ['3','4','5','6','7','8','9','10'] sbj_ids = ['a86a4375'] days = ['2'] save_folder = "C:\\Users\\wangnxr\\Documents\\rao_lab\\video_analysis\\vid_real_time\\" for sbj_id in sbj_ids: for day in days: result = [] start_time, end_time, start, end = get_disconnected_times("C:\\Users\\wangnxr\\Documents" + \ "\\rao_lab\\video_analysis\\disconnect_times\\" \ + sbj_id + "_" + str(day) + ".txt") vid_count = 0 cur_time = start_time vid_name = root_folder + sbj_id + "\\" + sbj_id + "_" + day + \ "\\" + sbj_id + "_" + day + "_" + str(vid_count).zfill(4) + ".avi" with open(save_folder + sbj_id + "_" + day + ".csv", "wb") as csvfile: timewriter = csv.writer(csvfile) while os.path.exists(vid_name): pos = check_disconnect(cur_time, timedelta(seconds = 1), start) for t in pos: cur_time += end[t] - start[t] - timedelta(seconds = 0.5) timewriter.writerow([str(vid_count), cur_time.year, cur_time.month, cur_time.day, cur_time.hour, cur_time.minute, cur_time.second]) result.append([vid_count, cur_time]) vid_len = timedelta(seconds = get_len(root_folder + sbj_id + "\\" + sbj_id + "_" + day + "\\" + sbj_id + "_" + day + "_" + str(vid_count).zfill(4) + ".avi")) pos = check_disconnect(cur_time, vid_len, start) cur_time += vid_len for t in pos: cur_time += end[t] - start[t] - timedelta(seconds = 0.5) vid_count += 1 vid_name = root_folder + sbj_id + "\\" + sbj_id + "_" + day + \ "\\" + sbj_id + "_" + day + "_" + str(vid_count).zfill(4) + ".avi" pickle.dump(result, open(save_folder + sbj_id + "_" + day + ".p", "wb"))
import gc sbj_id = "d6532718" conversion_file = "/home/nancy/Documents/data_release/%s.csv" % sbj_id disconnect_times_dir = "/home/nancy/Documents/disconnect_times/" orig_edf_dir = "/data/decrypted_edf/%s/" % sbj_id save_dir = "/space/nancy/data_by_day/" def get_sample_len(time1, time2): return max(0, int((time2-time1).total_seconds()*1000)) conversion = pandas.read_csv(conversion_file) for d in range(conversion.shape[0]): orig_files = ["%s/%s.edf" % (orig_edf_dir, orig_file) for orig_file in [conversion["file1"][d],conversion["file2"][d],conversion["file3"][d]] if not isinstance(orig_file, float)] start_end_times = [get_disconnected_times("%s/%s.txt" % (disconnect_times_dir, orig_file))[:2] for orig_file in [conversion["file1"][d],conversion["file2"][d],conversion["file3"][d]] if not isinstance(orig_file, float)] final_start_time = datetime.strptime("01-%02i-1000 " % conversion["day"][d] + conversion["start_time"][d], '%m-%d-%Y %H:%M:%S:%f') final_end_time = datetime.strptime("01-%02i-1000 " % conversion["day"][d] + conversion["end_time"][d], '%m-%d-%Y %H:%M:%S:%f') final_start_time_orig = datetime.strptime(conversion["date"][d] + " " + conversion["start_time"][d], '%m/%d/%Y %H:%M:%S:%f') final_end_time_orig = datetime.strptime(conversion["date"][d] + " " + conversion["end_time"][d], '%m/%d/%Y %H:%M:%S:%f') # Set up final edf file edf_files = [pyedflib.EdfReader(file) for file in orig_files] n_channels = edf_files[0].getSignalLabels().index('ECGR') edf_out = pyedflib.EdfWriter("%s/%s_day_%i.edf" % (save_dir, sbj_id, conversion["day"][d]), n_channels) # De-identify date of patient stay and update start time edf_out.setHeader(edf_files[0].getHeader())