Anda di halaman 1dari 80

-1-

Chapter 1

OVERVIEW

1.1 Overview of the Project Face recognition system is a software that is a biometric identification system. Having the s/w trained on a system and then a given a new face, the software can identify the face as recognized (Belonging to the Database) face or as a new face (not belonging to the Database).

The motive behind the project was that we could learn about Image processing and also being AI students, Image processing is considered to be one of the main fields of AI. This project also increased our knowledge about the image processing and also gaining knowledge about the Artificial Intelligence.

Regarding the applications point of view, Face Recognition actually belongs to Biometrics Security measures. However, Face Recognition has its immense importance as well particularly after 9/11. American DOD is spending millions of dollars on face recognition for tracking of terrorist particularly at Airports. It is yet not so commercial in the sense that no accurate versions of face recognition systems are in the market as compared to finger print recognition or retina scan but the main advantage of it is that actually its non pervasive security technology i.e. you can easily get a picture through a installed camera of a person and recognize it rather than him to do anything for you.

-2Face Recognition Systems could also be applied to:

Airport Surveillance Face Recognition systems could be integrated into CCTV networks of airports and used to scan for known terrorists and drug traffickers.

Private Surveillance In todays cut-throat corporate environment, companies are becoming more and more focused on protecting trade secrets, preserving customer confidentiality and preventing industrial sabotage. To this end, face recognition systems could be used to detect unauthorized personnel in private premises. Together with a private security firm this would provide companies with an extremely high level of internal security.

Access control for PCs in a Corporate Environment Logging on to PCs could be done by a combination of passwords and/or face recognition. This would allow to work unencumbered by complex security procedures yet still ensure a high degree of confidentiality for clients and protections of trade secrets.

Added Security for ATM transactions Access to Automated Teller Machines and other banking services could be controlled with methods like PIN numbers, Speech verification or IRIS recognition in combination with face recognition. Whilst face recognition alone would not alone yield such a high

-3degree of certainty for such a crucial application, it could be used together with other techniques to add an extra level of safety an security.

Mug Shot Matching for Law enforcement Agencies Law enforcement agencies could use face recognition software to match witnesses descriptions to known criminals. The ability of face recognition systems to determine identity with only partial information would be particularly useful here.

Improved Human-Computer Interaction After recognizing the identity and mood of their users, computerized equipment could modify their behaviour accordingly.

-4-

Chapter 2

Background Mathematics

First of all going directly into the details of Principal Component Analysis (PCA), its better to get familiar with some basic concepts of image processing.

2.1 Standard Deviation To understand standard deviation, we need a data set. Statisticians are usually concerned with taking a sample of a population. To use election polls as an example, the population is all the people in the country, whereas a sample is a subset of the population that the statisticians measure. The great thing about statistics is that by only measuring (in this case by doing a phone survey or similar) a sample of the population, you can work out what is most likely to be the measurement if you used the entire population.

In this statistics section, I am going to assume that our data sets are samples of some bigger population. There is a reference later in this section pointing to more information about samples and populations. Heres an example set:

-5I could simply use the symbol X to refer to this entire set of numbers. If I want to refer to an individual number in this data set, I will use subscripts on the symbol X to indicate a specific number. Eg Note that refers to the 3rd number in X, namely the number 4. you may see in some

is the first number in the sequence, not like

textbooks. Also, the symbol n will be used to refer to the number of elements in the set X.

There are a number of things that we can calculate about a data set. For example, we can calculate the mean of the sample. I assume that the reader understands what themean of a sample is, and will only give the formula:

Notice the symbol

(said X bar) to indicate the mean of the set X. All this formula

says is Add up all the numbers and then divide by how many there are. Unfortunately, the mean doesnt tell us a lot about the data except for a sort of middle point. For example, these two data sets have exactly the same mean (10), but are obviously quite different:

So what is different about these two sets? It is the spread of the data that is different. The Standard Deviation (SD) of a data set is a measure of how spread out the data is. How do we calculate it? The English definition of the SD is: The average distance

-6from the mean of the data set to a point. The way to calculate it is to compute the squares of the distance from each data point to the mean of the set, add them all up, divide by n-1 , and take the positive square root. As a formula:

Where s is the usual symbol for standard deviation of a sample. I hear you asking Why are you using n-1 and not n ?. Well, the answer is a bit complicated, but in general, if your data set is a sample data set, ie. you have taken a subset of the real-world (like surveying 500 people about the election) then you must use n-1 because it turns out that this gives you an answer that is closer to the standard deviation that would result if you had used the entire population, than if youd used n . If, however, you are not calculating the standard deviation for a sample, but for an entire population, then you should divide by n instead of n-1 .

-72.2 Variance Variance is another measure of the spread of data in a data set. In fact it is almost identical to the standard deviation. The formula is this:

You will notice that this is simply the standard deviation squared, in both the symbol ( ) and the formula (there is no square root in the formula for variance). is the usual

symbol for variance of a sample. Both these measurements are measures of the spread of the data. Standard deviation is the most common measure, but variance is also used. The reason why I have introduced variance in addition to standard deviation is to provide a solid platform from which the next section, covariance, can launch from.

2.3 Covariance The last two measures we have looked at are purely 1-dimensional. Data sets like this could be: heights of all the people in the room, marks for the last DSP exam etc. However many data sets have more than one dimension, and the aim of the statistical analysis of these data sets is usually to see if there is any relationship between the dimensions. For example, we might have as our data set both the height of all the students in a class, and the mark they received for that paper. We could then perform statistical analysis to see if the height of a student has any effect on their mark.

Standard deviation and variance only operate on 1 dimension, so that you could only calculate the standard deviation for each dimension of the data set independently of the

-8other dimensions. However, it is useful to have a similar measure to find out how much the dimensions vary from the mean with respect to each other.

Covariance is such a measure. Covariance is always measured between 2 dimensions. If you calculate the covariance between one dimension and itself, you get the variance. So, if you had a 3-dimensional data set (x,y,z) then you could measure the covariance between the x and y dimensions, the x and z dimensions, and the x and y dimensions. Measuring the covariance between x and x, or y and y, or z and z would give you the variance of the x, y and z dimensions respectively.

The formula for covariance is very similar to the formula for variance. The formula for variance could also be written like this:

where I have simply expanded the square term to show both parts.

So given that knowledge, here is the formula for covariance:

It is exactly the same except that in the second set of brackets, the X s are replaced by Ys. This says, in English, For each data item, multiply the difference between the x

-9value and the mean of x, by the the difference between the y value and the mean of y. Add all these up, and divide by (n-1).

How does this work? Lets use some example data. Imagine we have gone into the world and collected some 2-dimensional data, say, we have asked a bunch of students how many hours in total that they spent studying PP, and the mark that they received. So we have two dimensions, the first is the H dimension, the hours studied, and the second is the M dimension, the mark received. So what does it tell us? The exact value is not as important as its sign (ie. Positive or Negative).

If the value is positive, as it is here, then that indicates that both dimensions increase together, meaning that, in general, as the number of hours of study increased, so did the final mark. If the value is negative, then as one dimension increases, the other decreases. If we had ended up with a negative covariance here, then that would have said the opposite, that as the number of hours of study increased the the final mark decreased. In the last case, if the covariance is zero, it indicates that the two dimensions are independent of each other.

2.4 The covariance Matrix Recall that covariance is always measured between 2 dimensions. If we have a data set with more than 2 dimensions, there is more than one covariance measurement that can be calculated. For example, from a 3 dimensional data set (dimensions x,y,z) you could calculate cov(x,y), cov(x,z) and cov(y,z). In fact, for an n-dimensional data set, you can calculate n!/((n-2)!-2) different covariance values.

-10-

-11A useful way to get all the possible covariance values between all the different dimensions is to calculate them all and put them in a matrix. I assume in this tutorial that you are familiar with matrices, and how they can be defined. So, the definition for the covariance matrix for a set of data with n dimensions is:

Where

is a matrix with n rows and n columns, and

is the xth dimension.

All that this ugly looking formula says is that if you have an n-dimensional data set, then the matrix has n rows and columns (so is square) and each entry in the matrix is the result of calculating the covariance between two separate dimensions. E.g. the entry on row 2, column 3, is the covariance value calculated between the 2nd dimension and the 3rd dimension. An example. Well make up the covariance matrix for an imaginary 3 dimensional data set, using the usual dimensions x, y and z. Then, the covariance matrix has 3 rows and 3 columns, and the values are this:

Some points to note: Down the main diagonal, you see that the covariance value is between one of the dimensions and itself. These are the variances for that dimension. The other point is that since cov(a,b) = cov(b,a), the matrix is symmetrical about the main diagonal.

-122.5 Eigenvectors As you know, you can multiply two matrices together, provided they are compatible sizes. Eigenvectors are a special case of this. Consider the two multiplications between a matrix and a vector in Figure1. In the first example, the resulting vector is not an integer multiple of the original vector, whereas in the second example, the example is exactly 4 times the vector we began with. Why is this?

Well, the vector is a vector in 2 dimensional space. The vector

(from the second

example multiplication) represents an arrow pointing from the origin, (0,0) to the point (3,2) . The other matrix, the square one, can be thought of as a transformation matrix. If you multiply this matrix on the left of a vector, the answer is another vector that is transformed from its original position. It is the nature of the transformation that the eigenvectors arise from. Imagine a transformation matrix that, when multiplied on the left, reflected vectors in the line y=x. Then you can see that if there were a vector that lay on the line y=x, its reflection it itself. This vector (and all multiples of it, because it wouldnt matter how long the vector was), would be an eigenvector of that transformation matrix.

What properties do these eigenvectors have? You should first know that eigenvectors can only be found for square matrices. And, not every square matrix has eigenvectors. And, given an n*n matrix that does have eigenvectors, there are n of them. Given a 3*3 matrix, there are 3 eigenvectors.

-13-

Figure 1: Example of one Eigenvector and one non-Eigenvector

Figure2 : Example of how a scaled Eigenvector is still an Eigenvector

Another property of eigenvectors is that even if I scale the vector by some amount before I multiply it, I still get the same multiple of it as a result, as in Figure 2. This is because if you scale a vector by some amount, all you are doing is making it longer, not changing its direction. Lastly, all the eigenvectors of a matrix are perpendicular, ie. at right angles to each other, no matter how many dimensions you have. By the way, another word for perpendicular, in maths talk, is orthogonal. This is important because it means that you can express the data in terms of these perpendicular eigenvectors, instead of expressing them in terms of the and axes.

2.6 Eigenvalues Eigenvalues are closely related to eigenvectors, in fact, we saw an eigenvalue in Figure 2. Notice how, in both those examples, the amount by which the original vector was

-14scaled after multiplication by the square matrix was the same? In that example, the value was 4. 4 is the eigenvalue associated with that eigenvector. No matter what multiple of the eigenvector we took before we multiplied it by the square matrix, we would always get 4 times the scaled vector as our result (as in Figure 2.3).

2.7 Face viewed as a vector A face, which is an image, can be viewed as a vector. If the images width and height are w and h pixels respectively, the number of components of this vector will be w*h. Each pixel is coded by one vector component. The construction of this vector from an image is performed by a simple concatenation - the rows of the image are placed each beside one another, as shown on the figure 3.

Figure 3: formation of the faces vector from the faces image

2.8 Image Space The face vector described in the previous section belongs to a space. This space is the image space, the space of all images whose dimension is w by h pixels. The basis of the image space is composed of the following vectors:

-15-

Figure 4: Image spaces basis

All the faces look like each other. They all have two eyes, a mouth, a nose, etc. located at the same place. Therefore, all the face vectors are located in a very narrow cluster in the image space, as shown in the figure 5.

Figure 5: Image Space and face cluster

Hence, the full image space is not an optimal space for face description. The task presented here aims to build a face space which better describes the faces. The basis vectors of this face space are called the principal components.

-16-

The dimension of the image space is w*h. Of course, all the pixels of a face are not relevant, and each pixel depends on its neighbours. So, the dimension of the face space is less than the dimension of the image space. The dimension of the face space cannot be determined, but it is sure that it is to be far less than that of the image space.

The goal of the method presented here, Principal Components Analysis, is to reduce the dimension of a set or space so that the new basis better describes the typical models of the set. In our case the models are a set of training faces. The new basis will be constructed by linear combination. Components in this face space basis will be uncorrelated and will maximise the variance accounted for in the original variables.

Principal Components Analysis aims to catch the total variation in the set of the training faces, and to explain this variation by a few variables. The fact that it reduces the dimension is important. In fact, an observation described by a few variables is easier to handle and easier to understand than if it was defined by a huge amount of variables. And when many observations, or faces, have to be processed the dimensionality reduction is of first importance.

-17-

Chapter 3

Principal Components Analysis

Finally we come to Principal Components Analysis (PCA). What is it? It is a way of identifying patterns in data, and expressing the data in such a way as to highlight their similarities and differences. Since patterns in data can be hard to find in data of high dimension, where the luxury of graphical representation is not available, PCA is a powerful tool for analysing data.

The other main advantage of PCA is that once you have found these patterns in the data, and you compress the data, ie. by reducing the number of dimensions, without much loss of information. This chapter will take you through the steps you needed to perform a Principal Components Analysis on a set of data.

3.1 Step 1: Get some data In my simple example, I am going to use my own made-up data set. Its only got 2 dimensions, and the reason why I have chosen this is so that

I can provide plots of the data to show what the PCA analysis is doing at each step. The data I have used is found in Figure 3.1, along with a plot of that data.

3.2 Step 2: Subtract the mean For PCA to work properly, you have to subtract the mean from each of the data dimensions. The mean subtracted is the average across each dimension. So, all the x

-18< (the mean of the x values of all the data points) subtracted, and all the = subtracted from them. This produces a data set whose mean is zero.

values have y values have

-19-

Figure 6: PCA example data, original data on the left, data with the means subtracted on the right, and a plot of the data

3.3 Step 3: Calculate the covariance matrix Since the data is 2 dimensional, the covariance matrix will be 2*2. There are no surprises here, so I will just give you the result:

So, since the non-diagonal elements in this covariance matrix are positive, we should expect that both the x and y variable increase together.

