working feature extraction + knearest neighbour

This commit is contained in:
Jeena 2013-10-15 01:29:31 +02:00
parent 4b2bb43c62
commit 266ce7c5b0
139 changed files with 158 additions and 30 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*haar*

BIN
cat.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.3 KiB

BIN
cat2.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6 KiB

9
dir.py Normal file
View file

@ -0,0 +1,9 @@
import os
cat_dir = "img/cat"
dog_dir = "img/dog"
for directory in [cat_dir, dog_dir]:
for fn in os.listdir(directory):
print directory, fn

BIN
dog.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.5 KiB

BIN
dog2.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

41
facer.rb Normal file
View file

@ -0,0 +1,41 @@
require 'rubygems'
require 'opencv'
face_detectors = %w(
/usr/local/share/opencv/haarcascades/haarcascade_frontalface_default.xml
).collect { |fd| OpenCV::CvHaarClassifierCascade::load(fd) }
file_names = Dir.glob(ARGV[0] || "/Users/sandro/Ruby/image_labs/lib/facer/euruko2/*.jpg")
file_names.each_with_index do |file_name,i|
face_rectangles = []
opencv_load = OpenCV::IplImage.load(file_name)
face_detectors.each_with_index do |face_detector,k|
face_detector.detect_objects(opencv_load).each do |rect|
face_rectangles << [
rect.top_left.x, rect.top_left.y,
rect.top_right.x - rect.top_left.x,
rect.bottom_left.y - rect.top_left.y
]
end
end
File.open("#{File.basename(file_name)}.html","w") do |file|
file.write <<-HTML
<html>
<body style="margin:0px; background: url('file://#{file_name}') no-repeat top left;">
#{ face_rectangles.collect do |(x,y,w,h)|
"<div class=\"face\" style=\"position: absolute; left: #{x}px;
top: #{y}px; width: #{w}px; height: #{h}px; border: 2px solid red; \"></div>"
end.join("\n") }
</body>
</html>
HTML
end
puts "End #{file_name}"
end

View file

@ -1,42 +1,70 @@
#!/usr/bin/env python
import numpy as np
from math import pi
import cv2
import cv
import sys
import cv2, cv, sys, math, os, numpy
from scipy.spatial import KDTree
if len(sys.argv) > 1:
fn = sys.argv[1]
print 'loading %s ...' % fn
img1 = cv2.imread(fn, 0)
img = cv.LoadImage(fn, cv.CV_LOAD_IMAGE_GRAYSCALE)
size = cv.GetSize(img)
def extractFeatures(label):
temp = cv.CreateImage(size, img.depth, img.nChannels)
print temp
cv.Smooth(img, temp)
directory = "img/" + label + "/"
canny = cv2.Canny(temp, 50, 100)
color_dst = cv2.cvtColor(canny, cv2.COLOR_GRAY2BGR)
lines = cv2.HoughLinesP(canny, 1, pi/90, 20, np.array([]), 5)
features = []
try:
for line in lines[0]:
cv2.line(color_dst, (line[0], line[1]), (line[2], line[3]), cv.RGB(255,0,0), 1, 8)
except:
pass
for fn in os.listdir(directory):
print lines[0].size
img = cv2.imread(directory + fn, 0)
cv2.namedWindow("Original")
cv2.imshow("Original", img)
#temp = cv.CreateImage((100,100), cv.CV_8U, 1)
#cv.Smooth(img, temp)
cv2.namedWindow('Lines image')
cv2.imshow('Lines image', color_dst)
canny = cv2.Canny(img, 50, 100)
color_dst = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.waitKey()
# find colored
black_pixels = numpy.count_nonzero(img)
# find lines lines
lines = cv2.HoughLinesP(canny, 1, math.pi/360, 5, None, 10, 1)
lengths = []
angles = []
try:
for line in lines[0]:
x1, y1, x2, y2 = line
#cv2.line(color_dst, (x1, y1), (x2, y2), cv.RGB(255,0,0), 1, 8)
length = int(math.sqrt(math.pow((x1-x2), 2) + math.pow((y1-y2), 2)))
lengths.append(length)
angle = int(math.degrees(math.atan((y1-y2) / (x1-x2))))
angles.append(angle)
except:
pass
# print out everything
lines_count = len(lengths)
mid_length = sum(lengths) / lines_count
mid_angle = sum(angles) / lines_count
features.append([[lines_count, mid_length, mid_angle, black_pixels], label])
#cv2.namedWindow("Original")
#cv2.imshow("Original", img)
#cv2.namedWindow('Lines image ' + fn)
#cv2.imshow('Lines image ' + fn, color_dst)
return features
else:
print "Please give a image path"
if __name__ == "__main__":
arr = extractFeatures("cat") + extractFeatures("dog")
test_label = arr[0][1]
test_feature = arr[0][0]
labels = map(lambda a: a[1], arr)[1:]
features = map(lambda a: a[0], arr)[1:]
tree = KDTree(features)
d, i = tree.query(test_feature)
print test_label + " is predicted to be a " + labels[i]

