Browse Source

randnum.py works without hitch, must add SciPy/mdp code for PCA

master
Sam Black 11 years ago
parent
commit
0390121b97
3 changed files with 46 additions and 1050 deletions
  1. +28
    -27
      ee4h/facedetect.py
  2. +18
    -23
      ee4p/python/pca/randnum.py
  3. +0
    -1000
      ee4p/python/pca/randpca

+ 28
- 27
ee4h/facedetect.py View File

@@ -6,8 +6,7 @@ The program finds faces in a camera image or video stream and displays a red box
Original C implementation by: ?
Python implementation by: Roman Stanchak
"""
import sys
import os
import sys, os
from opencv.cv import *
from opencv.highgui import *
from PIL import Image
@@ -20,19 +19,19 @@ input_name = "../c/lena.jpg"

# Parameters for haar detection
# From the API:
# The default parameters (scale_factor=1.1, min_neighbors=3, flags=0) are tuned
# for accurate yet slow object detection. For a faster operation on real video
# images the settings are:
# scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING,
# The default parameters (scale_factor=1.1, min_neighbors=3, flags=0) are tuned
# for accurate yet slow object detection. For a faster operation on real video
# images the settings are:
# scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING,
# min_size=<minimum possible face size
min_size = cvSize(20,20)
image_scale = 1.2
haar_scale = 1.2
image_scale = 1.3
haar_scale = 1.3
min_neighbors = 2
haar_flags = CV_HAAR_DO_CANNY_PRUNING


def crop_count(facecropped):
def crop_count(facecropped, gcount):

#get image dimensions
iheight = int(facecropped.height / 2);
@@ -55,7 +54,7 @@ def crop_count(facecropped):
source = mouth;
grey = cvCreateImage (cvSize (mouth.width, mouth.height), 8, 1)
output = cvCreateImage (cvSize (mouth.width, mouth.height), 8, 1)

# Convert to greyscale
cvCvtColor (source, grey, CV_BGR2GRAY)
@@ -72,11 +71,13 @@ def crop_count(facecropped):
count += 1

print "Number of dark pixels: " + str(count)
#left for testing
#cvSaveImage("face.jpg", output);
if gcount < 10:
gcount = "0%s" % gcount
cvSaveImage("face-%s.jpg" % gcount, output);

def detect_and_draw( img ):
def detect_and_draw( img, gcount ):
# allocate temporary images
gray = cvCreateImage( cvSize(img.width,img.height), 8, 1 );
small_img = cvCreateImage( cvSize( cvRound (img.width/image_scale),
@@ -89,7 +90,7 @@ def detect_and_draw( img ):
cvResize( gray, small_img, CV_INTER_LINEAR );

cvEqualizeHist( small_img, small_img );
cvClearMemStorage( storage );

if( cascade ):
@@ -100,9 +101,9 @@ def detect_and_draw( img ):
print "detection time = %gms" % (t/(cvGetTickFrequency()*1000.));
if faces:
for face_rect in faces:
# the input to cvHaarDetectObjects was resized, so scale the
# the input to cvHaarDetectObjects was resized, so scale the
# bounding box of each face and convert it to two CvPo
# pt1 = cvPoint( int(face_rect.x*image_scale), int(face_rect.y*image_scale))
# pt2 = cvPoint( int((face_rect.x+face_rect.width)*image_scale),
# int((face_rect.y+face_rect.height)*image_scale) )
@@ -114,7 +115,7 @@ def detect_and_draw( img ):
face = cvRect(x,y,w,h);
crop = cvGetSubRect(img,face);
#cvSaveImage("face.jpg", crop);
crop_count(crop);
crop_count(crop, gcount);

if __name__ == '__main__':

@@ -131,26 +132,26 @@ if __name__ == '__main__':

else:
input_name = sys.argv[1]
# the OpenCV API says this function is obsolete, but we can't
# cast the output of cvLoad to a HaarClassifierCascade, so use this anyways
# the size parameter is ignored
cascade = cvLoadHaarClassifierCascade( cascade_name, cvSize(1,1) );
if not cascade:
print "ERROR: Could not load classifier cascade"
sys.exit(-1)

if input_name.isdigit():
capture = cvCreateCameraCapture( int(input_name) )
else:
capture = cvCreateFileCapture( input_name );
capture = cvCreateFileCapture( input_name );

if( capture ):
frame_copy = None
i = 0;
while True:
i = 0;
while True:
frame = cvQueryFrame( capture );
print i;
if( not frame ):
@@ -162,8 +163,8 @@ if __name__ == '__main__':
cvCopy( frame, frame_copy );
else:
cvFlip( frame, frame_copy, 0 );
detect_and_draw( frame_copy );
detect_and_draw( frame_copy, i );
i = i+1;
if( cvWaitKey( 10 ) >= 0 ):
break;
@@ -172,8 +173,8 @@ if __name__ == '__main__':
image = cvLoadImage( input_name, 1 );

if( image ):
detect_and_draw( image );
cvWaitKey(0);
cvDestroyWindow("result");

+ 18
- 23
ee4p/python/pca/randnum.py View File

@@ -1,22 +1,22 @@
#!/usr/bin/python
# randnum.py
# randnum.py
#
# Copyright 2008 Sam Black <samwwwblack@lapwing.org>
# Copyright 2008 Sam Black <samwwwblack@lapwing.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

import sys, os, random, numpy
from math import sqrt
@@ -70,24 +70,19 @@ vectordata = []
for line in range(0, len(tmpdata) - 1):
for col in range(0,71, 3):
if line == 0:
vectordata.append([float(tmpdata[line][col]), float(tmpdata[line][col+1]), float(tmpdata[line][col+2])])
vectordata.append([[float(tmpdata[line][col]), float(tmpdata[line][col+1]), float(tmpdata[line][col+2])]])
else:
vectordata[col/3].append([float(tmpdata[line][col]), float(tmpdata[line][col+1]), float(tmpdata[line][col+2])])
print vectordata[col/3]

tmpdataarrays = []
array_desc = [('x',numpy.float32),('y',numpy.float32),('z',numpy.float32)]
for data in vectordata:
for col in range(0, len(data) - 1):
print data[col]
tmpdataarrays.append(numpy.array(data[col], dtype=array_desc))
tmpdataarrays.append(numpy.array((data[col][0], data[col][1], data[col][2]), dtype=array_desc))

print tmpdataarrays
print "Created tmp arrays"

dataarrays = []
for arr in tmpdataarrays:
print arr
print arr.mean()
dataarrays.append[[arr, arr.mean(), arr.std(), arr.var()]]
print dataarrays
dataarrays.append([arr, arr.view((numpy.float32,3)).mean(), arr.view((numpy.float32,3)).std(), arr.view((numpy.float32,3)).var()])
print "Created data arrays"

+ 0
- 1000
ee4p/python/pca/randpca
File diff suppressed because it is too large
View File


Loading…
Cancel
Save