This commit is contained in:
Zhanpeng Yang 2024-10-08 18:14:34 +08:00
parent 641885f4f5
commit 38b031d68b
3 changed files with 82 additions and 68 deletions

View File

@ -10,7 +10,7 @@ set(CMAKE_CXX_STANDARD 20)
set(PROJ_NAME "SEMS-data-collection")
set(PROJ_VERSION_MAJOR 0)
set(PROJ_VERSION_MINOR 0)
set(PROJ_VERSION_PATCH 2.20240927_alpha)
set(PROJ_VERSION_PATCH 3.20241008_alpha)
#HelloWorld
project(${PROJ_NAME})
@ -27,6 +27,7 @@ add_library(SpectralCameraLib "${PROJECT_SOURCE_DIR}/src/spectral_camera.cpp")
add_library(DisplayLib "${PROJECT_SOURCE_DIR}/src/display.cpp")
add_library(DataPreprocessLib "${PROJECT_SOURCE_DIR}/src/data_preprocess.cpp")
add_library(VisibleLightCameraLIB "${PROJECT_SOURCE_DIR}/src/visible_light_camera.cpp")
add_library(UtilsLIB "${PROJECT_SOURCE_DIR}/src/utils.cpp")
link_directories (${PROJECT_SOURCE_DIR}/../CompliteEnv/Python37)
@ -46,6 +47,7 @@ target_link_libraries(${PROJ_NAME} PUBLIC DisplayLib)
target_link_libraries(${PROJ_NAME} PUBLIC opencv_core490 opencv_highgui490 opencv_imgproc490 opencv_photo490 opencv_imgcodecs490 opencv_video490 opencv_videoio490)
target_link_libraries(${PROJ_NAME} PUBLIC DataPreprocessLib)
target_link_libraries(${PROJ_NAME} PUBLIC UtilsLIB)

View File