-20-

3.4 Step 4: Calculate the covariance matrix Since the covariance matrix is square, we can calculate the eigenvectors and eigenvalues for this matrix. These are rather important, as they tell us useful information about our data. I will show you why soon. In the meantime, here are the eigenvectors and eigenvalues:

It is important to notice that these eigenvectors are both unit eigenvectors ie. Their lengths are both 1. This is very important for PCA, but luckily, most maths packages, when asked for eigenvectors, will give you unit eigenvectors.

So what do they mean? If you look at the plot of the data in Figure 7 then you can see how the data has quite a strong pattern. As expected from the covariance matrix, they two variables do indeed increase together. On top of the data I have plotted both the eigenvectors as well. They appear as diagonal dotted lines on the plot. As stated in the eigenvector section, they are perpendicular to each other. But, more importantly, they

-21provide us with information about the patterns in the data. See how one of the eigenvectors goes through the middle of the points, like drawing a line of best fit? That eigenvector is showing us how these two data sets are related along that line. The second eigenvector gives us the other, less important, pattern in the data, that all the points follow the main line, but are off to the side of the main line by some amount.

So, by this process of taking the eigenvectors of the covariance matrix, we have been able to extract lines that characterize the data. The rest of the steps involve transforming the data so that it is expressed in terms of them lines.

3.5 Step 5: Choosing components and forming a feature vector Here is where the notion of data compression and reduced dimensionality comes into it. If you look at the eigenvectors and eigenvalues from the previous section, you will notice that the eigenvalues are quite different values. In fact, it turns out that the eigenvector with the highest eigenvalue is the principle component of the data set.

In our example, the eigenvector with the larges eigenvalue was the one that pointed down the middle of the data. It is the most significant relationship between the data dimensions.

-22-

Figure 7: A plot of the normalised data (mean subtracted) with the eigenvectors of the covariance matrix overlayed on top.

In general, once eigenvectors are found from the covariance matrix, the next step is to order them by eigenvalue, highest to lowest. This gives you the components in order of significance. Now, if you like, you can decide to ignore the components of lesser significance. You do lose some information, but if the eigenvalues are small, you dont lose much. If you leave out some components, the final data set will have less dimensions than the original. To be precise, if you originally have n dimensions in your data, and so you calculate n eigenvectors and eigenvalues, and then you choose only the first eigenvectors, then the final data set has only dimensions.

-23What needs to be done now is you need to form a feature vector, which is just a fancy name for a matrix of vectors. This is constructed by taking the eigenvectors that you want to keep from the list of eigenvectors, and forming a matrix with these eigenvectors in the columns.

Given our example set of data, and the fact that we have 2 eigenvectors, we have two choices. We can either form a feature vector with both of the eigenvectors:

or, we can choose to leave out the smaller, less significant component and only have a single column:

3.6 Step 6: Deriving the new data set This is the final step in PCA, and is also the easiest. Once we have chosen the components (eigenvectors) that we wish to keep in our data and formed a feature vector,

-24we simply take the transpose of the vector and multiply it on the left of the original data set, transposed.

where RowFeatureVector is the matrix with the eigenvectors in the columns transposed so that the eigenvectors are now in the rows, with the most significant eigenvector at the top, and RowDataAdjust z is the mean-adjusted data transposed, ie. the data items are in each column, with each row holding a separate dimension. Im sorry if this sudden transpose of all our data confuses you, but the equations from here on are easier if we take the transpose of the feature vector and the data first, rather that having a little T symbol above their names from now on. FinalData is the final data set, with data items in columns, and dimensions along rows.

What will this give us? It will give us the original data solely in terms of the vectors we chose. Our original data set had two axes, x and y, so our data was in terms of them. It is possible to express data in terms of any two axes that you like. If these axes are perpendicular, then the expression is the most efficient. This was why it was important that eigenvectors are always perpendicular to each other. We have changed our data from being in terms of the axes x and y , and now they are in terms of our 2

eigenvectors. In the case of when the new data set has reduced dimensionality, ie. We have left some of the eigenvectors out, the new data is only in terms of the vectors that we decided to keep.

-25To show this on our data, I have done the final transformation with each of the possible feature vectors. I have taken the transpose of the result in each case to bring the data back to the nice table-like format. I have also plotted the final points to show how they relate to the components.

In the case of keeping both eigenvectors for the transformation, we get the data and the plot found in Figure 8. This plot is basically the original data, rotated so that the eigenvectors are the axes. This is understandable since we have lost no information in this decomposition.

The other transformation we can make is by taking only the eigenvector with the largest eigenvalue. The table of data resulting from that is found in Figure 9. As expected, it only has a single dimension. If you compare this data set with the one resulting from using both eigenvectors, you will notice that this data set is exactly the first column of the other. So, if you were to plot this data, it would be 1 dimensional, and would be points on a line in exactly the x positions of the points in the plot in Figure 8. We have effectively thrown away the whole other axis, which is the other eigenvector.

-26-

Figure 8: The table of data by applying the PCA analysis using both eigenvectors, and a plot of the new data points.

-27-

Figure 9: The data after transforming using only the most significant eigenvector

So what have we done here? Basically we have transformed our data so that is expressed in terms of the patterns between them, where the patterns are the lines that most closely describe the relationships between the data. This is helpful because we have now classified our data point as a combination of the contributions from each of those lines. Initially we had the simple x and y axes. This is fine, but the x and y values of each data point dont really tell us exactly how that point relates to the rest of the data. Now, the values of the data points tell us exactly where (i.e. above/below) the trend lines the data point sits. In the case of the transformation using both eigenvectors, we have simply altered the data so that it is in terms of those eigenvectors instead of the usual axes. But the single-eigenvector decomposition has removed the contribution due to the smaller eigenvector and left us with data that is only in terms of the other.

-28-

CHAPTER 4

Block Diagram

T r a in in g fa c e s : e a c h c o lu m n is a 4 0 9 6 c o m p o n e n ts v e c to r r e p r e s e n tin g a fa c e .

E ig e n fa c e s : e a c h c o lu m n i s a 4 0 9 6 com ponent r e p r e s e n tin g a n e ig e n fa c e .

P C A

S to r e d c o m p o n e n t s o f t h e tr a in in g f a c e s in th e fa c e s p a c e (3 0 c o m p o n e n ts p e r fa c e ) .

T ra n s p o s e

W h ic h s t o r e d fa c e is t h e n e a r e s t ?

R e s u lt

T e s t fa c e ( 4 0 9 6 p ix e ls )

T e s t f a c e 's c o m p o n e n ts in t h e fa c e s p a c e (3 0 c o m p o n e n ts )

Block Diagram of the Project

-29-

CHAPTER 5

Implementation in MATLAB

We have implemented the system in MATLAB as it provides the wide range of operations through its Image Processing Toolbox. Also, coding is easy in MATLAB because of many built-in functions and we need only a single line command.

5.1 Application of PCA on MATLAB Now, let us discuss how PCA is applied on MATLAB.

5.1.1 Training Set

5.1.1.1 Reading of an Image The first step is to obtain a set of faces used for recognition. Using imread command, faces are obtained in MATLAB.

