def obj_detection(my_img): st.set_option('deprecation.showPyplotGlobalUse', False) column1, column2 = st.beta_columns(2) column1.subheader("Input image") st.text("") # plt.figure(figsize=(16, 16)) # plt.imshow(my_img) # original = Image.open(image) #col1.header("Original") if my_img.mode != 'RGB': my_img = my_img.convert('RGB') column1.image(my_img, use_column_width=True) # column1.pyplot(use_column_width=True) # YOLO model : # load the YOLO network # net = cv2.dnn.readNet("yolov3_training_last.weights","yolov3_testing.cfg") # net = cv2.dnn.readNetFromDarknet("yolov4-custom.cfg","yolov4-custom_best.weights" ) net = cv2.dnn.readNet('yolov4-custom_best.weights', 'yolov4-custom.cfg') # labels = [] # with open("classes.txt", "r") as f: # labels = [line.strip() for line in f.readlines()] # loading all the class labels (objects) classes = [] with open("classes.txt", "r") as f: classes = f.read().splitlines() # names_of_layer = net.getLayerNames() # output_layers = [names_of_layer[i[0] - 1] for i in net.getUnconnectedOutLayers()] # generating colors for each object for later plotting font = cv2.FONT_HERSHEY_PLAIN colors = np.random.uniform(0, 255, size=(100, 3)) # colors = np.random.uniform(0, 255, size=(len(classes), 3)) print("Colors:", colors) # Image loading newImage = np.array(my_img.convert('RGB')) img = cv2.cvtColor(newImage, 1) height, width, channels = img.shape # Objects detection (Converting into blobs) # (image, scalefactor, size, mean(mean subtraction from each layer), swapRB(Blue to red), crop) # blob = cv2.dnn.blobFromImage(img, 0.00392, (inpWidth, inpHeight), (0, 0, 0), True, # crop=False) blob = cv2.dnn.blobFromImage(img, 1 / 255, (416, 416), (0, 0, 0), swapRB=True, crop=False) # sets the blob as the input of the network net.setInput(blob) # outputs = net.forward(output_layers) output_layers_names = net.getUnconnectedOutLayersNames() # layerOutputs = net.forward(output_layers_names) # get all the layer names # ln = net.getLayerNames() # ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] # names_of_layer = net.getLayerNames() # output_layers = [names_of_layer[i[0] - 1] for i in net.getUnconnectedOutLayers()] # feed forward (inference) and get the network output # measure how much it took in seconds # start = time.perf_counter() # outputs = net.forward(output_layers) outputs = net.forward(output_layers_names) # time_took = time.perf_counter() - start # print(f"Time took: {time_took:.2f}s") # The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes) t, _ = net.getPerfProfile() infLabel = 'Inference time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency()) # cv2.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255)) classID = [] confidences = [] boxes = [] # SHOWING INFORMATION CONTAINED IN 'outputs' VARIABLE ON THE SCREEN # loop over each of the layer outputs for op in outputs: for detection in op: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.2: # OBJECT DETECTED # Get the coordinates of object: center,width,height center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) # width is the original width of image h = int(detection[3] * height) # height is the original height of the image # use the center (x, y)-coordinates to derive the top and # and left corner of the bounding box # RECTANGLE COORDINATES x = int(center_x - w / 2) # Top-Left x y = int(center_y - h / 2) # Top-left y # To organize the objects in array so that we can extract them later boxes.append([x, y, w, h]) confidences.append(float(confidence)) classID.append(class_id) # score_threshold = st.sidebar.slider("Confidence_threshold", 0.00, 1.00, 0.5, 0.01) # nms_threshold = st.sidebar.slider("NMS_threshold", 0.00, 1.00, 0.4, 0.01) score_threshold = 0.2 st.sidebar.info(f"Confidence_threshold:{ score_threshold }") nms_threshold = 0.4 st.sidebar.info(f"NMS_threshold :{nms_threshold} ") st.sidebar.success(infLabel) indexes = cv2.dnn.NMSBoxes(boxes, confidences, score_threshold, nms_threshold) print("DNN Index:", indexes) font = cv2.FONT_HERSHEY_SIMPLEX items = [] for i in range(len(boxes)): if i in indexes.flatten(): x, y, w, h = boxes[i] # To get the name of object label = str.upper((classes[classID[i]])) # label = str(classes[class_ids[i]]) confidence = str(round(confidences[i], 2)) print("value of i:", i) color = colors[i] cv2.rectangle(img, (x, y), (x + w, y + h), color, 3) cv2.putText(img, label + " " + confidence, (x, y + 10), font, 0.25, (0, 0, 255), 1) items.append(label) st.text("") st.spinner('Model working....') column2.subheader("Output image") st.text("") # plt.figure(figsize=(15, 15)) # plt.imshow(img) # column2.pyplot(use_column_width=True) column2.image(img, use_column_width=True) if len(indexes) > 1: st.success("Found {} Objects - {}".format( len(indexes), [item for item in set(items)])) st.balloons() elif len(indexes) == 1: st.success("Found {} Object - {}".format(len(indexes), [item for item in set(items)])) st.balloons() else: st.warning("Found {} Object - {}".format(len(indexes), [item for item in set(items)]))
def time_pattern(): global target, daypara, df, df2, df_4pycaret, df_temp EPOCH = st.sidebar.slider("Epochs", 100, 1000) model = NeuralProphet( growth="linear", changepoints=None, n_changepoints=30, changepoints_range=0.95, trend_reg=0, trend_reg_threshold=False, yearly_seasonality="auto", weekly_seasonality=True, daily_seasonality="auto", seasonality_mode="additive", seasonality_reg=0, n_forecasts=30, n_lags=60, ##determines autoregression num_hidden_layers=0, d_hidden=None, ar_sparsity=None, learning_rate=None, epochs=EPOCH, loss_func="Huber", normalize="auto", impute_missing=True, ) metrics = model.fit(df2, validate_each_epoch=True, freq="D") future = model.make_future_dataframe(df2, periods=252, n_historic_predictions=len(df2)) with st.spinner("Training..."): forecast = model.predict(future) fig, ax = plt.subplots(1, 2, figsize=(17, 7)) ax[0].plot(metrics["MAE"], 'ob', linewidth=6, label="Training Loss") ax[0].plot(metrics["MAE_val"], '-r', linewidth=2, label="Validation Loss") ax[0].legend(loc='center right') ax[0].tick_params(axis='both', which='major') ax[0].set_xlabel("Epoch") ax[0].set_ylabel("Loss") ax[0].set_title("Model Loss (MAE)") ax[1].plot(metrics["SmoothL1Loss"], 'ob', linewidth=6, label="Training Loss") ax[1].plot(metrics["SmoothL1Loss_val"], '-r', linewidth=2, label="Validation Loss") ax[1].legend(loc='center right') ax[1].tick_params(axis='both', which='major') ax[1].set_xlabel("Epoch") ax[1].set_ylabel("Loss") ax[1].set_title("Model Loss (SmoothL1Loss)") st.subheader("Loss Check") st.pyplot() with st.spinner("Recognizing Time Pattern"): st.subheader("Time Pattern") model.plot_parameters() st.set_option('deprecation.showPyplotGlobalUse', False) st.pyplot()
import streamlit as st import pandas as pd import shap import matplotlib.pyplot as plt from sklearn import datasets from sklearn.ensemble import RandomForestRegressor st.set_page_config(page_title='Boston House Price Prediction', layout='wide') st.set_option('deprecation.showPyplotGlobalUse', False) st.write(""" # Boston House Price Prediction Made with **streamlit** by [riztekur](https://github.com/riztekur) """) st.write('---') # Loads the Boston House Price Dataset boston = datasets.load_boston() X = pd.DataFrame(boston.data, columns=boston.feature_names) Y = pd.DataFrame(boston.target, columns=["MEDV"]) # Sidebar # Header of Specify Input Parameters st.sidebar.header('Input Parameters') def user_input_features(): CRIM = st.sidebar.slider('CRIM', X.CRIM.min(), X.CRIM.max(), X.CRIM.mean()) ZN = st.sidebar.slider('ZN', X.ZN.min(), X.ZN.max(), X.ZN.mean()) INDUS = st.sidebar.slider('INDUS', X.INDUS.min(), X.INDUS.max(), X.INDUS.mean()) CHAS = st.sidebar.slider('CHAS', X.CHAS.min(), X.CHAS.max(), X.CHAS.mean())
# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import streamlit as st import numpy as np st.set_option("deprecation.showImageFormat", True) img = np.repeat(0, 10000).reshape(100, 100) st.image(img, caption="Black Square with deprecated format", format="JPEG", width=100) st.set_option("deprecation.showImageFormat", False) st.image(img, caption="Black Square with deprecated format", format="JPEG", width=100) st.image(img, caption="Black Square as JPEG", output_format="JPEG", width=100) st.image(img, caption="Black Square as PNG", output_format="PNG", width=100)
def app(): st.set_option('deprecation.showPyplotGlobalUse', False) if globals.selected2==-1: st.header("Company Wise Prediction") db_connection = msql.connect(host='portfoliomanagement.c5r1ohijcswm.ap-south-1.rds.amazonaws.com', database='portfolioManagement', user='******', password='******') query = "select distinct Symbol from companies" result = pd.read_sql(query, con=db_connection) globals.symbol_=st.selectbox("Select the Company", result.stack().tolist()) print("OutsideBlock") if st.button("Predict For "+globals.symbol_): globals.predRes = PredictorModel(globals.symbol_) #st.write("Tomorrows predicted price for " + globals.symbol_ + " is ", globals.predRes) globals.selected2 = 0 st.button("Next") else: db_connection = msql.connect(host='portfoliomanagement.c5r1ohijcswm.ap-south-1.rds.amazonaws.com', database='portfolioManagement', user='******', password='******') st.subheader("Insert Into Portfolio :shopping_bags:") symbol_=globals.symbol_ quantity = st.number_input("Enter The Quantity", value=1, min_value=1) query = "select Close from companyDateWise where Symbol='" + symbol_ + "' and Date=(SELECT MAX(Date) FROM companyDateWise WHERE Symbol='" + symbol_ + "')" resdf = pd.read_sql(query, con=db_connection) lastClose = resdf.at[0, 'Close'] netPofit = (globals.predRes - lastClose) * quantity totalCost = lastClose * quantity pred = round(globals.predRes,2) st.write("Do you want to add `" + str(quantity) + "` of `" + symbol_ + "` at Current Price `Rs " + str( lastClose) + "` and Predicted Price `Rs " + str(pred) + "` in your Portfolio") if st.button("Insert"): print(totalCost) print(globals.ser) print(globals.budget) if totalCost + globals.ser > globals.budget: st.error("Oops! You Are Exceeding The Budget") st.write("`Quick Help:` You can increase the budget from dashboard.") else: st.markdown(":robot_face: We are adding " + symbol_ + " to your portfolio :robot_face:") try: db_connection = msql.connect(host='portfoliomanagement.c5r1ohijcswm.ap-south-1.rds.amazonaws.com', database='portfolioManagement', user='******', password='******') if db_connection.is_connected(): print("Clicked") cursor = db_connection.cursor() cursor.execute("select database();") record = cursor.fetchone() print("You're connected to database: ", record) sql = "INSERT INTO portfolio VALUES (%s,%s,%s,%s,%s,%s,%s)" print(login.usr, symbol_, globals.predRes, date.today(), quantity, netPofit, totalCost) cursor.execute(sql, ( login.usr, symbol_, str(globals.predRes), date.today(), str(quantity), str(netPofit), str(totalCost))) print("Record inserted") # st.balloons() db_connection.commit() st.success(symbol_ + " successfully added to your portfolio") #globals.selected2 -= 1 except Error as e: print(e) st.error("0ops! " + symbol_ + " is already in your Portfolio") #globals.selected2 -= 1 A, C = st.beta_columns(2) if A.button("Back"): globals.selected2 -= 1 C.button("Refresh") st.subheader("More Details of `"+globals.symbol_+"` :information_source:") query = "select * from companies where Symbol='" + globals.symbol_ + "'" resdf = pd.read_sql(query, con=db_connection) st.write("Company's Name: `"+str(resdf.iloc[0]['Company_Name'])+"`") st.write("Sector: `" + str(resdf.iloc[0]['Sector']) + "`") st.write("Series: `" + str(resdf.iloc[0]['Series']) + "`") st.write("ISIN Code: `" + str(resdf.iloc[0]['ISIN_Code']) + "`") details = si.get_stats(globals.symbol_ + ".NS") details_new = details.rename(columns={'Attribute': 'Info'}) details_new.dropna(inplace=True) st.table(details_new)
def main(): menu = ["Home", "Pandas Profile", "Sweetviz", "About"] choice = st.sidebar.selectbox("Menu", menu) if choice == "Pandas Profile": st.subheader("Automated EDA with Pandas Profile") data_file = st.file_uploader("Upload CSV", type=['csv']) st.set_option('deprecation.showfileUploaderEncoding', False) if data_file is not None: df = pd.read_csv(data_file) st.dataframe(df.head()) profile = ProfileReport(df) st_profile_report(profile) elif choice == "Sweetviz": st.subheader("Automated EDA with Sweetviz") data_file = st.file_uploader("Upload CSV", type=['csv']) st.set_option('deprecation.showfileUploaderEncoding', False) if data_file is not None: df = pd.read_csv(data_file) st.dataframe(df.head()) if st.button("Generate Sweetviz Report"): st_display_sweetviz("SWEETVIZ_REPORT.html") #Normal Workflow (save .html) #report = sv.analyze(df) #report.show_html() elif choice == "About": st.subheader("Onur Erdogan tarafından yapılmıstır") #components.iframe('https://www.google.com) else: st.subheader("Home") components.html(""" <style> * {box-sizing: border-box} body {font-family: Verdana, sans-serif; margin:0} .mySlides {display: none} img {vertical-align: middle;} /* Slideshow container */ .slideshow-container { max-width: 1000px; position: relative; margin: auto; } /* Next & previous buttons */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; padding: 16px; margin-top: -22px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; } /* Position the "next button" to the right */ .next { right: 0; border-radius: 3px 0 0 3px; } /* On hover, add a black background color with a little bit see-through */ .prev:hover, .next:hover { background-color: rgba(0,0,0,0.8); } /* Caption text */ .text { color: #f2f2f2; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } /* Number text (1/3 etc) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* The dots/bullets/indicators */ .dot { cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: #717171; } /* Fading animation */ .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from {opacity: .4} to {opacity: 1} } @keyframes fade { from {opacity: .4} to {opacity: 1} } /* On smaller screens, decrease text size */ @media only screen and (max-width: 300px) { .prev, .next,.text {font-size: 11px} } </style> </head> <body> <div class="slideshow-container"> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="https://www.w3schools.com/howto/img_snow_wide.jpg" style="width:100%"> <div class="text">Caption Text</div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="https://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%"> <div class="text">Caption Two</div> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="https://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%"> <div class="text">Caption Three</div> </div> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> <br> <div style="text-align:center"> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> </div> <script> var slideIndex = 1; showSlides(slideIndex); function plusSlides(n) { showSlides(slideIndex += n); } function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { var i; var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); if (n > slides.length) {slideIndex = 1} if (n < 1) {slideIndex = slides.length} for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex-1].style.display = "block"; dots[slideIndex-1].className += " active"; } </script> """)
import streamlit as st import tensorflow as tf st.set_option('deprecation.showfileUploaderEncoding',False) #for ignoring the warnings @st.cache(allow_output_mutation=True) def load_model(): model=tf.keras.models.load_model('model_vgg19.h5') return model model=load_model() st.write(""" # Malaria Classification """ ) file = st.file_uploader("Please upload the cell image",type=["jpg","png"]) from PIL import Image,ImageOps import numpy as np def import_and_predict(image_data,model): size = (240,240) image = ImageOps.fit(image_data,size,Image.ANTIALIAS) img = np.asarray(image) img_reshape = img[np.newaxis,...] prediction = model.predict(img_reshape) return prediction if file is None: st.text("Please upload an image file") else: image = Image.open(file) st.image(image,use_column_width=True) predictions = import_and_predict(image,model)
def run_edu_pass_fail_prediction_app(): st.header('■Pass/Fail Prediction Demo') st.write('To know what makes students pass or fail.') st.sidebar.subheader('Data Upload') df_edu = pd.read_csv("data/eng_sample_pass_fail.csv") def download_link(object_to_download, download_filename, download_link_text): if isinstance(object_to_download, pd.DataFrame): object_to_download = object_to_download.to_csv( index=False, encoding='utf_8_sig') b64 = base64.b64encode(object_to_download.encode()).decode() return f'<a href="data:file/txt;base64,{b64}" download="{download_filename}">{download_link_text}</a>' tmp_download_link = download_link(df_edu, 'sample_pass_fail.csv', 'Download sample csv file.') st.sidebar.markdown(tmp_download_link, unsafe_allow_html=True) # st.sidebar.info(""" # [Download the sample csv file](https://github.com/59er/eng_learning_analytics_web/blob/master/sample_data/eng_sample_pass_fail_for_WEB.csv) # """) uploaded_file = st.sidebar.file_uploader( "File upload (Drag and drop or use [Browse files] button to import csv file. Only utf-8 format is available.)", type=['csv']) try: if uploaded_file is not None: df_edu = pd.read_csv(uploaded_file) display_data = st.sidebar.checkbox(label='Show uploaded data') if display_data: st.dataframe(df_edu) df = df_edu.drop(['ID', 'Teacher'], axis=1) target = 'Pass/Fail' encode = ['Class', 'Subject'] for col in encode: dummy = pd.get_dummies(df[col], prefix=col) df = pd.concat([df, dummy], axis=1) del df[col] target_mapper = {'Fail': 0, 'Pass': 1} def target_encode(val): return target_mapper[val] df['Pass/Fail'] = df['Pass/Fail'].apply(target_encode) X = df.drop(['Pass/Fail'], axis=1) Y = df['Pass/Fail'] clf = RandomForestClassifier(n_estimators=250, random_state=0) clf.fit(X, Y) df_drop = df.drop(['Pass/Fail'], axis=1) prediction = clf.predict(df_drop) st.subheader('Prediction result') score_assess = np.array(['Fail', 'Pass']) id = df_edu['ID'] result = score_assess[prediction] id = pd.DataFrame(id) result = pd.DataFrame(result) pred_result = pd.concat([id, result], axis=1) pred_result = pred_result.rename(columns={0: 'Result'}) st.dataframe(pred_result) # st.write(score_assess[prediction]) X = df.drop(['Pass/Fail'], axis=1) Y = df['Pass/Fail'] X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0) import sklearn.tree as tree load_clf = tree.DecisionTreeClassifier(random_state=0, max_depth=3) model = load_clf.fit(X_train, y_train) from sklearn.metrics import accuracy_score from sklearn.metrics import roc_curve, auc, roc_auc_score from sklearn.metrics import confusion_matrix pred = load_clf.predict_proba(X_test)[:, 1] y_pred = np.where(pred > 0.6, 1, 0) score = accuracy_score(y_pred, y_test) st.subheader('Prediction accuracy') st.write(score) auc_score = roc_auc_score(y_test, pred) st.subheader('AUC accuracy ') st.write(auc_score) features = X_train.columns importances = load_clf.feature_importances_ indices = np.argsort(importances) st.set_option('deprecation.showPyplotGlobalUse', False) plt.figure(figsize=(6, 6)) plt.barh(range(len(indices)), importances[indices], color='#377eb8', align='center') plt.yticks(range(len(indices)), features[indices]) plt.show() st.pyplot() #confusion matrix matrix = confusion_matrix(y_pred, y_test) class_names = ['Fail', 'Pass'] df = pd.DataFrame(matrix, index=class_names, columns=class_names) fig = plt.figure(figsize=(5, 5)) sns.heatmap(df, annot=True, cbar=None, cmap='Blues') plt.title('Prediction result') plt.tight_layout() plt.ylabel('Positive') plt.xlabel('Prediction') plt.show() st.pyplot(fig) from dtreeviz.trees import dtreeviz import graphviz as graphviz import streamlit.components.v1 as components viz = dtreeviz(model, X_train, y_train, target_name='Fail/Pass', feature_names=X_train.columns, class_names=['Fail', 'Pass']) def st_dtree(plot, height=None): dtree_html = f'<body>{viz.svg()}</body>' components.html(dtree_html, height=height) st_dtree( dtreeviz(model, X_train, y_train, target_name='Pass/Fail', feature_names=X_train.columns, class_names=['Fail', 'Pass']), 400) else: def user_input_features(): class_name = st.sidebar.selectbox( 'Class', ('A', 'B', 'C', 'D', 'E', 'F', 'G')) subject = st.sidebar.selectbox( 'Subject', ('History', 'Math', 'Literature')) subject_A = st.sidebar.slider('item1', 0, 100, 50) subject_B = st.sidebar.slider('item2', 0, 100, 50) subject_C = st.sidebar.slider('item3', 0, 100, 50) subject_D = st.sidebar.slider('item4', 0, 100, 50) subject_E = st.sidebar.slider('item5', 0, 100, 50) data = { 'Class': class_name, 'Subject': subject, 'item1': subject_A, 'item2': subject_B, 'item3': subject_C, 'item4': subject_D, 'item5': subject_E } features = pd.DataFrame(data, index=[0]) return features input_df = user_input_features() sample_data = pd.read_csv('data/eng_sample_pass_fail.csv') sample = sample_data.drop(columns=['Pass/Fail', 'ID', 'Teacher']) #df = sample.copy() df = pd.concat([input_df, sample], axis=0) # st.dataframe(df[:1]) encode = ['Class', 'Subject'] for col in encode: dummy = pd.get_dummies(df[col], prefix=col) df = pd.concat([df, dummy], axis=1) del df[col] df1 = df[:1] # st.subheader('入力データ') # if uploaded_file is not None: # st.write(df1) # else: st.write( 'The following is default sample data. Select class and subject then use the sliders for each item in the sidebar to get an idea of pass/fail prediction.' ) st.write(df1) load_clf = pickle.load(open('data/subject_pass_fail.pkl', 'rb')) prediction = load_clf.predict(df1) st.subheader('Prediction result') score_assess = np.array(['Fail', 'Pass']) st.write(score_assess[prediction]) df1 = sample_data.copy() encode = ['Class', 'Subject'] for col in encode: dummy1 = pd.get_dummies(df1[col], prefix=col) df1 = pd.concat([df1, dummy1], axis=1) del df1[col] target_mapper = {'Fail': 0, 'Pass': 1} def target_encode(val): return target_mapper[val] df1['Pass/Fail'] = df1['Pass/Fail'].apply(target_encode) X = df1.drop(['Pass/Fail', 'Teacher', 'ID'], axis=1) Y = df1['Pass/Fail'] X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0) # X1 = df1.drop(['得点'],axis=1) # prediction = load_clf.predict(X1) # st.subheader('得点予測結果') # st.dataframe(prediction) import sklearn.tree as tree load_clf = tree.DecisionTreeClassifier(random_state=0, max_depth=3) model = load_clf.fit(X_train, y_train) from sklearn.metrics import accuracy_score from sklearn.metrics import roc_curve, auc, roc_auc_score from sklearn.metrics import confusion_matrix pred = load_clf.predict_proba(X_test)[:, 1] y_pred = np.where(pred > 0.6, 1, 0) score = accuracy_score(y_pred, y_test) st.subheader('Prediction accuracy') st.write(score) auc_score = roc_auc_score(y_test, pred) st.subheader('AUC accuracy ') st.write(auc_score) features = X_train.columns importances = load_clf.feature_importances_ indices = np.argsort(importances) st.set_option('deprecation.showPyplotGlobalUse', False) plt.figure(figsize=(6, 6)) plt.barh(range(len(indices)), importances[indices], color='#377eb8', align='center') plt.yticks(range(len(indices)), features[indices]) plt.show() st.pyplot() #confusion matrix matrix = confusion_matrix(y_pred, y_test) class_names = ['Fail', 'Pass'] df = pd.DataFrame(matrix, index=class_names, columns=class_names) fig = plt.figure(figsize=(5, 5)) sns.heatmap(df, annot=True, cbar=None, cmap='Blues') plt.title('Prediction result') plt.tight_layout() plt.ylabel('Positive') plt.xlabel('Prediction') plt.show() st.pyplot(fig) fig = plt.figure(figsize=(5, 5)) explainer = shap.TreeExplainer(load_clf, X) shap_values = explainer.shap_values(X) st.subheader('Impact of explanatory variables (each item score) on\ the objective variable (final score): Class 0 has an impact for Fail, Class 1 for Pass)' ) fig = shap.summary_plot(shap_values, X, plot_type='bar') st.pyplot(fig) from dtreeviz.trees import dtreeviz import graphviz as graphviz import streamlit.components.v1 as components viz = dtreeviz(model, X_train, y_train, target_name='Fail/Pass', feature_names=X_train.columns, class_names=['Fail', 'Pass']) def st_dtree(plot, height=None): dtree_html = f'<body>{viz.svg()}</body>' components.html(dtree_html, height=height) st_dtree( dtreeviz(model, X_train, y_train, target_name='Pass/Fail', feature_names=X_train.columns, class_names=['Fail', 'Pass']), 400) except Exception as e: print(e)
def main(): title_info = st.empty() video_choose = st.empty() run_btn = st.empty() precision = st.empty() image = st.empty() confidence_label = st.empty() graph_display = st.empty() final_video = st.empty() title_info.title('Object Detection with YOLOv3') menu = ['Videos'] choice = st.sidebar.selectbox('Dev Menu', menu) if choice == 'Videos': cfg_vid = "/Users/erwanrivoalen/Downloads/darknet/build/darknet/x64/yolov3.cfg" image_vid = video_choose.file_uploader('Select a video file', type=['mp4', 'mov']) names_vid = "/Users/erwanrivoalen/Downloads/darknet/data/coco.names" weights_vid = "/Users/erwanrivoalen/yolov3.weights" submit_vid = run_btn.button('Run') if submit_vid: print(image_vid.name) vn = image_vid.name tfile = tempfile.NamedTemporaryFile(delete=False) tfile.write(image_vid.read()) vf = cv2.VideoCapture(tfile.name) #Ici il y avait un if video = vf writer = None h, w = None, None with open(names_vid) as f: labels = [line.strip() for line in f] network = cv2.dnn.readNetFromDarknet(cfg_vid, weights_vid) layers_names_all = network.getLayerNames() layers_names_output = \ [layers_names_all[i[0] - 1] for i in network.getUnconnectedOutLayers()] probability_minimum = 0.5 threshold = 0.3 colours = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8') lps = [] f = 0 t = 0 while True: # Capturing frame-by-frame ret, frame = video.read() # If the frame was not retrieved # e.g.: at the end of the video, # then we break the loop if not ret: break # Getting spatial dimensions of the frame # we do it only once from the very beginning # all other frames have the same dimension if w is None or h is None: # Slicing from tuple only first two elements h, w = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False) # Implementing forward pass with our blob and only through output layers # Calculating at the same time, needed time for forward pass network.setInput(blob) # setting blob as input to the network start = time.time() output_from_network = network.forward(layers_names_output) end = time.time() # Increasing counters for frames and total time f += 1 t += end - start # Showing spent time for single current frame print('Frame number {0} took {1:.5f} seconds'.format( f, end - start)) precision.write('Frame number {0} took {1:.5f} seconds'.format( f, end - start)) bounding_boxes = [] confidences = [] class_numbers = [] # Going through all output layers after feed forward pass for result in output_from_network: # Going through all detections from current output layer for detected_objects in result: scores = detected_objects[5:] class_current = np.argmax(scores) confidence_current = scores[class_current] if confidence_current > probability_minimum: box_current = detected_objects[0:4] * np.array( [w, h, w, h]) # Now, from YOLO data format, we can get top left corner coordinates # that are x_min and y_min x_center, y_center, box_width, box_height = box_current x_min = int(x_center - (box_width / 2)) y_min = int(y_center - (box_height / 2)) # Adding results into prepared lists bounding_boxes.append([ x_min, y_min, int(box_width), int(box_height) ]) confidences.append(float(confidence_current)) class_numbers.append(class_current) results = cv2.dnn.NMSBoxes(bounding_boxes, confidences, probability_minimum, threshold) yes = [] confi = [] rlist = [] if len(results) > 0: # Going through indexes of results for i in results.flatten(): x_min, y_min = bounding_boxes[i][0], bounding_boxes[i][ 1] box_width, box_height = bounding_boxes[i][ 2], bounding_boxes[i][3] colour_box_current = colours[class_numbers[i]].tolist() # Drawing bounding box on the original current frame roi = frame[y_min:y_min + box_height + 10, x_min:x_min + box_width + 10] cv2.rectangle(frame, (x_min, y_min), (x_min + box_width, y_min + box_height), colour_box_current, 2) text_box_current = '{}: {:.4f}'.format( labels[int(class_numbers[i])], confidences[i]) cv2.putText(frame, text_box_current, (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colour_box_current, 2) component = [ x_min, y_min, x_min + box_width, y_min + box_height, labels[int(class_numbers[i])] ] yes.append(component) confi.append(confidences[i]) for i in range(0, len(yes) - 1): yo = yes[i][4] for j in range(i + 1, len(yes)): if yo == yes[j][4]: yes[j][4] += f'{j}' rlist = relations(yes) if rlist: for i in range(0, len(rlist)): confidence_label.write( '{0} with a confidence of : {1} {2} {3} with a confidence of : {4}' .format(rlist[i][0], confi[0], rlist[i][1], rlist[i][2], confi[1])) G = nx.DiGraph() for triple in rlist: G.add_node(triple[0]) G.add_node(triple[1]) G.add_node(triple[2]) G.add_edge(triple[0], triple[1]) G.add_edge(triple[1], triple[2]) pos = nx.spring_layout(G) plt.figure() nx.draw(G, pos, edge_color='black', width=1, linewidths=1, node_size=500, node_color='seagreen', alpha=0.9, labels={node: node for node in G.nodes()}) plt.axis('off') st.set_option('deprecation.showPyplotGlobalUse', False) graph_display.pyplot() if writer is None: fourcc = cv2.VideoWriter_fourcc(*'mp4v') writer = cv2.VideoWriter('result' + vn, fourcc, 30, (frame.shape[1], frame.shape[0]), True) #streamlit.image(frame) name = 'result' + vn image.image(frame) #writer.write(frame) print() print('Total number of frames', f) print('Total amount of time {:.5f} seconds'.format(t)) print('FPS:', round((f / t), 1)) video.release() writer.release() videofile = name print(videofile) m = os.path.splitext(videofile)[0] os.system('ffmpeg -i ' + videofile + ' -vcodec libx264 ' + m + 'fmpeg.mp4') final_video.video(m + 'fmpeg.mp4')
def main(): # Disable warning that shows up st.set_option('deprecation.showPyplotGlobalUse', False) # Dictionaries, lists, and variables initialized analysisDictionary = { "name": 1, "host_id": 2, "host_name": 3, "neighbourhood": 5, "room_type": 8, "price": 9, "minimum_nights": 10, "number_of_reviews": 11 } mainOptionsDictionary = { "Neighbourhood Listings & Reviews Data": 1, "Pricing Data": 2 } neighborhoodNames = ('West Cambridge', 'North Cambridge', 'The Port', 'Neighborhood Nine', 'Riverside', 'Mid-Cambridge', 'Agassiz', 'Cambridgeport', 'East Cambridge', 'Strawberry Hill', 'Wellington-Harrington', 'Area 2/MIT') mainListingData = "airbnb_cambridge_listings_20201123.csv" extraListingData = "airbnb_cambridge_20201123.csv" filesDict = { 'Main Listing Info': mainListingData, 'Listing History & Additional Info': extraListingData } # Selecting file sidebar with title and header st.sidebar.title('AirBnB Cambridge Listing Data') st.sidebar.subheader('Created by Peter Kassabov') fileSelection = st.sidebar.selectbox("Select a file option: ", list(filesDict.keys())) if fileSelection == 'Main Listing Info': # Interface file selection details st.sidebar.header('Options') st.write("File Name: ", mainListingData) listingDataSelection = st.sidebar.radio( "Select an option: ", list(mainOptionsDictionary.keys())) # Main listing information dataframe created airbnbMainDF = pd.read_csv( os.path.join("airbnb_cambridge_listings_20201123.csv"), usecols=[ 'id', 'name', 'neighbourhood', 'room_type', 'price', 'number_of_reviews' ]) st.write("Dataframe chosen: ", airbnbMainDF) # Pivot tables and graphs made for neighbourhood data when selected if listingDataSelection == 'Neighbourhood Listings & Reviews Data': neighborhoodSelection = st.sidebar.radio( "Select which neighbourhood to view listings from: ", list(neighborhoodNames)) table1 = pd.pivot_table( data=airbnbMainDF, index=['neighbourhood'], aggfunc={'neighbourhood': np.count_nonzero}) table3 = pd.pivot_table(data=airbnbMainDF, index=['neighbourhood'], aggfunc={'number_of_reviews': np.sum}) st.write("Total listings in each neighbourhood:") st.write(table1) st.write( "Below is the total listings available for each neighbourhood shown in a bar chart:" ) table1bargraph = table1.plot( kind='bar', ylabel='Number of Listings', xlabel='Neighbourhood', title='Total Listings In Each Neighbourhood', color='green') st.pyplot() st.write("Total reviews for each neighbourhood:") st.write(table3) st.write( "Below is the total reviews for each neighborhood shown in a bar chart:" ) table3bargraph = table3.plot( kind='bar', ylabel='Number of Reviews', xlabel='Neighbourhood', title='Total Reviews For Each Neighbourhood', color='c') st.pyplot() # Pivot table and graph made for pricing data if listingDataSelection == 'Pricing Data': table2 = pd.pivot_table(data=airbnbMainDF, index=['neighbourhood'], aggfunc={'price': np.mean}) table2 = table2.sort_values(by=('price'), ascending=True) st.write( "Average price per night in each neighbourhood, from least expensive to most:" ) st.write(table2) st.write( "Below is the average price per night for each neighborhood displayed in a line chart:" ) table2linegraph = table2.plot( kind='line', ylabel='Price in $', xlabel='Neighbourhood', title='Average Price/Night For Each Neighbourhood', fontsize=6, color='indigo') st.pyplot() else: # Additional AirBnB listing CSV file loaded into dataframe and shown to user st.write("File Name: ", extraListingData) airbnbDatesDF = pd.read_csv( os.path.join("airbnb_cambridge_20201123.csv"), usecols=['listing_id', 'date']) st.write("Dataframe chosen: ", airbnbDatesDF) # Creating a pivot table to show the number of bookings for each unique ID table4 = pd.pivot_table(data=airbnbDatesDF, index=['listing_id'], aggfunc={'date': np.count_nonzero}) st.write("Number of bookings to date per ID:") st.write(table4)
def main(): st.title("Cow DataViewer") date = st.sidebar.selectbox( "date", [ f for f in os.listdir(cfg["data_root"]) if os.path.isfile(join(cfg["data_root"], f)) ], ) content = Content.from_str( (st.sidebar.selectbox("Content", Content.all(), index=0)) ) anns = shape_data(path=join(cfg["data_root"], date)) if content == Content.table: id_queries = list(anns.keys()) num_queries = [] acc1_list, acc5_list = [], [] for id_query in id_queries: acc1 = len(anns[id_query]["top1"]) acc5 = acc1 + len(anns[id_query]["top2_5"]) num_query = acc5 + len(anns[id_query]["top6_later"]) acc1_list.append(100 * acc1 / num_query) acc5_list.append(100 * acc5 / num_query) num_queries.append(num_query) # num_db = [1 for i in range(len(num_queries))] table_df = pd.DataFrame( { # "db": num_db, "query": num_queries, "Top1(%)": acc1_list, "Top5(%)": acc5_list, }, index=id_queries, ) st.markdown( f"### Data: {date.split('.')[0]}" ) st.markdown( f"### Result:\nTop1: {round(sum(acc1_list) / len(acc1_list), 1)}%, Top5: {round(sum(acc5_list) / len(acc5_list), 1)}%" ) table_df.plot.bar(y=["Top1(%)", "Top5(%)"], alpha=0.8, figsize=(12, 4)) st.pyplot() st.table(table_df) if content == Content.images: id_queries = list(anns.keys()) id_query: str = st.sidebar.selectbox("ID", id_queries) res_type: str = st.sidebar.selectbox("Type", ["top1", "top2_5", "top6_later"]) path_queries = list(anns[id_query][res_type].keys()) num_query = len(path_queries) if num_query == 0: st.markdown("None") return if num_query == 1: index = 1 else: index = st.sidebar.slider("Page index (1-index)", 1, num_query, value=1) # st.markdown( # f"Test: {date.split('-')[0]}\n\nTrain: {date.split('-')[1].split('.')[0]}\n - - - " # ) st.markdown(f"Page: {index}/{num_query}") index -= 1 # convert to 0-index path_query = path_queries[index] img_query = Image.open(path_query) img_query = pad_image(img_query) img_query = img_query.resize((224, 224)) path_db = anns[id_query][res_type][path_query] imgs_db = [] # assert len(path_db) == 10, "database length is not 10" for i, path in enumerate(path_db): id_db = path.split("/")[-2] img_db = cv2.imread(path) img_db = cv2.resize(img_db, (224, 224)) if id_db == id_query: img_db = Drawer.draw_id( img=img_db, text1=id_db, text2=f"Top{i + 1}", color1=(0, 0, 255) ) img_db = Drawer.draw_rectangle(img_db) else: img_db = Drawer.draw_id(img=img_db, text1=id_db, text2=f"Top{i + 1}") imgs_db.append(img_db) st.markdown("### Query:") st.image(img_query) st.markdown("### Database:") st.image( cv2.cvtColor(cv2.hconcat(imgs_db[:5]), cv2.COLOR_BGR2RGB), use_column_width=True, ) # st.image( # cv2.cvtColor(cv2.hconcat(imgs_db[5:10]), cv2.COLOR_BGR2RGB), # use_column_width=True, # ) st.set_option('deprecation.showPyplotGlobalUse', False)
import pytesseract #https://github.com/rohankokkula/TEATH #https://www.doubango.org/SDKs/mrz/docs/Data_validation.html #https://discuss.streamlit.io/t/i-get-an-error-everytime-i-change-anything-in-my-code/4706 #pytesseract.pytesseract.tesseract_cmd = r'C:\Users\sudhakar\AppData\Local\Tesseract-OCR\tesseract.exe' pytesseract.pytesseract.tesseract_cmd = '/app/.apt/usr/bin/tesseract' #pytesseract.pytesseract.tesseract_cmd = "Tesseract-OCR/tesseract.exe" #pytesseract.pytesseract.tesseract_cmd = '/app/.apt/usr/bin/tesseract' #pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" #pytesseract.pytesseract.tesseract_cmd = r"/usr/local/Tesseract-OCR/tesseract.exe" #pytesseract.pytesseract.tesseract_cmd = r"https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w64-setup-v5.0.0-alpha.20200328.exe" activities = ["Home", "Extract Ids", "About"] choice = st.sidebar.selectbox("Select Activities", activities) st.set_option('deprecation.showfileUploaderEncoding', False) st.set_option('deprecation.showPyplotGlobalUse', False) st.set_option('deprecation.showPyplotGlobalUse', False) hide_streamlit_style = """ <style> #MainMenu {visibility: hidden;} footer {visibility: hidden;} """ st.markdown(hide_streamlit_style, unsafe_allow_html=True) FILE_TYPES = ["csv", "py", "png", "jpg"] st.subheader( "This App for recognizing machine readable zones (MRZ) from scanned identification documents. in around 90% of the cases, whenever there is a clearly visible MRZ on a page, it will recognize it and extract the text to the best of the abilities of the underlying OCR engine.so one of the easiest methods is to recognize it from an image file. \n 10% failed examples seem to be most often either clearly badly scanned documents. \n " )
def main(): local_css("style.css") activities = ["ABOUT", "Text extraction", "Text Detection"] choice = st.sidebar.selectbox("Select Activty", activities) if choice == "Text extraction": st.set_option('deprecation.showfileUploaderEncoding', False) html_temp = """ <div style="background-color:indigo;padding:10px"> <h2 style="color:white;text-align:center;">Text Extraction from scanned Image</h2> </div> """ st.markdown(html_temp, unsafe_allow_html=True) image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg']) if image_file is not None: our_image = Image.open(image_file) st.title("Original Image") new_img = np.array(our_image.convert('RGB')) res_image = image_resize(new_img, 750, 750) st.image(res_image) dpi_image = set_image_dpi(image_file) st.title("CROP Image") st.subheader("Do you want to crop the image ? ") st.write("To crop the image following conditions should satisfy") st.write("1.Four corners of the page should be visible") st.write("2.Background of the image should be plain") crop = st.selectbox("Crop", ["", "NO", "YES"]) if crop == "YES": screenCnt = 0 croped_img = croping_image(dpi_image) croped_img = image_resize(croped_img) st.image(croped_img) st.subheader("Do you wish to apply the changes?") crop2 = st.selectbox("crop(confirmation)", ["", "NO", "YES"]) if crop2 == "YES": croped_img = croped_img elif crop2 == "NO": croped_img = dpi_image else: pass else: st.subheader("Image is considered to be croped") croped_img = dpi_image croped_img = image_resize(croped_img, 750, 750) st.title("ROTATE Image") st.subheader("Do you need to rotate the text?") rotate = st.selectbox("Rotate", ["", "NO", "YES"]) if rotate == "YES": rotated_img = text_rotation(croped_img) st.image(rotated_img) clear_image = remove_noise_and_smooth(rotated_img) text = pytesseract.image_to_string(clear_image, lang='eng') elif rotate == "NO": st.write("Image is considered to be in straight text") rotated_img = croped_img st.image(rotated_img) clear_image = remove_noise_and_smooth(rotated_img) text = pytesseract.image_to_string(clear_image, lang='eng') else: pass if st.button("Show Text"): st.write(text) if st.button("Sentiment analysis"): st.subheader("SENTIMENT ANALYSIS OF THE TEXT") st.title(sentiment_analysis(text)) if st.button("Visualizing the text"): word_cloud(text) wordcloud = WordCloud().generate(text) plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.show() st.pyplot() st.subheader("Do you need to get the text as mail?") mail = st.selectbox("Mail", ["", "NO", "YES"]) if mail == "YES": mail_id = st.text_input("Enter the mail address", "Type Here") if st.button("Get_Mail"): server = smtplib.SMTP_SSL("smtp.gmail.com", 465) server.login("*****@*****.**", "Abcd#1234") st.write("Login Success") server.sendmail("*****@*****.**", mail_id, msg=text) st.write("Email sent successfully") else: pass timestr = time.strftime("%Y%m%d-%H%M%S") file_name = "Document" + timestr + ".txt" st.subheader("Do you need to get the text as document?") docs = st.selectbox("Document", ["", "NO", "YES"]) if docs == "YES": file_to_download = write_of_file(text, file_name) st.info("Saved Result as ::{}".format(file_name)) d_link = make_downloable_file(file_to_download) st.markdown(d_link, unsafe_allow_html=True) else: pass elif choice == "ABOUT": html_temp = """ <div style="background-color:green;padding:2px"> <h2 style="color:white;text-align:center;">\t\t\tWelcome to Text Extraction using Tesseract OCR</h2> </div> """ st.markdown(html_temp, unsafe_allow_html=True) #st.title("\t\t\tWelcome to Text Extraction using Tesseract OCR") st.subheader("Image To Text Extraction") st.write( "We have developed an pretty good tool for conversion of image to text" ) st.title("Lets Start!!") st.subheader("Image Upload") st.write( "->Click Text Extraction from the Select Activity drop down in sidebar" ) st.write("-> Now its time to upload your image in upload section") st.write( "-->Then click browse and upload your image also you wil get preview of your image" ) st.title("Image Preview") st.title("Crop Section") st.subheader("Condition for Crop") st.write("To crop the image following conditions should satisfy \n ") st.write("1.Four corners of the page should be visible") st.write("2.Background of the image should be plain") st.subheader("Crop Section") st.write( "-->Do you want to crop the image ?\nif your image satisfy above category go for crop by clicking YES from drop down" ) st.title("Rotate Section") st.write( "->->if you want to rotate your image sometimes you have images in slanting order so you can reshape here" ) st.title("click Read text to see your coverted text") st.title("Salient Features ") st.subheader( "We provide pre determined features \n 1)Email-If you choose this enter the mail id so you will get your converted text in your inbox" ) st.write( "2)Sentiment Analysis - If you want to see the sentiment analysis of text click sentiment analysis" ) st.write( "3)Word Cloud-We provide word cloud to understand the text easily with visualization" ) st.write( "4)Export-If you need to get the converted text in Text document(.txt)click YES to download the file" ) st.title("\t\t\tThank you") st.title("Hope you enjoyed this conversion") else: st.set_option('deprecation.showfileUploaderEncoding', False) html_temp = """ <div style="background-color:blue;padding:10px"> <h2 style="color:white;text-align:center;">Text Detection and Localization</h2> </div> """ st.markdown(html_temp, unsafe_allow_html=True) image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg']) if image_file is not None: our_image = Image.open(image_file) new_img = np.array(our_image.convert('RGB')) res_image = image_resize(new_img, 750, 750) st.image(res_image) if st.button("Show"): text_localization(new_img)
def main(): activities = ['About', 'Monitoring System', 'Developers'] option = st.sidebar.selectbox('Menu Bar:', activities) if option == 'About': html_temp = """ <div style = "background-color: yellow; padding: 10px;"> <center><h1>ABOUT OUR PROJECT</h1></center> </div><br> """ st.markdown(html_temp, unsafe_allow_html=True) st.title("What is Money Laundering?") image = Image.open('images/1.png') st.image(image, use_column_width=True) st.set_option('deprecation.showfileUploaderEncoding', False) #to remove error st.subheader( 'Money laundering is the illegal process of concealing the origins of money obtained illegally by passing it through a complex sequence of banking transfers or commercial transactions.' ) st.title('What is Transaction Monitoring?') st.subheader( 'Anti-money laundering (AML) transaction monitoring software allows banks and other financial institutions to monitor customer transactions on a daily basis or in real-time for risk.' ) st.subheader( 'By combining this information with analysis of customers’ historical information and account profile, the software can provide financial institutions with a “whole picture” analysis of a customer’s profile, risk levels, and predicted future activity, and can also generate reports and create alerts to suspicious activity' ) st.title('Our Product') st.subheader('Working of the product in a nutshell') image = Image.open('images/2.png') st.image(image, use_column_width=True) st.set_option('deprecation.showfileUploaderEncoding', False) #to remove error st.title('The future prospects ') st.subheader( 'With increasing crony capitalism and corruption due to the slightest of inefficiencies and people’s urges to make the most money in the shortest period of time, even not considering the legal implications, it is almost certain that we will see an increase in the amount of money being laundered in the global economy, without an interference by major players like financial institutions, consulting firms etc. ' ) st.subheader( 'To make our project self sustainable and ever adapting according to the market scenario, an integration of a simple Artificial neural network will be done to the model created, so that, as datasets become larger, the efficiency does not take a hit, and the model learns from itself, just like all of us. ' ) elif option == 'Monitoring System': st.title("TRANSACTION MONITORING SYSTEM") image = Image.open('images/Magnify_Monitoring.jpg') st.image(image, use_column_width=True) st.set_option('deprecation.showfileUploaderEncoding', False) #to remove error st.subheader("Please Upload Your Dataset") data = st.file_uploader("Upload your dataset", type=['csv']) if data is not None: df = pd.read_csv(data) st.dataframe(df.head(10)) st.success("Data Successfully loaded") model = pickle.load(open("final_model.pkl", "rb")) def preprocessData(dataFrame): #dataFrame = dataFrame.drop('step','nameOrig','nameDest'],axis = 1) dataFrame = dataFrame.drop(['step', 'nameOrig', 'nameDest'], axis=1) x_new = dataFrame x_new = pd.get_dummies(x_new) return x_new if st.button("Predict"): st.balloons() x_new = preprocessData(df) df['y_prednew'] = model.predict(x_new) df.to_csv('final_result.csv') st.title("Your Output is") st.dataframe(df)
def app(): st.title('Analysis of Model Performances') eval_tab = pd.read_csv('eval_tab.csv') eval_tab_cfs = pd.read_csv('eval_tab_cfs.csv') eval_tab_ffs = pd.read_csv('eval_tab_ffs.csv') eval_tab_lfs = pd.read_csv('eval_tab_lfs.csv') st.write("Table with measures of different models with all features") st.write(eval_tab) #visualising the performance st.set_option('deprecation.showPyplotGlobalUse', False) st.write("Visualisation of performance of the models with all features") model = eval_tab['Model'] values = [ eval_tab['Accuracy'], eval_tab['Precision'], eval_tab['Recall'], eval_tab['F-1'] ] n = len(values) w = .15 x = np.arange(0, len(model)) plt.figure(figsize=(25, 15)) plt.title( "Comparison of Accuracy,Preccision,Recall,F-1 among Different classifiers" ) for i, value in enumerate(values): position = x + (w * (1 - n) / 2) + i * w plt.bar(position, value, width=w, label=f'{eval_tab.columns[i+1]}') plt.xticks(x, model) plt.ylabel('Precise Value') plt.ylim(80, 100) plt.legend() st.pyplot() st.write("Table with measures of different models with Corelated features") st.write(eval_tab_cfs) #visualising the performance st.set_option('deprecation.showPyplotGlobalUse', False) st.write("Visualisation of performance of the models with all features") model = eval_tab_cfs['Model'] values = [ eval_tab_cfs['Accuracy'], eval_tab_cfs['Precision'], eval_tab_cfs['Recall'], eval_tab_cfs['F-1'] ] n = len(values) w = .15 x = np.arange(0, len(model)) plt.figure(figsize=(25, 15)) plt.title( "Comparison of Accuracy,Preccision,Recall,F-1 among Different classifiers" ) for i, value in enumerate(values): position = x + (w * (1 - n) / 2) + i * w plt.bar(position, value, width=w, label=f'{eval_tab.columns[i+1]}') plt.xticks(x, model) plt.ylabel('Precise Value') plt.ylim(80, 100) plt.legend() st.pyplot() st.write( "Table with measures of different models with Forward feature Selection method" ) st.write(eval_tab_ffs) #visualising the performance st.set_option('deprecation.showPyplotGlobalUse', False) st.write("Visualisation of performance of the models with all features") model = eval_tab_ffs['Model'] values = [ eval_tab_ffs['Accuracy'], eval_tab_ffs['Precision'], eval_tab_ffs['Recall'], eval_tab_ffs['F-1'] ] n = len(values) w = .15 x = np.arange(0, len(model)) plt.figure(figsize=(25, 15)) plt.title( "Comparison of Accuracy,Preccision,Recall,F-1 among Different classifiers" ) for i, value in enumerate(values): position = x + (w * (1 - n) / 2) + i * w plt.bar(position, value, width=w, label=f'{eval_tab_ffs.columns[i+1]}') plt.xticks(x, model) plt.ylabel('Precise Value') plt.ylim(80, 100) plt.legend() st.pyplot() st.write( "Table with measures of different models with Lasso feature Selection") st.write(eval_tab_lfs) #visualising the performance st.set_option('deprecation.showPyplotGlobalUse', False) st.write( "Visualisation of performance of the models with Lasso Feature Selection" ) model = eval_tab_lfs['Model'] values = [ eval_tab_lfs['Accuracy'], eval_tab_lfs['Precision'], eval_tab_lfs['Recall'], eval_tab_lfs['F-1'] ] n = len(values) w = .15 x = np.arange(0, len(model)) plt.figure(figsize=(25, 15)) plt.title( "Comparison of Accuracy,Preccision,Recall,F-1 among Different classifiers" ) for i, value in enumerate(values): position = x + (w * (1 - n) / 2) + i * w plt.bar(position, value, width=w, label=f'{eval_tab_lfs.columns[i+1]}') plt.xticks(x, model) plt.ylabel('Precise Value') plt.ylim(80, 100) plt.legend() st.pyplot()
def image_selector(): st.set_option('deprecation.showfileUploaderEncoding', False) return st.sidebar.file_uploader(' ', type=['png', 'jpg', 'jpeg'])
def main(): frontendtemplate = FrontEndTemplate() init_invoice_df = getInitDataFrame() plot_one_df = get_city_size(init_invoice_df) # TODO show sidebar showSideBar() if btn_dic['btn_datashow']: st.markdown(frontendtemplate.title_temp.format('Data View'), unsafe_allow_html=True) st.dataframe(init_invoice_df[[ 'invoice_number', 'product_name', 'quantity', 'unit_price', 'invoice_number', 'invoice_date', 'store_address', ]]) # TODO st.markdown(frontendtemplate.title_temp.format('各類別數量'), unsafe_allow_html=True) plot_cat_cal = init_invoice_df.groupby( 'cat_id')['invoice_number'].count() plot_cat_cal.plot(kind='bar', rot=85, figsize=(10, 6)) st.set_option('deprecation.showPyplotGlobalUse', False) st.pyplot() # st.bar_chart(plot_cat_cal, width=20,use_container_width=True) # st.dataframe(init_invoice_df.groupby('cat_id')['unit_price'].count()) # TODO st.markdown(frontendtemplate.title_temp.format('各載具在各類別的數量'), unsafe_allow_html=True) plot_catcnid_cal = init_invoice_df.groupby( ['carrier_number', 'cat_id'], as_index=False).size() st.dataframe(plot_catcnid_cal) # TODO st.markdown(frontendtemplate.title_temp.format('各類別在各縣市上的數量'), unsafe_allow_html=True) # TODO 分欄 left_column, right_column = st.beta_columns(2) with left_column: left_column.write("飲料沖泡") plot_one_df.loc[plot_one_df['cat_id'] == 1, ['city', 'size']].plot.bar(x='city', y='size', rot=85) left_column.pyplot() left_column.write("美食生鮮") plot_one_df.loc[plot_one_df['cat_id'] == 3, ['city', 'size']].plot.bar(x='city', y='size', rot=85) left_column.pyplot() left_column.write("居家生活") plot_one_df.loc[plot_one_df['cat_id'] == 5, ['city', 'size']].plot.bar(x='city', y='size', rot=85) left_column.pyplot() left_column.write("家電") plot_one_df.loc[plot_one_df['cat_id'] == 7, ['city', 'size']].plot.bar(x='city', y='size', rot=85) left_column.pyplot() left_column.write("零食") plot_one_df.loc[plot_one_df['cat_id'] == 9, ['city', 'size']].plot.bar(x='city', y='size', rot=85) left_column.pyplot() left_column.write("菸酒") plot_one_df.loc[plot_one_df['cat_id'] == 13, ['city', 'size']].plot.bar(x='city', y='size', rot=85) left_column.pyplot() left_column.write("生活休閒") plot_one_df.loc[plot_one_df['cat_id'] == 15, ['city', 'size']].plot.bar(x='city', y='size', rot=85) left_column.pyplot() with right_column: right_column.write("麵食料理") plot_one_df.loc[plot_one_df['cat_id'] == 2, ['city', 'size']].plot.bar(x='city', y='size', rot=85) right_column.pyplot() right_column.write("保健生機") plot_one_df.loc[plot_one_df['cat_id'] == 4, ['city', 'size']].plot.bar(x='city', y='size', rot=85) right_column.pyplot() right_column.write("美容保養") plot_one_df.loc[plot_one_df['cat_id'] == 6, ['city', 'size']].plot.bar(x='city', y='size', rot=85) right_column.pyplot() right_column.write("箱包服飾") plot_one_df.loc[plot_one_df['cat_id'] == 8, ['city', 'size']].plot.bar(x='city', y='size', rot=85) right_column.pyplot() right_column.write("其他") plot_one_df.loc[plot_one_df['cat_id'] == 10, ['city', 'size']].plot.bar(x='city', y='size', rot=85) right_column.pyplot() right_column.write("3C") plot_one_df.loc[plot_one_df['cat_id'] == 12, ['city', 'size']].plot.bar(x='city', y='size', rot=85) right_column.pyplot() right_column.write("寵物專區") plot_one_df.loc[plot_one_df['cat_id'] == 14, ['city', 'size']].plot.bar(x='city', y='size', rot=85) right_column.pyplot() if btn_dic['btn_predict']: classifierName = ClassifierByName(df=init_invoice_df) result = classifierName.testByData([getproductword]) st.markdown(frontendtemplate.title_temp.format('預測結果'), unsafe_allow_html=True) st.markdown( """<h3 style="color:#0877E3;font-weight:bold;">準確率為:0.9633427718680646</h3>""", unsafe_allow_html=True) outputHtml = predictShow(getproductword, result[0]) st.markdown(outputHtml, unsafe_allow_html=True) st.markdown( """<h3 style="color:#0877E3;font-weight:bold;">各類別數據量</h3>""", unsafe_allow_html=True) catval_df = dfValueCount(init_invoice_df) catval_df.value_counts().plot(kind='bar', rot=85, figsize=(10, 6)) st.set_option('deprecation.showPyplotGlobalUse', False) st.pyplot() if btn_dic['btn_rec']: invoice_afterFilter_df, resdf, final_dict = getFilterDataFrame( init_invoice_df, selected_cat_list, getwordListStr) # TODO 主頁 st.markdown(frontendtemplate.title_temp.format('此次查詢Infos'), unsafe_allow_html=True) st.subheader("此次查詢共{}筆".format(invoice_afterFilter_df.shape[0])) st.dataframe(invoice_afterFilter_df[[ 'invoice_number', 'product_name', 'store_address', 'quantity', 'unit_price', ]]) # # 分群資料 # st.dataframe(resdf.head(25)) group_users_cal_dict = resdf.set_index(['group_name' ])['users'].to_dict() group_users_count_dict = resdf.set_index(['group_name' ])['users_count'].to_dict() # # 分群人數資料 # st.write(group_users_cal_dict) # TODO 分欄 left_column, right_column = st.beta_columns(2) # Or even better, call Streamlit functions inside a "with" block: with left_column: left_column.markdown(frontendtemplate.title_temp.format('曾經購買'), unsafe_allow_html=True) used_buy_items = final_dict['used_buy'] # left_column.write(used_buy_items) for item in used_buy_items: card_temp_body = "" group_name = item['group_name'] result = item['result'] outputHtmlList = imageListShow(result) for item in outputHtmlList: card_temp_body += item # print(type(group_users_cal_dict[group_name])) group_users_cal_dict[group_name] = group_users_cal_dict[ group_name].replace('\r', '\\r').replace('\n', '\\n') # users_list = json.loads(group_users_cal_dict[group_name], strict=False) # print(users_list) card_temp = frontendtemplate.card_temp_up + card_temp_body + frontendtemplate.card_temp_down left_column.markdown(card_temp.format( '#F6800A', group_name, group_users_count_dict[group_name]), unsafe_allow_html=True) with right_column: right_column.markdown(frontendtemplate.title_temp.format('未來可能購買'), unsafe_allow_html=True) feature_buy_items = final_dict['feature_buy'] # right_column.write(feature_buy_items) for item in feature_buy_items: card_temp_body = "" group_name = item['group_name'] result = item['result'] outputHtmlList = imageListShow(result) for item in outputHtmlList: card_temp_body += item card_temp = frontendtemplate.card_temp_up + card_temp_body + frontendtemplate.card_temp_down right_column.markdown(card_temp.format( '#03b6fc', group_name, group_users_count_dict[group_name]), unsafe_allow_html=True)
def main(): side_img = Image.open("images/emotion3.jpg") with st.sidebar: st.image(side_img, width=300) st.sidebar.subheader("Menu") website_menu = st.sidebar.selectbox( "Menu", ("Emotion Recognition", "Project description", "Our team", "Leave feedback", "Relax")) st.set_option('deprecation.showfileUploaderEncoding', False) if website_menu == "Emotion Recognition": st.sidebar.subheader("Model") model_type = st.sidebar.selectbox("How would you like to predict?", ("mfccs", "mel-specs")) em3 = em6 = em7 = gender = False st.sidebar.subheader("Settings") st.markdown("## Upload the file") with st.beta_container(): col1, col2 = st.beta_columns(2) # audio_file = None # path = None with col1: audio_file = st.file_uploader("Upload audio file", type=['wav', 'mp3', 'ogg']) if audio_file is not None: if not os.path.exists("audio"): os.makedirs("audio") path = os.path.join("audio", audio_file.name) if_save_audio = save_audio(audio_file) if if_save_audio == 1: st.warning("File size is too large. Try another file.") elif if_save_audio == 0: # extract features # display audio st.audio(audio_file, format='audio/wav', start_time=0) try: wav, sr = librosa.load(path, sr=44100) Xdb = get_melspec(path)[1] mfccs = librosa.feature.mfcc(wav, sr=sr) # # display audio # st.audio(audio_file, format='audio/wav', start_time=0) except Exception as e: audio_file = None st.error( f"Error {e} - wrong format of the file. Try another .wav file." ) else: st.error("Unknown error") else: if st.button("Try test file"): wav, sr = librosa.load("test.wav", sr=44100) Xdb = get_melspec("test.wav")[1] mfccs = librosa.feature.mfcc(wav, sr=sr) # display audio st.audio("test.wav", format='audio/wav', start_time=0) path = "test.wav" audio_file = "test" with col2: if audio_file is not None: fig = plt.figure(figsize=(10, 2)) fig.set_facecolor('#d1d1e0') plt.title("Wave-form") librosa.display.waveplot(wav, sr=44100) plt.gca().axes.get_yaxis().set_visible(False) plt.gca().axes.get_xaxis().set_visible(False) plt.gca().axes.spines["right"].set_visible(False) plt.gca().axes.spines["left"].set_visible(False) plt.gca().axes.spines["top"].set_visible(False) plt.gca().axes.spines["bottom"].set_visible(False) plt.gca().axes.set_facecolor('#d1d1e0') st.write(fig) else: pass # st.write("Record audio file") # if st.button('Record'): # with st.spinner(f'Recording for 5 seconds ....'): # st.write("Recording...") # time.sleep(3) # st.success("Recording completed") # st.write("Error while loading the file") if model_type == "mfccs": em3 = st.sidebar.checkbox("3 emotions", True) em6 = st.sidebar.checkbox("6 emotions", True) em7 = st.sidebar.checkbox("7 emotions") gender = st.sidebar.checkbox("gender") elif model_type == "mel-specs": st.sidebar.warning("This model is temporarily disabled") else: st.sidebar.warning("This model is temporarily disabled") # with st.sidebar.beta_expander("Change colors"): # st.sidebar.write("Use this options after you got the plots") # col1, col2, col3, col4, col5, col6, col7 = st.beta_columns(7) # # with col1: # a = st.color_picker("Angry", value="#FF0000") # with col2: # f = st.color_picker("Fear", value="#800080") # with col3: # d = st.color_picker("Disgust", value="#A52A2A") # with col4: # sd = st.color_picker("Sad", value="#ADD8E6") # with col5: # n = st.color_picker("Neutral", value="#808080") # with col6: # sp = st.color_picker("Surprise", value="#FFA500") # with col7: # h = st.color_picker("Happy", value="#008000") # if st.button("Update colors"): # global COLOR_DICT # COLOR_DICT = {"neutral": n, # "positive": h, # "happy": h, # "surprise": sp, # "fear": f, # "negative": a, # "angry": a, # "sad": sd, # "disgust": d} # st.success(COLOR_DICT) if audio_file is not None: st.markdown("## Analyzing...") if not audio_file == "test": st.sidebar.subheader("Audio file") file_details = { "Filename": audio_file.name, "FileSize": audio_file.size } st.sidebar.write(file_details) with st.beta_container(): col1, col2 = st.beta_columns(2) with col1: fig = plt.figure(figsize=(10, 2)) fig.set_facecolor('#d1d1e0') plt.title("MFCCs") librosa.display.specshow(mfccs, sr=sr, x_axis='time') plt.gca().axes.get_yaxis().set_visible(False) plt.gca().axes.spines["right"].set_visible(False) plt.gca().axes.spines["left"].set_visible(False) plt.gca().axes.spines["top"].set_visible(False) st.write(fig) with col2: fig2 = plt.figure(figsize=(10, 2)) fig2.set_facecolor('#d1d1e0') plt.title("Mel-log-spectrogram") librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz') plt.gca().axes.get_yaxis().set_visible(False) plt.gca().axes.spines["right"].set_visible(False) plt.gca().axes.spines["left"].set_visible(False) plt.gca().axes.spines["top"].set_visible(False) st.write(fig2) if model_type == "mfccs": st.markdown("## Predictions") with st.beta_container(): col1, col2, col3, col4 = st.beta_columns(4) mfccs = get_mfccs(path, model.input_shape[-1]) mfccs = mfccs.reshape(1, *mfccs.shape) pred = model.predict(mfccs)[0] with col1: if em3: pos = pred[3] + pred[5] * .5 neu = pred[2] + pred[5] * .5 + pred[4] * .5 neg = pred[0] + pred[1] + pred[4] * .5 data3 = np.array([pos, neu, neg]) txt = "MFCCs\n" + get_title(data3, CAT3) fig = plt.figure(figsize=(5, 5)) COLORS = color_dict(COLOR_DICT) plot_colored_polar(fig, predictions=data3, categories=CAT3, title=txt, colors=COLORS) # plot_polar(fig, predictions=data3, categories=CAT3, # title=txt, colors=COLORS) st.write(fig) with col2: if em6: txt = "MFCCs\n" + get_title(pred, CAT6) fig2 = plt.figure(figsize=(5, 5)) COLORS = color_dict(COLOR_DICT) plot_colored_polar(fig2, predictions=pred, categories=CAT6, title=txt, colors=COLORS) # plot_polar(fig2, predictions=pred, categories=CAT6, # title=txt, colors=COLORS) st.write(fig2) with col3: if em7: model_ = load_model("model4.h5") mfccs_ = get_mfccs(path, model_.input_shape[-2]) mfccs_ = mfccs_.T.reshape(1, *mfccs_.T.shape) pred_ = model_.predict(mfccs_)[0] txt = "MFCCs\n" + get_title(pred_, CAT7) fig3 = plt.figure(figsize=(5, 5)) COLORS = color_dict(COLOR_DICT) plot_colored_polar(fig3, predictions=pred_, categories=CAT7, title=txt, colors=COLORS) # plot_polar(fig3, predictions=pred_, categories=CAT7, # title=txt, colors=COLORS) st.write(fig3) with col4: if gender: with st.spinner('Wait for it...'): gmodel = load_model("model_mw.h5") gmfccs = get_mfccs(path, gmodel.input_shape[-1]) gmfccs = gmfccs.reshape(1, *gmfccs.shape) gpred = gmodel.predict(gmfccs)[0] gdict = [["female", "woman.png"], ["male", "man.png"]] ind = gpred.argmax() txt = "Predicted gender: " + gdict[ind][0] img = Image.open("images/" + gdict[ind][1]) fig4 = plt.figure(figsize=(3, 3)) fig4.set_facecolor('#d1d1e0') plt.title(txt) plt.imshow(img) plt.axis("off") st.write(fig4) # if model_type == "mel-specs": # st.markdown("## Predictions") # st.warning("The model in test mode. It may not be working properly.") # if st.checkbox("I'm OK with it"): # try: # with st.spinner("Wait... It can take some time"): # global tmodel # tmodel = load_model_cache("tmodel_all.h5") # fig, tpred = plot_melspec(path, tmodel) # col1, col2, col3 = st.beta_columns(3) # with col1: # st.markdown("### Emotional spectrum") # dimg = Image.open("images/spectrum.png") # st.image(dimg, use_column_width=True) # with col2: # fig_, tpred_ = plot_melspec(path=path, # tmodel=tmodel, # three=True) # st.write(fig_, use_column_width=True) # with col3: # st.write(fig, use_column_width=True) # except Exception as e: # st.error(f"Error {e}, model is not loaded") elif website_menu == "Project description": import pandas as pd import plotly.express as px st.title("Project description") st.subheader("GitHub") link = '[GitHub repository of the web-application]' \ '(https://github.com/CyberMaryVer/speech-emotion-webapp)' st.markdown(link, unsafe_allow_html=True) st.subheader("Theory") link = '[Theory behind - Medium article]' \ '(https://talbaram3192.medium.com/classifying-emotions-using-audio-recordings-and-python-434e748a95eb)' st.markdown(link + ":clap::clap::clap: Tal!", unsafe_allow_html=True) with st.beta_expander("See Wikipedia definition"): components.iframe( "https://en.wikipedia.org/wiki/Emotion_recognition", height=320, scrolling=True) st.subheader("Dataset") txt = """ This web-application is a part of the final **Data Mining** project for **ITC Fellow Program 2020**. Datasets used in this project * Crowd-sourced Emotional Mutimodal Actors Dataset (**Crema-D**) * Ryerson Audio-Visual Database of Emotional Speech and Song (**Ravdess**) * Surrey Audio-Visual Expressed Emotion (**Savee**) * Toronto emotional speech set (**Tess**) """ st.markdown(txt, unsafe_allow_html=True) df = pd.read_csv("df_audio.csv") fig = px.violin(df, y="source", x="emotion4", color="actors", box=True, points="all", hover_data=df.columns) st.plotly_chart(fig, use_container_width=True) st.subheader("FYI") st.write( "Since we are currently using a free tier instance of AWS, " "we disabled mel-spec and ensemble models.\n\n" "If you want to try them we recommend to clone our GitHub repo") st.code( "git clone https://github.com/CyberMaryVer/speech-emotion-webapp.git", language='bash') st.write( "After that, just uncomment the relevant sections in the app.py file " "to use these models:") elif website_menu == "Our team": st.subheader("Our team") st.balloons() col1, col2 = st.beta_columns([3, 2]) with col1: st.info("*****@*****.**") st.info("*****@*****.**") st.info("*****@*****.**") with col2: liimg = Image.open("images/LI-Logo.png") st.image(liimg) st.markdown( f""":speech_balloon: [Maria Startseva](https://www.linkedin.com/in/maria-startseva)""", unsafe_allow_html=True) st.markdown( f""":speech_balloon: [Tal Baram](https://www.linkedin.com/in/tal-baram-b00b66180)""", unsafe_allow_html=True) st.markdown( f""":speech_balloon: [Asher Holder](https://www.linkedin.com/in/asher-holder-526a05173)""", unsafe_allow_html=True) elif website_menu == "Leave feedback": st.subheader("Leave feedback") user_input = st.text_area("Your feedback is greatly appreciated") user_name = st.selectbox( "Choose your personality", ["checker1", "checker2", "checker3", "checker4"]) if st.button("Submit"): st.success(f"Message\n\"\"\"{user_input}\"\"\"\nwas sent") if user_input == "log123456" and user_name == "checker4": with open("log0.txt", "r", encoding="utf8") as f: st.text(f.read()) elif user_input == "feedback123456" and user_name == "checker4": with open("log.txt", "r", encoding="utf8") as f: st.text(f.read()) else: log_file(user_name + " " + user_input) thankimg = Image.open("images/sticky.png") st.image(thankimg) else: import requests import json url = 'http://api.quotable.io/random' if st.button("get random mood"): with st.beta_container(): col1, col2 = st.beta_columns(2) n = np.random.randint(1, 1000, 1)[0] with col1: quotes = { "Good job and almost done": "checker1", "Great start!!": "checker2", "Please make corrections base on the following observation": "checker3", "DO NOT train with test data": "folk wisdom", "good work, but no docstrings": "checker4", "Well done!": "checker3", "For the sake of reproducibility, I recommend setting the random seed": "checker1" } if n % 5 == 0: a = np.random.choice(list(quotes.keys()), 1)[0] quote, author = a, quotes[a] else: try: r = requests.get(url=url) text = json.loads(r.text) quote, author = text['content'], text['author'] except Exception as e: a = np.random.choice(list(quotes.keys()), 1)[0] quote, author = a, quotes[a] st.markdown(f"## *{quote}*") st.markdown(f"### ***{author}***") with col2: st.image(image=f"https://picsum.photos/800/600?random={n}")
def mask_detection(): st.title("Face mask detection") activities = ["Image", "Webcam", "Video"] st.set_option('deprecation.showfileUploaderEncoding', False) choice = st.sidebar.selectbox("Mask Detection on?", activities) if choice == 'Image': st.subheader("Detection on image") image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg']) # upload image if image_file is not None: our_image = Image.open(image_file) # making compatible to PIL im = our_image.save('./images/out.jpg') saved_image = st.image(image_file, caption='image uploaded successfully', use_column_width=True) if st.button('Process'): st.image(RGB_img, use_column_width=True) if choice == 'Webcam': st.subheader("Detection on webcam") # vs = VideoStream(src=0).start() # time.sleep(2.0) cap = get_cap() frameST = st.empty() param = st.sidebar.slider('choose your value') ap = argparse.ArgumentParser() ap.add_argument("-f", "--face", type=str, default="face_detector", help="path to face detector model directory") ap.add_argument("-m", "--model", type=str, default="mask_detector.model", help="path to trained face mask detector model") ap.add_argument("-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections") args = vars(ap.parse_args()) # load our serialized face detector model from disk print("[INFO] loading face detector model...") prototxtPath = os.path.sep.join([args["face"], "deploy.prototxt"]) weightsPath = os.path.sep.join( [args["face"], "res10_300x300_ssd_iter_140000.caffemodel"]) faceNet = cv2.dnn.readNet(prototxtPath, weightsPath) # load the face mask detector model from disk print("[INFO] loading face mask detector model...") maskNet = load_model(args["model"]) while True: ret, frame = cap.read() #print(frame.shape) # Stop the program if reached end of video print("Done processing !!!") (locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet) # loop over the detected face locations and their corresponding # locations for (box, pred) in zip(locs, preds): # unpack the bounding box and predictions (startX, startY, endX, endY) = box (mask, withoutMask) = pred # determine the class label and color we'll use to draw # the bounding box and text label = "Mask" if mask > withoutMask else "No Mask" color = (0, 255, 0) if label == "Mask" else (0, 0, 255) # include the probability in the label label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100) # display the label and bounding box rectangle on the output # frame cv2.putText(frame, label, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2) cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2) cv2.waitKey(1) # Release device # cap.release() # break frameST.image(frame, channels="BGR") if choice == 'Video': st.subheader("Detection on Video") st.title("Play Uploaded File") uploaded_file = st.file_uploader("Choose a video...", type=["mp4"]) temporary_location = False if uploaded_file is not None: g = io.BytesIO(uploaded_file.read()) ## BytesIO Object temporary_location = "testout_simple.mp4" with open(temporary_location, 'wb') as out: ## Open temporary file as bytes out.write(g.read()) ## Read bytes into file # close file out.close() # vs = VideoStream(src=0).start() # time.sleep(2.0) scaling_factorx = 0.7 scaling_factory = 0.7 image_placeholder = st.empty() if temporary_location: while True: cap = get_cap_vid(temporary_location) frameST = st.empty() param = st.sidebar.slider('choose your value') ap = argparse.ArgumentParser() ap.add_argument("-f", "--face", type=str, default="face_detector", help="path to face detector model directory") ap.add_argument( "-m", "--model", type=str, default="mask_detector.model", help="path to trained face mask detector model") ap.add_argument( "-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections") args = vars(ap.parse_args()) # load our serialized face detector model from disk print("[INFO] loading face detector model...") prototxtPath = os.path.sep.join( [args["face"], "deploy.prototxt"]) weightsPath = os.path.sep.join( [args["face"], "res10_300x300_ssd_iter_140000.caffemodel"]) faceNet = cv2.dnn.readNet(prototxtPath, weightsPath) # load the face mask detector model from disk print("[INFO] loading face mask detector model...") maskNet = load_model(args["model"]) while True: ret, frame = cap.read() #print(frame.shape) # Stop the program if reached end of video frame = cv2.resize(frame, None, fx=scaling_factorx, fy=scaling_factory, interpolation=cv2.INTER_AREA) print("Done processing !!!") (locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet) # loop over the detected face locations and their corresponding # locations for (box, pred) in zip(locs, preds): # unpack the bounding box and predictions (startX, startY, endX, endY) = box (mask, withoutMask) = pred # determine the class label and color we'll use to draw # the bounding box and text label = "Mask" if mask > withoutMask else "No Mask" color = (0, 255, 0) if label == "Mask" else (0, 0, 255) # include the probability in the label label = "{}: {:.2f}%".format( label, max(mask, withoutMask) * 100) # display the label and bounding box rectangle on the output # frame cv2.putText(frame, label, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2) cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2) cv2.waitKey(1) # Release device # cap.release() # break frameST.image(frame, channels="BGR")
def main(): st.title("FIFA 20 CLUB & COUNTRY ANALYSIS") st.markdown("---") activities = ['CLUB', 'COUNTRY'] # CHOOSE EITHER ONE choice = st.sidebar.selectbox("Select", activities) #CREATING A SIDE NAVIGATION FORM st.sidebar.markdown("<hr>", unsafe_allow_html=True) if st.sidebar.button("Show Club Names"): st.sidebar.write(club_list) st.sidebar.markdown("<hr>", unsafe_allow_html=True) if st.sidebar.button("Show Country Names"): st.sidebar.write(country_list) st.sidebar.markdown("<hr>", unsafe_allow_html=True) st.sidebar.write("Reach Me Here!") link = '[GitHub](https://github.com/305kishan)' st.sidebar.markdown(link, unsafe_allow_html=True) link = '[Kaggle](https://www.kaggle.com/kishan305)' st.sidebar.markdown(link, unsafe_allow_html=True) link = '[LinkedIn](https://www.linkedin.com/in/305kishan/)' st.sidebar.markdown(link, unsafe_allow_html=True) st.sidebar.markdown("<hr>", unsafe_allow_html=True) if choice == 'CLUB': st.header('CLUB ANALYSIS') st.markdown("<br>", unsafe_allow_html=True) input_club = st.text_input("Enter A Club Name", "Real Madrid") input_club = string.capwords(input_club) if input_club not in club_list: #CHECKING INPUT VALIDITY st.text("Club Name Not Found in DataBase") elif st.button( "SUBMIT"): # IF INPUT IS VALID PROCEEDING TO FURTHER STEPS data1 = club(input_club).reset_index(drop=True) # DISPLAYING DATA HEADER st.write(data1.head()) # NUMBER OF PLAYERS IN SQUAD input_club_strength = club_strength(data1) st.write('Current Squad Strength of ', input_club, 'is', input_club_strength, ' Players') # SQUAD VALUE IN MILLION EUROS input_club_value = club_value(data1) st.write('Current Squad Value of', input_club, 'is', input_club_value, ' Millions Euro') # SQUAD WAGES IN MILLION EUROS input_club_wage = club_wage(data1) st.write(input_club, 'Spends ', input_club_wage, ' Millions Euros Per week as Wages to its Players') # AVERAGE AGE OF PLAYERS input_club_average_age = average_age(data1) st.write(input_club, 'Squad\'s have an average age of', input_club_average_age, 'Years') # AVERAGE OVR OF PLAYERS input_club_average_ovr = average_ovr(data1) st.write(input_club, 'Squad\'s have an average OVR of ', input_club_average_ovr) # PLOTTING OF DATA st.subheader('PLOTS') st.set_option('deprecation.showPyplotGlobalUse', False) # AGE WISE PLAYER COUNT plot_age(data1) # OVR WISE PLAYER COUNT plot_ovr(data1) # NATIONALITY WISE PLAYER COUNT plot_nationality(data1) # CONTRACT EXPIRATION WISE PLAYER COUNT plot_contractexp(data1) # POSITION WISE PLAYER COUNT plot_position(data1) # DISPLAYING PLAYERS NAME & OVR tempdf = data1.sort_values(by='overall') fig = px.bar(tempdf, x='short_name', y='overall', color='overall') fig['layout']['yaxis1'].update(title='', range=[60, 100], dtick=5, autorange=False) fig.update_layout(title='OVR of Players', xaxis_title="Player's Name", yaxis_title="OVR") st.plotly_chart(fig) # DISPLAYING PLAYERS NAME & POTENTIAL tempdf = data1.sort_values(by='potential') fig = px.bar(tempdf, x='short_name', y='potential', color='potential') fig['layout']['yaxis1'].update(title='', range=[60, 100], dtick=5, autorange=False) fig.update_layout(title='Potential of Players', xaxis_title="Player's Name", yaxis_title=" Potential") st.plotly_chart(fig) # DISPLAYING PLAYERS NAME & WAGES tempdf = data1.sort_values(by='wage_eur') fig = px.bar(tempdf, x='short_name', y='wage_eur', color='wage_eur') fig.update_layout(title='Weekly wages of Players', xaxis_title="Player's Name", yaxis_title="Wage in Euro") st.plotly_chart(fig) # DISPLAYING PLAYERS NAME & VALUES tempdf = data1.sort_values(by='value_eur') fig = px.bar(tempdf, x='short_name', y='value_eur', color='value_eur') fig.update_layout(title='Values of Players', xaxis_title="Player's Name", yaxis_title="Value in Euro") st.plotly_chart(fig) # DISPLAYING PLAYERS NAME & VALUES tempdf = data1.sort_values(by='contract_valid_until') fig = px.bar(tempdf, x='short_name', y='contract_valid_until', color='contract_valid_until') fig['layout']['yaxis1'].update(title='', range=[2019, 2026], dtick=5, autorange=False) fig.update_layout(title='Contract of Players', xaxis_title="Player's Name", yaxis_title="Contract Expiration") st.plotly_chart(fig) if choice == 'COUNTRY': st.header('COUNTRY ANALYSIS') st.markdown("<br>", unsafe_allow_html=True) input_country = st.text_input("Enter A Country Name", "India") input_country = string.capwords(input_country) if input_country not in country_list: #CHECKING INPUT VALIDITY st.text("Country Name Not Found in DataBase") elif st.button( "SUBMIT"): # IF INPUT IS VALID PROCEEDING TO FURTHER STEPS data1 = country(input_country).reset_index(drop=True) # COUNT OF PLAYERS FROM A COUNTRY input_country_playerscount = country_playerscount(data1) st.write('Number of Players from ', input_country, 'is', input_country_playerscount, ' Players') # COMBINED PLAYERS VALUE input_country_value = club_value(data1) st.write('Current Combined Player\'s Value of', input_country, ' is ', input_country_value, ' Millions Euro') # AVG AGE OF SQUAD input_country_avg_age = average_age(data1) st.write(input_country, 'Player\'s have an average age of ', input_country_avg_age, ' Years') # AVG OVR input_country_avg_ovr = average_ovr(data1) st.write(input_country, 'Player\'s have an average OVR of ', input_country_avg_ovr) # PLOTTING OF DATA # COUNT OF PLAYERS WITH DIFF AGES plot_age(data1) # COUNT OF PLAYERS WITH DIFFERENT OVRs plot_ovr(data1)
import streamlit as st #importing streamlit and tensorflow import tensorflow as tf import cv2 import keras from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, BatchNormalization, Flatten import numpy as np from PIL import Image, ImageOps st.set_option( 'deprecation.showfileUploaderEncoding', False ) #on loading a streamlit app we get a warning, this line prevents us from getting that warning @st.cache( allow_output_mutation=True ) #this line prevent us from loading the model again and again and will help in storing the model in cache once it has been loaded def load_model(): #loading our model model = tf.keras.models.load_model('/content/BrainTumorModel .h5') return model model = load_model() #defining the header or title of the page that the user will be seeing st.markdown( "<h1 style='text-align: center; color: Black;'>Brain Tumor Classifier</h1>", unsafe_allow_html=True) st.markdown( "<h3 style='text-align: center; color: Black;'>All you have to do is Upload the MRI scan and the model will do the rest!</h3>", unsafe_allow_html=True)
def main(): activities = ["Upload Photo", "Trombinoscope", "About"] choice = st.sidebar.selectbox("Select Activty", activities) ######################################################################## if choice == 'About': st.subheader("About Face Detection App") st.markdown("Built with Streamlit by Alexandra, Thomas and Andrei") st.text("Projet Fin Etudes OpenSpace") ######################################################################### elif choice == 'Upload Photo': # disable warning signs: st.set_option("deprecation.showfileUploaderEncoding", False) st.subheader("Upload Photo") # displays a file uploader widget and return to BytesIO image_byte = st.file_uploader( label="Select a picture containing faces:", type=['jpg', 'png']) # detect faces in the loaded image max_faces = 0 rois = [] # region of interests (arrays of face areas) if image_byte is not None: image_array = byte_to_array(image_byte) face_locations = face_recognition.face_locations(image_array) for idx, (top, right, bottom, left) in enumerate(face_locations): # save face region of interest to list rois.append(image_array[top:bottom, left:right].copy()) # Draw a box around the face and label it cv2.rectangle(image_array, (left, top), (right, bottom), COLOR_DARK, 2) cv2.rectangle(image_array, (left, bottom + 35), (right, bottom), COLOR_DARK, cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(image_array, f"#{idx}", (left + 5, bottom + 25), font, .55, COLOR_WHITE, 1) st.image(BGR_to_RGB(image_array), width=720) max_faces = len(face_locations) if max_faces > 0: # select interested face in picture face_idx = st.selectbox("Select face#", range(max_faces)) roi = rois[face_idx] st.image(BGR_to_RGB(roi), width=min(roi.shape[0], 300)) # initial database for known faces DB = init_data() face_encodings = DB[COLS_ENCODE].values dataframe = DB[COLS_INFO] try: # compare roi to known faces, show distances and similarities face_to_compare = face_recognition.face_encodings(roi)[0] dataframe['distance'] = face_recognition.face_distance( face_encodings, face_to_compare) dataframe['similarity'] = dataframe.distance.apply( lambda distance: f"{face_distance_to_conf(distance):0.2%}") st.dataframe( dataframe.sort_values("distance").iloc[:5].set_index( 'name')) # add roi to known database if st.checkbox('Add it to known faces'): face_name = st.text_input('Name:', '') face_des = st.text_input('Desciption:', '') if st.button('Add'): encoding = face_to_compare.tolist() DB.loc[len(DB)] = [face_name, face_des] + encoding DB.to_csv(PATH_DATA, index=False) except IndexError: st.subheader("This image is not relatable to our dataset") else: st.write('No human faces detected.') ##################################################################################### elif choice == 'Trombinoscope': st.subheader("Trombinoscope") if st.button('Start', key="start camera"): alert_placeholder = st.empty() while True: video_capture = cv2.VideoCapture(0) frame = capture_face(video_capture) name, similarity, frame = recognize_frame(frame) FRAME_WINDOW.image(frame) alert_placeholder.write("") if similarity > 0.75: # label = f"**{name}**: *{similarity:.2%} likely*" # st.markdown(label) alert_placeholder.text(f"{name}: {similarity:.2%} likely") if cv2.waitKey(1) & 0xFF == ord('q'): break
# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import streamlit as st st.set_option("deprecation.showfileUploaderEncoding", True) result = st.file_uploader("Drop a file:", type=["txt"]) # result = st.file_uploader("Drop a file:", type=["txt"], accept_multiple_files=False) if result is not None: st.text(result.getvalue()) else: st.text("No upload") st.set_option("deprecation.showfileUploaderEncoding", False) st.file_uploader("Disable deprecation", type=["txt"]) # result = st.file_uploader( # "Drop multiple files:", type=["txt"], accept_multiple_files=True # ) # if result is not None: # strings = sorted([s.getvalue() for s in result])
def main(): """Face Detection App""" st.title("Face Detection App") st.text("Build with Streamlit and OpenCV") activities = ["Detection", "About"] choice = st.sidebar.selectbox("Select Activty", activities) if choice == 'Detection': st.subheader("Face Detection") image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg']) st.set_option('deprecation.showfileUploaderEncoding', False) if image_file is not None: our_image = Image.open(image_file) st.text("Original Image") # st.write(type(our_image)) st.image(our_image) enhance_type = st.sidebar.radio( "Enhance Type", ["Original", "Gray-Scale", "Contrast", "Brightness", "Blurring"]) if enhance_type == 'Gray-Scale': new_img = np.array(our_image.convert('RGB')) img = cv2.cvtColor(new_img, 1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # st.write(new_img) st.image(gray) elif enhance_type == 'Contrast': c_rate = st.sidebar.slider("Contrast", 0.5, 3.5) enhancer = ImageEnhance.Contrast(our_image) img_output = enhancer.enhance(c_rate) st.image(img_output) elif enhance_type == 'Brightness': c_rate = st.sidebar.slider("Brightness", 0.5, 3.5) enhancer = ImageEnhance.Brightness(our_image) img_output = enhancer.enhance(c_rate) st.image(img_output) elif enhance_type == 'Blurring': new_img = np.array(our_image.convert('RGB')) blur_rate = st.sidebar.slider("Brightness", 0.5, 3.5) img = cv2.cvtColor(new_img, 1) blur_img = cv2.GaussianBlur(img, (11, 11), blur_rate) st.image(blur_img) elif enhance_type == 'Original': st.image(our_image, width=None) else: st.image(our_image, width=None) # Face Detection task = ["Faces", "Smiles", "Eyes", "Cannize", "Cartonize"] feature_choice = st.sidebar.selectbox("Find Features", task) if st.button("Process"): if feature_choice == 'Faces': result_img, result_faces = detect_faces(our_image) st.image(result_img) st.success("Found {} faces".format(len(result_faces))) elif feature_choice == 'Smiles': result_img = detect_smiles(our_image) st.image(result_img) elif feature_choice == 'Eyes': result_img = detect_eyes(our_image) st.image(result_img) elif feature_choice == 'Cartonize': result_img = cartonize_image(our_image) st.image(result_img) elif feature_choice == 'Cannize': result_canny = cannize_image(our_image) st.image(result_canny) elif choice == 'About': st.subheader("About Face Detection App") st.markdown("Built with Streamlit by ChromaniqueJ") st.text("Jayant Biradar") st.success("Completed")
def main(): # Sidebar st.sidebar.header("About -") st.sidebar.info("pdm04_st_Mid_exam") # Title st.title("Mid exam - pdm04, 김채영") st.header("- EDA of Pima diabetes data -") st.set_option('deprecation.showPyplotGlobalUse', False) # Get the data from github df = pd.read_csv("https://github.com/Redwoods/Py/raw/master/pdm2020/my-note/py-pandas/data/diabetes.csv") st.dataframe(df) # Return dataframe if st.checkbox("Show Data description"): st.dataframe(df.describe()) # shape st.subheader("shape") df.shape # st.subheader("Check & cleaning data") df.isnull().values.any(), df.isna().sum() vars = df.columns st.write(vars) df = df[vars].dropna() df.shape if st.checkbox("Skew of attribute distributions"): skew = df.skew() st.write(skew) st.markdown('- 데이터 왜곡도') st.markdown("* * *") # st.header("- Visualizing data -") # st.subheader("Check the balance of classes in the data through plot") if st.checkbox("Outcome plot"): classes=df.Outcome sns.countplot(classes, label='count') st.pyplot() nDB,DB=classes.value_counts() st.write('False: non-diabetes',nDB) st.write('True: diabetes',DB) classes.value_counts(), type(classes) st.text("0 : 정상인, 1 : 당뇨병 환자") st.markdown("* * *") # st.subheader("Show the data as a chart") if st.checkbox("chart"): st.line_chart(df) st.markdown("* * *") # st.subheader("Univariate plots:") # if st.checkbox("Histograms"): st.subheader("Histograms") plt.rcParams['figure.figsize'] = [12, 10] # set the figure size st.write(df.hist()) st.pyplot() if st.checkbox("Density Plots"): st.subheader("Density Plots") st.write(df.plot(kind='density', subplots=True, layout=(3,3), sharex=False)) st.pyplot() if st.checkbox("Box and Whisker Plots"): st.subheader("Box and Whisker Plots") st.write(df.plot(kind= 'box', subplots=True, layout=(3,3), sharex=False, sharey=False)) st.pyplot() st.markdown("* * *") # st.subheader("Multivariate Plots:") # if st.checkbox("Correlation plot"): st.subheader("Correlation plot") df.corr() plt.figure(figsize=(12,10)) sns.heatmap(df.corr(),annot=True, cmap= "RdYlGn", vmin=-1, vmax=1) st.pyplot() if st.checkbox("Compute correlation matrix"): st.subheader("Correlations of attributes in the data") correlations = df.corr(method = 'pearson') st.write(correlations) st.markdown('- 값이 1에 가까울수록 상관성이 있음!') if st.checkbox("result"): st.markdown('- 상관성 분석 결과\n' ' * Age vs. Pregnancies : 0.54\n' ' * Glucose vs. Outcome : 0.47\n' ' * SkinThickness vs. Insulin : 0.44\n' ' * SkinThickness vs. BMI : 0.39\n') st.markdown('- 상관성이 높은 변수들에 대한 좀 더 자세한 시각화가 필요하다.') st.markdown("* * *") # # Import required package from pandas.plotting import scatter_matrix plt.rcParams['figure.figsize'] = [12, 12] if st.checkbox("Scatter Plot Matrix"): st.subheader("Scatter Plot Matrix") scatter_matrix(df) plt.show() st.pyplot() if st.checkbox("Scatter Plot_1"): st.subheader("Scatter Plot") sns.pairplot(df, hue="Outcome", markers=["o", "s"],palette="husl") st.pyplot() if st.checkbox("Scatter Plot_2"): st.subheader("0, 1을 noDM, DM으로 변경") df_temp = df.copy() df_temp['Outcome'] = df_temp['Outcome'].replace([0, 1],['noDM', 'DM']) sns.pairplot(df_temp, hue='Outcome', markers=["o", "s"],palette="husl") st.pyplot() st.markdown("* * *") # if st.checkbox("6 high correlation"): st.subheader("상관성이 높은 6개의 특성에 대한 산포도") high_corr = ['Pregnancies', 'Glucose', 'SkinThickness', 'Insulin', 'BMI','Age', 'Outcome'] df_temp2 = df.copy() df_temp2['Outcome'] = df_temp2['Outcome'].replace([0, 1],['noDM', 'DM']) sns.pairplot(df_temp2[high_corr], hue='Outcome') st.pyplot() if st.checkbox("3 high correlation"): st.subheader("상관성이 높은 3개의 특성에 대한 산포도") highest_corr = ['Pregnancies', 'Age', 'Outcome'] df_temp3 = df.copy() df_temp3['Outcome'] = df_temp3['Outcome'].replace([0, 1],['noDM', 'DM']) sns.pairplot(df_temp3[highest_corr], hue='Outcome') st.pyplot() st.markdown("* * *") # st.subheader("Advanced plots:") # if st.checkbox("Standarization of data and Violinplot"): st.markdown('- Standarization of data (Normalization)') df_n = (df - df.mean())/df.std() df_n y=df.Outcome df2=pd.concat([y, df_n.iloc[:,0:8]], axis=1) y.shape,df2.shape df3=pd.melt(df2,id_vars='Outcome', var_name='features',value_name='values') df3.head(), df3.shape st.subheader("Violinplot") plt.figure(figsize=(10,10)) sns.violinplot(x='features', y='values', hue='Outcome', data=df3, split=True, inner='quart') plt.xticks(rotation=45) st.pyplot() # if st.checkbox("Customizing seaborn plot"): st.subheader("Customizing seaborn plot") sns.set(style='whitegrid', palette='muted') plt.figure(figsize=(10,10)) sns.swarmplot(x='features', y='values', hue='Outcome', data=df3) plt.xticks(rotation=45) st.pyplot() st.markdown("* * *")
def main(): colorr = """ <style> body{ background-color:rgb(209, 228, 37); } </style> """ st.markdown(colorr, unsafe_allow_html=True) option = ["About", "Images Detections", "Camera"] choices = st.sidebar.selectbox("Choices", option) if choices == "About": st.title("Welcome to Web OpenCV applications") st.header( "Please select the images Detections or Camera from sidebar options" ) st.title("Please follow the steps ") st.header("For Images Detections Tab") st.write( "1. First select the Images Detections tab from sidebar selectbox") st.write("2. You can select the formats like .png , jpg , jpeg ") st.write( "3. After that you can do eye detection , car detection and face detection in it" ) st.header("For Camera Detections") st.write("First you will se the Normal and Grey buttons at sidebar ") st.write( "If you select the normal then it turn on your webcam at normal and then press Start button" ) st.write( "If you selected the Grey button it will open your webcam in grey mode" ) if choices == "Images Detections": st.title("Images Detection") type_of_files = ['png', 'jpg', 'jpeg'] st.set_option('deprecation.showfileUploaderEncoding', False) images = st.file_uploader("Upload", type=type_of_files) if st.button("Show"): if images is not None: to_this = Image.open(images) st.image(to_this) elif choices == "Camera": st.title("Welcome to detection") st.info("Please Start Button after work done") st.sidebar.title("Select the modes") if st.button("Start Button"): st.warning("Go to the sidebar options") if st.sidebar.button("Grey"): cap = cv2.VideoCapture(0) img = st.empty() st.warning("Please Press start button again to stop this") while True: cond, frames = cap.read() grey = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY) frames = cv2.flip(frames, 1) frames = imutils.resize(frames, width=700) img.image(grey, channels="RGB") #cv2.imshow("Grey Mode",grey) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows() if st.sidebar.button("Normal"): cap = cv2.VideoCapture(0) img = st.empty() while True: cond, frames = cap.read() frames = cv2.flip(frames, 1) frames = imutils.resize(frames, width=700) img.image(frames, channels="BGR") #cv2.imshow("camera Frame",frames) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows() types_detect = [ "Select", 'Eye & Face Detections', 'Full body Detections', 'Car Detections' ] func = st.sidebar.selectbox("Select the Detections ", types_detect) face_file = cv2.CascadeClassifier( 'haarcascade_frontalface_default.xml') eye_file = cv2.CascadeClassifier('haarcascade_eye.xml') body = cv2.CascadeClassifier("haarcascade_fullbody.xml") car_file = cv2.CascadeClassifier("haarcascade_car.xml") if func == "Eye & Face Detections": st.title("Face and Eye Detection") if st.button("Start"): def detecting(gray, frame): faces = face_file.detectMultiScale(gray, 1.3, 3) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 321, 0), 2) gray_1 = gray[y:y + h, x:x + w] color_1 = frame[y:y + h, x:x + w] eyes = eye_file.detectMultiScale(gray_1, 1.3) for (e_x, e_y, e_w, e_h) in eyes: img = st.empty() cv2.rectangle(color_1, (e_x, e_y), (e_x + e_w, e_y + e_h), (213, 524, 0), 4) return frame cap = cv2.VideoCapture(0) img = st.empty() while True: ret, frame = cap.read() frame = cv2.flip(frame, 1) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame = imutils.resize(frame, width=700) y = detecting(gray, frame) img.image(y, channels="BGR") if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows() elif func == "Full body Detections": st.title("Human Detections") if st.button("Start Recognisation"): cap = cv2.VideoCapture( "walking.avi") # You can provide 0 for your webcam. img = st.empty() while True: ret, frame = cap.read() grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame = imutils.resize(frame, width=700) HUMAN = body.detectMultiScale(grey, 1.1, 2) for (x, y, w, h) in HUMAN: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 245), 3) img.image(frame, channels='BGR') if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows() elif func == 'Car Detections': st.title("Car Detections") if st.button("Start"): cap = cv2.VideoCapture(0) img = st.empty() while True: ret, frame = cap.read() # grey=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) frame = cv2.flip(frame, 1) frame = imutils.resize(frame, width=800) cars = car_file.detectMultiScale(frame, 1.3, 2) for (x, y, w, h) in cars: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 265), 4) img.image(frame, channels="BGR") if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows()
def clustering(): global target, daypara, df, df2, df_4pycaret, df_temp st.write(df) lookback = len(df.index) * (-1) X = np.array(df["price"][lookback:]) sum_of_squared_distances = [] K = range(1, 15) for k in K: km = KMeans(n_clusters=k) km = km.fit(X.reshape(-1, 1)) sum_of_squared_distances.append(km.inertia_) kn = KneeLocator(K, sum_of_squared_distances, S=1.0, curve="convex", direction="decreasing") kn.plot_knee(figsize=(7, 3)) st.set_option('deprecation.showPyplotGlobalUse', False) st.subheader("Search Number of Regime") st.pyplot() st.subheader("Plotting in Reallity") with st.spinner("Loading Chart..."): kmeans = KMeans(n_clusters=kn.knee).fit(X.reshape(-1, 1)) c = kmeans.predict(X.reshape(-1, 1)) minmax = [] for i in range(kn.knee): minmax.append([-np.inf, np.inf]) for i in range(len(X)): cluster = c[i] if X[i] > minmax[cluster][0]: minmax[cluster][0] = X[i] if X[i] < minmax[cluster][1]: minmax[cluster][1] = X[i] plt.figure(figsize=(11, 5), dpi=30) plt.title("Clustering Pressure/Support of {}".format(target), fontsize=20) plt.ylabel("price") index_p = [] index_s = [] a = np.transpose(minmax) a = np.sort(a) for i in range(len(X)): colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'] c = kmeans.predict(X[i].reshape(-1, 1))[0] color = colors[c] if X[i] in a[1]: index_s.append(i) if X[i] in a[0]: index_p.append(i) plt.scatter(i, X[i], c=color, s=20, marker="o") for i in range(len(minmax)): plt.hlines(a[0][i], xmin=index_p[i] - 10, xmax=index_p[i] + 10, colors="red", linestyle="--") plt.text(index_p[i] - 15, a[0][i], "Pressure= {:.2f}".format(a[0][i]), fontsize=13) plt.hlines(a[1][i], xmin=index_s[i] - 10, xmax=index_s[i] + 10, colors="b", linestyle="--") plt.text(index_s[i] - 15, a[1][i], "Support= {:.2f}".format(a[1][i]), fontsize=13) st.set_option('deprecation.showPyplotGlobalUse', False) st.pyplot()
def run_cluster_app(): st.header('■Cluster Analysis') st.write('- To group students with similar learning characteristics.') st.sidebar.subheader('Data upload') df_edu = pd.read_csv("data/eng_sample_data_cluster.csv") def download_link(object_to_download, download_filename, download_link_text): if isinstance(object_to_download,pd.DataFrame): object_to_download = object_to_download.to_csv(index=False, encoding = 'utf_8_sig') b64 = base64.b64encode(object_to_download.encode()).decode() return f'<a href="data:file/txt;base64,{b64}" download="{download_filename}">{download_link_text}</a>' tmp_download_link = download_link(df_edu, 'sample_cluster.csv', 'Download sample csv file.') st.sidebar.markdown(tmp_download_link, unsafe_allow_html=True) # st.sidebar.info(""" # [Download the sample csv file](https://github.com/59er/eng_learning_analytics_web/blob/master/sample_data/eng_sample_data_cluster_for_WEB.csv) # """) try: uploaded_file = st.sidebar.file_uploader("File upload (Drag and drop or use [Browse files] button to import csv file. Only utf-8 format is available.)", type=["csv"]) # uploaded_file = st.file_uploader( # label = 'File Upload(drag and drop csv/Excel)', # type = ['csv', 'xlsx'] # ) if uploaded_file is not None: df_edu = pd.read_csv(uploaded_file) uploaded_file.seek(0) display_data = st.sidebar.checkbox(label = 'Show uploaded data') if display_data: st.dataframe(df_edu) # df_edu.columns = ['','Literature','History','Math','Physics'] # st.write(df_edu.columns) dat_i = df_edu.set_index('ID') dat_i.describe().round(2) dat_i.mean(axis=1) pred = KMeans(n_clusters = 3).fit_predict(dat_i) dat_i1 = dat_i.copy() dat_i1['cluster_id'] = pred dat_i1['cluster_id'].value_counts() # z = linkage(dat_i, metric = 'euclidean', method = 'ward') # st.set_option('deprecation.showPyplotGlobalUse', False) # fig = plt.figure(figsize = (12,6), facecolor = 'w') # ax = fig.add_subplot(title= '樹形図: 全体') # dendrogram(z) # st.pyplot(fig) st.set_option('deprecation.showPyplotGlobalUse', False) st.write(sns.clustermap(dat_i,col_cluster = False, cmap='Blues', linewidth=.5)) st.pyplot() else: df_edu = pd.read_csv('data/eng_sample_data_cluster.csv') show_df = st.sidebar.checkbox('Show DataFreme') if show_df == True: st.write(df_edu) df_edu.columns = ['','Literature','Reading','History','Math','Physics'] dat_i = df_edu.set_index('') dat_i.describe().round(2) dat_i.mean(axis=1) pred = KMeans(n_clusters = 3).fit_predict(dat_i) dat_i1 = dat_i.copy() dat_i1['cluster_id'] = pred dat_i1['cluster_id'].value_counts() # z = linkage(dat_i, metric = 'euclidean', method = 'ward') # st.set_option('deprecation.showPyplotGlobalUse', False) # fig = plt.figure(figsize = (12,6), facecolor = 'w') # ax = fig.add_subplot(title= '樹形図: 全体') # dendrogram(z) # st.pyplot(fig) st.set_option('deprecation.showPyplotGlobalUse', False) st.write(sns.clustermap(dat_i,col_cluster = False, cmap='Blues', linewidth=.5)) st.pyplot() st.write('This example suggests that there are two groups: those who are good at humanities subjects and those who are good at science subjects.') except Exception as e: st.header('ERROR: Data inconsistency. Check data format to be uploaded.') print('Data inconsistency error')
# -*- coding: utf-8 -*- """ Created on Sun Dec 27 20:12:36 2020 @author: rejid4996 """ import streamlit as st import numpy as np import pandas as pd from fuzzywuzzy import fuzz import base64 from io import BytesIO st.set_option('deprecation.showfileUploaderEncoding', False) def main(): """NLP App with Streamlit""" st.sidebar.title("Text Matching") st.sidebar.subheader("Similarity check") st.success( "For more contents please subscribe to my Youtube Channel https://www.youtube.com/channel/UCgOwsx5injeaB_TKGsVD5GQ" ) st.info("Text Matching using Fuzzywuzzy") uploaded_file = st.sidebar.file_uploader( "Your base file should have two columns which contains the text that has to be matched",
def main(): # ============================================================================= # st.title("Default Predictor") # ============================================================================= st.markdown( "<h1 style='text-align: center; color: red;'>Default Predictor</h1>", unsafe_allow_html=True) html_temp = """ <div style="background-color:tomato;padding:10px"> <h2 style="color:white;text-align:center;">Credit Card Default Payment Prediction </h2> </div> """ st.markdown(html_temp, unsafe_allow_html=True) # ============================================================================= # PAY_1 = st.text_input("PAY_1","Repayment Status in September") # PAY_2 = st.text_input("PAY_2","Repayment Status in August") # PAY_3 = st.text_input("PAY_3","Repayment Status in July") # PAY_4 = st.text_input("PAY_4","Repayment Status in June") # PAY_5 = st.text_input("PAY_5","Repayment Status in May") # PAY_6 = st.text_input("PAY_6","Repayment Status in April") # ============================================================================= NAME = st.text_input("Name of the Client") AGE = st.number_input("AGE") AGE = int(AGE) PAY_1 = st.selectbox( 'PAY_1(REPAYMENT STATUS IN SEPTEMBER)', ("0 Months Delay", "1 Month Delay", "2 Months Delay", "3 Months Delay", "4 Months Delay", "5 Months Delay", "6 Months Delay", "7 Months Delay", "8 Months Delay")) x = PAY_1.split(" ") PAY_1 = int(x[0]) PAY_2 = st.selectbox( 'PAY_2(REPAYMENT STATUS IN AUGUST)', ("0 Months Delay", "1 Month Delay", "2 Months Delay", "3 Months Delay", "4 Months Delay", "5 Months Delay", "6 Months Delay", "7 Months Delay", "8 Months Delay")) y = PAY_2.split(" ") PAY_2 = int(y[0]) # ============================================================================= # BILL_AMT1 = st.number_input("BILL_AMT1") # BILL_AMT1=int(BILL_AMT1) # ============================================================================= BILL_AMT2 = st.number_input("BILL_AMT2(BILL AMOUNT IN AUGUST)") BILL_AMT2 = int(BILL_AMT2) PAY_AMT1 = st.number_input( "PAY_AMT1(AMOUNT OF PREVIOUS PAYMENT IN SEPTEMBER)") PAY_AMT1 = int(PAY_AMT1) BILL_AMT3 = st.number_input("BILL_AMT3(BILL AMOUNT IN JULY)") BILL_AMT3 = int(BILL_AMT3) PAY_AMT2 = st.number_input("PAY_AMT2(PREVIOUS PAYMENT IN AUGUST)") PAY_AMT2 = int(PAY_AMT2) # ============================================================================= # Closeness_1 = st.number_input("Closeness_1") # Closeness_2 = st.number_input("Closeness_2") # #Closeness_3 = st.text_input("Closeness_3","Repayment Status in April") # # Closeness_4 = st.number_input("Closeness_4") # ============================================================================= #Closeness_6 = st.text_input("Closeness_6","Repayment Status in April") LIMIT_BAL = st.number_input("LIMIT_BAL(CREDIT LIMIT GIVEN TO THE PERSON)") LIMIT_BAL = int(LIMIT_BAL) #Closeness_1=(LIMIT_BAL) - (BILL_AMT1) Closeness_2 = (LIMIT_BAL) - (BILL_AMT2) Closeness_3 = (LIMIT_BAL) - (BILL_AMT3) #Closeness_4=(LIMIT_BAL) - (BILL_AMT4) # top_12_rf=['PAY_1', 'PAY_2', 'BILL_AMT1', 'PAY_AMT1', 'AGE', #'Closeness_1', 'PAY_AMT2', 'Closeness_4', 'BILL_AMT2', 'Closeness_2', 'Closeness_3', 'Closeness_6'] option = st.selectbox( 'Select A Classifier', ('CatBoost', 'Voting Classifier(DT,KNN,LR)', 'Voting Classifier(DT,KNN,GNB)', 'StackingClassifier(KNN,RF,LR)')) st.write('You selected:', option) values = instance_values(PAY_1, PAY_2, BILL_AMT2, BILL_AMT3, PAY_AMT1, PAY_AMT2, AGE, Closeness_2, Closeness_3) #print(type(values)) result, probability, text = "", "", "" if st.button("Predict"): with st.spinner('Predicting The Results...'): result, probability, fig1, fig2 = predict_default( PAY_1, PAY_2, BILL_AMT2, BILL_AMT3, PAY_AMT1, PAY_AMT2, AGE, Closeness_2, Closeness_3, option) if result == 1: name = NAME if len(NAME) >= 1 else "Person" text = "{} is more likely to default next month payment.".format( name) st.success('{}. \nProbability of default is {} '.format( text, probability)) st.plotly_chart(fig1) st.plotly_chart(fig2) #Using Google Text To Speech API ta_tts = gTTS( '{}. Probability of default is {} percent, which is {} percent more than the threshold value of 50% ' .format(text, probability * 100, (probability * 100) - 50)) ta_tts.save("trans.mp3") audio_file = open("trans.mp3", "rb") audio_bytes = audio_file.read() st.audio(audio_bytes, format="audio/ogg") # ============================================================================= # audio_file = open('default.wav', 'rb') # audio_bytes = audio_file.read() # st.audio(audio_bytes, format='audio/wav') # ============================================================================= else: name = NAME if len(NAME) >= 1 else "Person" text = " {} is likely to Make the Payment on Time".format(name) st.success('{}. \n Probability of default is {} '.format( text, probability)) st.plotly_chart(fig1) st.plotly_chart(fig2) ta_tts = gTTS( '{}. probability of default is {} percent which is {} percent less than the threshold value of 50%' .format(text, probability * 100, 50 - (probability * 100))) ta_tts.save("trans.mp3") audio_file = open("trans.mp3", "rb") audio_bytes = audio_file.read() st.audio(audio_bytes, format="audio/ogg") if st.button("Explain LIME Results"): st.text( "Note:'CLOSENESS' represents how far was the particular bill amount from the Limit Balance of the person" ) #Lime Explainer classifier = model_selection(option) limeexplainer = interpret_model(sampled_df, feature_set, classifier) values = instance_values(PAY_1, PAY_2, BILL_AMT2, BILL_AMT3, PAY_AMT1, PAY_AMT2, AGE, Closeness_2, Closeness_3) explainable_exp = limeexplainer.explain_instance( np.array(values), classifier.predict_proba, num_features=len(feature_set)) explain = explainable_exp.as_pyplot_figure() st.pyplot(explain) # Display explainer HTML object components.html(explainable_exp.as_html(), height=800) if st.button("Explain SHAP Results"): with st.spinner('Generating Explanations...'): classifier = model_selection(option) feat_plot, summary = shap_explainer(sampled_df, feature_set, classifier) st.set_option('deprecation.showPyplotGlobalUse', False) st.pyplot(feat_plot) print("\n") st.pyplot(summary) #for plots in dependance_plots: #st.pyplot(plots) if st.button("Precision Recall Curve For the Classifier You selected"): classifier = model_selection(option) fig = plot_prec_recall_vs_tresh(sampled_df, feature_set, classifier, option) st.pyplot(fig) st.text("")