Skip to content

Instantly share code, notes, and snippets.

View otakbeku's full-sized avatar
🚀
Journey to the top

Ais otakbeku

🚀
Journey to the top
View GitHub Profile
@otakbeku
otakbeku / cache.py
Created May 10, 2022 14:41 — forked from leonjza/cache.py
Simple SQLite Cache
#!/usr/bin/python
import os
import errno
import sqlite3
import sys
from time import time
from cPickle import loads, dumps
import logging
@otakbeku
otakbeku / rust-python-cffi.md
Created April 2, 2022 04:08 — forked from seanjensengrey/rust-python-cffi.md
Calling Rust from Python/PyPy using CFFI (C Foreign Function Interface)

This is a small demo of how to create a library in Rust and call it from Python (both CPython and PyPy) using the CFFI instead of ctypes.

Based on http://harkablog.com/calling-rust-from-c-and-python.html (dead) which used ctypes

CFFI is nice because:

  • Reads C declarations (parses headers)
  • Works in both CPython and PyPy (included with PyPy)
  • Lower call overhead than ctypes
@otakbeku
otakbeku / modified_fpgrowth.py
Last active November 12, 2021 08:41
A modified FP-Growth
import re
import pyfpgrowth as fp
import numpy as np
class treeNode:
def __init__(self, name_value, num_of_occur, parent_node):
self.name = name_value
self.count = num_of_occur
self.next_node = None
@otakbeku
otakbeku / extractor.py
Last active August 24, 2021 17:36
Video frame extractor
import cv2
from datetime import timedelta
import time
# Manual timestamp extractor
def get_frame_from_timestamp(filepath, timestamp):
str_timestamp = time.strptime(timestamp,'%H:%M:%S')
exact_time = timedelta(hours=str_timestamp.tm_hour,minutes=str_timestamp.tm_min,seconds=str_timestamp.tm_sec).total_seconds()
cap = cv2.VideoCapture(filepath)
camtime = cap.get(cv2.CAP_PROP_POS_MSEC)/1000
@otakbeku
otakbeku / install.sh
Created July 27, 2021 08:20 — forked from newcarrotgames/install.sh
Python code for running vqgan+clip on your own machine. Shamelessy taken from this colab: https://colab.research.google.com/drive/1go6YwMFe5MX6XM9tv-cnQiSTU50N9EeT#scrollTo=mFo5vz0UYBrF. USE AT YOUR OWN RISK!
# I _hope_ this is everything but there may be some other libs I forgot or you don't have,
# so just use pip to install them if vqgan_clip.py complains about missing modules.
# Make some folder, cd into it, and run this.
git clone https://github.com/openai/CLIP
git clone https://github.com/CompVis/taming-transformers
pip install ftfy regex tqdm omegaconf pytorch-lightning
pip install kornia
pip install stegano
apt install exempi
pip install python-xmp-toolkit
@otakbeku
otakbeku / modelserve.py
Last active August 4, 2020 07:41
Pytorch Model serve
import torch
import torchvision.models as models
import torchvision.transforms as transforms
import torch.nn.functional as F
import os
import requests
import json
import io
from PIL import Image
# XAI
from captum.attr import Saliency
from captum.attr import visualization as viz
import torchvision
def attribute_image_features(model, algorithm, input_data, target_label, **kwargs):
model.zero_grad()
tensor_attributions = algorithm.attribute(
input_data,
target=target_label,
class ComplexImgAugTransform:
# Based on imgaug documentation
def __init__(self):
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
self.aug = iaa.Sequential([# apply the following augmenters to most images
iaa.Scale((299, 299)),
iaa.Fliplr(0.5), # horizontally flip 50% of all images
iaa.Flipud(0.2), # vertically flip 20% of all images
# crop images by -5% to 10% of their height/width
sometimes(iaa.CropAndPad(
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('/path/')
img_blur = cv2.medianBlur(img[:,:,1], 5) # Using Green channel color
plt.imshow(img_blur)
_, test_img = cv2.threshold(img_blur, 160, 255, cv2.THRESH_BINARY)
test_contour, _ = cv2.findContours(test_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
largest_contour = max(test_contour, key=cv2.contourArea)
import matplotlib.pyplot as plt
import cv2
img = cv2.imread('/path/')
split_img = cv2.split(img) # Splitting channel
histSize = 256
histRange = (0, 256) # the upper boundary is exclusive
accumulate = False