Imread command is used to read an image from the file and stores it in an array. E.g. I = imread(face.jpg);

Where I is the array in which image is stored. Here JPEG is the file format. The other file formats can also be used like bmp, tif, pgm, gif etc.

-305.1.1.2 Normalization of Images After obtaining a set of faces, we need to normalize all images i.e. faces. In image processing, normalization is a process that changes the range of pixel intensity values. Normalization is necessary to reduce errors due to lightening conditions. Normalization is a linear process. For normalization, first we take mean and standard deviation of each image.

For mean and standard deviation, built-in functions are already in MATLAB, so we need only single command mean(matrix) calculates mean columnwise.

In our coding, each column represents one image and thats how we calculate mean of each image. Std(matrix) calculates standard deviation of each image.

Then by using these mean and standard deviation values, we normalize each image.

5.1.1.3 Mean Image After normalizing faces, we find a mean image. Mean image is constructed by taking mean of all corresponding pixels of all images.

Here mean is calculated as mean(matrix, dim) dim=2

i.e. taking mean row wise in order to take mean of every pixel of all images.

-315.1.1.4 Covariance and Eigen Vectors The covariance between two variables equals the correlation times the product of the variables standard deviations. The covariance of a variable with itself is the variables variance. In statistics and probability theory, the covariance matrix is a matrix of covariance between elements of a vector. It is the natural generalization to higher dimensions, of the concept of the variance of a scalar-valued random variable. If X is a column vector with n scalar random variable components, and k is the expected value of the kth element of X, i.e., k = E(Xk), then the covariance matrix is defined as:

The (i,j) element is the covariance between Xi and Xj. After normalization of images, we calculate covariance of images and then determine its eigen vectors. Obtaining eigen values is the basically the PCA (Principal Components Analysis). We say that is an eigen value of A if there exists a non-zero vector x such that

-32where x is called an eigenvector. We can determine eigen vectors and eigen values by single command in MATLAB. [vector, value] = eig(matrix) where vector represents eigen vector and value represents eigen value.

5.1.1.5 Eigen Faces After determining eigen vectors and sorting them, we determine eigen faces. The sorting is done in ascending order because eigen faces with bigger eigen values will explain more of the variation in the set of faces, i.e. will be more distinguishing. By default, sorting is done in ascending order. We sort the eigen values and also store their previous indices. [S, indices] = sort(eigen values) Then by applying normalization formula to sorted eigen vectors, we get eigen faces. This is our achievement of obtaining training set.

5.1.1.6 Weight of an Image When we obtain the training set, we calculate the weight of each image in the training set. The wait is calculated by dot product of eigen faces and normalized images. In MATLAB, dot product is taken by dot(eigen faces, images)

-335.1.2 Recognition Procedure First we read an image which is to be recognized. The image is user input for which we use command input ( ) to take input from user. We apply the same procedure of normalizing image as applied on given set of images. After that, we calculate the difference between input image and the mean image obtained from set of given images. Next, we calculate the dot product between eigen faces and the input image. This is actually the Face Space Representation of the input image. Using this dot product, mean and eigen faces, we reconstruct the input image. Then, we find the weight of the input image in the same manner as discussed above. Here the dot product is taken between eigen faces and difference calculated above.

5.1.2.1 Euclidean Distance Euclidean distance is the ordinary distance between the two points. The Euclidean distance between two points and is

In MATLAB, we calculate Euclidean distance by using the command norm. norm(image) then we find maximum and minimum values of the distance between input image and each image the training set. We set a threshold value that identifies that the face is known or unknown or it really the face or not.

-345.2 Results of Face Recognition We set a threshold of maximum and minimum values of Euclidean distance and with the help of these maximum and minimum values, we determine that the face is known or unknown and whether it is face or not. Lets take some test cases:

If the face is known then result will be

Maximum Value = 14827

Minimum Value = 11960

-35-

Next, we consider the case where face is unknown

Maximum Value = 14506

Minimum Value = 13321

-36-

In last, we consider the case when recognized object is not a face

Maximum Value = 17072

Minimum Value = 15260

From the above discussion, it is observed that if the distance(min) exceeds 15000 then it is not a face and if the maximum distance is within 15000 and minimum distance exceeds 12000 then it is face but unknown. If the maximum distance is within 15000 and minimum distance is within 12000 then it is a known face.

-37-

Chapter 6

Final Implementation

In this phase we have to implement the algorithm in some language and compile a binary file in order to make the algorithm more useable, user-friendly and interactive. To provide even better GUI we have to connect it to Macromedia Flash. We can adopt two approaches: 1. Code the algorithm in MATLAB and interface it with Macromedia Flash 2. Code the algorithm directly in Macromedia Flashs ActionScript 3. Code the algorithm in some other convenient language and then connect it to Macromedia Flash using appropriate channels like XML sockets, streams, etc. Hard-coding the algorithm in any language isnt a good idea. Itll remove flexibility in algorithm. Coding it using ActionScript is worst because ActionScript is not so powerful. It is interpreted language hence its slow. ActionScript is designed keeping in mind that itll be used to grab data from other sources and present that. Weve decided to use Java if well use any programming language for convenience. To connect MATLAB to Java we have two approaches: 1. Write the modified Java instructions right into the MATLAB IDE because MATLAB itself runs on a JVM (Java Virtual Machine). 2. Use some library for communication between MATLAB and Java and then write code normally as done in normal Java programs.

-38The options weve opted are: 1. Keep the algorithm in MATLAB because itll provide us more flexibility if we need to change it. 2. Use Java to communicate with MATLAB and then provide data to Macromedia Flash. In this context Java will be acting as a proxy. Were not yet sure whether will we write Java code right into the MATLAB IDE or connect it using any library. 3. Use Macromedia Flash at presentation layer. Pick the data using XML sockets.

Following is the procedure to write inline Java code in MATLAB:

6.1 Connecting Java and MATLAB


The MATLAB desktop runs a java virtual machine. To use a java class, you must first import it. For example, you could type:
import javax.swing.*;

You can then use any of these classes, but you must make some changes in syntax. The following is a list of those that come to mind: In MATLAB, you do not need to type "new"--objects are created as needed. In MATLAB, you use 'single quotes' where you use "double quotes" in java. In java you call a routine with no inputs by putting () after its name. This is unnecessary in MATLAB.

-39

In MATLAB you're not required to put semicolon in the end of every statement (when only one statement is written on one line).

For example, try this: User command:

import javax.swing.*

MATLAB Response: [nothing] User command:

J = JFrame('Hi there')

MATLAB Response: J= javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout, title=Hithere!,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE, rootPane=javax.swing.JRootPane[,0,0,0x0,invalid, layout=javax.swing.JRootPane$RootLayout,alignmentX=null,alignmentY=null, border=,flags=385,maximumSize=,minimumSize=,preferredSize=], rootPaneCheckingEnabled=true]

-40User command:
L = JLabel('A Label');

MATLAB Response: L = javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=null, border=,flags=0,maximumSize=,minimumSize=,preferredSize=,defaultIcon=, disabledIcon=,horizontalAlignment=LEADING, horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=, text=Hi there!, verticalAlignment=CENTER,verticalTextPosition=CENTER] User command:
P = J.getContentPane

