Skip to content

Instantly share code, notes, and snippets.

@realphongha
realphongha / install_nodejs_local.sh
Last active January 8, 2024 07:02
Install Nodejs on user directory
# https://www.johnpapa.net/node-and-npm-without-sudo/
VERSION=v18.18.0
DISTRO=linux-x64
wget https://nodejs.org/dist/$VERSION/node-$VERSION-$DISTRO.tar.xz
tar -xvf node-$VERSION-$DISTRO.tar.xz
rm node-$VERSION-$DISTRO.tar.xz
mkdir ~/.nodejs
mv node-$VERSION-$DISTRO ~/.nodejs
@realphongha
realphongha / get_video_fps.py
Created September 6, 2023 07:21
Get FPS of a video using Python, OpenCV and Ffmpeg.
import cv2
import ffmpeg
def get_fps_fast(video_path):
# using only opencv-python package, fast but can be inaccurate
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
raise Exception(f"Cannot open {video_path}")
fps = cap.get(cv2.CAP_PROP_FPS)
@realphongha
realphongha / .tmux.conf
Last active July 9, 2024 08:40
My .tmux.conf
### Basic configs
# source file
unbind r
bind r source-file ~/.tmux.conf
# new prefix
# unbind C-b
# set -g prefix C-a
# act like vim
@realphongha
realphongha / warp_affine_numpy.py
Created February 22, 2023 09:22
cv2 warpAffine() re-implemented using numpy (both python and cython version)
import numpy as np
def warp_affine(img, M, fill_value=127):
new_img = np.full_like(img, fill_value, dtype=img.dtype)
h, w = img.shape[:2]
y, x = np.indices((h, w))
x = x.flatten()
y = y.flatten()
ones = np.ones((h * w, 1))
@realphongha
realphongha / img_concat.py
Last active September 14, 2023 04:50
Images horizontal and 2d concat
import numpy as np
import cv2
def img_horizontal_concat(img1, img2, img_size=(240, 480)):
img1_resized = cv2.resize(img1, img_size)
img2_resized = cv2.resize(img2, img_size)
return np.concatenate((img1_resized, img2_resized), axis=1)
@realphongha
realphongha / multiclass_nms.py
Created January 5, 2023 03:01
Multiclass nms
def iou_calc(boxes1, boxes2):
boxes1_area = (boxes1[2] - boxes1[0]) * (boxes1[3] - boxes1[1])
boxes2_area = (boxes2[2] - boxes2[0]) * (boxes2[3] - boxes2[1])
left_up = np.maximum(boxes1[:2], boxes2[:2])
right_down = np.minimum(boxes1[2:-2], boxes2[2:-2])
inter_section = np.maximum(right_down - left_up, 0.0)
inter_area = inter_section[0] * inter_section[1]
@realphongha
realphongha / draw_bboxes_on_image.py
Created January 4, 2023 07:52
Draw bounding boxes on image
import cv2
def bbox_percentage_to_pixel(bbox, h, w):
x1, y1, x2, y2 = bbox
return x1 * w, y1 * h, x2 * w, y2 * h
def draw_bboxes_on_image(img, bboxes, show=True, percentage=True):
h, w = img.shape[:2]
@realphongha
realphongha / horizontal_concat.py
Created December 9, 2022 08:04
Concatenate two cv2 images horizontally
import cv2
import numpy as np
def horizontal_concat(img1, img2, img_size):
img1_resized = cv2.resize(img1, img_size)
img2_resized = cv2.resize(img2, img_size)
return np.concatenate((img1_resized, img2_resized), axis=1)
@realphongha
realphongha / perf_monitor.py
Last active May 9, 2024 03:32
Python performance monitor as a Mixin
import numpy as np
class PerfMonitorMixin:
PERF_MONITOR = {}
def update_perf(self, name, latency, ignore_classwise=False):
if not ignore_classwise:
name = self.__class__.__name__ + "_" + name
if name not in PerfMonitorMixin.PERF_MONITOR: