Beispiel #1
0
//--------------------------------------------------------------
void ofApp::setup(){
    //we can now get back a list of devices.
    vector<ofVideoDevice> devices = vidGrabber.listDevices();
    
    for(int i = 0; i < devices.size(); i++){
        cout << devices[i].id << ": " << devices[i].deviceName;
        if( devices[i].bAvailable ){
            cout << endl;
        }else{
            cout << " - unavailable " << endl;
        }
    }
    vidGrabber.setDeviceID(1);

    
    
    vidGrabber.setVerbose(true);
    vidGrabber.initGrabber(width, height);
    recorded = (unsigned char*)malloc(3 * height * width * record_size);
    tmp = (unsigned char*)malloc(3 * height * width);
    back = (unsigned char*)malloc(3 * height * width);
    merged = (unsigned char*)malloc(3 * height * width);
    show = vidGrabber.getPixels();
    udpConnection.Create();
    udpConnection.Bind(1511);
    udpConnection.SetNonBlocking(true);
    for (int i = 0; i < trackers_cnt; i++) {
        tracker t = tracker(i);
        trackers.push_back(t);
    }
    outfile.open("/users/yui/desktop/yellow_imgs/teacher/log.txt", std::ios_base::app);
    
}
void ofApp::setup() {
    
    ofEnableSmoothing();
    ofEnableAlphaBlending();
    ofSetFrameRate(60);
    ofSetVerticalSync(true);
    ofEnableDepthTest();
    ofEnableAntiAliasing();

    memset( dmxData_, 0, DMX_DATA_LENGTH );
    
    //open the device
    dmxInterface_ = ofxGenericDmx::createDevice(DmxDevice::DMX_DEVICE_RAW);
    bool opened = dmxInterface_->open();
    if ( dmxInterface_ == 0 || !opened ) {
        printf( "No FTDI Device Found\n" );
    } else {
        printf( "isOpen: %i\n", dmxInterface_->isOpen() );
    }
    
    printf("ofxGenericDmx addon version: %s.%s\n", ofxGenericDmx::VERSION_MAJOR, ofxGenericDmx::VERSION_MINOR);
    
    std::string file = "Lightweave_loops2.json";
    std::string columnsFile = "Lightweave_columns2.json";
    std::string facesFile = "Lightweave_faces2.json";
    
    bool parsingSuccessful = result.open(file);
    
    bool parsingSuccessfulColumn = columnGeometry.open(columnsFile);

    bool parsingSuccessfulFaces = faceGeometry.open(facesFile);

    
    for (int region = 0; region < 6; region++) {
        string blah = "region" + ofToString(region);
        for (int rings = 0; rings < result[blah].size(); rings++) {
            string ring = "ring" + ofToString(rings);
            for (int pointPos = 0; pointPos < 3; pointPos++) {
                string point = "point" + ofToString(pointPos);
            }
        }
    }
        
    //setupUDP();
    
    camWidth = 320;
    camHeight = 240;

    vector<ofVideoDevice> devices = vidGrabber.listDevices();
    for (int i = 0; i < devices.size(); i++) {
        if (devices[i].bAvailable) {
            ofLogNotice() << devices[i].id << ": " << devices[i].deviceName;
        } else {
            ofLogNotice() << devices[i].id << ": " << devices[i].deviceName << " - unavailable ";
        }
    }
    
    for (int i = 0; i < devices.size(); i++) {
        if (!devices[i].deviceName.find("USB")) {
            cout << devices[i].id << endl;
            pcCams.push_back(devices[i].id);
        }
    }
    

    vidGrabber.setDeviceID(pcCams[0]);
//    vidGrabber.setDeviceID(0);

    vidGrabber.initGrabber(320,240);
    
    vidGrabber1.setDeviceID(pcCams[1]);
//    vidGrabber1.setDeviceID(0);

    vidGrabber1.initGrabber(320,240);
    
    colorImg1.allocate(320,240);
    grayImage1.allocate(320,240);
    grayBg1.allocate(320,240);
    grayDiff1.allocate(320,240);
    
    colorImg.allocate(320,240);
    grayImage.allocate(320,240);
    grayBg.allocate(320,240);
    grayDiff.allocate(320,240);
    
    bLearnBackground = true;
    bLearnBackground1 = true;
    threshold = 80;
    drawOne = false;
    
    bottomSwarm.a = 1.1f;
    bottomSwarm.b = (curWidth/4.0);
    bottomSwarm.c = 100.0;
    bottomSwarm.bVel = 1.0;
    
    xPos = 0;
    yPos = 0;
    zPos = 0;
    
    cam.setPosition(result["region0"]["ring0"]["point0"][0].asFloat(),result["region0"]["ring0"]["point0"][1].asFloat(),result["region0"]["ring0"]["point0"][2].asFloat());
    cam.lookAt(ofVec3f(result["region0"]["ring1"]["point0"][0].asFloat(),result["region0"]["ring1"]["point0"][1].asFloat(),result["region0"]["ring1"]["point0"][2].asFloat()));
    cam.rotate(ofRadToDeg(PI/2), 1.0, 0.0, 0.0);
    cam.setFov(32.0);
    
    sphereZPos = 25.9297;
    sphereXPos = 364.928;
    
    for (int i = 0; i < 20; i++) {
        spheresXPos[i] = ofRandom(result["region0"]["ring0"]["point0"][0].asFloat()-500, result["region0"]["ring0"]["point0"][0].asFloat()+500.0);
        spheresZPos[i] = ofRandom(result["region0"]["ring0"]["point0"][2].asFloat()-100.0, result["region0"]["ring0"]["point0"][2].asFloat()+100.0);
    }
    
    /* LIGHTING */
    ofSetSmoothLighting(true);

    pointLight.setDiffuseColor( ofColor(0.f, 255.f, 0.f));
    
    pointLight.setSpecularColor( ofColor(255.f, 255.f, 255.f));
    pointLight.setPosition(result["region0"]["ring0"]["point0"][0].asFloat(),result["region0"]["ring0"]["point0"][1].asFloat(),result["region0"]["ring0"]["point0"][2].asFloat());
    
    material.setShininess( 64 );
    
    colorHue = ofRandom(0, 250);
    colorHue2 = ofRandom(0, 250);
    
    lightColor.setBrightness( 180.f );
    lightColor.set(250,250,210);
    
    materialColor.setBrightness(250.f);
    materialColor.set(100,100,100);
    
    lightColor.setHue(colorHue);
    pointLight.setDiffuseColor(lightColor);
    
    materialColor.setHue(colorHue);
    material.setSpecularColor(materialColor);
    
    
    materialColor.set(255.0,0.0,0.0);
    columnMaterial.setSpecularColor(materialColor);
    
    materialColor.set(55.0,55.0,55.0);
    peopleMaterial.setSpecularColor(materialColor);
    
    cameraColor1.set(0.0, 0.0, 255.0);
    cameraColor2.set(0.0, 0.0, 255.0);
    columnColor.set(255, 0, 0);
    activeColor.set(0.0,0.0,255.0);

}
void ofApp::draw() {
    
    ofBackground(0.0, 0.0, 0.0);
    
    ofSetColor(255.0,255.0,255.0);
    ofFill();
    for (int i = 0; i < 10; i++) {
        ofDrawBitmapString(verbalInstructions[i], 10, (i*20)+600);
    }
    
    if (cameraInfoIsOn) {
        ofSetColor(255.0, 255.0, 255.0);
        ofFill();
        
        /* drawing cameras */
        ofSetColor(255.0,0.0,0.0);
        ofDrawBitmapString("Active column", 10, 30+10);
        ofSetColor(0.0,0.0,255.0);
        ofDrawBitmapString("Active camera #", 10, 50+10);
        ofSetColor(255,105,180);
        ofDrawBitmapString("# of Pedestrians", 10, 60+20);
        ofSetColor(255.0, 255.0, 255.0);
        int cnt = 0;
        for (int i = 300-70; i <= 300+700; i+=70) {
            if (cnt == 4 || cnt == 5 ) {
                ofSetColor(cameraColor1);
                ofDrawBitmapString("["+ofToString(cnt+1)+"]", i+22, 50+10);
                ofSetColor(255,105,180);
                ofDrawBitmapString((int)micLevelsTopNew[cnt], i+35, 60+10);
                cout << i+35 << endl;
                ofSetColor(255.0, 255.0, 255.0);
            } else {
                if (simulationIsOn) {
                    ofSetColor(255,105,180);
                } else {
                    ofSetColor(100,100,100);
                }
                ofDrawBitmapString((int)micLevelsTopNew[cnt], i+35, 60+10);
                cout << i+35 << endl;
                ofSetColor(255.0, 255.0, 255.0);
                ofDrawBitmapString("["+ofToString(cnt+1)+"]", i+22, 50+10);
            }
            cnt++;
        }
        cout << "" << endl;
        
        /* Draw Columns */
        cnt = 0;
        ofFill();
        ofSetColor(100.0, 100.0, 100.0);
        for (int i = 300; i <= 300+700; i+=70) {
            if (cnt == 4) {
                ofSetColor(columnColor);
                ofDrawCircle(i, 45, 7);
                ofSetColor(100.0, 100.0, 100.0);
            } else {
                ofDrawCircle(i, 45, 7);
            }
            cnt++;
        }
        int max_pos = 0;
        int max_element = -1000;
        for (int i = 0; i < 12; i++) {
            if (micLevelsTopNew[i] > max_element) {
                max_pos = i;
                max_element = micLevelsTopNew[i];
            }
        }
        //ofVec2f btm = absColumnPositionTop[max_pos];
        ofVec2f btm = cameraPositionsTop[max_pos];
        ofVec2f desired =  btm - swarmPosition;
        float d = sqrt((desired.x*desired.x) + (desired.y+desired.y));
        float r = ofMap(ofClamp(d, 0.0, 700.0), 0.0, 700.0, 25.0, 76.5);
        ofColor swarmColor = ofColor(255.0, 0.0, 255.0);
        ofSetColor(swarmColor);
        ofDrawRectangle(swarmPosition.x, 45, 10, 50);
        ofSetColor(columnColor);
        ofFill();
        ofDrawCircle(578, 270, 14);
    
        
        for (int i = 0; i < contourFinder.nBlobs; i++){
            contourFinder.blobs[i].draw(238, 150);
            
            ofSetColor(255);
            if(contourFinder.blobs[i].hole){
                ofDrawBitmapString("hole",
                                   contourFinder.blobs[i].boundingRect.getCenter().x + 360,
                                   contourFinder.blobs[i].boundingRect.getCenter().y + 540);
            }
        }
        ofNoFill();
        ofSetColor(cameraColor1);
        ofDrawBitmapString("Camera 5", 238, 140);
        ofDrawRectangle(238, 150, 340, 240);
        ofSetColor(255,255,255,50.0);
        colorImg.draw(238, 150);

        
        for (int i = 0; i < contourFinder1.nBlobs; i++){
            contourFinder1.blobs[i].draw(578, 150);
            
            // draw over the centroid if the blob is a hole
            ofSetColor(255);
            if(contourFinder1.blobs[i].hole){
                ofDrawBitmapString("hole",
                                   contourFinder1.blobs[i].boundingRect.getCenter().x + 360,
                                   contourFinder1.blobs[i].boundingRect.getCenter().y + 540);
            }
        }
        ofNoFill();
        ofSetColor(cameraColor2);
        ofDrawBitmapString("Camera 6", 578, 140);
        ofDrawRectangle(578, 150, 340, 240);
        ofSetColor(255,255,255,50.0);
        colorImg1.draw(578, 150);
    }
    
    ofEnableLighting();
    pointLight.enable();
    material.begin();
    ofPushMatrix();
    cam.begin();
    peopleMaterial.begin();
    if (simulationIsOn) {
        for (int i = 0; i < 20; i++) {
            ofSpherePrimitive cyl;
            cyl.setPosition(spheresXPos[i]+=ofRandom(1.5), spheresZPos[i], 10.9297);
            cyl.set(10, 10);
            cyl.draw();
            if (spheresXPos[i] >= 0.0) {
                spheresXPos[i] = ofRandom(result["region0"]["ring0"]["point0"][0].asFloat()-500, result["region0"]["ring0"]["point0"][0].asFloat()+500.0);
            }
        }
    }
    peopleMaterial.end();
    ofSetColor(100.0);
    ofFill();
    
    int ct = 0;
    for (int region = 0; region < 3; region++) {
        string reg = "region" + ofToString(region);
        for (int pointPos = 0; pointPos < 4; pointPos++) {
            if (region == 1 && pointPos == 3) {
            } else {
                string point = "point" + ofToString(pointPos);
                ofCylinderPrimitive cyl;
                cyl.setPosition(columnGeometry[reg][point][0].asFloat(), columnGeometry[reg][point][1].asFloat(), columnGeometry[reg][point][2].asFloat()-90);
                cyl.set(2.0, 130.0);
                cyl.rotate(90, ofVec3f(1.0, 0.0, 0.0));
                if (ct == 4) {
                    columnMaterial.begin();
                    cyl.draw();
                    columnMaterial.end();
                } else {
                    cyl.draw();
                }
                ct++;
            }
        }
    }
    material.end();
    ofDisableLighting();


    newDrawRegion(gaussianBottom, 0, 3, false);
    
    ofSetColor(155.0, 155.0, 155.0);
    ofFill();
    for (int face = 0; face < 5; face++) {
        string fac = "face" + ofToString(face);
        ofPoint p1;
        ofPoint p2;
        ofPoint p3;
        ofPoint p4;
        p1.set(ofVec3f(faceGeometry[fac]["point0"][0].asFloat(),faceGeometry[fac]["point0"][1].asFloat(),faceGeometry[fac]["point0"][2].asFloat()));
        p2.set(ofVec3f(faceGeometry[fac]["point1"][0].asFloat(),faceGeometry[fac]["point1"][1].asFloat(),faceGeometry[fac]["point1"][2].asFloat()));
        p3.set(ofVec3f(faceGeometry[fac]["point2"][0].asFloat(),faceGeometry[fac]["point2"][1].asFloat(),faceGeometry[fac]["point2"][2].asFloat()));
        p4.set(ofVec3f(faceGeometry[fac]["point3"][0].asFloat(),faceGeometry[fac]["point3"][1].asFloat(),faceGeometry[fac]["point3"][2].asFloat()));
        ofDrawLine(p1, p2);
        ofDrawLine(p2, p3);
        ofDrawLine(p3, p4);
        ofDrawLine(p4, p1);
    }
    cam.end();
    ofPopMatrix();

    sendToDMX();
    
    vector<ofVideoDevice> devices = vidGrabber.listDevices();

    
    //ofDrawBitmapString(pcCams[0], 400, 50);
    //ofDrawBitmapString(pcCams[1], 400, 100);
}