Skip to content

Instantly share code, notes, and snippets.

View smeschke's full-sized avatar

Stephen Meschke smeschke

View GitHub Profile
@smeschke
smeschke / head nod detection
Last active April 6, 2024 21:17
Detection of Head Nods using OpenCV in Python
import cv2
import numpy as np
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('/home/sm/Desktop/nodcontrol.avi',fourcc, 20.0, (640,480))
#dinstance function
def distance(x,y):
import math
return math.sqrt((x[0]-y[0])**2+(x[1]-y[1])**2)
@smeschke
smeschke / webcam histogram
Created March 22, 2017 23:39
displays hue value histogram from webcam feed
#import cv2 for vision
#numpy to generate backgrounds
import cv2
import numpy as np
#capture video from the webcam
cap = cv2.VideoCapture(0)
#read one frame
_, img = cap.read()
@smeschke
smeschke / hybrid_tracker.py
Last active July 12, 2017 17:59
Tracker uses optical flow to track a point in video, saves coordinates in a .csv file
import cv2
import numpy as np
#path to source video
source_path = '/home/stephen/Desktop/105.avi'
#out path to save tracking data
output_path = '/home/stephen/Desktop/ball2.csv'
#parameter of lk optical flow
www = 15 #www is the diameter of the object in pixles
@smeschke
smeschke / paste_image_into.py
Created March 31, 2017 23:32
Seamlessly pastes an image into video using tracked coordinates
import cv2
import numpy as np
cols, rows = 1100 , 750
fps = 30.0
video_out_name = '/home/stephen/Desktop/finished.avi'
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(video_out_name,fourcc, fps, (cols,rows))
@smeschke
smeschke / tracking with colorspaces
Created May 10, 2017 17:59
Tracks the largest red, green, or pink object found in a video. Outputs a .csv file with the x,y coordinates for each frame.
import cv2
import numpy as np
#h,s,v range of the object to be tracked
#h,s,v,h1,s1,v1 = 27,0,0,82,190,255 #GREEN
#h,s,v,h1,s1,v1 = 168,154,99,177,255,255 #RED
h,s,v,h1,s1,v1 = 156,74,76,166,255,255 #pink
threshold_value = 0
output_path = '/home/stephen/Desktop/423_pink.csv'
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
def nothing(arg): pass
#takes an image, and a lower and upper bound
#returns only the parts of the image in bounds
def only_color(frame, (b,r,g,b1,r1,g1), morph):
@smeschke
smeschke / box tracking
Created May 19, 2017 14:53
box tracking
import cv2
import numpy as np
cap = cv2.VideoCapture('/home/stephen/Desktop/fedex.avi')
h,s,v,h1,s1,v1 = 104,0,134,133,255,255#values for the blue
morph = 1
last_box_depart = -10000 #frame number that the last box departed
tracking_box = False
box_ready = False
@smeschke
smeschke / keras for juggling
Last active April 3, 2020 09:34
predict juggling patterns using keras
import numpy as np, os, cv2
from keras.utils import to_categorical
from keras.models import load_model
#reads images from folder (images must be labeled 0.png, 1.png, etc...)
def read_from_folder(folder, pattern, image_number, stop):
images = []
while image_number < stop:
path = folder + pattern + '/' + str(image_number) + '.png'
img = cv2.imread(path, 0)
import cv2, numpy as np, os
#parameters
working_dir = '/home/stephen/Desktop/keras_demo/'
cap = cv2.VideoCapture(1)
org, font, scale, color, thickness, linetype = (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (234,12,123), 2, cv2.LINE_AA
#chromakey values
h,s,v,h1,s1,v1 = 16,0,64,123,111,187
#amount of data to use
@smeschke
smeschke / gist:df2c5794287fa7044817dc3e00d61351
Last active June 24, 2024 06:51
Using Keras from the Webcam
import cv2, numpy as np, os
#parameters
working_dir = '/home/stephen/Desktop/keras_demo/'
cap = cv2.VideoCapture(0)
org, font, scale, color, thickness, linetype = (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (234,12,123), 2, cv2.LINE_AA
#chromakey values
h,s,v,h1,s1,v1 = 16,0,64,123,111,187 #green
h,s,v,h1,s1,v1 = 0,74,53,68,181,157 #skin tone