├── README.md ├── classifiers └── haarcascade_frontalface_default.xml ├── face_detector.py ├── face_detector_camera.py ├── haarcascade_trainer ├── data │ ├── __init__.py │ └── __init__.py~ ├── haarcascade_trainer.py ├── neg │ ├── __init__.py │ └── __init__.py~ └── pos │ ├── __init__.py │ └── __init__.py~ ├── test_image1.jpg └── test_image2.jpg /README.md: -------------------------------------------------------------------------------- 1 | Object Detection using OpenCV and Python 2 | 3 | 1. Face Detector: Detects faces in an image 4 | Classifier: haarcascade_frontalface_default.xml classifier from OpenCV library 5 | 6 | 7 | 2. Face Detector: Detects face/faces in frames(webcam) 8 | Classifier: haarcascade_frontalface_default.xml classifier from OpenCV library 9 | 10 | 3. HAAR Cascade trainer: Script to train your haar cascade classifier 11 | -------------------------------------------------------------------------------- /face_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | ''' 4 | Face Detector: Detects faces in a image 5 | classifier: haarcascade_frontalface_default.xml from OpenCV library 6 | ''' 7 | 8 | # load classifier 9 | classifier = cv2.CascadeClassifier('classifiers/haarcascade_frontalface_default.xml') 10 | 11 | # load image 12 | img = cv2.imread('test_image1.jpg') 13 | 14 | # convert image to grayscale 15 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 16 | 17 | # detect faces 18 | faces = classifier.detectMultiScale(gray, 1.3, 5) 19 | 20 | # draw rectangles on image for every detected face 21 | for (x,y,w,h) in faces: 22 | img = cv2.rectangle(img, (x,y), (x+w,y+h), (255, 0, 0), 2) 23 | 24 | # finally show image 25 | cv2.imshow('img', img) 26 | cv2.waitKey(0) 27 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /face_detector_camera.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | ''' 4 | Face Detector 2: Detects face/faces in frames(webcam) 5 | classifier: haarcascade_frontalface_default.xml from OpenCV library 6 | ''' 7 | 8 | # load classifier 9 | faceCascade = cv2.CascadeClassifier('classifiers/haarcascade_frontalface_default.xml') 10 | 11 | video_capture = cv2.VideoCapture(0) 12 | 13 | while True: 14 | # Capture frame-by-frame 15 | ret, frame = video_capture.read() 16 | 17 | # Convert frame to grayscale 18 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 19 | 20 | # Detect faces 21 | faces = faceCascade.detectMultiScale( 22 | gray, 23 | scaleFactor=1.3, 24 | minNeighbors=5, 25 | minSize=(30, 30) 26 | ) 27 | 28 | # Draw a rectangle around the faces 29 | for (x, y, w, h) in faces: 30 | cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) 31 | 32 | # Display the resulting frame 33 | cv2.imshow('Video', frame) 34 | 35 | if cv2.waitKey(1) & 0xFF == ord('q'): 36 | break 37 | 38 | # When everything is done, release the capture 39 | video_capture.release() 40 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /haarcascade_trainer/data/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kailashahirwar/opencv-objectdetector/e987105493fd0fad31457312d109e847797800be/haarcascade_trainer/data/__init__.py -------------------------------------------------------------------------------- /haarcascade_trainer/data/__init__.py~: -------------------------------------------------------------------------------- 1 | __author__ = 'kailash' 2 | -------------------------------------------------------------------------------- /haarcascade_trainer/haarcascade_trainer.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | 4 | ''' 5 | This script is to help you to generate a haar casscade classifier with ease. 6 | Steps: 7 | 1. Put positive images in pos folder. Images should be of same dimensions. 8 | 2. Put negative_images(background images) in neg folder. 9 | Background images should be of larger size than positive images. 10 | 3. Create a data folder which will contain all cascade and classifier files. 11 | 4. Change Paramters based on your number of positive and negative images. 12 | ''' 13 | 14 | # Directory with positive images 15 | dir_pos_name = "pos" 16 | 17 | # Directory with negative images 18 | dir_neg_name = "neg" 19 | 20 | # Dimension of samples 21 | image_width = "20" 22 | image_height = "20" 23 | 24 | # Name of files to contain list of images in respective folders 25 | file_pos_name = "positive_images.info" 26 | bg_file = "bg.txt" 27 | 28 | number_of_samples = "1000" # Number of samples you want to generate 29 | width = "20" 30 | height = "20" 31 | 32 | vector_file_name = "samples.vec" 33 | 34 | data_folder = "data" 35 | numStages = "50" 36 | nsplits = "2" 37 | minhitrate = "0.999" 38 | maxfalsealarm = "0.5" 39 | 40 | # numPos and numNeg are number of samples it takes for every stage. 41 | numPos = "1000" 42 | numNeg = "1000" 43 | 44 | ''' 45 | Commands 46 | ''' 47 | # find pos -iname "*.*" -exec echo \{\} 1 0 0 100 40 \; > positive_images.info 48 | 49 | # find neg -iname "*.*" > bg.txt 50 | 51 | # opencv_createsamples -info positive_images.info -num 550 -w 48 -h 24 -vec samples.vec 52 | 53 | # opencv_traincascade -data data -vec samples.vec -bg bg.txt -numStages 10 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -numPos 500 -numNeg 500 -w 48 -h 24 54 | 55 | ''' 56 | This method is to format a command which will list all images from pos folder 57 | into "positive_images.info" file with number of objects and position of objects 58 | in corresponding image. 59 | ''' 60 | def create_command(dir_pos_name, image_width, image_height, file_pos_name): 61 | command = "find "+dir_pos_name+" -iname \"*.*\" -exec echo \{\} 1 0 0 "+image_width+" "+image_height+" \; > "+file_pos_name 62 | return command 63 | 64 | ''' 65 | This method is to format a command which will create a file "bg.txt" containing 66 | list of images in neg folder (one image per line). 67 | ''' 68 | def create_command1(dir_neg_name, bg_file): 69 | command = "find "+dir_neg_name+" -iname \"*.*\" > "+bg_file 70 | return command 71 | 72 | ''' 73 | This method is to format a command which will create positive samples out of 74 | positive_images and background images. 75 | ''' 76 | def create_command2(file_pos_name, number_of_samples, width, height, vector_file_name): 77 | command = "opencv_createsamples -info "+file_pos_name+" -num "+str(number_of_samples)+" -w "+str(width)+" -h "+str(height)+" -vec "+vector_file_name 78 | return command 79 | 80 | ''' 81 | This method is to format a command which will train a casscade classifier. 82 | ''' 83 | def create_command3(data_folder, vector_file_name, bg_file, numStages, nsplits, minhitrate, maxfalsealarm, numPos, numNeg, width, height): 84 | command = "opencv_traincascade -data "+data_folder+" -vec "+vector_file_name+" -bg "+bg_file+" -numStages "+numStages+" -nsplits "+nsplits+" -minhitrate "+minhitrate+" -maxfalsealarm "+maxfalsealarm+" -numPos "+numPos+" -numNeg "+numNeg+" -w "+width+" -h "+height 85 | return command 86 | 87 | ''' 88 | Create commands 89 | ''' 90 | command1 = create_command(dir_pos_name, image_width, image_height, file_pos_name) 91 | command2 = create_command1(dir_neg_name, bg_file) 92 | command3 = create_command2(file_pos_name, number_of_samples, width, height, vector_file_name) 93 | command4 = create_command3(data_folder, vector_file_name, bg_file, numStages, nsplits, minhitrate, maxfalsealarm, numPos, numNeg, width, height) 94 | 95 | # Run commands 96 | os.system(command1) 97 | os.system(command2) 98 | os.system(command3) 99 | os.system(command4) -------------------------------------------------------------------------------- /haarcascade_trainer/neg/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kailashahirwar/opencv-objectdetector/e987105493fd0fad31457312d109e847797800be/haarcascade_trainer/neg/__init__.py -------------------------------------------------------------------------------- /haarcascade_trainer/neg/__init__.py~: -------------------------------------------------------------------------------- 1 | __author__ = 'kailash' 2 | -------------------------------------------------------------------------------- /haarcascade_trainer/pos/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kailashahirwar/opencv-objectdetector/e987105493fd0fad31457312d109e847797800be/haarcascade_trainer/pos/__init__.py -------------------------------------------------------------------------------- /haarcascade_trainer/pos/__init__.py~: -------------------------------------------------------------------------------- 1 | __author__ = 'kailash' 2 | -------------------------------------------------------------------------------- /test_image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kailashahirwar/opencv-objectdetector/e987105493fd0fad31457312d109e847797800be/test_image1.jpg -------------------------------------------------------------------------------- /test_image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kailashahirwar/opencv-objectdetector/e987105493fd0fad31457312d109e847797800be/test_image2.jpg --------------------------------------------------------------------------------