Skip to content

Commit

Permalink
Configuration file loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
bikz05 committed Jun 19, 2015
1 parent ba511b0 commit a466ebb
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 15 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ test-object-detector

_The `test-object-detector` will download the [UIUC Image Database for Car Detection](https://cogcomp.cs.illinois.edu/Data/Car/) and train a classifier to detect cars in an image. The SVM model files will be stored in `data/models`, so that they can be resused later on._

* All the configurations are in the `data/config/config.cfg` configuration files. You can change it as per your need.

# About the modules

1. `extract-features.py` -- This module is used to extract HOG features of the training images.
2. `train-classifier.py` -- This module is used to train the classifier.
3. `nms.py` -- This module performs Non Maxima Suppression.
4. `test-classifier.py` -- This module is used to test the classifier using a test image

4. `test-classifier.py` -- This module is used to test the classifier using a test image.
5. `config.py` -- Imports the configuration variables from `config.cfg`.

## Some of the results

Expand Down Expand Up @@ -62,3 +64,4 @@ _Detections after NMS_
4. [Non-Maximum Suppression for Object Detection in Python](http:https://www.pyimagesearch.com/2014/11/17/non-maximum-suppression-object-detection-python/)
5. [(Faster) Non-Maximum Suppression in Python](http:https://www.pyimagesearch.com/2015/02/16/faster-non-maximum-suppression-python/)
6. [Texture Matching using Local Binary Patterns (LBP), OpenCV, scikit-learn and Python](http:https://hanzratech.in/2015/05/30/local-binary-patterns.html)
7. [Detección de objetos Course by Coursera](https://www.coursera.org/course/deteccionobjetos)
17 changes: 17 additions & 0 deletions data/config/config.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[hog]
min_wdw_sz: [100, 40]
step_size: [10, 10]
orientations: 9
pixels_per_cell: [8, 8]
cells_per_block: [3, 3]
visualize: False
normalize: True

[nms]
threshold: .3

[paths]
pos_feat_ph: ../data/features/pos
neg_feat_ph: ../data/features/neg
model_path: ../data/models/svm.model

Binary file modified data/models/svm.model_02.npy
Binary file not shown.
Binary file modified data/models/svm.model_03.npy
Binary file not shown.
1 change: 1 addition & 0 deletions object-detector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .train-classifier import *
from .test-classifier import *
from .nms import *
from .config import *
21 changes: 21 additions & 0 deletions object-detector/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
Set the config variable.
'''

import ConfigParser as cp
import json

config = cp.RawConfigParser()
config.read('../data/config/config.cfg')

min_wdw_sz = json.loads(config.get("hog","min_wdw_sz"))
step_size = json.loads(config.get("hog", "step_size"))
orientations = config.getint("hog", "orientations")
pixels_per_cell = json.loads(config.get("hog", "pixels_per_cell"))
cells_per_block = json.loads(config.get("hog", "cells_per_block"))
visualize = config.getboolean("hog", "visualize")
normalize = config.getboolean("hog", "normalize")
pos_feat_ph = config.get("paths", "pos_feat_ph")
neg_feat_ph = config.get("paths", "neg_feat_ph")
model_path = config.get("paths", "model_path")
threshold = config.getfloat("nms", "threshold")
8 changes: 3 additions & 5 deletions object-detector/extract-features.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import argparse as ap
import glob
import os
from config import *

if __name__ == "__main__":
# Argument Parser
Expand All @@ -22,9 +23,6 @@
pos_im_path = args["pospath"]
neg_im_path = args["negpath"]

pos_feat_ph = "../data/features/pos"
neg_feat_ph = "../data/features/neg"

des_type = args["descriptor"]

# If feature directories don't exist, create them
Expand All @@ -39,7 +37,7 @@
for im_path in glob.glob(os.path.join(pos_im_path, "*")):
im = imread(im_path, as_grey=True)
if des_type == "HOG":
fd = hog(im, orientations=9, pixels_per_cell=(8,8), cells_per_block=(3,3), visualise=False, normalise=True)
fd = hog(im, orientations, pixels_per_cell, cells_per_block, visualize, normalize)
fd_name = os.path.split(im_path)[1].split(".")[0] + ".feat"
fd_path = os.path.join(pos_feat_ph, fd_name)
joblib.dump(fd, fd_path)
Expand All @@ -49,7 +47,7 @@
for im_path in glob.glob(os.path.join(neg_im_path, "*")):
im = imread(im_path, as_grey=True)
if des_type == "HOG":
fd = hog(im, orientations=9, pixels_per_cell=(8,8), cells_per_block=(3,3), visualise=False, normalise=True)
fd = hog(im, orientations, pixels_per_cell, cells_per_block, visualize, normalize)
fd_name = os.path.split(im_path)[1].split(".")[0] + ".feat"
fd_path = os.path.join(neg_feat_ph, fd_name)
joblib.dump(fd, fd_path)
Expand Down
12 changes: 5 additions & 7 deletions object-detector/test-classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cv2
import argparse as ap
from nms import nms
from config import *

def sliding_window(image, window_size, step_size):
'''
Expand Down Expand Up @@ -43,10 +44,9 @@ def sliding_window(image, window_size, step_size):
min_wdw_sz = (100, 40)
step_size = (10, 10)
downscale = args['downscale']
visualize = args['visualize']
visualize_det = args['visualize']

# Load the classifier
model_path = '../data/models/svm.model'
clf = joblib.load(model_path)

# List to store the detections
Expand All @@ -65,9 +65,7 @@ def sliding_window(image, window_size, step_size):
if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[1] != min_wdw_sz[0]:
continue
# Calculate the HOG features
fd = hog(im_window, orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(3, 3), visualise=False, normalise=True)
# Perform the predictions
fd = hog(im_window, orientations, pixels_per_cell, cells_per_block, visualize, normalize)
pred = clf.predict(fd)
if pred == 1:
print "Detection:: Location -> ({}, {})".format(x, y)
Expand All @@ -78,7 +76,7 @@ def sliding_window(image, window_size, step_size):
cd.append(detections[-1])
# If visualize is set to true, display the working
# of the sliding window
if visualize:
if visualize_det:
clone = im_scaled.copy()
for x1, y1, _, _, _ in cd:
# Draw the detections at this scale
Expand All @@ -100,7 +98,7 @@ def sliding_window(image, window_size, step_size):
cv2.waitKey()

# Perform Non Maxima Suppression
detections = nms(detections, .3)
detections = nms(detections, threshold)

# Display the results after performing NMS
for (x_tl, y_tl, _, w, h) in detections:
Expand Down
2 changes: 1 addition & 1 deletion object-detector/train-classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import argparse as ap
import glob
import os
from config import *

if __name__ == "__main__":
# Parse the command line arguments
Expand Down Expand Up @@ -39,7 +40,6 @@
clf = LinearSVC()
print "Training a Linear SVM Classifier"
clf.fit(fds, labels)
model_path = '../data/models/svm.model'
# If feature directories don't exist, create them
if not os.path.isdir(os.path.split(model_path)[0]):
os.makedirs(os.path.split(model_path)[0])
Expand Down

0 comments on commit a466ebb

Please sign in to comment.