|
assignment 2
2.01.05
blur
Attempt at compressing a video image, by selecting only a specific area of video to show through ellipse shapes.
A spoof on video conferencing.
the code:
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
public class CircleFace extends VideoClient{
float redLower = .35f; //might run the old track skin to get these right for your conditions
float redUpper = .55f;
float greenLower = .26f;
float greenUpper = .35f;
ArrayList patchesOfSkin = new ArrayList();
BoxOfPixels bestHead = null;
public static void main(String[] args) { //this is always the first method
// called by java when you program is run, like set up in processing
frame = new FaceVideoClient();
frame.setTitle(frame.getClass().getName());
frame.setSize(kWidth * 3, kHeight);
frame.setLocation(100, 100);
//frame.setUndecorated(true);
frame.setVisible(true);
}
CircleFace(){
super();
}
public void newFrame() { //called for me by pixel source
long startTime = System.currentTimeMillis();
ps.grabFrame();
patchesOfSkin = new ArrayList();
int[] rgb;
int[] brgb;
int actualWidth = ps.getVideoWidth(); //sometimes the width you get is// slightly different than the// width you asked for
for (int row = 0; row < kHeight ; row++) {
//REPEAT FOR EACH ROW OF PIXELS
int winningStreak =0;
for (int column = 0; column < actualWidth ; column++) {
//REPEAT FOR EACH PIXEL IN THE ROW
rgb = ps.getPixel(column, row);
////
float total = rgb[1] + rgb[2] + rgb[3];
float normalizedRed = rgb[1]/total;
float normalizedGreen = rgb[2]/total;
if (normalizedRed < redUpper && normalizedRed > redLower && normalizedGreen < greenUpper && normalizedGreen > greenLower){
winningStreak++;
// ps.setPixel(column,row,0,255,0,255);
//skin pixel
}else{
//check if this was the end of a good winning streak
if (winningStreak > 4){ // instead of blurring
lookForAHome(column,row,winningStreak);
}
winningStreak = 0; //after all this was a bad pixel
}
}//END FOR EACH PIXEL IN A ROW
}//END FOR EACH ROW OF PIXELS
videoImage = ps.getImage();
//look for the biggest area of skin
int biggestArea = 0;
BoxOfPixels bestHead = null;
for (int headNum = 0; headNum < patchesOfSkin.size(); headNum++) {
BoxOfPixels thisHead = (BoxOfPixels) patchesOfSkin.get(headNum);
Rectangle thisRect = thisHead.getRect();
int thisArea = thisHead.getArea();
if (thisArea > biggestArea){
biggestArea = thisArea;
bestHead = thisHead;
}
}
//send it over the network
if( bestHead != null){
Rectangle bestRect = bestHead.getRect();
// here i need to get a jpg, then paste over the jpg, a pixel array of 50 radius
// from the video
// something like
//faceImage = (head.jpg)
//for ______
//faceImage.getpixel(x,y,50,50);
// this stuff stretches the face and makes a rectangle
BufferedImage faceImage = ps.getImage().getSubimage(bestRect.x,bestRect.y, bestRect.width, bestRect.height) ;
if (faceImage != null && faceImage.getWidth() > 10){
byte[] compressedImageArray = bufferedImageToByteArray(faceImage);
//send the compressed array to the server
mySocket.send(compressedImageArray);
}
}
videoImage = ps.getImage();
repaint();
elapsedTime = System.currentTimeMillis()-startTime;
}
public void lookForAHome(int _col, int _row, int _run){
//look for which existing patch it might belong to.
boolean foundAHome = false;
for (int headNum = 0; headNum < patchesOfSkin.size(); headNum++) {
BoxOfPixels thisHead = (BoxOfPixels) patchesOfSkin.get(headNum);
// check the beginning and end of the run
if (thisHead.isNear(_col, _row) || thisHead.isNear(_col-_run, _row)) {
thisHead.includeItInRect(_col, _row);
foundAHome = true;
break; //don't look through the rest of them
}
}
//if it does not belong to another patch , make a new one
if (foundAHome == false) {
BoxOfPixels thisHead = new BoxOfPixels();
thisHead.includeItInRect(_col, _row);
patchesOfSkin.add( thisHead);
}
}
public void keyPressed(KeyEvent keyEvent) {
String keyString = KeyEvent.getKeyText(keyEvent.getKeyCode()); //change
// regular
// text
if (keyString.equals("Up")) { //use arrow keys to adjust a
redUpper = redUpper + .01f;
redLower = redLower + .01f;
} else if (keyString.equals("Down")) {
redUpper = redUpper - .01f;
redLower = redLower - .01f;
} else if (keyString.equals("Right")) { //use arrow keys to adjust a
greenUpper = greenUpper + .01f;
greenLower = greenLower + .01f; // variable
} else if (keyString.equals("Left")) {
greenUpper = greenUpper - .01f;
greenLower = greenLower - .01f;
}else{
//ask your ancestor if he wants a crack as this as well
super.keyPressed( keyEvent);
}
put( redLower + "< Red >" + redUpper + " " + greenLower + "< Green >" + greenUpper);
}
}
back
|