BIN
img/.DS_Store vendored Normal file

Binary file not shown.

BIN
img/cat/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

BIN
img/cat/10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

BIN
img/cat/11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

BIN
img/cat/12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

BIN
img/cat/13.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

BIN
img/cat/14.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

BIN
img/cat/15.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

BIN
img/cat/16.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

BIN
img/cat/17.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7 KiB

BIN
img/cat/18.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

BIN
img/cat/19.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

BIN
img/cat/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

BIN
img/cat/20.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

BIN
img/cat/21.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
img/cat/22.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

BIN
img/cat/23.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

BIN
img/cat/24.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

BIN
img/cat/25.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

BIN
img/cat/26.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

BIN
img/cat/27.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

BIN
img/cat/28.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
img/cat/29.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
img/cat/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9 KiB

BIN
img/cat/30.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

BIN
img/cat/31.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

BIN
img/cat/32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

BIN
img/cat/33.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

BIN
img/cat/34.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

BIN
img/cat/35.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

BIN
img/cat/36.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

BIN
img/cat/37.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

BIN
img/cat/38.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8 KiB

BIN
img/cat/39.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

BIN
img/cat/4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

BIN
img/cat/40.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

BIN
img/cat/41.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

BIN
img/cat/42.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

BIN
img/cat/43.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7 KiB

BIN
img/cat/44.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

BIN
img/cat/45.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

BIN
img/cat/46.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

BIN
img/cat/47.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

BIN
img/cat/48.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8 KiB

BIN
img/cat/49.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
img/cat/5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

BIN
img/cat/50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

BIN
img/cat/51.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

BIN
img/cat/52.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
img/cat/54.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

BIN
img/cat/55.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5 KiB

BIN
img/cat/56.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

BIN
img/cat/57.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

BIN
img/cat/58.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

BIN
img/cat/59.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

BIN
img/cat/6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

BIN
img/cat/60.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
img/cat/61.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

BIN
img/cat/62.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

BIN
img/cat/63.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

BIN
img/cat/64.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

BIN
img/cat/65.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
img/cat/7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

BIN
img/cat/8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

BIN
img/cat/9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

BIN
img/dog/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

BIN
img/dog/10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

BIN
img/dog/11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

BIN
img/dog/12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
img/dog/13.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

BIN
img/dog/14.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

BIN
img/dog/15.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6 KiB

BIN
img/dog/16.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

BIN
img/dog/17.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7 KiB

BIN
img/dog/18.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
img/dog/19.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

BIN
img/dog/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

BIN
img/dog/20.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

BIN
img/dog/21.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

BIN
img/dog/22.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

BIN
img/dog/23.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

BIN
img/dog/24.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

BIN
img/dog/25.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

BIN
img/dog/26.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

BIN
img/dog/27.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
img/dog/28.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
img/dog/29.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

BIN
img/dog/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

BIN
img/dog/30.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

BIN
img/dog/31.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

BIN
img/dog/32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

BIN
img/dog/33.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Some files were not shown because too many files have changed in this diff Show more