MATLAB Response: P = javax.swing.JPanel[null.contentPane,0,0,0x0,invalid, layout=javax.swing.JRootPane$1,alignmentX=null,alignmentY=null, border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

-41User command:
P.add(L)

MATLAB Response: ans = javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=null, border=,flags=0,maximumSize=,minimumSize=,preferredSize=, defaultIcon=,disabledIcon=,horizontalAlignment=LEADING ,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=, text=Hi there!,verticalAlignment=CENTER,verticalTextPosition=CENTER] User command:

J.setSize(200,200);

MATLAB Response: [nothing] User command:


J.setVisible(1)

MATLAB Response: [A window frame appears captioned: "Hi there" appears with a label Label"] in it saying: "A

-42You can also import your own classes. There are two things that you must get working in order to do this. You must tell MATLAB where to look for classes. To do this, you must make sure that the file "classpath.txt" contains the directories in which you class files reside. There is a default classpath.txt which does contain ".", so changing to the correct directory should work. For information on how to locate "classpath.txt" and create your own (you probably put it in your home directory or a directory called~/MATLAB) In my current installation, then only way I can get MATLAB to read java classes is by starting MATLAB in the directory in which they reside MATLAB does not necessarily accept code from the latest compilers. It might be necessary to add the flag "-target 1.3.1" to my call to javac. For example, you can compile a class VectorChooser.java (whose code is given below) and typing:

javac -target 1.3.1 VectorChooser.java

Note: Code of VectorChooser.java is given on the following four pages.

-436.1.1 Code of VectorChooser.java:


import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;

public class VectorChooser extends JFrame {

JSlider sliders[];

int dimension;

public VectorChooser(int dimension) { super("Vector Chooser");

int i;

this.dimension = dimension;

JPanel contentPane = new JPanel(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS)); sliders = new JSlider[dimension];

for (i = 0; i < dimension; i++) { sliders[i] = new JSlider(JSlider.VERTICAL, 0, 10, 0); sliders[i].setMajorTickSpacing(10); sliders[i].setMinorTickSpacing(1);

-44sliders[i].setPaintTicks(true); sliders[i].setPaintLabels(true); sliders[i].setBorder( BorderFactory.createEmptyBorder(0,0,10,0));

contentPane.add(sliders[i]);

contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10, 10)); setContentPane(contentPane);

public double[] getAll() { double ret[];

Integer converter; int i;

ret = new double[dimension]; for (i = 0; i < dimension; i++) { converter = new Integer(getOne(i)); ret[i] = converter.doubleValue(); }

return ret; }

-45public void setAll(double values[]) {

Double converter; int i;

if (values.length != dimension) { System.err.println("wrong number of values."); } else { for (i = 0; i < dimension; i++) { converter = new Double(values[i]); setOne(i,converter.intValue()); } } }

public int getOne(int i) {

int ret; if ((i >= 0) && (i < dimension)) { ret = sliders[i].getValue(); } else { ret = -1; System.err.println("i must be between 0 and dimension-1"); }

return ret; }

-46-

public void setOne(int i, int value) { if ((i >= 0) && (i <= dimension)) { sliders[i].setValue(value); } else { System.err.println("i must be between 0 and dimension-1"); } }

public static void main(String[] args) { VectorChooser vc = new VectorChooser(4); vc.pack(); vc.setVisible(true); } }

Let's now give VectorChooser a spin: User command:

-47-

import VectorChooser

MATLAB Response: [nothing] User command:

v = VectorChooser(4)

MATLAB Response:
v =

VectorChooser[frame1,0,0,0x0,invalid,hidden, layout=java.awt.BorderLayout,title=Vector Chooser,resizable, normal,defaultCloseOperation=HIDE_ON_CLOSE, rootPane=javax.swing.JRootPane[,0,0,0x0,invalid, layout=javax.swing.JRootPane$RootLayout,alignmentX=null, alignmentY=null,border=,flags=385,maximumSize=,minimumSize=, preferredSize=],rootPaneCheckingEnabled=true]

User command:
v.setVisible(1)

-48MATLAB Response: [A window appears with nothing in it has its main panels size is 0x0] User command: v.setSize(250, 200) MATLAB Response: [The windows size is changed to 250x200] User command:

v.getAll

MATLAB Response: ans = 0 0 0 0

User command:

v.setAll(10*rand(1,4))

-49MATLAB Response: [nothing] User command:

v.getAll

MATLAB Response: ans = 9 2 6 4 User command:

v.setOne(1,0)

MATLAB Response: [nothing]

User command:

v.getOne(0)

-50MATLAB Response: ans = 9

Try moving the sliders. You'll see that MATLAB can read them! You can also have a java process running within MATLAB call MATLAB, but that is a story for another day.

-51-

6.2 Recompiling Classes


Unfortunately, if you recompile your java class, MATLAB will not know about it until you quit and restart MATLAB. However, theres a cure for this. The rough idea is that you create a class loader for your class, and then access it through the class loader. After you recompile, you kill the class loader and then create a new instance of it, which then reads the recompiled class. 6.2.1 Explanation: 6.2.1.1 Loading and Reloading a Java Class from Matlab

Matlab has a facility for creating instances of Java classes and for invoking methods on those instances. There is interest in having an interactive environment for selectively creating instances and invoking methods of Java classes, and Matlab seems to provide this. Unfortunately, quoting from the Mathworks web site regarding restrictions on unloading Java classes If you load a Java class into MATLAB and, sometime later, modify and recompile the class, you must exit and restart MATLAB in order to use the updated class definition. This restriction exists because the Java VM does not provide a way to unload a Java class once it has been loaded. Exiting and restarting MATLAB terminates and restarts the Java VM, which then allows you to load the updated class.

-52No combination of import or clear commands from the Matlab environment works because this restriction is a characteristic of the Java VM, that creating the first object instance of a class loads that class and once a class is loaded it stays loaded. The reasons why Java has to be that way are discussed on the SUN web site. Java has a facility called a class loader which may allow reloading a compiled class according to a pattern. The Java runtime has a default class loader employed when creating an object instance with the new operator. Any object created in that fashion along with the class loaded to create that object go in the namespace of that default class loader, and that namespace is immutable you can add classes to that namespace but you cannot take them away or reload them. Any new class loader that you create with a ClassLoader object establishes a new name space into which you can load classes and create objects. That new name space is also immutable, but every time you create a new ClassLoader object instance, you create a fresh namespace that allows loading a recompiled class and seeing the changes. You can simply overwrite or set to null references to old ClassLoader object instances to let them be garbage collected, but you don't need to force garbage collection because each new ClassLoader object starts from fresh. Objects created with classes loaded by the new class loader can only be accessed using the Java reflection API you can't get at them in the manner of objects created by the new operator because they are in a namespace not accessible by the regular means. The reflection API is the way of inspecting objects and invoking methods on those objects at runtime. This shouldn't be a problem because Matlab invokes methods on Java objects using reflection anyway. But it turns out that for whatever reason, Matlab will not recognize instances of type Object created in a new class loader namespace as valid Java

