// get the first camera found void cameraGet(tCamera* camera) { tPvUint32 count, connected; tPvCameraInfo list; count = PvCameraList(&list, 1, &connected); if (count == 1) { camera->UID = list.UniqueId; cout << "got camera " << list.SerialString << endl; } else throw "failed to find a camera\n"; }
// get the first camera found bool CameraGet(tCamera* Camera) { tPvUint32 count,connected; tPvCameraInfo list; count = PvCameraList(&list,1,&connected); if(count == 1) { Camera->UID = list.UniqueId; printf("got camera %s\n",list.SerialString); return true; } else return false; }
void init() { CHECK_ERR( PvInitialize(), "Failed to initialize Prosilica API" ); // Spend up to 1s trying to find a camera. Finding no camera is not // an error; the user may still be able open one by IP address. for (int tries = 0; tries < 5; ++tries) { cameraNum = PvCameraList(cameraList, MAX_CAMERA_LIST, NULL); if (cameraNum) return; usleep(200000); } /// @todo Callbacks for add/remove camera? }
void ListCameras() { tPvCameraInfo cameraList[MAX_CAMERA_LIST]; unsigned long cameraNum = 0; unsigned long cameraRle; // first, get list of reachable cameras. cameraNum = PvCameraList(cameraList,MAX_CAMERA_LIST,NULL); // keep how many cameras listed are reachable cameraRle = cameraNum; // then we append the list of unreachable cameras. if (cameraNum < MAX_CAMERA_LIST) { cameraNum += PvCameraListUnreachable(&cameraList[cameraNum], MAX_CAMERA_LIST-cameraNum, NULL); } if(cameraNum) { // and display them for(unsigned int i=0;i<cameraNum;i++) { if(i < cameraRle) printf("%s - %7s - Unique ID = %lu\n",cameraList[i].SerialString, cameraList[i].DisplayName, cameraList[i].UniqueId); else printf("%s - %7s - Unique ID = %lu (*)\n",cameraList[i].SerialString, cameraList[i].DisplayName, cameraList[i].UniqueId); } if(cameraNum != cameraRle) printf("(*) camera is not reachable\n"); } else printf("sorry, no camera was detected. Is there any plugged?\n"); }
//-------------------------------------------------------------------- void ofxVideoGrabberPvAPI::listDevices(){ // lazy initialization of the Prosilica API if( !bPvApiInitiated ){ int ret = PvInitialize(); if( ret == ePvErrSuccess ) { ofLog(OF_LOG_VERBOSE, "PvAPI initialized"); } else { ofLog(OF_LOG_ERROR, "unable to initialize PvAPI"); return; } bPvApiInitiated = true; } ofLog(OF_LOG_NOTICE, "-------------------------------------"); // get a camera from the list tPvUint32 count, connected; tPvCameraInfo list[maxConcurrentCams]; count = PvCameraList( list, maxConcurrentCams, &connected ); if( connected > maxConcurrentCams ) { ofLog(OF_LOG_NOTICE, "more cameras connected than will be listed"); } if(count >= 1) { ofLog(OF_LOG_NOTICE, "listing available capture devices"); for( int i=0; i<count; ++i) { ofLog(OF_LOG_NOTICE, "got camera %s - %s - id: %i\n", list[i].DisplayName, list[i].SerialString, list[i].UniqueId); } } else { ofLog(OF_LOG_NOTICE, "no cameras found"); } ofLog(OF_LOG_NOTICE, "-------------------------------------"); }
// get the first camera found bool CameraGrab() { return PvCameraList(&(GCamera.Info),1,NULL) >= 1; }
// Initialize camera input bool CvCaptureCAM_PvAPI::open( int index ) { tPvCameraInfo cameraList[MAX_CAMERAS]; tPvCameraInfo camInfo; tPvIpSettings ipSettings; if (PvInitialize()) { } //return false; Sleep(1000); //close(); int numCameras=PvCameraList(cameraList, MAX_CAMERAS, NULL); if (numCameras <= 0 || index >= numCameras) return false; Camera.UID = cameraList[index].UniqueId; if (!PvCameraInfo(Camera.UID,&camInfo) && !PvCameraIpSettingsGet(Camera.UID,&ipSettings)) { /* struct in_addr addr; addr.s_addr = ipSettings.CurrentIpAddress; printf("Current address:\t%s\n",inet_ntoa(addr)); addr.s_addr = ipSettings.CurrentIpSubnet; printf("Current subnet:\t\t%s\n",inet_ntoa(addr)); addr.s_addr = ipSettings.CurrentIpGateway; printf("Current gateway:\t%s\n",inet_ntoa(addr)); */ } else { fprintf(stderr,"ERROR: could not retrieve camera IP settings.\n"); return false; } if (PvCameraOpen(Camera.UID, ePvAccessMaster, &(Camera.Handle))==ePvErrSuccess) { //Set Pixel Format to BRG24 to follow conventions /*Errcode = PvAttrEnumSet(Camera.Handle, "PixelFormat", "Bgr24"); if (Errcode != ePvErrSuccess) { fprintf(stderr, "PvAPI: couldn't set PixelFormat to Bgr24\n"); return NULL; } */ tPvUint32 frameWidth, frameHeight, frameSize; unsigned long maxSize; char pixelFormat[256]; PvAttrUint32Get(Camera.Handle, "TotalBytesPerFrame", &frameSize); PvAttrUint32Get(Camera.Handle, "Width", &frameWidth); PvAttrUint32Get(Camera.Handle, "Height", &frameHeight); PvAttrEnumGet(Camera.Handle, "PixelFormat", pixelFormat,256,NULL); maxSize = 8228; //PvAttrUint32Get(Camera.Handle,"PacketSize",&maxSize); if (PvCaptureAdjustPacketSize(Camera.Handle,maxSize)!=ePvErrSuccess) return false; if (strcmp(pixelFormat, "Mono8")==0) { grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 1); grayframe->widthStep = (int)frameWidth; frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3); frame->widthStep = (int)frameWidth*3; Camera.Frame.ImageBufferSize = frameSize; Camera.Frame.ImageBuffer = grayframe->imageData; } else if (strcmp(pixelFormat, "Mono16")==0) { grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 1); grayframe->widthStep = (int)frameWidth; frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 3); frame->widthStep = (int)frameWidth*3; Camera.Frame.ImageBufferSize = frameSize; Camera.Frame.ImageBuffer = grayframe->imageData; } else if (strcmp(pixelFormat, "Bgr24")==0) { frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3); frame->widthStep = (int)frameWidth*3; Camera.Frame.ImageBufferSize = frameSize; Camera.Frame.ImageBuffer = frame->imageData; } else return false; // Start the camera PvCaptureStart(Camera.Handle); // Set the camera to capture continuously if(PvAttrEnumSet(Camera.Handle, "AcquisitionMode", "Continuous")!= ePvErrSuccess) { fprintf(stderr,"Could not set Prosilica Acquisition Mode\n"); return false; } if(PvCommandRun(Camera.Handle, "AcquisitionStart")!= ePvErrSuccess) { fprintf(stderr,"Could not start Prosilica acquisition\n"); return false; } if(PvAttrEnumSet(Camera.Handle, "FrameStartTriggerMode", "Freerun")!= ePvErrSuccess) { fprintf(stderr,"Error setting Prosilica trigger to \"Freerun\""); return false; } return true; } fprintf(stderr,"Error cannot open camera\n"); return false; }
// Initialize camera input bool CvCaptureCAM_PvAPI::open( int index ) { tPvCameraInfo cameraList[MAX_CAMERAS]; tPvCameraInfo camInfo; tPvIpSettings ipSettings; if (PvInitialize()) { } //return false; Sleep(1000); //close(); int numCameras=PvCameraList(cameraList, MAX_CAMERAS, NULL); if (numCameras <= 0 || index >= numCameras) return false; Camera.UID = cameraList[index].UniqueId; if (!PvCameraInfo(Camera.UID,&camInfo) && !PvCameraIpSettingsGet(Camera.UID,&ipSettings)) { /* struct in_addr addr; addr.s_addr = ipSettings.CurrentIpAddress; printf("Current address:\t%s\n",inet_ntoa(addr)); addr.s_addr = ipSettings.CurrentIpSubnet; printf("Current subnet:\t\t%s\n",inet_ntoa(addr)); addr.s_addr = ipSettings.CurrentIpGateway; printf("Current gateway:\t%s\n",inet_ntoa(addr)); */ } else { fprintf(stderr,"ERROR: could not retrieve camera IP settings.\n"); return false; } if (PvCameraOpen(Camera.UID, ePvAccessMaster, &(Camera.Handle))==ePvErrSuccess) { tPvUint32 frameWidth, frameHeight; unsigned long maxSize; // By Default, try to set the pixel format to Mono8. This can be changed later // via calls to setProperty. Some colour cameras (i.e. the Manta line) have a default // image mode of Bayer8, which is currently unsupported, so Mono8 is a safe bet for // startup. monocrome = (PvAttrEnumSet(Camera.Handle, "PixelFormat", "Mono8") == ePvErrSuccess); PvAttrUint32Get(Camera.Handle, "Width", &frameWidth); PvAttrUint32Get(Camera.Handle, "Height", &frameHeight); // Determine the maximum packet size supported by the system (ethernet adapter) // and then configure the camera to use this value. If the system's NIC only supports // an MTU of 1500 or lower, this will automatically configure an MTU of 1500. // 8228 is the optimal size described by the API in order to enable jumbo frames maxSize = 8228; //PvAttrUint32Get(Camera.Handle,"PacketSize",&maxSize); if (PvCaptureAdjustPacketSize(Camera.Handle,maxSize)!=ePvErrSuccess) return false; resizeCaptureFrame(frameWidth, frameHeight); return startCapture(); } fprintf(stderr,"Error cannot open camera\n"); return false; }
// Initialize camera input bool CvCaptureCAM_PvAPI::open( int index ) { tPvCameraInfo cameraInfo[MAX_CAMERAS]; if (PvInitialize()) return false; usleep(250000); //close(); int numCameras = PvCameraList(cameraInfo, MAX_CAMERAS, NULL); if (numCameras <= 0 || index >= numCameras) return false; Camera.UID = cameraInfo[index].UniqueId; if (PvCameraOpen(Camera.UID, ePvAccessMaster, &(Camera.Handle))==ePvErrSuccess) { //Set Pixel Format to BRG24 to follow conventions Errcode = PvAttrEnumSet(Camera.Handle, "PixelFormat", "Bgr24"); if (Errcode != ePvErrSuccess) { fprintf(stderr, "PvAPI: couldn't set PixelFormat to Bgr24\n"); return NULL; } tPvUint32 frameWidth, frameHeight, frameSize; PvAttrUint32Get(Camera.Handle, "TotalBytesPerFrame", &frameSize); PvAttrUint32Get(Camera.Handle, "Width", &frameWidth); PvAttrUint32Get(Camera.Handle, "Height", &frameHeight); // Create an image (24 bits RGB Color image) frame = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 3); frame->widthStep = frameWidth*3; Camera.Frame.ImageBufferSize = frameSize; Camera.Frame.ImageBuffer = frame->imageData; // Start the camera PvCaptureStart(Camera.Handle); // Set the camera to capture continuously if(PvAttrEnumSet(Camera.Handle, "AcquisitionMode", "Continuous")!= ePvErrSuccess) { fprintf(stderr,"Could not set Prosilica Acquisition Mode\n"); return false; } if(PvCommandRun(Camera.Handle, "AcquisitionStart")!= ePvErrSuccess) { fprintf(stderr,"Could not start Prosilica acquisition\n"); return false; } if(PvAttrEnumSet(Camera.Handle, "FrameStartTriggerMode", "Freerun")!= ePvErrSuccess) { fprintf(stderr,"Error setting Prosilica trigger to \"Freerun\""); return false; } return true; } return false; }
//-------------------------------------------------------------------- bool ofxVideoGrabberPvAPI::initGrabber(int w, int h, bool setUseTexture){ width = w; height = h; tPvErr ret; //PvAPI return codes bUseTexture = setUseTexture; memset( &cameraUID, 0, sizeof(cameraUID) ); memset( &cameraHandle, 0, sizeof(cameraHandle) ); memset( &cameraFrame, 0, sizeof(cameraFrame) ); //---------------------------------- 1 - open the sequence grabber // lazy initialization of the Prosilica API if( !bPvApiInitiated ){ ret = PvInitialize(); if( ret == ePvErrSuccess ) { ofLog(OF_LOG_VERBOSE, "PvAPI initialized"); } else { ofLog(OF_LOG_ERROR, "unable to initialize PvAPI"); return false; } bPvApiInitiated = true; } //---------------------------------- 3 - buffer allocation // Create a buffer big enough to hold the video data, // make sure the pointer is 32-byte aligned. // also the rgb image that people will grab pixels = new unsigned char[width*height]; // check for any cameras plugged in int waitIterations = 0; while( PvCameraCount() < 1 ) { ofSleepMillis(250); waitIterations++; if( waitIterations > 8 ) { ofLog(OF_LOG_ERROR, "error: no camera found"); return false; } } //---------------------------------- 4 - device selection tPvUint32 count, connected; tPvCameraInfo list[maxConcurrentCams]; count = PvCameraList( list, maxConcurrentCams, &connected ); if(count >= 1) { bool bSelectedDevicePresent = false; if(bChooseDevice) { //check if selected device is available for( int i=0; i<count; ++i) { if( deviceID == list[i].UniqueId ) { bSelectedDevicePresent = true; cameraUID = list[i].UniqueId; } } } if( !bSelectedDevicePresent ){ cameraUID = list[0].UniqueId; ofLog(OF_LOG_NOTICE, "cannot find selected camera -> defaulting to first available"); ofLog(OF_LOG_VERBOSE, "there is currently an arbitrary hard limit of %i concurrent cams", maxConcurrentCams); } } else { ofLog(OF_LOG_ERROR, "no cameras available"); return false; } //---------------------------------- 5 - final initialization steps ret = PvCameraOpen( cameraUID, ePvAccessMaster, &cameraHandle ); if( ret == ePvErrSuccess ){ ofLog(OF_LOG_VERBOSE, "camera opened"); } else { if( ret == ePvErrAccessDenied ) { ofLog(OF_LOG_ERROR, "camera access denied, probably already in use"); } ofLog(OF_LOG_ERROR, "failed to open camera"); return false; } unsigned long FrameSize = 0; ret = PvAttrUint32Get( cameraHandle, "TotalBytesPerFrame", &FrameSize ); if( ret == ePvErrSuccess ){ // allocate the buffer for the single frame we need cameraFrame.ImageBuffer = new char[FrameSize]; cameraFrame.ImageBufferSize = FrameSize; ofLog(OF_LOG_VERBOSE, "camera asked for TotalBytesPerFrame"); } else { ofLog(OF_LOG_ERROR, "failed to allocate capture buffer"); return false; } ret = PvCaptureStart(cameraHandle); if( ret == ePvErrSuccess ){ ofLog(OF_LOG_VERBOSE, "camera set to capture mode"); } else { if( ret == ePvErrUnplugged ){ ofLog(OF_LOG_ERROR, "cannot start capture, camera was unplugged"); } ofLog(OF_LOG_ERROR, "cannot set to capture mode"); return false; } ret = PvAttrEnumSet(cameraHandle,"FrameStartTriggerMode","Freerun"); if( ret == ePvErrSuccess ){ ofLog(OF_LOG_VERBOSE, "camera set to continuous mode"); } else { ofLog(OF_LOG_ERROR, "cannot set to continous mode"); return false; } ret = PvCommandRun(cameraHandle,"AcquisitionStart"); if( ret == ePvErrSuccess ){ ofLog(OF_LOG_VERBOSE, "camera continuous acquisition started"); } else { // if that fail, we reset the camera to non capture mode PvCaptureEnd(cameraHandle) ; ofLog(OF_LOG_ERROR, "cannot start continuous acquisition"); return false; } bGrabberInited = true; //loadSettings(); ofLog(OF_LOG_NOTICE,"camera is ready now"); //---------------------------------- 6 - setup texture if needed if (bUseTexture){ // create the texture, set the pixels to black and // upload them to the texture (so at least we see nothing black the callback) tex.allocate(width,height,GL_LUMINANCE); memset(pixels, 0, width*height); tex.loadData(pixels, width, height, GL_LUMINANCE); } // we are done return true; }
void ListCameras() { tPvCameraInfo cameraList[MAX_CAMERA_LIST]; unsigned long cameraNum = 0; unsigned long cameraRle; while(gStop == false) { printf("***********************************\n"); // first, get list of reachable cameras. cameraNum = PvCameraList(cameraList,MAX_CAMERA_LIST,NULL); // keep how many cameras listed are reachable cameraRle = cameraNum; // then we append the list of unreachable cameras. if (cameraNum < MAX_CAMERA_LIST) { cameraNum += PvCameraListUnreachable(&cameraList[cameraNum], MAX_CAMERA_LIST-cameraNum, NULL); } if(cameraNum) { struct in_addr addr; tPvIpSettings Conf; tPvErr lErr; // display info for(unsigned long i=0;i<cameraNum;i++) { if(i < cameraRle) { // get the camera's IP configuration if((lErr = PvCameraIpSettingsGet(cameraList[i].UniqueId,&Conf)) == ePvErrSuccess) { addr.s_addr = Conf.CurrentIpAddress; printf("%s - %8s - Unique ID = % 8lu IP@ = %15s [%s]\n",cameraList[i].SerialString, cameraList[i].DisplayName, cameraList[i].UniqueId, inet_ntoa(addr), cameraList[i].PermittedAccess & ePvAccessMaster ? "available" : "in use"); } else printf("%s - %8s - Unique ID = % 8lu (unavailable, %u)\n",cameraList[i].SerialString, cameraList[i].DisplayName, cameraList[i].UniqueId,lErr); } else printf("%s - %8s - Unique ID = % 8lu (*)\n",cameraList[i].SerialString, cameraList[i].DisplayName, cameraList[i].UniqueId); } if(cameraNum != cameraRle) printf("(*) camera is not reachable\n"); } else printf("No camera detected ...\n"); fflush(stdout); Sleep(1500); } printf("**************************************\n"); }