Skip to content

Instantly share code, notes, and snippets.

View humpydonkey's full-sized avatar

Asia humpydonkey

  • San Francisco, Bay Area
  • 00:20 (UTC -07:00)
  • LinkedIn in/yazhoucao
View GitHub Profile
@humpydonkey
humpydonkey / four_corner_consistency.py
Created November 16, 2023 23:30
Check consistency of four corners of an image
import itertools
import PIL.Image
import numpy as np
def four_corners_similar(image: PIL.Image.Image):
pixel_value_diff_tolerance = 40 # tolerance for pixel value difference, 0-255
discrepancy_threshold = 0.01 # max percentage of pixels that can be different between corners to be considered similar
corner_image_ratio = 0.06
crop_size = int(corner_image_ratio * min(image.size))
# print(crop_size)
@humpydonkey
humpydonkey / view_images_in_grid.py
Last active November 15, 2023 05:35
Visualize a list of images in a grid
import PIL.Image
from PIL import ImageFont
from torchvision.utils import make_grid as torch_make_grid
from torchvision.transforms.functional import pil_to_tensor, to_pil_image
from pathlib import Path
import platform
def make_grid(images: list[PIL.Image.Image | str]):
if isinstance(images[0], str):
images = [PIL.Image.open(img) for img in images]
@humpydonkey
humpydonkey / mask_extractor_rmbg.py
Last active November 17, 2023 19:51
Extract segmentation masks via rmgb
import logging
import numpy as np
import PIL.Image
import matplotlib.pyplot as plt
from datasets import load_dataset
from rembg import remove
import cv2
def show_mask(mask, ax, random_color=False):
@humpydonkey
humpydonkey / mask_extractor_sam.py
Last active November 5, 2023 22:21
Extract segmentation masks via SAM
import logging
import numpy as np
import PIL.Image
from segment_anything import SamPredictor, sam_model_registry
import matplotlib.pyplot as plt
logger = logging.getLogger(__name__)
model_type = "default"
_SAM_CKPT = "sam_vit_h_4b8939.pth"
sam = sam_model_registry[model_type](checkpoint=_SAM_CKPT).to(device="cuda")
@humpydonkey
humpydonkey / vega_lite_layered_bar_chart_example
Created September 12, 2021 01:42
vega lite layered bar chart
{
"config":
{
"background": "#ffffff",
"axis":
{
"labelColor": "#262730",
"titleColor": "#262730",
"gridColor": "rgba(38, 39, 48, 0.2)",
"labelFont": "IBM Plex Sans, sans-serif",
class Solution {
// f(n) = 1 / n + (n - 2) / n * f(n - 1)
//
// f(n) = 1/n -> 1st person picks his own seat
// + 1/n * 0 -> 1st person picks last one's seat
// + (n-2)/n * ( ->1st person picks one of seat from 2nd to (n-1)th
// 1/(n-2) * f(n-1) -> 1st person pick 2nd's seat
// 1/(n-2) * f(n-2) -> 1st person pick 3rd's seat
// ......
// 1/(n-2) * f(2) -> 1st person pick (n-1)th's seat
class Solution {
// The unbounded knapsack problem
// dp[i][j]: the fewest # coins among 0 to i-1 to make up to amount j
// dp[i][j] = min(dp[i-1][j], dp[i-1][j-coins[i]] + 1, dp[i][j-coins[i]] + 1)
// since dp[i][j-coins[i]] includes dp[i-1][j-coins[i]], thus:
// dp[i][j] = min(dp[i-1][j], dp[i][j-coins[i]] + 1)
// dp[i][j] < 0 means there is no solution.
//
// Time: O(n * amount)
// Space: O(n * amount)
class Solution {
// A variant of the knapsack problem
// dp[i][j]: is there a subset of stones (0 to i-1) can sum up to weight j
// dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]]
// result: last stone weight = the shortest distance between any two nodes among dp[n][0] ... dp[n][W]
//
// Time: O(n * totalWeight)
// Space: O(n * totalWeight)
public int lastStoneWeightII(int[] stones) {
int totalWeight = Arrays.stream(stones).sum();
class Solution {
// We maintain a 2D array , dp[n][subSetSum]
// For an array element i and sum j in array nums,
// dp[i][j]=true if the sum j can be formed by array elements
// in subset nums[0]..nums[i], otherwise dp[i][j]=false
//
// Time: O(n * sum)
// Space: O(n * sum)
public boolean canPartition(int[] nums) {
int totalSum = Arrays.stream(nums).sum();
class Solution {
// Time: O(nlogn)
// Space: O(n)
public int lastStoneWeight(int[] stones) {
if (stones.length == 1) {
return stones[0];
}
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
for (int weight : stones) {
queue.add(weight);