Skip to content

Instantly share code, notes, and snippets.

View garybradski's full-sized avatar

Gary Bradski garybradski

View GitHub Profile
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 / 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)
@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 / 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 / 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 / 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 / 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 / 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 / text_2_floatlist.py
Last active October 6, 2017 03:07
Just a function that reads a line of a text file, strips out [ ] and converts to list of floats
def text_to_floatlist(pathfile):
f = open(pathfile, 'r')
line = f.readline().strip('[]')
f.close()
return [float(x) for x in line.split()]
#Use
rect = text_to_floatlist(seg_bb[frame_count])
if(len(rect) < 4):
logger.critical("Length of rectangle not long enough %d",int(len(rect)) )
@garybradski
garybradski / matplotlib_AND_opencv_imshow.py
Created October 4, 2017 23:09
Using python matplotlib and opencv cv2 imshow in the same loop using the keyboard to trigger pause or run
import cv2
from matplotlib import pyplot as plt
. . .
#DO PYTHON MATPLOTLIB IMAGE DISPLAY
plt.imshow(image_seg)
plt.draw()
plt.show() # , plt.draw(), plt.show()
plt.pause(0.01)