2 Star 47 Fork 18

天涯 / RobustVideoMattingGUI

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
rungui.cpp 32.09 KB
一键复制 编辑 原始数据 按行查看 历史
天涯 提交于 2024-02-17 18:27 . modified: RVM/gui_edit.py
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
/*
* @file name: File name
* @Date: 2023-11-04 10:16:12
* @LastEditors: YuanMing
* @LastEditTime: 2024-02-17 17:38:12
* @Describe:
*/
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0A000000
#endif
#include <windows.h>
#include <windowsx.h>
#include <wingdi.h>
#include <unistd.h>
#include <stdio.h>
#include <winuser.h>
#include <psapi.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <vector>
#include <regex>
#include <thread>
#include <codecvt>
#include <stdlib.h>
#include <filesystem>
#include "imgui.h"
#include "imgui_impl_opengl3.h"
#include "imgui_impl_win32.h"
#include "implot.h"
#include "Python.h"
#include "nvml.h"
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <GL/GL.h>
#include <tchar.h>
using namespace std;
// Data stored per platform window
struct WGL_WindowData
{
HDC hDC;
};
// utility structure for realtime plot
struct ScrollingBuffer
{
int MaxSize;
int Offset;
ImVector<ImVec2> Data;
ScrollingBuffer(int max_size = 2000)
{
MaxSize = max_size;
Offset = 0;
Data.reserve(MaxSize);
}
void AddPoint(float x, float y)
{
if (Data.size() < MaxSize)
Data.push_back(ImVec2(x, y));
else
{
Data[Offset] = ImVec2(x, y);
Offset = (Offset + 1) % MaxSize;
}
}
void Erase()
{
if (Data.size() > 0)
{
Data.shrink(0);
Offset = 0;
}
}
};
// nvmlDLL动态调用函数定义
HINSTANCE hNVML_DLL = NULL;
typedef nvmlReturn_t (*nvmlInit_fun)(void);
typedef nvmlReturn_t (*nvmlDeviceGetCount_fun)(unsigned int *);
typedef nvmlReturn_t (*nvmlDeviceGetHandleByIndex_fun)(unsigned int, nvmlDevice_t *);
typedef nvmlReturn_t (*nvmlDeviceGetName_fun)(nvmlDevice_t, char *, unsigned int);
typedef nvmlReturn_t (*nvmlDeviceGetUtilizationRates_fun)(nvmlDevice_t, nvmlUtilization_t *);
typedef const char *(*nvmlErrorString_fun)(nvmlReturn_t);
nvmlInit_fun pNvmlInit;
nvmlDeviceGetCount_fun pNvmlDeviceGetCount;
nvmlDeviceGetHandleByIndex_fun pNvmlDeviceGetHandleByIndex;
nvmlDeviceGetName_fun pNvmlDeviceGetName;
nvmlDeviceGetUtilizationRates_fun pNvmlDeviceGetUtilizationRates;
nvmlErrorString_fun pNvmlErrorString;
// pythonDLL动态调用函数定义
HINSTANCE hPython_DLL = NULL;
typedef PyAPI_FUNC(void) (*Py_SetPythonHome_fun)(const wchar_t *);
typedef PyAPI_FUNC(void) (*Py_Initialize_fun)(void);
typedef PyAPI_FUNC(int) (*Py_IsInitialized_fun)(void);
typedef PyAPI_FUNC(int) (*PyRun_SimpleStringFlags_fun)(const char *, PyCompilerFlags *);
Py_SetPythonHome_fun pPy_SetPythonHome;
Py_Initialize_fun pPy_Initialize;
Py_IsInitialized_fun pPy_IsInitialized;
PyRun_SimpleStringFlags_fun pPyRun_SimpleStringFlags;
// Data
static HGLRC g_hRC;
static WGL_WindowData g_MainWindow;
static int g_Width;
static int g_Height;
// Forward declarations of helper functions
bool CreateDeviceWGL(HWND hWnd, WGL_WindowData *data);
void CleanupDeviceWGL(HWND hWnd, WGL_WindowData *data);
void ResetDeviceWGL();
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
string infoDisp;
/// 执行cmd指令并返回结果
string getCmdResult(const string &strCmd)
{
char buf[10240] = {0};
FILE *pf = NULL;
if ((pf = popen(strCmd.c_str(), "r")) == NULL)
{
return "";
}
string strResult;
while (fgets(buf, sizeof buf, pf))
{
strResult += buf;
}
pclose(pf);
unsigned int iSize = strResult.size();
if (iSize > 0 && strResult[iSize - 1] == '\n') // linux
{
strResult = strResult.substr(0, iSize - 1);
}
return strResult;
}
void Stringsplit(string str, const char split, vector<string> &res)
{
istringstream iss(str); // 输入流
string token; // 接收缓冲区
while (getline(iss, token, split)) // 以split为分隔符
{
res.push_back(token);
}
}
bool is_chinese(const std::string &str)
{
for (int i = 0; i < str.length(); i++)
{
if (str[i] > 0x80)
return true;
}
return false;
}
// 从 nvidia-smi -L 命令中提取 GPU 名称
std::string ExtractGPUName()
{
std::string result = "";
char buffer[128];
std::string cmd = "nvidia-smi -L";
FILE *pipe = popen(cmd.c_str(), "r");
if (!pipe)
{
std::cerr << "Error executing command." << std::endl;
return result;
}
while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
{
result += buffer;
}
pclose(pipe);
// 去掉换行符
result.erase(result.find_last_not_of("\n\r") + 1);
// get GPU name
std::regex pattern("GPU \\d: (.+?) \\(UUID:");
std::smatch match;
if (std::regex_search(result, match, pattern) && match.size() > 1)
{
return match[1];
}
else
{
return "null";
}
}
int thread_monitor(string &info, int &state, int &nvidiadevicenum)
{
state = 0;
info += "开始检测RVM GUI运行环境。\r\n";
char pathbuffer[65535];
GetModuleFileName(NULL, pathbuffer, 65535);
string path = pathbuffer;
if (is_chinese(path) == true)
{
info += "RVM GUI所在目录路径中含有中文字符,可能会影响RVM GUI运行,建议将RVM GUI放置到纯英文路径中执行。\r\n";
}
else
{
info += "RVM GUI所在目录路径中无中文字符,继续其它环境检测。\r\n";
}
info += "检测NVIDIA显卡是否存在,如果存在将启动NVIDIA显存和使用率监测。\r\n";
if (hNVML_DLL == NULL)
hNVML_DLL = LoadLibrary("nvml.dll");
if (hNVML_DLL != NULL)
{
info += "已检测到nvml.dll。\r\n";
pNvmlInit = (nvmlInit_fun)GetProcAddress(hNVML_DLL, "nvmlInit"); //
if (pNvmlInit != NULL)
{
nvmlReturn_t result;
unsigned int device_count, i;
// First initialize NVML library
result = pNvmlInit();
pNvmlDeviceGetCount = (nvmlDeviceGetCount_fun)GetProcAddress(hNVML_DLL, "nvmlDeviceGetCount"); //
if (pNvmlDeviceGetCount != NULL)
{
// result = nvmlInit();
result = pNvmlDeviceGetCount(&device_count);
// result = nvmlDeviceGetCount(&device_count);
if (NVML_SUCCESS != result)
{
info += "未能获取NVIDIA显卡数量: ";
pNvmlErrorString = (nvmlErrorString_fun)GetProcAddress(hNVML_DLL, "nvmlErrorString");
if (pNvmlErrorString == NULL)
{
// info += nvmlErrorString(result);
info += pNvmlErrorString(result);
}
info += "\r\n";
nvidiadevicenum = 0;
}
else
{
info += "已找到NVIDIA显卡数量:" + to_string(device_count) + "\r\n";
nvidiadevicenum = device_count;
}
}
else
{
info += "nvml.dll动态调用:nvmlDeviceGetCount函数读取失败。\r\n";
nvidiadevicenum = 0;
}
pNvmlDeviceGetHandleByIndex = (nvmlDeviceGetHandleByIndex_fun)GetProcAddress(hNVML_DLL, "nvmlDeviceGetHandleByIndex"); //
if (pNvmlDeviceGetHandleByIndex == NULL)
{
info += "nvml.dll动态调用:nvmlDeviceGetHandleByIndex函数读取失败。\r\n";
}
pNvmlDeviceGetName = (nvmlDeviceGetName_fun)GetProcAddress(hNVML_DLL, "nvmlDeviceGetName"); //
if (pNvmlDeviceGetName == NULL)
{
info += "nvml.dll动态调用:nvmlDeviceGetName函数读取失败。\r\n";
}
pNvmlDeviceGetUtilizationRates = (nvmlDeviceGetUtilizationRates_fun)GetProcAddress(hNVML_DLL, "nvmlDeviceGetUtilizationRates"); //
if (pNvmlDeviceGetUtilizationRates == NULL)
{
info += "nvml.dll动态调用:nvmlDeviceGetUtilizationRates函数读取失败。\r\n";
}
}
else
{
info += "未能读取nvml.dll,当前设备可能未安装有NVIDIA显卡或NVIDIA显卡驱动\r\n";
nvidiadevicenum = 0;
}
}
else
{
FreeLibrary(hNVML_DLL);
hNVML_DLL = NULL;
}
string runcmd;
string pythonpath;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
// 隐藏掉可能出现的cmd命令窗口
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
ZeroMemory(&pi, sizeof(pi));
if (access("./python310/python.exe", F_OK) == 0)
{
info += "已检测到一体化python310环境:当前目录\\python310\\python.exe,启动RVM GUI主界面。\r\n";
// ShellExecute(NULL,"open","cmd","/k .\\python310\\python.exe gui.py",NULL,SW_SHOW);
runcmd = "./python310/python.exe gui.py";
std::filesystem::path path = std::filesystem::path("./Python310/");
string python_absloutepath = std::filesystem::canonical(path).string();
// pythonpath = "./python310/";
pythonpath = std::filesystem::canonical(path).string();
std::cout << "find internal python:" << pythonpath << endl;
wchar_t wpythonpathtemp[65535];
mbstowcs(wpythonpathtemp, pythonpath.c_str(), pythonpath.length());
AddDllDirectory(wpythonpathtemp);
std::cout << "add internal python scripts path to DLLdirectory:" << pythonpath << endl;
string pythonScriptsPath = std::filesystem::canonical(path / "Scripts").string();
mbstowcs(wpythonpathtemp, pythonScriptsPath.c_str(), pythonScriptsPath.length());
AddDllDirectory(wpythonpathtemp);
std::cout << "add internal python scripts path to DLLdirectory:" << pythonScriptsPath << endl;
string env_pythonpath = "PYTHONPATH=" + std::filesystem::canonical(path / "Lib/site-packages").string();
string env_pythonhome = "PYTHONHOME=" + std::filesystem::canonical(path).string();
if (putenv(env_pythonpath.c_str()) != 0)
{
std::cout << "add PYTHONPATH env failed:" << env_pythonpath << endl;
}
else
{
std::cout << "add PYTTHONPATH env succ:" << env_pythonpath << endl;
}
if (putenv(env_pythonhome.c_str()) != 0)
{
std::cout << "add PYTHONHOME env failed:" << env_pythonhome << endl;
}
else
{
std::cout << "add PYTHONHOME env succ:" << env_pythonhome << endl;
}
std::string env = getenv("PATH");
env += ";" + std::filesystem::canonical(path / "Scripts").string() + ";" + std::filesystem::canonical(path).string();
std::string newEnv = "PATH=" + env;
if (putenv(newEnv.c_str()) != 0)
{
std::cout << "set new PATH env failed:" << newEnv << endl;
}
else
{
cout << "set new PATH env succ:" << getenv("PATH") << endl;
}
}
else
{ // 未找到一体化python环境,检查系统内置pytho环境
info += "未检测到一体化python310环境:当前目录\\python310\\python.exe未发现。\r\n";
info += "开始检测系统内置python环境是否存在。\r\n";
string cmd = string("python -V");
string strRe = getCmdResult(cmd);
if (strRe.length() < 7)
{
info += "未检测到系统内置python环境,请安装python3.10环境。\r\n";
int msgboxID = MessageBox(NULL, (LPCSTR) "未找到一体化python环境或者系统内置python环境,请重新确认整合python环境是否存在或安装python3.10环境。", (LPCSTR) "错误", MB_ICONERROR | MB_OK);
return -1;
}
else
{
string strRe_head = strRe.substr(0, 6);
if (strRe_head.compare("Python") != 0)
{
info += "未检测到系统内置python环境,请安装python3.10环境。\r\n";
int msgboxID = MessageBox(NULL, (LPCSTR) "未找到一体化python环境或者系统内置python环境,请重新确认整合python环境是否存在或安装python3.10环境。", (LPCSTR) "错误", MB_ICONERROR | MB_OK);
return -1;
}
else
{
info += "已检测到系统内置python环境,确认python版本是否满足要求,建议应大于3.10。\r\n";
string strRe_ver = strRe.substr(7, strRe.length() - 7);
vector<string> strList;
Stringsplit(strRe_ver, '.', strList); // 将子串存放到strList中
int ver0 = atoi(strList[0].c_str());
int ver1 = atoi(strList[1].c_str());
// int ver2=atoi(strList[2].c_str());
if (ver0 >= 3 && ver1 >= 10)
{
info += "系统内置python环境版本不低于3.10,启动RVM GUI主界面。\r\n";
// system("python gui.py");
runcmd = "python gui.py";
string cmd = string("where python");
string strRe = getCmdResult(cmd);
string path = strRe.substr(0, strRe.find("python.exe", 0));
pythonpath = path;
}
else
{
info += "系统内置python环境版本低于3.10,请安装python3.10环境。\r\n";
int msgboxID = MessageBox(NULL, (LPCSTR) "系统内置python环境版本低于3.10,请安装python3.10环境,请重新确认整合python环境是否存在或安装python3.10环境。", (LPCSTR) "错误", MB_ICONERROR | MB_OK);
return -1;
}
}
}
}
// getCmdResult("nvidia-smi");
wchar_t wpythonpathtemp[65535];
mbstowcs(wpythonpathtemp, pythonpath.c_str(), pythonpath.length());
if (hPython_DLL == NULL)
{
string pythondllname = pythonpath + "\\python310.dll";
// cout<<pythondllname<<endl;
hPython_DLL = LoadLibrary(pythondllname.c_str());
cout << GetLastError() << endl;
}
if (hPython_DLL != NULL)
{
cout << "load python310.dll succ." << endl;
pPy_SetPythonHome = (Py_SetPythonHome_fun)GetProcAddress(hPython_DLL, "Py_SetPythonHome"); //
if (pPy_SetPythonHome == NULL)
{
info += "python310.dll动态调用:Py_SetPythonHome函数读取失败。\r\n";
cout << "python310.dll动态调用:Py_SetPythonHome函数读取失败。" << endl;
return -3;
}
pPy_Initialize = (Py_Initialize_fun)GetProcAddress(hPython_DLL, "Py_Initialize"); //
if (pPy_Initialize == NULL)
{
info += "python310.dll动态调用:Py_Initialize函数读取失败。\r\n";
cout << "python310.dll动态调用:Py_Initialize函数读取失败。" << endl;
return -4;
}
pPy_IsInitialized = (Py_IsInitialized_fun)GetProcAddress(hPython_DLL, "Py_IsInitialized"); //
if (pPy_IsInitialized == NULL)
{
info += "python310.dll动态调用:Py_IsInitialized函数读取失败。\r\n";
cout << "python310.dll动态调用:Py_IsInitialized函数读取失败。" << endl;
return -5;
}
pPyRun_SimpleStringFlags = (PyRun_SimpleStringFlags_fun)GetProcAddress(hPython_DLL, "PyRun_SimpleStringFlags"); //
if (pPyRun_SimpleStringFlags == NULL)
{
info += "python310.dll动态调用:PyRun_SimpleStringFlags函数读取失败。\r\n";
cout << "python310.dll动态调用:PyRun_SimpleStringFlags函数读取失败。" << endl;
return -6;
}
// Py_SetPythonHome(wpythonpathtemp);
pPy_SetPythonHome(wpythonpathtemp);
pPy_Initialize(); // 使用python之前,要调用Py_Initialize();这个函数进行初始化
if (!pPy_IsInitialized())
{
info += "启动python失败,请检查python环境是否正常。\r\n";
cout << "启动python失败,请检查python环境是否正常。" << endl;
return -2;
}
else
{
info += "开始启动python RVM GUI。\r\n";
cout << "python dll func initialized succ,start run gui.py" << endl;
pPyRun_SimpleStringFlags("import os,sys", NULL); // 执行import语句,把当前路径加入路径中,为了找到math_test.py
// PyRun_SimpleString("sys.path.append('./')"); //这句不用也可以,只是确保能找到py文件
pPyRun_SimpleStringFlags("print(os.getcwd())", NULL); // 测试打印当前路径
state = 1;
pPyRun_SimpleStringFlags("exec(open('./gui.py', encoding = 'utf-8').read())", NULL); // python3的exec方法
}
}
else
{
info += "未找到python310.dll,请确认该文件是否存在于" + pythonpath + "目录下\r\n";
cout << "load python310.dll failed." << endl;
return -2;
}
return 0;
}
int main(int argc, char *argv[])
{
// Create application window
// ImGui_ImplWin32_EnableDpiAwareness();
// 设置高分辨率显示,如果系统低于win10,应注释掉此函数
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
WNDCLASSEXW wc = {sizeof(wc), CS_OWNDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, L"ImGui Example", NULL};
::RegisterClassExW(&wc);
HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"RVM GUI 启动信息及进程状态监测窗口", WS_OVERLAPPEDWINDOW, 0, 0, 800, 1000, NULL, NULL, wc.hInstance, NULL);
if (!hwnd)
return -1;
// Initialize OpenGL
if (!CreateDeviceWGL(hwnd, &g_MainWindow))
{
CleanupDeviceWGL(hwnd, &g_MainWindow);
::DestroyWindow(hwnd);
::UnregisterClassW(wc.lpszClassName, wc.hInstance);
return 1;
}
wglMakeCurrent(g_MainWindow.hDC, g_hRC);
// Show the window
::ShowWindow(hwnd, SW_SHOWDEFAULT);
::UpdateWindow(hwnd);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImPlot::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark();
// ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplWin32_InitForOpenGL(hwnd);
ImGui_ImplOpenGL3_Init();
// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
// io.Fonts->AddFontDefault();
// io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
// io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
// io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
// io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
// ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
ImFont *font = io.Fonts->AddFontFromFileTTF("./Dependencies/fonts/oppo_Sans.ttf", 28.0f, NULL, io.Fonts->GetGlyphRangesChineseFull());
IM_ASSERT(font != NULL);
// Our state
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
infoDisp = u8"";
int examstatus = 0;
bool monitor_thread_has_started = false;
HANDLE proc_handle;
// Main loop
bool done = false;
int nvidiadevicenum = 0;
int mem_state_data_maxsize = 100;
int nvidia_state_data_maxsize = 100;
float time_run = 0;
ScrollingBuffer mem_workingset_state;
ScrollingBuffer mem_pagefile_state;
vector<ScrollingBuffer> nvidia_memstate_list;
vector<ScrollingBuffer> nvidia_gpustate_list;
thread t1(thread_monitor, ref(infoDisp), ref(examstatus), ref(nvidiadevicenum));
while (!done)
{
// Poll and handle messages (inputs, window resize, etc.)
// See the WndProc() function below for our to dispatch events to the Win32 backend.
MSG msg;
while (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
if (msg.message == WM_QUIT)
done = true;
}
if (done)
break;
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
// if (show_demo_window)
// ImGui::ShowDemoWindow(&show_demo_window);
// ImPlot::ShowDemoWindow(&show_demo_window);
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
// static float f = 0.0f;
// static int counter = 0;
ImGuiWindowFlags window_flags = 0 | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize;
ImGui::Begin(u8"启动信息及状态查看", NULL, window_flags); // Create a window called "Hello, world!" and append into it.
ImGui::StyleColorsLight();
RECT rect;
GetClientRect(hwnd, &rect);
ImGui::SetWindowSize(ImVec2(rect.right - rect.left, rect.bottom - rect.top));
ImGui::SetWindowPos(ImVec2(0, 0));
time_run += ImGui::GetIO().DeltaTime;
if (ImGui::CollapsingHeader("启动信息"))
{
ImGui::TextWrapped(infoDisp.c_str());
// ImGui::End();
}
if (ImGui::CollapsingHeader("本进程内存使用监测"))
{
// ImGuiWindowFlags window_flags = 0 | ImGuiWindowFlags_NoMove;
// ImGui::Begin(u8"状态窗口", NULL, window_flags); // Create a window called "Hello, world!" and append into it.
// ImGui::StyleColorsLight();
// ImGui::SetWindowSize(ImVec2(800, 400));
// ImGui::SetWindowPos(ImVec2(0, 200));
if (examstatus == 1)
{
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
float workingsetvalue = pmc.WorkingSetSize / 1024.0 / 1024.0 / 1024.0;
float pagefilevalue = pmc.PagefileUsage / 1024.0 / 1024.0 / 1024.0;
string str1 = "WorkingSet内存使用:" + to_string(workingsetvalue) + "GB";
string str2 = "Pagefile交换内存使用:" + to_string(pagefilevalue) + "GB";
mem_workingset_state.AddPoint(time_run, workingsetvalue);
mem_pagefile_state.AddPoint(time_run, pagefilevalue);
ImGui::Text(str2.c_str());
ImGui::Text(str1.c_str());
if (ImPlot::BeginPlot("内存监测"))
{
// ImPlot::PlotLine("My Line Plot", x_data, y_data, 1000);
ImPlot::SetupAxes("运行时间/s", "使用内存/GB", ImPlotAxisFlags_AutoFit, ImPlotAxisFlags_AutoFit);
ImPlot::SetupAxisLimits(ImAxis_X1, time_run - 10.0, time_run, ImGuiCond_Always);
// ImPlot::SetupAxisLimits(ImAxis_Y1f,0,1);
ImPlot::SetNextLineStyle(IMPLOT_AUTO_COL, 2.0f);
ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.5f);
ImPlot::PlotLine("Pagefile", &mem_pagefile_state.Data[0].x, &mem_pagefile_state.Data[0].y, mem_pagefile_state.Data.size(), ImPlotLineFlags_Shaded, mem_pagefile_state.Offset, 2 * sizeof(float));
ImPlot::SetNextLineStyle(IMPLOT_AUTO_COL, 2.0f);
ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.5f);
ImPlot::PlotLine("WorkingSet", &mem_workingset_state.Data[0].x, &mem_workingset_state.Data[0].y, mem_workingset_state.Data.size(), ImPlotLineFlags_Shaded, mem_workingset_state.Offset, 2 * sizeof(float));
ImPlot::EndPlot();
}
}
}
if (nvidiadevicenum > 0 & pNvmlInit != NULL)
{
nvmlReturn_t result;
if (nvidia_gpustate_list.size() < nvidiadevicenum)
{
ScrollingBuffer tscrollbuff1, tscrollbuff2;
nvidia_gpustate_list.push_back(tscrollbuff1);
nvidia_memstate_list.push_back(tscrollbuff2);
}
for (int i = 0; i < nvidiadevicenum; i++)
{
string gputitle;
string nvidiainfo;
nvmlDevice_t device;
char name[NVML_DEVICE_NAME_BUFFER_SIZE];
nvmlPciInfo_t pci;
// result = nvmlDeviceGetHandleByIndex(i, &device);
if (pNvmlDeviceGetHandleByIndex != NULL)
{
result = pNvmlDeviceGetHandleByIndex(i, &device);
if (NVML_SUCCESS != result)
{
std::cout << "get device failed " << endl;
continue;
}
if (pNvmlDeviceGetName != NULL)
{
result = pNvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE);
// result = nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE);
if (NVML_SUCCESS == result)
{
nvidiainfo += "NVIDIA GPU ID: " + to_string(i) + "";
nvidiainfo += "名称:";
nvidiainfo += name;
// ImGui::Text(nvidiainfo.c_str());
}
gputitle = "NVIDIA GPU ID " + to_string(i) + ":" + name + "本进程使用";
if (ImGui::CollapsingHeader(gputitle.c_str()))
{
// 使用率
nvmlUtilization_t utilization;
if (pNvmlDeviceGetUtilizationRates != NULL)
{
result = pNvmlDeviceGetUtilizationRates(device, &utilization);
// result = nvmlDeviceGetUtilizationRates(device, &utilization);
if (NVML_SUCCESS == result)
{
nvidiainfo = "GPU 使用率: ";
nvidiainfo += to_string(utilization.gpu) + "%%";
nvidiainfo += "显存使用率 ";
nvidiainfo += to_string(utilization.memory) + "%%";
nvidia_gpustate_list[i].AddPoint(time_run, utilization.gpu);
nvidia_memstate_list[i].AddPoint(time_run, utilization.memory);
}
ImGui::Text(nvidiainfo.c_str());
if (ImPlot::BeginPlot(gputitle.c_str()))
{
// ImPlot::PlotLine("My Line Plot", x_data, y_data, 1000);
ImPlot::SetupAxes("运行时间/s", "使用率/%", ImPlotAxisFlags_AutoFit, ImPlotAxisFlags_AutoFit);
ImPlot::SetupAxisLimits(ImAxis_X1, time_run - 10.0, time_run, ImGuiCond_Always);
// ImPlot::SetupAxisLimits(ImAxis_Y1f,0,1);
ImPlot::SetNextLineStyle(IMPLOT_AUTO_COL, 2.0f);
ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.5f);
ImPlot::PlotLine("gpu", &nvidia_gpustate_list[i].Data[0].x, &nvidia_gpustate_list[i].Data[0].y, nvidia_gpustate_list[i].Data.size(), ImPlotLineFlags_Shaded, nvidia_gpustate_list[i].Offset, 2 * sizeof(float));
ImPlot::SetNextLineStyle(IMPLOT_AUTO_COL, 2.0f);
ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.5f);
ImPlot::PlotLine("mem", &nvidia_memstate_list[i].Data[0].x, &nvidia_memstate_list[i].Data[0].y, nvidia_memstate_list[i].Data.size(), ImPlotLineFlags_Shaded, nvidia_memstate_list[i].Offset, 2 * sizeof(float));
ImPlot::EndPlot();
}
}
}
}
}
}
}
ImGui::End();
// Rendering
ImGui::Render();
glViewport(0, 0, g_Width, g_Height);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Present
::SwapBuffers(g_MainWindow.hDC);
if (monitor_thread_has_started == false)
{
t1.detach();
monitor_thread_has_started = true;
}
usleep(10000);
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplWin32_Shutdown();
ImPlot::DestroyContext();
ImGui::DestroyContext();
CleanupDeviceWGL(hwnd, &g_MainWindow);
wglDeleteContext(g_hRC);
::DestroyWindow(hwnd);
::UnregisterClassW(wc.lpszClassName, wc.hInstance);
return 0;
}
// Helper functions
bool CreateDeviceWGL(HWND hWnd, WGL_WindowData *data)
{
HDC hDc = ::GetDC(hWnd);
PIXELFORMATDESCRIPTOR pfd = {0};
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
const int pf = ::ChoosePixelFormat(hDc, &pfd);
if (pf == 0)
return false;
if (::SetPixelFormat(hDc, pf, &pfd) == FALSE)
return false;
::ReleaseDC(hWnd, hDc);
data->hDC = ::GetDC(hWnd);
if (!g_hRC)
g_hRC = wglCreateContext(data->hDC);
return true;
}
void CleanupDeviceWGL(HWND hWnd, WGL_WindowData *data)
{
wglMakeCurrent(NULL, NULL);
::ReleaseDC(hWnd, data->hDC);
}
// Forward declare message handler from imgui_impl_win32.cpp
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
// Win32 message handler
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
return true;
switch (msg)
{
case WM_SIZE:
if (wParam != SIZE_MINIMIZED)
{
g_Width = LOWORD(lParam);
g_Height = HIWORD(lParam);
}
return 0;
case WM_SYSCOMMAND:
if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
return 0;
break;
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}
return ::DefWindowProcW(hWnd, msg, wParam, lParam);
}
Python
1
https://gitee.com/ymfjly/RobustVideoMatting.git
git@gitee.com:ymfjly/RobustVideoMatting.git
ymfjly
RobustVideoMatting
RobustVideoMattingGUI
RVMGUI.SCTOOL

搜索帮助