diff --git a/haar_cascades/OpenCV_haar_cascades.md b/haar_cascades/OpenCV_haar_cascades.md new file mode 100644 index 00000000..2b02dff5 --- /dev/null +++ b/haar_cascades/OpenCV_haar_cascades.md @@ -0,0 +1,21 @@ +## Instructions + +First off, the OpenCV Haar Cascades training feature is awful. There has to be a better way. Anyways, here are steps. + + python negative_images.py + cd /Users/ryanzotti/Documents/repos/opencv-haar-classifier-training + find ./positive_images -iname "*.jpg" > positives.txt + find ./negative_images -iname "*.jpg" > negatives.txt + perl bin/createsamples.pl positives.txt negatives.txt samples 1500 "opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 80 -h 40" + conda create --name python2 python=2 + source activate python2 + opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000 -numNeg 600 -w 80 -h 40 -mode ALL -precalcValBufSize 1024 -precalcIdxBufSize 1024 + +## References + +* http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html +* https://github.com/mrnugget/opencv-haar-classifier-training +* http://www.trevorsherrard.com/Haar_training.html +* http://docs.opencv.org/3.1.0/dc/d88/tutorial_traincascade.html +* http://www.pyimagesearch.com/2016/06/20/detecting-cats-in-images-with-opencv/ +* http://note.sonots.com/SciSoftware/haartraining.html \ No newline at end of file diff --git a/haar_cascades/negative_images.py b/haar_cascades/negative_images.py new file mode 100644 index 00000000..d7cf5313 --- /dev/null +++ b/haar_cascades/negative_images.py @@ -0,0 +1,20 @@ +import cv2 +import numpy as np +from random import randint + +negative_image_file_path = "/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/final_processed_data_3_channels.npz" +output_path = "/Users/ryanzotti/Documents/repos/opencv-haar-classifier-training/negative_images" +npzfile = np.load(negative_image_file_path) +train_predictors = npzfile['train_predictors'] +shape = train_predictors.shape +total_frames = shape[0] +height, width = shape[1], shape[2] +negative_images = [] +negative_images_count = 3000 +for i in range(negative_images_count): + random_index = randint(0, total_frames) + random_image = train_predictors[random_index] + #cv2.imshow(str(random_index),random_image) + random_image_file_name = output_path+"/random_image_"+str(i)+".jpg" + cv2.imwrite(random_image_file_name, random_image) +print("Finished") \ No newline at end of file diff --git a/show_stop_signs.py b/show_stop_signs.py new file mode 100644 index 00000000..d107a04d --- /dev/null +++ b/show_stop_signs.py @@ -0,0 +1,62 @@ +import cv2 +import sys +from util import shell_command +import numpy as np +from random import randint + + +def process_image_set(dir,pixel_size): + processed_images = [] + for file_name in file_names: + image = cv2.imread(dir + "/" + file_name) + height, width = image.shape[0], image.shape[1] + if width == height: + resized = cv2.resize(image, (pixel_size, pixel_size), interpolation=cv2.INTER_AREA) + processed_images.append(resized) + else: + print(file_name + " is not a square! It has dimensions " + str(image.shape)) + sys.exit(1) + processed_images = np.array(processed_images) + return processed_images + +# Generates negative images from previous driving session. +def random_negative_images(): + pass + +pixel_size = 24 + +# Assumes positive images are each separate files +positive_image_dir = "/Users/ryanzotti/Dropbox/StopSigns" +shell_cmd = 'ls {dir}'.format(dir=positive_image_dir) +dir_contents = str(shell_command(shell_cmd)).replace("b\'","").split("\\n") +file_names = [file_name for file_name in dir_contents if "JPG" in file_name] +positive_images = process_image_set(positive_image_dir,pixel_size) + +# Assumes negative images are taken from existing numpy zip file. +# I used my training data. This is ideal because it is +# representive of backgrounds that I would realistically see in a +# production setting +negative_image_file_path = "/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/final_processed_data_3_channels.npz" +npzfile = np.load(negative_image_file_path) +train_predictors = npzfile['train_predictors'] +shape = train_predictors.shape +total_frames = shape[0] +height, width = shape[1], shape[2] +negative_images = [] +negative_images_count = 3000 +for i in range(negative_images_count): + random_index = randint(0, total_frames) + random_image = train_predictors[random_index] + cv2.imshow(str(random_index),random_image) + height_bound = height - 24 + width_bound = width - 24 + random_height_start = randint(0,height_bound) + random_height_end = random_height_start + pixel_size + random_width_start = randint(0,width_bound) + random_width_end = random_width_start + pixel_size + random_image_section = random_image[random_height_start:random_height_end,random_width_start:random_width_end] + negative_images.append(random_image_section) +negative_images = np.array(negative_images) +#np.savez(session_path + '/predictors_and_targets', predictors=predictors,targets=targets) + +print("Finished")