-53objects. Such an object can be accessed in Java without reflection by having an interface or base class in the default namespace and by casting the class loader-created object to that interface or base class. With Matlab, that doesn't work either. What does work is to create a proxy object in the default namespace to return to Matlab, and to have the proxy object call the corresponding methods of the target object that lives in the class loader namespace. Class Proxy allows creating and configuring a proxy object at runtime. The ingredients are a class called ClassLoad to create an object instance inside the namespace of a new class loader instance as well as create the proxy object to allow Matlab to see that object, a public interface class called InPlace defining the methods Matlab is able to invoke on that object, and a class called SineFilter that implements that interface. ClassLoad does all this at runtime so it does not need to be changed if you substitute a new interface and a new target class for InPlace and SineFilter InPlace and SineFilter may be given different names. InPlace defines an interface which is not reloadable without exiting and restarting Matlab, but once the interface is defined, class SineFilter can be recompiled and the changes will be visible from a Matlab session. If this system is used in a classroom setting, ClassLoad is supplied by the instructor and does not change, InPlace can be supplied by the instructor or by the student to specify an interface, and it can only change between Matlab session, and SineFilter can be supplied by the student as part of completing an assignment. There are also some restrictions on directories where files can go. My setup puts each student account on a virtual network drive F: with F:\matlab the default work directory for student Matlab projects. The files ClassLoad.java and InPlace.java can go in that directory, but the file SineFilter.java cannot go there if I want to be able to reload that

-54class. F:\matlab is on the default Java classpath, and any classes found there will be loaded with the default class loader, defeating what we are trying to accomplish. I put SineFilter.java in F:\matlab\myjava. When compiling it, I need to tell it where class InPlace is found, so from F:\matlab\myjava I invoke javac -classpath .. SineFilter.java which tells the Java compiler to look for the next directory up (.. same as F:\matlab) to find InPlace.class. From the Matlab command window, one may invoke >> cl = ClassLoad('F:\\matlab\\myjava/') to create a reloadable object from a class found in F:\matlab\myjava. Note the peculiar syntax (required by Java) for specifying this directory. Creating an object instance is as simple as >>sinegen = cl.NewInstance('SineFilter') I can create the first 4 samples of a cosine wave of the default frequency of 100 Hz, peak value 1, and phase 0 for a sampling frequency of 22.05 kHz by >>sinegen.Init(1.0/22.05) >>sinegen.Apply([0.0, 0.0, 0.0, 0.0]) The Apply() method takes an input array and adds the sine wave to that array that is why I supply a 4-element 0-valued input array to see 4 samples of the cosine function.

