Skip to content

Instantly share code, notes, and snippets.

View yptheangel's full-sized avatar
Working from home

Choo Wilson yptheangel

Working from home
View GitHub Profile
@yptheangel
yptheangel / shap_streamlit_xgb.py
Created October 17, 2022 09:33
shap_streamlit_xgb
import shap
import streamlit as st
import streamlit.components.v1 as components
import xgboost
import matplotlib.pyplot as plt
@st.cache
def load_data():
return shap.datasets.boston()
@yptheangel
yptheangel / bouncing_logo.js
Created August 24, 2022 16:46
bouncing logo p5js script
let x;
let y;
let xspeed;
let yspeed;
let r,g,b ;
function preload() {
logo = loadImage("logo.png")
}
@yptheangel
yptheangel / doccano_jsonl_spacy3_read_converter.py
Created May 7, 2022 16:24
snippet to read .jsonl from Doccano NER annotator and converting into spacy v3 format. filter spans is optional, uncomment if you do not want overlapping span
custom_data_path = 'path_to_your_ner_dataset.jsonl'
import json
with open(custom_data_path, 'r', encoding="utf8") as json_file:
json_list = list(json_file)
dataset = [json.loads(jline) for jline in json_list]
print(f"number of examples : {len(dataset)}")
@yptheangel
yptheangel / shap_feature_importance.py
Created December 22, 2021 07:18
shap feature importance
import numpy as np
import pandas as pd
import shap
explainer = shap.TreeExplainer("your_tree_model")
shap_values = explainer.shap_values(X_train)
scores= np.abs(shap_values).mean(0)
feature_importance = pd.DataFrame(list(zip(X_train.columns, sum(scores))), columns=['feature','feature_importance_score'])
feature_importance.sort_values(by=['feature_importance_score'], ascending=False,inplace=True)
@yptheangel
yptheangel / augment_brightness.py
Created September 28, 2021 11:22
Change/Augment brightness of an image using OpenCV
import cv2
import numpy as np
def augment_brightness(img, scale):
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(img)
scale = 0.5
v = np.clip(v * scale, 0, 255, out=v)
img = cv2.merge((h, s, v))
@yptheangel
yptheangel / streamlit_for_computer_vision.py
Last active October 5, 2021 14:16
Streamlit example for computer vision. To run, just execute python streamlit_for_computer_vision.py
import streamlit as st
import numpy as np
from PIL import Image
import cv2
from io import BytesIO
import base64
import uuid
import re
@st.cache
@yptheangel
yptheangel / objectron_video.py
Last active August 15, 2021 04:50
Video inference of Mediapipe's Objectron. Currently only supports, chairs, shoes, cups and cameras.
import mediapipe as mp
import cv2
mp_objectron = mp.solutions.objectron
mp_drawing = mp.solutions.drawing_utils
if __name__ == '__main__':
objectron = mp_objectron.Objectron(static_image_mode=True,
max_num_objects=5,
min_detection_confidence=0.5,
@yptheangel
yptheangel / check_bqtables_metadata.py
Created August 4, 2021 10:04
List out BigQuery tables metadata, sizes in GB, row_count, creation and last modified datetime and save into a csv using pandas dataframe
from google.cloud import bigquery
import os
import pandas as pd
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "YOURGCPSERVICEACCOUNTKEY.json"
GCP_PROJECT_ID = "YOURGCPPROJECT"
client = bigquery.Client(project=GCP_PROJECT_ID)
datasets = list(client.list_datasets())
project = client.project
@yptheangel
yptheangel / add_watermark.py
Created August 2, 2021 16:01
add watermark to image using opencv by using steps: insert text at center of image, blend two images together to make watermark transparent
import cv2
filename = "yourfilename.jpg"
frame = cv2.imread(filename)
overlay = frame.copy()
text = "I love opencv"
font, font_scale, font_thickness = cv2.FONT_ITALIC, 3.5, 5
textsize = cv2.getTextSize(text, font, font_scale, font_thickness)[0] # get text size
@yptheangel
yptheangel / hptuning_config_gridsearch.yml
Created February 4, 2021 04:24
Grid search hptuning_config.yml for GCP AI platform hypertune, recommend to bayesian optimization instead,
trainingInput:
scaleTier: CUSTOM
masterType: n1-standard-4
hyperparameters:
goal: MAXIMIZE
maxTrials: 90
maxParallelTrials: 8
algorithm: GRID_SEARCH
hyperparameterMetricTag: val_accuracy
params: