Skip to content

Instantly share code, notes, and snippets.

View InputBlackBoxOutput's full-sized avatar
🐮

Rutuparn Pawar InputBlackBoxOutput

🐮
  • Boston, MA
View GitHub Profile
@InputBlackBoxOutput
InputBlackBoxOutput / remove_duplicate.py
Created October 4, 2021 11:07
Remove duplicate files in a directory identified by comparing SHA256 hash
import os
import hashlib
import glob
# Get the SHA-256 hash of a file
def sha256(fname, size=4096):
sha256_hash = hashlib.sha256()
with open(fname, 'rb') as f:
for byte_block in iter(lambda: f.read(4096), b""):
@InputBlackBoxOutput
InputBlackBoxOutput / pixels.py
Created October 12, 2021 02:39
Perform operations on pixels in an image
import cv2
import numpy as np
from PIL import Image
def process_pixels(img):
pixel_data = list(img.getdata())
pixels = np.array(pixel_data).flatten()
width = img.size[0]
height = img.size[1]
@InputBlackBoxOutput
InputBlackBoxOutput / resize.py
Created October 12, 2021 02:54
Resize an image to a square image with the help of minimal padding
from PIL import Image
def resize_image(img_path, desired_size = 300, background_color=None):
img = Image.open(img_path)
old_size = img.size
if background_color == None:
background_color = img.load()[0,0]
@InputBlackBoxOutput
InputBlackBoxOutput / split.py
Created October 12, 2021 03:06
Split an image dataset into train, validation and test set for the Keras ImageDataGenerator
import os
import glob
import random
import shutil
# Parse through the respective class directories and make a list of image file paths
class_1 = glob.glob("class_1/*.png")
class_1 += glob.glob("class_1/*.png")
class_2 = glob.glob("class_2/*.png")
@InputBlackBoxOutput
InputBlackBoxOutput / image-utils.py
Created October 18, 2021 10:37
Functions to save and show an image in IPYNB notebooks
import matplotlib.pyplot as plt
def save_image(img, filename="image.png"):
plt.plot(), plt.imshow(img)
plt.xticks([]), plt.yticks([])
plt.axes("off")
plt.savefig(filename)
def show_image(img, title="title"):
plt.plot(), plt.imshow(img)
@InputBlackBoxOutput
InputBlackBoxOutput / yosys.py
Created January 5, 2022 13:07
Python wrapper for Yosys
import os, re, subprocess, json
class Yosys:
def __init__(self, path):
self.yosys = subprocess.Popen(
[path, '-Q', '-T'], universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
def process(self, filepath, save=False):
if not os.path.isfile(filepath):
@InputBlackBoxOutput
InputBlackBoxOutput / main.py
Last active January 29, 2022 04:46
Channel routing using the Left edge algorithm
from router import router, plotter
# A = ['0', 'A', 'D', 'E', 'A', 'F', 'G', '0', 'D', 'I', 'J', 'J']
# B = ['B', 'C', 'E', 'C', 'E', 'B', 'F', 'H', 'I', 'H', 'G', 'I']
# A = ['A', '0', 'B', 'C']
# B = ['A', 'B', '0', 'C']
# A = ['N1', '0', 'N2', 'N3']
@InputBlackBoxOutput
InputBlackBoxOutput / segmentation.py
Created January 22, 2022 13:14
Perform skin segmentation by combining masks obtained by thresholding an image using multipe colour models
# Perform skin segmentation by combining masks obtained by thresholding an image using multipe colour models
# Modified from source: https://medium.com/swlh/human-skin-color-classification-using-the-threshold-classifier-rgb-ycbcr-hsv-python-code-d34d51febdf8
import cv2
import numpy as np
import matplotlib.pyplot as plt
def generate_RGB_mask(img):
B_Frame, G_Frame, R_Frame = [img[..., BGR] for BGR in range(3)]
@InputBlackBoxOutput
InputBlackBoxOutput / face-detection.html
Created February 5, 2022 07:23
Mediapipe: Face detection
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Face detection</title>
<script
src="https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]/camera_utils.js"
crossorigin="anonymous"
></script>
<script
@InputBlackBoxOutput
InputBlackBoxOutput / face-mesh.html
Created February 5, 2022 07:23
Mediapipe: Face Mesh
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Face mesh</title>
<script
src="https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]/camera_utils.js"
crossorigin="anonymous"
></script>
<script