Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freezes when called from thread #87

Open
gbar2019 opened this issue Oct 7, 2019 · 3 comments
Open

Freezes when called from thread #87

gbar2019 opened this issue Oct 7, 2019 · 3 comments

Comments

@gbar2019
Copy link

gbar2019 commented Oct 7, 2019

Hello all,
I am using cvui to display a window that will be used as UI and will be present in the user's screen while my application is running, to diplay button, info etc. Hence, i want to spawn a thread that updates the cvui window constantly, while my application is ran by other threads. So, i have created a class utilizing the UI, with function updateShowUI which updates and shows the UI through a thread. So what i do is:

UIComponent *uic = new UIComponent();
thread uiT(&UIComponent::updateShowUI, uic);

With the constructor of UIComponent class containing the cvui::init function etc:

UIComponent::UIComponent()
{
	UIframe = cv::Mat(cv::Size(650, 150), CV_8UC3);
	cvui::init(MAIN_WINDOW_NAME);
}

updateShowUI function is:

void UIComponent::updateShowUI()
{
while (true) {
		UIframe = cv::Scalar(49, 52, 49);

		cvui::text(UIframe, 40, 40, "To exit this app click the button below or press Q (shortcut for the button below).");
		if (cvui::button(UIframe, 300, 80, "&Quit")) {
			break;
		}
		cvui::update();

		cv::imshow(MAIN_WINDOW_NAME, UIframe);
		if (waitKey(20) == 27)
			break;
	}
}

When spawning a thread to run the above function, i find that it freezes when it tries to execute the "imshow" function. What can be the problem?

@Dovyski
Copy link
Owner

Dovyski commented Oct 8, 2019

Hello and thank you for your contribution!

I don't think cvui can actually work in a multi-thread environment since OpenCV's cv::imshow() itself is not thread-enabled. I have tried to do what you are trying to do in the past, but nothing I have tried actually worked.

I am afraid this is a limitation beyond what cvui can overcome. However, if you find a way to make your demo work, it would be a pleasure to know more about your approach, so I can create a demo to help other developers.

@gbar2019
Copy link
Author

gbar2019 commented Oct 8, 2019

In fact, if you initialize with
cvui::init(MAIN_WINDOW_NAME,-1,false); instead, it will work. But be advised that it is static, so you cannot actually interfere with anything like buttons and trackbars, you can only show static content like images and video...
If i find a way to introduce user interference i will let you know...Thank you!

@sim1234
Copy link

sim1234 commented Jun 24, 2020

I managed to run the UI in a python thread with no problems. This is the thread function:

frame = numpy.zeros((480, 800, 3), numpy.uint8)
state = [False]
cv2.namedWindow("test", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("test", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cvui.init("test", 0, False)
while True:
    frame[:] = (49, 52, 49)
    cvui.checkbox(frame, 10, 15, "My checkbox", state)
    cvui.update("test")
    cv2.imshow("test", frame)
    if cv2.waitKey(1) == 27 or cv2.getWindowProperty("test", cv2.WND_PROP_VISIBLE) < 1:
          break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants