Video Issue with MBP internal camera
in
Core Library Questions
•
2 years ago
Hello!
I'm trying to update and run an old code I played around with, and when I try to run it, the camera turns on, and then off, leaving me with a blank screen instead of the result.
Here's the code:
- // Game of life parameters
- float density = 0.08;
- int[][][] world;
- // parameters:
- int finalX = 640;
- int finalY = 480;
- int vidX = 640;
- int vidY = 480;
- int vidpix = vidX*vidY;
- int[] oldpixels = new int [vidpix];
- int threshold = 170;
- boolean fromRef= false;
- int glife_vidX = finalX;
- int glife_vidY = finalY;
- // static variables:
- boolean newFrame;
- boolean once= false;
- int targets;
- float scal = finalX/vidX;
- //particle variables
- int num=35000;
- int counter=0;
- int flow=1;
- int life=10;
- int speed=128;
- int nodes=2;
- particle[] parts=new particle[num];
- particle[] glife_parts=new particle[num];
- int glife_counter = 0;
- int counter3 = 0;
- float alpha;
- import processing.video.*;
- Capture video;
- //////////////////////////////////
- void setup() {
- size(finalX, finalY);
- video = new Capture(this,vidX, vidY, 30);
- //smooth();
- alpha = 0;
- for (int i=0; i<num; i++){
- parts[i]=new particle(num, 1000, 1000, 0, 0, 0, speed, nodes, 255);
- }
- for (int i=0; i<num; i++){
- glife_parts[i]=new particle(num, 1000, 1000, 0, 0, 0, speed, nodes, 255);
- }
- //background(0);
- frameRate(16);
- glife_vidX = width;
- glife_vidY = height;
- world = new int[glife_vidX+1][glife_vidY+1][2];
- //stroke(255, 50, 0);
- // Set random cells to 'on'
- for (int i = 0; i < (glife_vidX * glife_vidY * density * threshold); i++) {
- world[(int)random(glife_vidX)][(int)random(glife_vidY)][1] = 1;
- }
- }
- void captureEvent(Capture video){
- video.read();
- }
- void loop() {
- background(0);
- if (!once || keyPressed){ // if a key is pressed, get a reference frame
- for(int i=0; i<vidpix; i++){
- oldpixels[i]=video.pixels[i];
- }
- once= true;
- }
- //if (counter3 % 10 == 0)
- //{
- if (newFrame){
- analyzeChangeVideo();
- }
- //}
- counter3++;
- if (!fromRef){ // if reference mode = every frame
- for(int i=0; i<vidpix; i++){
- oldpixels[i]=video.pixels[i]; // after processing, new pixels are copied as "old" if fromRef=true
- }
- // if the altering func is class-style, update it here.
- }
- playGameOfLife();
- for (int x = 0; x < glife_vidX ; x++) {
- for (int y = 0; y < glife_vidY ; y++) {
- if (world[x][y][1] == 1)
- {
- glife_parts[glife_counter].positionX[0]= x;
- glife_parts[glife_counter].positionY[0]= y;
- glife_parts[glife_counter].alpha= 255;
- if (glife_counter<num-1){
- glife_counter++;
- } else {
- glife_counter=0;
- }
- }
- }
- }
- for (int i=0; i<num; i++){
- //parts[i].update();
- //parts[i].render();
- glife_parts[i].render();
- }
- }
- void videoEvent(){
- if (!newFrame){
- newFrame = true;
- }
- }
- void analyzeChangeVideo(){
- targets = 0;
- int x;
- int y;
- for(int i=0; i<vidpix; i++){
- float r = int(red(video.pixels[i]));
- float g = int(green(video.pixels[i]));
- float b = int(blue(video.pixels[i]));
- float rc = abs(r-red(oldpixels[i]));
- float gc = abs(g-green(oldpixels[i]));
- float bc = abs(b-blue(oldpixels[i]));
- float change = rc+gc+bc;
- x=int(i%vidX*scal); //the x location of the pixel that changed
- y=int(i/vidX*scal); //the y location of the pixel that changed
- //inverter - for tv displays
- x=(finalX-x);
- if (change>threshold){ // this is where you decide what to do with the pixels
- //do something here. you have x, y.
- for (int k=0; k<flow; k++){
- parts[counter].life=life;
- parts[counter].alpha= 255;
- for (int l=0; l<parts[counter].nodes; l++){
- // parts[counter].positionX[l]= x;
- // parts[counter].positionY[l]= y;
- glife_parts[glife_counter].positionX[0]= x;
- glife_parts[glife_counter].positionY[0]= y;
- }
- // Set changed cells to 'on'
- //for (int z = 0; z < glife_vidX * glife_vidY * density; z++) {
- //}
- if (counter<num-1){
- counter++;
- } else {
- counter=0;
- }
- if (glife_counter<num-1){
- glife_counter++;
- } else {
- glife_counter=0;
- }
- }
- world[x][y][1] = 1;
- //print("X: " + x);
- //rectMode(CENTER_DIAMETER);
- //rect(x,y,1,1);
- }
- }
- }
- class particle{
- float x, y, px, py;
- float xspeed, yspeed, xForce, yForce;
- int xSpeedLimit=30;
- int ySpeedLimit=30;
- int life;
- int origLife;
- float radCount=random(6.27);
- float factor;
- int nodes;
- float[] positionX;
- float[] positionY;
- float radSpeed= 50f;
- int dir1, dir2;
- float alpha;
- particle(int numpart, float xin, float yin, int lif, float xp, float yp, int sp, int nds, float ap){
- nodes= nds;
- positionX= new float[nodes];
- positionY= new float[nodes];
- for (int i=0; i<nodes; i++){
- positionX[i]= xin;
- positionY[i]= yin;
- }
- x=positionX[0];
- y=positionY[0];
- speed=sp;
- alpha=ap;
- xForce=random(speed)-speed/2;
- yForce=random(speed)-speed/2;
- life=lif-int(random(100));
- origLife=lif;
- factor=random(speed*2)-speed;
- factor= 3;
- if (random(2)>1){
- dir1= 1;
- }else{
- dir1= -1;
- }
- if (random(2)>1){
- dir2= 1;
- }else{
- dir2= -1;
- }
- }
- void update(){
- if (alpha>0){
- x= positionX[0];
- y= positionY[0];
- float xDif, yDif;
- float r2, rot, grav;
- xspeed= xForce+int(factor*(cos(radCount)));
- yspeed= yForce-int(factor*(sin(radCount)));
- //move the points according to the speeds
- x+=xspeed;//+random(3);
- y+=yspeed;//+random(3);
- positionX[0]= x;
- positionY[0]= y;
- radCount+=radSpeed;
- life--;
- alpha-=5;
- }else{
- for (int i=0; i<nodes; i++){
- positionX[i]= 1000;
- positionY[i]= 1000;
- }
- xForce=random(speed)-speed/2;
- yForce=random(speed)-speed/2;
- life=origLife;
- alpha=255;
- }
- }
- void render(){
- if(alpha>0){
- for (int i=0; i<nodes-1; i++){
- //stroke(255, 50, 0);
- //point(positionX[i], positionY[i]);
- rect(positionX[i], positionY[i],1,1);
- stroke(255, 50, 0);
- }
- for (int i=0; i<nodes-1; i++){
- positionX[i+1]= positionX[i];
- positionY[i+1]= positionY[i];
- }
- }
- }
- }
- // Count the number of adjacent cells 'on'
- int neighbors(int x, int y)
- {
- return world[(x + 1) % glife_vidX][y][0] +
- world[x][(y + 1) % glife_vidY][0] +
- world[(x + glife_vidX - 1) % glife_vidX][y][0] +
- world[x][(y + glife_vidY - 1) % glife_vidY][0] +
- world[(x + 1) % glife_vidX][(y + 1) % glife_vidY][0] +
- world[(x + glife_vidX - 1) % glife_vidX][(y + 1) % glife_vidY][0] +
- world[(x + glife_vidX - 1) % glife_vidX][(y + glife_vidY - 1) % glife_vidY][0] +
- world[(x + 1) % glife_vidX][(y + glife_vidY - 1) % glife_vidY][0];
- }
- void playGameOfLife()
- {
- // Drawing and update cycle
- for (int x = 0; x < glife_vidX; x=x+1) {
- for (int y = 0; y < glife_vidY; y=y+1) {
- if (world[x][y][1] == 1)
- {
- world[x][y][0] = 1;
- //rectMode(CENTER_DIAMETER);
- //rect(x,y,1,1);
- }
- if (world[x][y][1] == -1)
- {
- world[x][y][0] = 0;
- }
- world[x][y][1] = 0;
- }
- }
- // Birth and death cycle
- for (int x = 0; x < glife_vidX; x=x+1) {
- for (int y = 0; y < glife_vidY; y=y+1) {
- int count = neighbors(x, y);
- if (count == 3 && world[x][y][0] == 0)
- {
- world[x][y][1] = 1;
- }
- if ((count < 2 || count > 3) && world[x][y][0] == 1)
- {
- world[x][y][1] = -1;
- }
- }
- }
- }
This code is a bit of a mashup between 2 other bits of code. It was originally "written" in processing 068, but i'm trying to get it working again. My quicktime is updated and my camera works with the basic video capture example.
Any help would be very much appreciated.
Thanks in advance.
Paulo Azevedo
1