Best Python code snippet using avocado_python
ocr.py
Source:ocr.py  
1from PIL import Image2import pytesseract3import argparse4import cv25import os6import numpy as np7import re8import nltk9nltk.download('stopwords')10from nltk.corpus import stopwords11from nltk.stem.porter import PorterStemmer12from sklearn.feature_extraction.text import CountVectorizer13# load the example image and convert it to grayscale14image = cv2.imread("example6.png")15#cv2.imshow("image",image)16#cv2.waitKey(0)17#print type(image)18gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)19#cv2.imshow("grayscale",gray)20#cv2.waitKey(0)21#print type(gray)22_ ,gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)23#cv2.imshow("grayscale_tesh",gray)24#cv2.waitKey(0)25 26# make a check to see if median blurring should be done to remove27# noise28gray = cv2.medianBlur(gray, 3)29#cv2.imshow("grayscale_blurr",gray)30#cv2.waitKey(0)31 32# write the grayscale image to disk as a temporary file so we can33# apply OCR to it34filename = "{}.png".format(os.getpid())35print (filename)36cv2.imwrite(filename, gray)37# load the image as a PIL/Pillow image, apply OCR, and then delete38# the temporary file39text = pytesseract.image_to_string(Image.open(filename))40os.remove(filename)41print(text)42text=text.split()43test=' '.join(text)44x_test=[]45from nltk import tokenize46#nltk.download('punkt')47x_test=tokenize.sent_tokenize(test)48x_size=len(x_test)49###########################################################################################################50######## or or orr or    audio  speech #####################51import speech_recognition as sr52 53# obtain audio from the microphone54r = sr.Recognizer()55with sr.Microphone() as source:56    57     58    print("Say something!")59    audio = r.listen(source)60   61# recognize speech using Google Speech Recognition62    try:63         64        # for testing purposes, we're just using the default API key65        # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`66        # instead of `r.recognize_google(audio)`67        x_test = r.recognize_google(audio)68        print("Google Speech Recognition thinks you said " + x_test)69    except sr.UnknownValueError:70         71        print("Google Speech Recognition could not understand audio")72    except sr.RequestError as e:73        print("Could not request results from Google Speech Recognition service; {0}".format(e))74    75    76###########################################################################################################77###########################################################################################################78######## emotion detection79from PIL import Image80import pytesseract81import argparse82import cv283import os84import numpy as np85import re86import nltk87nltk.download('stopwords')88from nltk.corpus import stopwords89from nltk.stem.porter import PorterStemmer90from sklearn.feature_extraction.text import CountVectorizer91###importing data sets92import pandas as pd93dataset=pd.read_csv('texttsv.tsv',delimiter='\t',encoding = "ISO-8859-1")94#dataset=pd.read_csv('Restaurant_Reviews.tsv',delimiter='\t',quoting=3)95##categorizing input96x=dataset.iloc[:,0]97y=dataset.iloc[:,1]98from sklearn.preprocessing import LabelEncoder,OneHotEncoder99label_y=LabelEncoder()100y_cat1=label_y.fit_transform(y)101onehotencoder=OneHotEncoder(categorical_features=[0])102y_cat=onehotencoder.fit_transform(y_cat1.reshape(-1,1)).toarray()103y_cat=y_cat[:,1:13]104##################################################################################105############ ########        train neural networks ############106ps=PorterStemmer()107corpus=[]108for i in range (0,40000):109    110    test_train=re.sub('[^a-zA-z]',' ',dataset['content'][i])111    test_train=test_train.lower()112    test_train=test_train.split()113    test_train=[ps.stem(word) for word in test_train   ]114    test_train=' '.join(test_train)115    corpus.append(test_train)116cv=CountVectorizer(max_features=30000)117x=cv.fit_transform(corpus).toarray()118#y_cat=y_cat[0:2000]119##################################################################################120####### spiltting121from sklearn.model_selection import train_test_split122X_train, X_test, Y_train, Y_test = train_test_split(x, y_cat, test_size = 0.2, random_state = 0)   123import keras124from keras.models import Sequential125#from.keras.models import Dense126from keras.layers.core import Dense127from keras.models import load_model128from keras.layers import Dropout129clasy=Sequential()130clasy.add(Dense(output_dim = 8000, init = 'uniform', activation = 'relu', input_dim=4000 ))131clasy.add(Dropout(rate = 0.3))132#clasy.add(Dense(output_dim = 600, init = 'uniform', activation = 'relu'))133#clasy.add(Dropout(rate = 0.1))134#clasy.add(Dense(output_dim = 3000, init = 'uniform', activation = 'relu'))135#clasy.add(Dropout(rate = 0.3))136clasy.add(Dense(output_dim = 8000, init = 'uniform', activation = 'relu'))137#clasy.add(Dense(output_dim = 4800, init = 'uniform', activation = 'relu'))138clasy.add(Dropout(rate = 0.3))139#clasy.add(Dense(output_dim = 12000, init = 'uniform', activation = 'relu'))140clasy.add(Dense(output_dim = 12, init = 'uniform', activation = 'sigmoid'))141clasy.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])142#clasy.fit(x,y_cat,batch_size=10,nb_epoch=15)143clasy.fit(X_train,Y_train,batch_size=10,nb_epoch=1)144#clasy=load_model("2000_3000_3000_12.h5")145#clasy.save('5k-4k-5k-12.h5')146y_pred=clasy.predict(X_test)147from sklearn.metrics import accuracy_score148accuracy_score(Y_test, y_pred.round())149#accuracy_score(np.array(Y_test, y_pred), np.ones((2, 2)))150########################### predict ing ###############################################151##nltk processing152ps=PorterStemmer()153combined_all=[0,0,0,0,0,0,0,0,0,0,0,0]154for m in range (0,x_size):155    corpus=[]156    for i in range (0,40000):157        test_train=re.sub('[^a-zA-z]',' ',dataset['content'][i])158        test_train=test_train.lower()159        test_train=test_train.split()160        test_train=[ps.stem(word) for word in test_train if not word in set(stopwords.words('english'))]161        test_train=' '.join(test_train)162        corpus.append(test_train)163    164    165#testing_prepossing166    test_pre=re.sub('[^a-zA-z]',' ',x_test[m])167    test_pre=test_pre.lower()168    test_pre=test_pre.split()169    test_pre=[ps.stem(word) for word in test_pre if not word in set(stopwords.words('english'))]170    test_pre=' '.join(test_pre)171    corpus.append(test_pre)   172 173#tokenizing174    175    cv=CountVectorizer(max_features=2000)176    x=cv.fit_transform(corpus).toarray()177#y=dataset.iloc[:,1].values178    x_pred_val=[]179    x_pred_val=x[40000:40001,0:2000]180    x=x[0:40000,0:2000]181#co rpus=x[0:40000,0:30000]182   183    184##predict185    y_pred=clasy.predict(x_pred_val)186   187   188    prob_pred=[]189    prob_pred1=[]190    for k in range (0,12):191        prob_pred=y_pred[0][k]192        prob_pred=prob_pred * 100193        prob_pred=float("{0:.2f}".format(prob_pred))194        prob_pred1.append(prob_pred)195    pred_val=[]196    pred_val=["boredom","empty","enthusiam","fun","happiness","hate","love","netural","relief","sadness","suprise","worry"]197#### to print it to i console #####198    #combined = np.vstack((pred_val, prob_pred1)).T199    #print('yout text was = ',x_test[m])200    #for a in range (0,12):201        #print(pred_val[a],'=',prob_pred1[a],'%')202        203    for q in range (0,12):204        combined_all[q]=combined_all[q] +  prob_pred1[q]205        206        207#################  printing to file #############208######## delete outputfilr if existed #########209    with open('outputfile.txt', 'a') as f:210        print(x_test[m],'\n',file=f)211        for r in range (0,12): 212            print(pred_val[r],'=',prob_pred1[r],'%', file=f)213        print('\n',file=f)214    f.close()215with open('outputfile.txt', 'a') as f:216    print('over all emotion','\n',file=f)   217    for w in range (0,12):  218        combined_all[w]=combined_all[w]/x_size219        combined_all[w]=float("{0:.2f}".format(  combined_all[w]))220        print(pred_val[w],'=',combined_all[w],'%', file=f)221f.close()...data_splitter.py
Source:data_splitter.py  
1import numpy as np2class Data_splitter(object):3    """docstring for Data_splitter"""4    def __init__(self):5        return678    def auto_split_data(self, num_valid, num_test, X, y):9        l = len(set(y))10        standard = self.cnt_labels_rate(y, l)11        # é¦å
åå²åºéªè¯é12        valid_pre = self.lowest_square_idx(num_valid, y, l, standard)13        # print(valid_pre)14        # åå²15        X_valid = X[valid_pre:valid_pre+num_valid]16        y_valid = y[valid_pre:valid_pre+num_valid]17        # åå²åå©ä¸çyåå¹¶18        y_ = np.r_[y[:valid_pre], y[valid_pre+num_valid:]]19        # æµè¯é20        test_pre = self.lowest_square_idx(num_test, y_, l, standard)21        # print(test_pre)22        if (test_pre >= valid_pre):23            test_pre += num_valid24            test_bck = test_pre + num_test25        elif (test_pre + num_test > valid_pre):26            test_bck = num_valid + num_test + test_pre27        # print(test_pre)28        X_test = X[test_pre:test_bck]29        y_test = y[test_pre:test_bck]30        X_train, y_train = self.split_train(valid_pre, test_pre, num_valid, num_test, X, y)31        return X_train, X_valid, X_test, y_train, y_valid, y_test323334    # åå²è®ç»é35    def split_train(self, valid_pre, test_pre, num_valid, num_test, X, y):36        if test_pre >= valid_pre:37            pre_l = test_pre38            pre_s = valid_pre39            num_l = num_test40            num_s = num_valid41        elif (test_pre + num_test > valid_pre):42            X_train = np.r_[X[:test_pre], X[num_valid + num_test + test_pre:]]43            y_train = np.r_[y[:test_pre], y[num_valid + num_test + test_pre:]]44            return X_train, y_train45        else:46            pre_s = test_pre47            pre_l = valid_pre48            num_l = num_valid49            num_s = num_test50        X_train = np.r_[np.r_[X[:pre_s], X[pre_s+num_s:pre_l]], X[pre_l+num_l:]]51        y_train = np.r_[np.r_[y[:pre_s], y[pre_s+num_s:pre_l]], y[pre_l+num_l:]]52        return X_train, y_train535455    # 忹弿å°ç䏿 56    def lowest_square_idx(self, num, y, l, standard):57        square = np.zeros(len(y) - num)58        for i in range(len(y) - num):59            # 计ç®ä¸æ åæ¹å·®æå°çä¸ç»æ°æ®60            w = (self.cnt_labels_rate(y[i:i + num], l) - standard)61            square[i] = w.dot(w.T)62        # æ¾å°æå°å¼ç䏿 63        pre = np.argwhere(square == min(square))[0][0]64        return pre656667    # 计ç®å½åæ°æ®çå叿¯ä¾68    def cnt_labels_rate(self, y, l):69        cnt = np.zeros(l)70        for i in range(l):71            cnt[i] = (y == i).sum() / y.shape[0]
...stimuli_notebook.py
Source:stimuli_notebook.py  
1from ExperimentSpecificCode._2017_03_28_Neuroseeker_Auditory_Double.Stimulus import arnes_basic_analysis as ba2data_path = r'F:\Neuroseeker\Neuroseeker_2017_03_28_Anesthesia_Auditory_DoubleProbes'3overwrite = True4binwidth = 0.055stimulus = 'ToneSequence'6pre = 37post = 38plot_type = 'psth'9test_pre = 0.410test_post = 0.111min_rate = 0.512ba.tuning(data_path=data_path, overwrite=overwrite, binwidth=binwidth, stimulus=stimulus, frequencies_index=[0, 1, 2, 3],13          pre=pre, post=post, plot_type=plot_type, test_pre=test_pre,  test_post=test_post, min_rate=min_rate)14overwrite = True15binwidth = 0.0516pre = 217post = 218test_pre = 0.519test_post = 0.220min_rate = 221ba.tones(data_path=data_path, overwrite=overwrite, binwidth=binwidth, pre=pre, post=post,22          test_pre=test_pre, test_post=test_post, min_rate=min_rate)23template = 12024df = pd.DataFrame(index=[template])25try:26    temp = df['a'].loc[template]27    temp[7, :] = np.ones(100) *528except:29    temp = np.empty((10, 100))30    temp[0, :] = np.ones(100) * 331finally:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
