Skip to content

Instantly share code, notes, and snippets.

View garybradski's full-sized avatar

Gary Bradski garybradski

View GitHub Profile
@garybradski
garybradski / rect_write.py
Created October 15, 2017 23:52
writing zero filled numerically sequential files
def rect_files(start,stop):
startstop = range(start,stop)
for i in startstop:
openstr = "%09d.txt"%(i) #zero justify out to 9 places
file = open(openstr,"w")
file.write("[ 0, 0, 0, 0 ]")
file.close()
@garybradski
garybradski / bash tricks
Created October 16, 2017 01:27
Useful operations, bash script
#!/bin/bash
# Useful stuff for scripts, Gary Bradski
#Python
## For python, you should run your files through yapf
sudo pip install yapf
yapf -i <path/file_to_be_yapfed>
##To convert python 2 to 3 in place:
2to3 -w <path/file_to_be_converted>
@garybradski
garybradski / skeletonize.py
Created October 18, 2017 02:51
Skeletonize a single channel opencv image or 2D numpy array
def skeletonize(img):
'''
Adapted from:
https://opencvpython.blogspot.com/2012/05/skeletonization-using-opencv-python.html
'''
assert len(img.shape) == 2 #make sure its single channel
size = np.size(img)
tenth_size = size/10
skel = np.zeros(img.shape,np.uint8)
@garybradski
garybradski / connected_components.py
Created December 9, 2017 00:17
Connected Components in Python
import cv2
import numpy as np
def connected_components(thresh_img):
thresh_img = mog_mask.copy()
contours, hierarchy = cv2.findContours(thresh_img, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
fg_mask = np.zeros(frame.shape[:2], np.uint8)
i = 0
cs = []
@garybradski
garybradski / avi2pgn.py
Last active January 31, 2018 04:09
AVI2PGM.py
import cv2
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument('--video', help='path to video to convert to images.')
parser.add_argument('--out', help='path to output directory.')
args = parser.parse_args()
if not os.path.exists(args.out):
@garybradski
garybradski / gist:6f850c9f53bf529389b1f099b815d91b
Created March 15, 2018 00:39
De-dupe de-duplicate files or images
fdupes -rdN <dir>
# r is recursive
# d is keep the first file of the duplicated files
# N is run silent so you don't have to say yes or no
@garybradski
garybradski / IOU.py
Created June 30, 2018 09:37
Python implementation of intersection over union, iou
# From https://gist.github.com/zacharybell/8d9b1b25749fe6494511f843361bb167
import numpy as np
def mean_iou(labels, predictions, n_classes):
mean_iou = 0.0
seen_classes = 0
for c in range(n_classes):
labels_c = (labels == c)
pred_c = (predictions == c)
import sys
import argparse
import cv2
MIN_PYTHON = (3, 3)
if sys.version_info < MIN_PYTHON:
sys.exit("Python %s.%s or later is required.\n" % MIN_PYTHON)
@garybradski
garybradski / rw_numpy_json.py
Created December 20, 2018 04:46
Read/write json of numpy array
import numpy as np
import codecs, json
#In order ot store an numpy array as a .json file
a = np.arange(10).reshape(2,5) # a 2 by 5 array
b = a.tolist() # nested lists with same data, indices
# Obviously, if you already have list, you don't/can't .tolist() it
file_path = "/path.json" ## your path variable
json.dump(b, codecs.open(file_path, 'w', encoding='utf-8'), separators=(',', ':'), sort_keys=True, indent=4) ### this saves the array in .json format
@garybradski
garybradski / corner_nearest_neighbor.py
Created January 9, 2019 05:13
Convolve to find axis aligned corners. Also, find nearest pixel
def convert_bin_img_to_corners(img):
'''
Mark corners with bias being pixels origin as (x,y): (0.5, 0.5)
:param img: Binary image of field
:return: Image of corners marked with 255, all else is 0
'''
kernel = np.ones(
(2, 2), np.float32) # Make convolution kernel 2x2 of 0.25s
ret, img_thresh = cv2.threshold(
img, 1, 1, cv2.THRESH_BINARY) # Make img into [0,1]