Skip to content

Instantly share code, notes, and snippets.

View aredden's full-sized avatar
:octocat:
Building awesome things.

Alex Redden aredden

:octocat:
Building awesome things.
  • Remote
View GitHub Profile
@aredden
aredden / format_error.py
Last active June 19, 2022 18:33
proper python error formatting without necessary non-zero exit code.
from traceback import format_exception
def format_error(e: Exception, limit: int = 3):
"""Format exception as a string.
Args:
e (Exception): Raised exception object.
limit (int): limit of error call stack lines to be included in the formatted string.
"""
@aredden
aredden / simple_image_grid.py
Created May 5, 2022 20:01
Creates a grid of images with 'nrows' number of image rows, images must all be the same shape.
from typing import List
import numpy as np
def image_grid(images: List[np.ndarray], nrows: int):
"""
Creates a grid of images with 'nrows' number of image rows, images must all be the same shape.
"""
assert len(images) > nrows, "Must have more images than rows"
ncols = int(np.ceil(len(images) / nrows))
images_size = images[0].shape[:2]
grid_img = np.zeros((nrows * images_size[0], ncols * images_size[1], 3), dtype=np.uint8)
@aredden
aredden / face_removal.py
Last active March 7, 2022 04:28
Non-rectangular face removal using cv2 convextHull, mediapipe FaceMesh, and assorted numpy magic.
import cv2
import numpy as np
import mediapipe as mp
image = cv2.imread("./input.png")
mask = np.zeros_like(image)
segm = mp.solutions.mediapipe.python.solutions.face_mesh.FaceMesh(
True,
min_detection_confidence=0.6
)
landmarks = segm.process(image).multi_face_landmarks
@aredden
aredden / build-opencv-cuda.sh
Last active October 3, 2021 18:41
Shell script for installing opencv with cuda features enabled into a linux conda environment with python~3.8, numpy and cudatoolkit 11.4.
export CPLUS_INCLUDE_PATH=$CONDA_PREFIX/lib/python3.8
export NVIDIA_ARCH_CAPABILITY=8.6 #Change this to your gpu's compute capability
# This script requires a directory structure which looks like the following,
# with opencv-master from git, opencv_contrib-master from git, and a build directory
# with only this script inside. You must execute this shell script from inside the build
# directory, with your desired conda environment activated.
# The initial setup is copied from here: https://docs.opencv.org/3.4.15/d7/d9f/tutorial_linux_install.html
# With steps 2-5 replaced with this shell script.
# ├── build