-55I can edit SineFilter.java (say, to change the default magnitude from 1.0 to 2.0), recompile it, repeat >> sinegen = cl.NewInstance('SineFilter) >>sinegen.Init(1.0/22.05) >>sinegen.Apply([0.0, 0.0, 0.0, 0.0]) to see the output at twice the amplitude. By the way, to produce a 2000 Hz sine wave with default amplitude and phase I can invoke >>sinegen.SetParams([2000.0]); >>sinegen.Init(1.0/22.05) >>sinegen.Apply([0.0, 0.0, 0.0, 0.0]) (SetParams() needs to precede Init()), or to produce a 440 Hz sine wave of amplitude 1.5 >>sinegen.SetParams([440.0,1.5]); >>sinegen.Init(1.0/22.05) >>sinegen.Apply([0.0, 0.0, 0.0, 0.0])

-56If we had decided to use any library to connect MATLAB to regular Java classes, following is the procedure: Theres an Java archive named JLab for this purpose. This Java library was developed so that Java developers may have the power of Matlab available to them. The software I write for research lends itself to Java, however, there are many occasions when prototyping Java code with complex algorithms depending on other peoples code can be time consuming and a thankless task. Colleagues and advocates of Matlab encouraged me to take it up, I had to agree, Matlab was a fantastic prototyping environment but any results would still have to be implemented in Java. To overcome this barrier I've created this Java library which allows you to execute commands from Java as if you were at the Matlab prompt and send and receive matrices to and from Matlab. Other similar tools exist, most notably JMatLink developed by Stefan M? JMatLink was inspiration for developing this tool. JMatLink has some memory leak issues, is not compatible with the latest Matlab header files and the Java code is not packaged (meaning all calling code has to reside in the default package, new as of JDK 1.4). JLab has been developed and tested in a Windows environment with the Suns JDK 1.4 and Matlab 7 R14. I have made the method names similar to those used in JMatLink as I expect JMatLink users are the most likely users of this library. This tool requires machine native codes to run, as such the binary distributions made available on this page are for MS Windows platforms only. If this software library is used, it must be referenced as:
@misc{Toft2005, author = "Toft, I. E.", title = "{JLab Connecting Java to Matlab}.

-57\url{http://www.abcxyz123.com/abdullah/index.html}",

howpublished = "NED University of Engineering and Technology, Computers & Information Systems Department., year = "2006"

The method summary of the MatlabEngine class is:


1. void closeEngine()

Closes the Matlab engine for the MatLabEngine instance.


2. int evalString(java.lang.String cmd)

Evaluates a command in the Matlab engine.


3. double[][] getDoubleMatrix(java.lang.String varName)

Retrieve a named matrix as a 2D array of doubles.


4. static MatlabEngine getEngineInstance()

Provides access to the single instance of MatLabEngine.


5. int[][] getIntMatrix(java.lang.String varName)

Retrieve a named matrix as a 2D array of ints.


6. void openEngine()

Opens the Matlab engine for the instance of MatLabEngine.

-587. void putDoubleMatrix(java.lang.String varName, double[][] matrix)

Put a double precision matrix into the Matlab workspace.

8. void putDoubleMatrix(java.lang.String varName, double[][] matrix,

boolean check)

Put a double precision matrix into the Matlab workspace.


9. void putIntMatrix(java.lang.String varName, int[][] matrix)

Put a integer precision matrix into the Matlab workspace.


10. void putIntMatrix(java.lang.String varName, int[][] matrix,

boolean check)

Put a integer precision matrix into the Matlab workspace. The next step is to connect the Java classes to Macromedia Flash.

-59-

6.3 Connecting Java to Macromedia Flash


For an interface, Flash can be a great option; however, for the backend, it's not an option to replace business logic, query management, and security. For robust performance and enterprise capabilities, you need Java. Both Flash and Java promote themselves as a cross-device, cross platform solutions, so they are natural partners. Connecting the two is often up to the Java developer, and to start, you need to know your options on how to get across to Flash.

First, here's a bit about Flash. For application developers, Flash offers a scripting EMCA-based language called ActionScript and a palette of components. The components aren't as robust as Swing, but they cover the basics for common interfaces. Some Flash components are data-aware, and more are offered in the newest version, Flash MX 2004. Developers can write functions and attach them to component events, much like the listener model in Java.

In Flash MX 2004, for the first time, Macromedia offers two versions of their Flash development tool: Flash and Flash Professional. The newest Flash 7 player runs Flash movies developed with either tool. Flash Professional provides extras such as data and web service binding, video integration improvements, and device emulators. Flash Professional also offers a new metaphor for designers in addition to its animation framebased timeline: form or screen based design, which will be helpful to application designers.

Both Flash versions can call Java code contained in JSPs, servlets, beans, or published web services. By default, the Java code must be in the same subdomain as the Flash

-60movie, but with the new version of Flash, you can supply an xml policy file to allow other locations. Of course, the backend Java code can contact other domains.

Java developers using Flash need to know how to do a few common tasks: receiving and sending Flash data and dealing with data type issues. Getting information out of a formlike interface is easy with Flash, and sending information in that interface is often as simple as printing to an out object. Here are some methods to send and receive Flash data from Java: from simplest to complex modes.

6.3.1 URL Encoding Outbound, URL encoding is the simplest model of calling a JSP or servlet. On an event in Flash, you can send a URL with parameters encoded in strings. On the Flash side, you call the URL:
getURL("http://www.server.com/thequiz.jsp?question1=Yes&question2=No", "_blank","POST");

On the java side, you simply parse the request parameters and process them. A drawback is that URL requests need somewhere to load a response page, and it attempts to load it in the same window, in a frame, or in another specified window. You may not want this behavior in your design. It could be ideal to popup a window or to be the final step in an application. For example, for a series of Flash frames, you could save the user's work, and then commit them at the end with a call to a JSP. However, if the Flash session must reload, it loses the user's work.

-61-

6.3.2 LoadVars Object As an improvement over straight URL encoding, you can use a LoadVars object in the ActionScripting model to call Java code. It's a real exchange with a Java backend, allowing sending and receiving. With a LoadVars object, you still have to follow URL encoding, and it's an asynchronous HTTP transmission, but it doesn't unload your Flash movie. You can use a GET or POST. Here's what the ActionScript would look like in a situation where a JSP is called with a LoadVars object. The JSP will make a query based on a showcase id number and return the results to an ActionScript called capture_desc.

showcase_desc = new LoadVars(); showcase_desc.showcase_id = showcase_id; showcase_desc.sendAndLoad("fillShowcasedesc.jsp", showcase_desc, "POST"); showcase_desc.onLoad = capture_desc;

On the java side, the code simply pulls off the parameters on the request object. Session can be maintained if you use session objects in Java. All you have to do is have Java code waiting to collect the data. Here's the line for the JSP called above:
String showcase_id = request.getParameter("showcase_id");

Then you can write data back out to Flash:


out.println("&description" "=" showcase_desc);

-62-

Is it that simple? Yes, it is. On the Flash side, you can get a response back on the LoadVars object on the success of the transaction and handle it, which is a real exchange with Java backend. However, the Flash interface waits for the HTTP response, which can cause apparent hesitation. If you are loading the Flash interface with data, it's helpful to change the Flash interface to indicate "loading" before requesting making the LoadVars request. The returned data to Flash can be loaded into the interface.
function capture_desc(success)

if (success == true) {

showcase_description = "";

showcase_description = showcase_desc.description;

-63-

6.3.3 XML Objects In the same vein as LoadVars, you can send or request XML and manage it through an ActionScript object. Flash can read XML as, basically, a long string set on the response object or XML files. Flash doesn't have a sophisticated parser or verifier, and you have to write ActionScript to load the XML information, but it can be quite handy. In the case of list components, they can be easier to load with an XML object than a LoadVars object. For example, you can format the rows returned on a query as XML to load components.
function capture_showcase(success)

{ if (success == true) {

mainTag = new XML();

elementTag = new XML();

dataList = new Array();

elementList = new Array();

mainTag = this.firstChild;

if (mainTag.nodeName == "dataSet") {

dataList = mainTag.childNodes;

for (i=0; i<=dataList.length; i ) {

if (dataList[i].nodeName == "data") {

elementList = dataList[i].childNodes;

-64for (j=0; j<=elementList.length; j ) {

elementTag = elementList[j];

elementType = elementTag.nodeName;

if (elementType == "showcase_id") {

ID = elementTag.firstChild.nodeValue;}

if (elementType == "showcase_title") {

Show_Title = elementTag.firstChild.nodeValue;}

}lstShowcase.addItem(ID, Show_Title);

}}}}}

lstShowcase.setChangeHandler("showcase_change"); }

Again, the request is an HTTP request. In Flash Professional, you can use an XML connector object to bind XML sources with components. When you use XML objects, the Java code writes out string data. No header on the XML is used. Of course, if you send and receive XML, the Java code takes on the larger role to correct the format and verify it, and in the case of my example, there's more code that is not shown that handled a query and a resultset.
out.print( "" );

out.print( "" title "" );

out.print( "" showcase_id "");

out.print( "");

-65-

6.3.4 XML Sockets Instead of using an XML object through HTTP, you can use a XML-socket persistent connection. This connection is ideal for chat or gaming applications. On the Flash side, you create an XMLSocket object, and when it receives XML, the onXML event is triggered.
myConn = new XMLSocket;

myConn.connect("http://www.myflashserver.com", 1025);

myConn.onXML = processXML;

myConn.close();

In the above case, you'd write an ActiveScript function called processXML that would interpret the incoming XML document.

On the java side, it's easy to communicate through XML Sockets. The code is the same as any simple socket application: create a socket, through port 1024 or higher, and an inputstream and read your data into a stringbuffer: send data out on an outputstream through that socket. The XML documents don't use a DTD. The Flash XML documents are like a long string: it skips the XML header, and the end of file marker is a single byte zero.

XML Sockets do have a couple of drawbacks. The XML transfer is faster this way, but it's the least secure method of communication. Not only that, some firewalls will not

-66allow this sort of connection . If you want security with more speed than LoadVars, you should consider Remoting.

6.3.5 Remoting All of the previous methods basically transfer string information. If you want to transfer more sophisticated data with faster responses, Flash offers a protocol called Remoting. With Remoting, you can directly call methods on Java objects and transfer a variety of data types. Remoting uses HTTP or HTTPS, but Flash uses a proprietary, optimized, binary format, AMF or Action Media Format. To use Remoting, the Flash developer must install additional ActionScript libraries, which are available for free from Macromedia. (The new version of Flash Professional comes with these libraries.)

On the server side, a "gateway" servlet must be installed. (Don't forget to configure a web.xml file.) Macromedia's JRun servers have Remoting installed, but you can install Remoting with any Java server. The gateway servlet acts as a factory for connections to Java objects. The gateway maps incoming data to target Java data types or to an ASObject, and maps outgoing data to target ActionScript data types or to an ASObject. You can use these mappings or you can create an ASObject and handle getting and setting data yourself. You can include Java objects on the ASObjects, but since it is serialized, you can't have objects inside objects that you include. Connecting to Flash through Remoting is simple; the task of designing how you want to handle data casting would be where you'd spend the most time.

-67-

Object Mapping Chart

ActionScript data type


Number (primitive data type) Boolean (primitive data type) String ActionScript (AS) null undefined Ordered array Named array Date object XML object RecordSet

Java data type


Double Boolean String object Map null null ArrayList Map Date org.w3c.dom Document RowSet

On the Flash side, you have to include Remoting, and create a connection, get a service object, and call the java method.
# include "NetServices.as";

netServices.setDefaultGatewayUrl("http://myhost/gateway");

var gatewayConn = Netservices.createGatewayConnection();

var service = gatewayConn.getService("com.mycompany.myJava", this);

service.getCustomers(new handleCustomers());

Results are sent to an ActionScript function, in this case called handleCustomers(), that you would write on the Flash side.

-68The Java code is simple. ASObject must be imported or Serializable be implemented on the class.
import flashgateway.io.ASObject;

public class service {

public ASObject getCustomers(){

ASObject cust = new ASObject("custList");

cust.put("99", "Smith");

cust.put("101", "Anderson");

return cust;

With Remoting, you can use HTTPS for security purposes, but Remoting can expose some security concerns, since it allows direct calls. To avoid problems, set up security policies on your Java classes to prevent wholesale access to your libraries.

Remoting makes development on the Flash side easier, because you can use an ActionScript extension called DataGlue to simply the task of binding and formatting data for the interface. With DataGlue, for example, you can bind recordset objects and its formatting to Flash interface components with a couple of lines of code. With the

-69new Flash Professional 2004, Macromedia extends this idea into the authoring interface, by adding a feature that makes binding components to web services and XML sources as easy as filling out property sheets. In the Component Inspector, the Flash developer can use the Component Inspector and basically click through some tabbed dialog boxes and data bind interface components to published java web services or XML sources, instead of writing lines of ActionScript. They can use it tune the formatting too. This feature is available with or without Remoting. For the Java developer, it's a bit more work to create published web services, but it makes Java methods available to Flash development almost like a tag.

-70-

6.3.6 Remoting with Communication Server If you use Remoting, you can enhance its performance by using Macromedia's Communication Server with it. The server is an additional product to purchase, but if you employ a lot of video and audio, it's worth it. The server offers the most efficient streaming of video and audio to Flash interfaces. On the data side, it caches and polls data for changes, so you'll see good performance improvements in reporting interfaces. The Java coding required is the same as Remoting without the Server, but you may want to revisit your security plans. The Communication Server uses a proprietary protocol: RTMP, which is a TCP/IP protocol, so you can't use HTTPS.

As you can see getting across to Flash with Java is so very easy, and using this combination a great example of MVC architecture. The first step is making a decision on how to connect, and I hope this article's been a good survey for you. There's been a lot of talk about using Coldfusion or PHP to do the job, but Java is a more robust language to architect a backend than PHP or Coldfusion tags. Also the Java APIs are cross platform, which allows you to grow into new markets. For example, as Flash's market continues to grow into the device area, Java's library support would be key to making cross-device Flash applications for hand-helds. Java and Flash are natural partners, and together will make a successful partnership in a growing number of application architectures.

-71-

6.4 Final Option weve opted


So plenty of options and mechanisms are described above, and we said we were going to do it using Matlab, Java and Macromedia Flash. But since we know from previous data that Matlab is built on top of JRE (Java Runtime Environment) and has many built-in JIT Compilers, Interpreter, etc. we can put our Java code directly into Matlab and connect it directly to Macromedia Flash. Weve plenty of options, how to connect Java and Macromedia Flash including (but not limited to): URL Encoding LoadVars Object XML Objects XML Sockets Remoting Remoting with Communication Server

The above options progress from most simple to complex and powerful. Since well be using Matlab as our Java compiler and interpreter we cant use Remoting as well as Remoting with Communitaion Server because they need Java classes running in a server environment like Apache Tomcat, Macromedia JRun or RedHat JBoss. Well be running the Java commands in Matlab IDE so XML Sockets is the best option we can implement. Also among all of the above methods, XML Sockets is the fastest.

-72-

6.5 An illustration of the opted method


A small and simple example of connecting Matlab to Macromedia Flash MX 2004 using Java is shown below. This description will be very simple and fast because all the necessary details are given in this chapter above. Step 1 Start a new Matlab instance. Step 2 Run (in Matlab): import java.net.*; To import all built-in Java Network classes. Step 3 Run (in Matlab): import java.io.*; To import all built-in Java Input/Output classes. Step 4 Run (in Matlab): ssock = ServerSocket(1026); To create a server socket whod be listing on the port number 1026 of the local computer.

-73Step 5 Run (in Matlab): sock = ssock.accept; This command is used to listen to client request of connection. The cursor will be halted here until a client makes a connection and then will return a Socket object in variable sock. Step 6 Now run Macromedia Flash MX 2004. Step 7 Create a new project. Step 8 Draw a Button components (bring it from the components pane).

Step 9 Select this button and change its instance name to objBut.

-74Step 10 Now click the blank scene area and extend the Actions panel shown in the above picture and write the following code: objBut.addEventListener("click", receiveData); myConn = new XMLSocket; function receiveData() { myConn.connect("localhost", 1026); myConn.onData = processData; // myConn.close(); } function processData(src) { trace (src); this.onXML(new XML(src)); }

-75-

Step 11 Run the movie by pressing Ctrl+Enter. Step 12 Click the button labeled Button. Step 13 Now the function (accept) call in Matlab should be released with the output: sock = Socket[addr=/127.0.0.1,port=4484,localport=1026]

-76Step 14 Now create a output stream from Matlab to send data to Flash by running the following command: os = sock.getOutputStream; The stream will be saved in variable os but it can only write one byte of data at a time. In response to this command Matlab says: os = java.net.SocketOutputStream@170f98 So os is an object of java.net.SocketOutputStream class Step 15 To pass entire Strings we need a buffered output stream, i.e. PrintWriter class. To have it, run the following command: out = PrintWriter(os) Matlab says: out = java.io.PrintWriter@1c221b2 Step 16 Define a string to send by a command like: str='<MESSAGE USER="John" TEXT="Hello, my name is John!"/>' Matlab says: str = <MESSAGE USER="John" TEXT="Hello, my name is John!" />

-77Step 17 Now send data to Flash using the socket using: out.print(str) Step 18 Since the stream is buffered flush the stream using: out.flush Step 19 End the output by sending byte zero (0) using: os.write(0) Step 20 Now look at the Flash window, youve got the output:

Following is the snapshot of the Matlabs workspace window at this time:

-78-

CHAPTER 7

CONCLUSION

Finally this discussion can be concluded as the project bears a good relation to various real world applications. Nowadays, Security is the major concern in everyday life such as human machine interfaces and image retrieval. Also, in the field of recognition, Face Recogntion is a new concept and everyday it gains more popularity and importance.

The technique PCA which has been used in this project is also the most common technique and easy to implement. This research is going on for finding better techniques and there have been many good results. In future, this project can be enhanced for better performance.

-79-

BIBLIOGRAPHY

1. www.Wikipedia.org 2. Face Recognition using Principle Component Analysis by Kyungnam Kim , Department of Computer Science, Maryland University, College Park, MD 20742, USA. 3. Face Recognition Handbook by Jim and Dickens 4. A tutorial on Principal Component Analysis by Lindsay I Smith, February 26, 2002 5. Digital Image Processing, Second Edition by Rafael C. Gonzalez, Richard E. Woods, Pearson Education Press 6. Digital Image Processing by Rafael C. Gonzalez, Richard E. Woods, Steven L. Eddins, Pearson Education Press 7. www.Mathswork.com 8. www.google.com

-80-

Anda mungkin juga menyukai