def imBreak(rem, tot, progress): W_bg, H_bg = (350, 450) background = Image.new('RGB', (W_bg, H_bg), color=(0, 0, 0)) pointsize = 80 draw = ImageDraw.Draw(background) file = 'images/break/0.png' img = Image.open('{0:s}'.format(file), 'r') W, H = img.size bar_full_w = int(rem * W / tot) bar_empty_w = W_bg bar_h = H // 15 base_h = H - bar_h # base_w = p = rem / tot R = int((1 - p**0.7) * 255) G = int(p**0.7 * 255) B = 0 bar_full = Image.new('RGB', (bar_full_w, bar_h), color=(R, G, B)) bar_empty = Image.new('RGB', (bar_empty_w, bar_h), color=(R // 4, G // 4, B // 4)) img.paste(bar_empty, (W_bg // 2 - bar_empty_w // 2, base_h)) img.paste(bar_full, (W_bg // 2 - bar_empty_w // 2, base_h)) bar_w, bar_h = (W_bg // 20 - 6, H_bg // 40) bar_full = Image.new('RGB', (bar_w, bar_h), color=(0, 255, 0)) bar_empty = Image.new('RGB', (bar_w, bar_h), color=(0, 255 // 4, 0)) full_num = int(progress * 20) base_w = 3 base_h = 0 cnt = 0 while cnt < 20: if full_num > 0: background.paste(bar_full, ((6 + bar_w) * cnt + base_w, base_h)) full_num -= 1 else: background.paste(bar_empty, ((6 + bar_w) * cnt + base_w, base_h)) cnt += 1 font_progress = ImageFont.truetype( '/usr/share/fonts/truetype/ubuntu/UbuntuMono-B.ttf', pointsize * 2 // 5) text_progress = '> {0:d}% Complete <'.format(int(progress * 100)) text_size_progress = draw.textsize( text_progress, font=font_progress) # the size of the text box! x_progress = (W_bg / 2) - (text_size_progress[0] / 2) y_progress = bar_h + 30 - (text_size_progress[1] / 2) draw.text((x_progress, y_progress), text_progress, font=font_progress, fill=(100, 100, 100)) background.paste(img, (W_bg // 2 - img.size[1] // 2, 80)) background.save('images/break.png') imChange('break', resize=False)
def takeBreak(sec): t = time.time() prev = t index = 1 while index <= 5: imChange('break/{0:d}'.format(6 - index)) while time.time() - t < index * sec / 5: if time.time() - prev > 1: print(sec2clk((int)(t + 300 - time.time())), end='\r') prev = time.time() None index = index + 1 imChange('break/0') walkman.play('playback/beep.wav')
def playFile(truth, subject, sender=None): dictn = {'rest1': 0.5, 'delay': 1.5, 'listen': 1.5} t = list() t.append(time.time()) # Load file data, fs = walkman.load(truth['path']) print('Starting trial {0:s}...'.format(truth['path'])) # Playing file t.append(time.time()) # append time when starts playing print('Playing') walkman.play(data, fs) # Keypress time t.append(time.time()) # append time when stops playing imChange('keypress') print('Keypress...') keypress = 3 # getch() t.append(time.time()) # append time when key is pressed while keypress > 2: keypress = ord(getch()) - ord(str(1)) + 1 print(keypress) # while int(float(keypress)) > 2: # keypress = getch() # print(int(float(keypress))) with open('recordings/{0:s}/keys.csv'.format(subject), 'a+') as file: file.write( '{0:d},{1:s},{2:d},{3:s},{4:s},{5:5.2f},{6:5.3f},{7:5.3f}\n'. format(truth['prompt'], truth['path'], truth['npkr'], str(keypress), truth['trial'], t[1] - t[0], t[2] - t[0], t[3] - t[0])) if keypress == truth['npkr']: imChange('correct') score = 1 else: imChange('wrong') score = 0 time.sleep(1) imChange('blank') # trial ends return score
def playFile(truth, sample, model, trans, nnet_dict): t = list() # Starting trial t.append(time.time()) imChange('blank') data, fs = walkman.load(truth['path']) print('Starting trial {0:s}...'.format(truth['path'])) while time.time() - t[0] < 1.5: None # File will play after 1.5 seconds t.append(time.time()) imChange('listen') while time.time() - t[0] < 2: None # Playing file print('Playing') walkman.play(data, fs) while time.time() - t[0] < 3.5: None # Rest time begins t.append(time.time()) print('Rest') imChange('rest') while time.time() - t[0] < 5: None # Speak time print('Speak') imChange('speak') t.append(time.time()) rec, fs = walkman.record_nowait(2.5, 16000, 1) while (time.time() - t[3] < 2.5): None # Recording ends here. Scoring can now begin t.append(time.time()) imChange('triangle') print('Saving') walkman.save(sample['path'], rec, 16000) # Doing VAD first = time.time() # sample['path'] = 'recordings/data2.wav' vad.makeVad(sample['path'], 'temp.wav') sample['path'] = 'temp.wav' # Scoring print('Scoring') sc = score(sample, truth, model, trans, nnet_dict) print(sc) print('Scored in {0:.2f} seconds\n'.format(time.time() - first)) imChange('blank') return t
def phase2b(truth, sample): #, model, trans, nnet_dict): dict = config.getPhase2b() t = list() # Starting trial t.append(time.time()) imChange('blank') data, fs = walkman.load(truth['endpath']) print('Starting trial {0:s}...'.format(truth['path'])) while time.time() - t[0] < dict['rest1']: None t.append(time.time()) imChange(truth['image'], path=truth['impath']) print('Image') while time.time() - t[0] < dict['rest1'] + dict['image']: None imChange('blank') time.sleep(dict['rest2']) # Speak time print('Speak') imChange('speak') t.append(time.time()) rec, fs = walkman.record_nowait(2.5, 16000, 1) while (time.time() - t[2] < dict['record']): None # Recording ends here. Scoring can now begin t.append(time.time()) imChange('triangle') print('Saving') walkman.save(sample['path'], rec, 16000) # Playing file imChange('listen') print('Playing') walkman.play(data, fs) while time.time() - t[3] < dict['listen']: None # # Doing VAD # first = time.time() # # sample['path'] = 'recordings/data2.wav' # vad.makeVad(sample['path'], 'temp.wav') # sample['path'] = 'temp.wav' # # Scoring # print('Scoring') # sc = score(sample, truth, model, trans, nnet_dict) # print(sc) # print('Scored in {0:.2f} seconds\n'.format(time.time() - first)) # # Show score image # imScore(sc) # time.sleep(dict['score']) return t
def phase2a(truth, sample): dict = config.getPhase2a() t = list() # Starting trial t.append(time.time()) imChange('blank') data, fs = walkman.load(truth['endpath']) print('Starting trial {0:s}...'.format(truth['path'])) while time.time() - t[0] < dict['rest1']: None t.append(time.time()) imChange(truth['image'], path=truth['impath']) print('Image') while time.time() - t[0] < dict['rest1'] + dict['image']: None imChange('listen') time.sleep(dict['delay']) # Playing file print('Playing') walkman.play(data, fs) while time.time() - t[0] < dict['rest1'] + dict['image'] + dict['listen']: None # Rest time begins t.append(time.time()) print('Rest') imChange('rest') while time.time( ) - t[0] < dict['rest1'] + dict['image'] + dict['listen'] + dict['rest2']: None # Speak time print('Speak') imChange('speak') t.append(time.time()) rec, fs = walkman.record_nowait(2.5, 16000, 1) while (time.time() - t[3] < dict['record']): None # Recording ends here. Scoring can now begin t.append(time.time()) imChange('triangle') print('Saving') walkman.save(sample['path'], rec, 16000) imChange('blank') return t
from functions.pauser import Pauser from getch import getch csv_path = './data/' file = open("pause.txt","w") file.write("Play") file.close() def isTrial(): if len(sys.argv) > 1: if any(x == 'trial' for x in sys.argv[1:]): return True return False imChange('blank') # if open_site: # may be needed for linux os # os.system('xdg-open main.html') os.system('clear') print('Enter stimuli category (chin/eng): ') stim_category = '{0:s}'.format(input()) if stim_category == 'chin': train_csv_filename = 'train_stimuli_list_chineese.csv' test_csv_filename = 'train_stimuli_list_chineese.csv' elif stim_category == 'eng': train_csv_filename = 'train_stimuli_list_english.csv' test_csv_filename = 'train_stimuli_list_english.csv' else: print('Check stimilus category!') print(1/0)
def imScore(score): if score > 0.7: imChange('good') else: imChange('bad')
# ------------------------------- # dict = config.getGeneral() keypress_after_break = dict[ 'keypress'] # Needs a manual keypress in the terminal for 'True' start_prompt = dict['start'] break_time = dict['break'] # Seconds short_break_time = dict['short_break'] trials_per_break = dict[ 'trials'] # One trial is one complete cycle of playback, recording and scoring of a single .wav file trials_per_short_break = dict['short_trials'] open_site = False # Automatically opens the site # ------------------------------- # imChange('blank') if open_site: os.system('xdg-open main.html') os.system('clear') prompts = xls2list('data/prompts.xlsx') labels = pd.read_excel('data/labels.xlsx') print('Enter subject number : ') subject = 'S{0:s}'.format(input()) outfolder = 'recordings/{0:s}'.format(subject) out_time = 'timestamp/{0:s}.mat'.format(subject) os.system('mkdir -p {0:s}'.format(outfolder)) # model = {'eng':load_model('models/eng/model_cut.h5'), 'jap':load_model('models/jap/model_cut.h5')} # trans = {'eng':sio.loadmat('models/eng/feature_transform.mat'), 'jap':sio.loadmat('models/jap/feature_transform.mat')}