Skip to content

Instantly share code, notes, and snippets.

View curegit's full-sized avatar

curegit curegit

View GitHub Profile
@curegit
curegit / beatseg.py
Created July 23, 2024 10:28
拍の位置を考慮してトラックを断片化する
import itertools
import os
import shutil
import soundfile as sf
import librosa
import librosa.beat
import librosa.effects
beat = 4
@curegit
curegit / ssh.desktop
Last active July 1, 2024 14:19
SSH 端末を開くデスクトップショートカット
#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=/usr/bin/gnome-terminal -- ssh atlantica
Name=SSH
Comment=SSH
@curegit
curegit / nonlocal-means.py
Last active June 14, 2024 06:15
OpenCV で Non-local Means フィルタ
import cv2
import numpy as np
def nlmeans(i, o, h=10, hColor=10, templateWindowSize=9, searchWindowSize=27):
img = cv2.imdecode(np.frombuffer(open(i, "rb").read(), np.uint8), cv2.IMREAD_COLOR | cv2.IMREAD_ANYDEPTH)
denoised = cv2.fastNlMeansDenoisingColored(img, h=h, hColor=hColor, templateWindowSize=templateWindowSize, searchWindowSize=searchWindowSize)
ok, bin = cv2.imencode(".png", denoised, [cv2.IMWRITE_PNG_COMPRESSION, 9])
assert ok
open(o, "wb").write(bin.tobytes())
@curegit
curegit / bilateral-filter.py
Last active June 14, 2024 08:39
OpenCV でバイラテラルフィルタ
import cv2
import numpy as np
def bilateral(i, o, d=7, sigmaColor=32, sigmaSpace=10):
img = cv2.imdecode(np.frombuffer(open(i, "rb").read(), np.uint8), cv2.IMREAD_COLOR | cv2.IMREAD_ANYDEPTH)
denoised = cv2.bilateralFilter(img, d=d, sigmaColor=sigmaColor, sigmaSpace=sigmaSpace)
ok, bin = cv2.imencode(".png", denoised, [cv2.IMWRITE_PNG_COMPRESSION, 9])
assert ok
open(o, "wb").write(bin.tobytes())
@curegit
curegit / extstats.py
Last active April 1, 2024 10:24
ディレクトリツリーに存在する拡張子を集計するスクリプト
#!/usr/bin/env python3
import sys
import os
from pathlib import Path
from collections import Counter
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
@curegit
curegit / once.py
Created March 11, 2024 05:57
高々一回実行される副作用を定義するデコレータ
import functools
def once(func):
fst = True
result = None
@functools.wraps(func)
def wrapper(*args, **kwargs):
nonlocal fst, result
if fst:
@curegit
curegit / memmap.py
Created February 27, 2024 05:42
大規模配列のファイルからの部分読み出し例
import numpy as np
large_arr = np.random.normal(size=(3, 10000, 10000)).astype("float64")
np.save("example.npy", large_arr)
import time
import random
from numpy.lib.format import open_memmap
@curegit
curegit / pytorch-lanczos2x.py
Last active November 1, 2023 01:38
PyTorch における Lanczos 補間による 2D マップ二倍拡大層の実装
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
def lanczos(x, n):
return 0.0 if abs(x) > n else np.sinc(x) * np.sinc(x / n)
class Lanczos2xUpsampler(nn.Module):
def __init__(self, n=3):
@curegit
curegit / tile-anim.py
Last active August 12, 2023 23:34
複数のアニメーション画像をタイル状に並べて一つの画像にするスクリプト
#!/usr/bin/env python3
x = 3
y = 2
background = "white"
gap = 10
duration = 100
anims = [
"APNGs/a1.png",
"APNGs/a2.png",
@curegit
curegit / autossh.service
Created June 5, 2023 14:53
AutoSSH によるポートフォワーディングのサービス化例
[Unit]
Description=AutoSSH
After=network.target
[Service]
User=pi
Group=pi
Type=simple
ExecStart=/usr/bin/autossh vps -N -R 8000:10.10.10.10:3389
KillSignal=SIGQUIT