@ -14,13 +14,12 @@
#include <fstream>
#include "visible_light_camera.h"
#include<windows.h>
#include <windows.h>
#include <chrono>
extern unsigned long long int last_file_time=0;
extern unsigned long long int frame_num=0;
extern unsigned long long int last_file_time = 0;
extern unsigned long long int frame_num = 0;
int check_update_file_handle(std::string save_dir, std::ofstream &h_raw_spectal_file, std::ofstream &h_header_file, unsigned long long max_file_size, std::ofstream &h_visible_light_file, bool enable_visible_light_camera)
{
@ -34,52 +33,57 @@ int check_update_file_handle(std::string save_dir, std::ofstream &h_raw_spectal_
std::string raw_data_file_name = save_dir + std::to_string(now_t);
h_raw_spectal_file.open((raw_data_file_name + std::string(".bin").data()), std::ios::out | std::ios::binary | std::ios::app);
h_header_file.open((raw_data_file_name + std::string(".csv").data()), std::ios::out | std::ios::app);
if (enable_visible_light_camera)
{
if (h_visible_light_file.is_open())
{
h_visible_light_file.close();
}
{
h_visible_light_file.close();
}
h_visible_light_file.open((raw_data_file_name + std::string(".img").data()), std::ios::out | std::ios::binary | std::ios::app);
}
unsigned long long int now=(unsigned long long int)now_t;
if (last_file_time!=0){
float t=(float)(now-last_file_time)/1000;
unsigned long long int now = (unsigned long long int)now_t;
if (last_file_time != 0)
{
float t = (float)(now - last_file_time) / 1000;
// std::cout<<now<<last_file_time<<t<<frame_num<<std::endl;
float fps= frame_num/t;
printf("FPS %.2f\n",fps);
frame_num=0;
float fps = frame_num / t;
printf("FPS %.2f\n", fps);
frame_num = 0;
}
last_file_time=now;
last_file_time = now;
return 0;
}
int main()
{
// std::cout << "Hello" << std::endl;
char szPath[512] = {0};
GetModuleFileName(NULL, szPath, sizeof(szPath)-1);
GetModuleFileName(NULL, szPath, sizeof(szPath) - 1);
std::string exe_dir(szPath);
//这里返回的是exe文件的完整路径包括文件名所以下面执行两边就是先取出exe文件名再去除exe所在文件夹
//即实现找到exe所在文件夹的上层文件夹的绝对路径
// 这里返回的是exe文件的完整路径包括文件名所以下面执行两边就是先取出exe文件名再去除exe所在文件夹
// 即实现找到exe所在文件夹的上层文件夹的绝对路径
size_t lastSlashIndex = exe_dir.find_last_of('\\');
exe_dir=exe_dir.substr(0,lastSlashIndex); //+1是为了也包含最后一个反斜杠
exe_dir = exe_dir.substr(0, lastSlashIndex); //+1是为了也包含最后一个反斜杠
char currentPath[32767];
GetEnvironmentVariable("PATH", currentPath, 32767);
std::string newPath = exe_dir + ";" + std::string(currentPath);
SetEnvironmentVariable("PATH", newPath.c_str());
lastSlashIndex = exe_dir.find_last_of('\\');
exe_dir=exe_dir.substr(0,lastSlashIndex+1); //+1是为了也包含最后一个反斜杠
exe_dir = exe_dir.substr(0, lastSlashIndex + 1); //+1是为了也包含最后一个反斜杠
std::cout << "Configs file path is " << exe_dir + "configs\\data_collection_configs.yaml" << std::endl;
std::cout << "Configs file path is " <<exe_dir+"configs\\data_collection_configs.yaml"<< std::endl;
YAML::Node configs = YAML::LoadFile(exe_dir+"configs\\data_collection_configs.yaml");
YAML::Node configs = YAML::LoadFile(exe_dir + "configs\\data_collection_configs.yaml");
SpectralCamera *spec_camera = new SpectralCamera();
spec_camera->listDevices();
@ -103,42 +107,39 @@ int main()
visible_light_camera->init(configs["visible_light_camera_configs"]["video_capture_index"].as<int>());
}
std::ofstream h_raw_spectal_file,h_header_file, h_visible_light_file;
check_update_file_handle(save_dir, h_raw_spectal_file, h_header_file, max_file_size, h_visible_light_file, enable_visible_light_camera);
std::ofstream h_raw_spectal_file, h_header_file, h_visible_light_file;
check_update_file_handle(save_dir, h_raw_spectal_file, h_header_file, max_file_size, h_visible_light_file, enable_visible_light_camera);
bool flag_run = true;
while (flag_run)
{
spec_camera->read();
spec_camera->save(h_raw_spectal_file, h_header_file);
frame_num++;
spec_camera->save(h_raw_spectal_file, h_header_file);
frame_num++;
display_spectral_image(spec_camera->pFrameBuffer, spec_camera->nWidth, spec_camera->nHeight,"Spectral Image");
display_spectral_image(spec_camera->pFrameBuffer, spec_camera->nWidth, spec_camera->nHeight, "Spectral Image");
// 如果启动了可见光相机
if (enable_visible_light_camera)
{
visible_light_camera->read();
visible_light_camera->save(h_visible_light_file, h_header_file);
display_img(visible_light_camera->image_frame, "Camera");
}
if (enable_visible_light_camera)
{
visible_light_camera->read();
visible_light_camera->save(h_visible_light_file, h_header_file);
display_img(visible_light_camera->image_frame, "Camera");
}
h_header_file << std::endl;
h_header_file << std::endl;
if (h_raw_spectal_file.tellp()>max_file_size){
check_update_file_handle(save_dir, h_raw_spectal_file, h_header_file, max_file_size, h_visible_light_file, enable_visible_light_camera);
if (h_raw_spectal_file.tellp() > max_file_size)
{
check_update_file_handle(save_dir, h_raw_spectal_file, h_header_file, max_file_size, h_visible_light_file, enable_visible_light_camera);
}
if (_kbhit())
{
char key = _getch();
if ( (key == 27)|(key == 'q'))
if ((key == 27) | (key == 'q'))
{
flag_run=false;
flag_run = false;
}
}
}
@ -146,15 +147,15 @@ check_update_file_handle(save_dir, h_raw_spectal_file, h_header_file, max_file_s
spec_camera->close();
delete spec_camera;
if (h_raw_spectal_file.is_open())
{
h_raw_spectal_file.close();
}
if (h_header_file.is_open())
{
h_header_file.close();
}
if (h_visible_light_file.is_open())
{
h_visible_light_file.close();
}
{
h_raw_spectal_file.close();
}
if (h_header_file.is_open())
{
h_header_file.close();
}
if (h_visible_light_file.is_open())
{
h_visible_light_file.close();
}
}

View File

@ -46,6 +46,20 @@ Error:
int SpectralCamera::init(int deviceTypeIndex, double FrameRate, double ExposureTime, int BinningSpatial, int BinningSpectral, bool BinningAverage, long long RingBufferSize)
{
// char szPath[512] = {0};
// GetModuleFileName(NULL, szPath, sizeof(szPath) - 1);
// std::string exe_dir(szPath);
// // 这里返回的是exe文件的完整路径包括文件名所以下面执行两边就是先取出exe文件名再去除exe所在文件夹
// // 即实现找到exe所在文件夹的上层文件夹的绝对路径
// size_t lastSlashIndex = exe_dir.find_last_of('\\');
// std::string autoclick_path = exe_dir.substr(0, lastSlashIndex + 1) + "autoClick.exe"; //+1是为了也包含最后一个反斜杠
// wchar_t *wstr = new wchar_t[autoclick_path.length() + 1];
// mbstowcs(wstr, autoclick_path.c_str(), autoclick_path.length() + 1);
// wprintf(L"Started autoClick.exe in %ls\n", wstr);
HINSTANCE state = ShellExecuteW(NULL, L"open", L"autoClick.exe", NULL, NULL, SW_SHOW);
// autoClick.exe以及依赖的msvcp140d.dllucrtbased.dllvcruntime140d.dll在lib文件夹中
wprintf(L"Started autoClick.exe. The state code is %d\n", state);
@ -66,7 +80,7 @@ int SpectralCamera::init(int deviceTypeIndex, double FrameRate, double ExposureT
this->ExposureTime,
this->BinningSpatial,
this->BinningSpectral,
this->BinningAverage,this->RingBufferSize);
this->BinningAverage, this->RingBufferSize);
wprintf(L"******************************************************\n");
SI_CHK(SI_Open(this->deviceTypeIndex, &this->deviceHandle));
@ -123,7 +137,6 @@ Error:
return -1;
}
int SpectralCamera::stop(void)
{
SI_CHK(SI_Command(this->deviceHandle, L"Acquisition.Stop"));
@ -136,7 +149,6 @@ Error:
return -1;
}
int SpectralCamera::start(void)
{
SI_CHK(SI_Command(this->deviceHandle, L"Acquisition.Start"));
@ -149,9 +161,9 @@ Error:
return -1;
}
int SpectralCamera::reset_FrameRate( double Value)
{
SI_CHK(SI_SetFloat(this->deviceHandle, L"Camera.FrameRate", Value));
int SpectralCamera::reset_FrameRate(double Value)
{
SI_CHK(SI_SetFloat(this->deviceHandle, L"Camera.FrameRate", Value));
return 0;
Error:
if (SI_FAILED(nError))
@ -161,9 +173,9 @@ Error:
return -1;
}
int SpectralCamera::reset_ExposureTime( double Value)
{
SI_CHK(SI_SetFloat(this->deviceHandle, L"Camera.ExposureTime", Value));
int SpectralCamera::reset_ExposureTime(double Value)
{
SI_CHK(SI_SetFloat(this->deviceHandle, L"Camera.ExposureTime", Value));
return 0;
Error:
if (SI_FAILED(nError))
@ -182,7 +194,6 @@ void SpectralCamera::save(std::ofstream &h_raw_spectral_file, std::ofstream &h_h
h_header_file << now_t << "," << raw_spectral_file_size << "," << this->nFrameSize;
}
int SpectralCamera::read(void)
{
SI_CHK(SI_Wait(this->deviceHandle, this->pFrameBuffer, &this->nFrameSize, &this->nFrameNumber, 10000));