1

I’m trying to develop a text detection application with Pytorch and Opencv in Python. I can use Pytorch tensor with Opencv like below.

val = y[0,:,:,0].data.cpu().numpy()
cv2.threshold(val , 0.4, 1, 0)

But it takes a lot of time. I need to do this operation by using the tensor object. How can I do that?

1 Answer 1

4

Given that the last 0 in your threshold call means cv.THRESH_BINARY, it follows this function:

enter image description here

As your maxval is set to 1, you can replace this threshold call with something like this:

(y[0,:,:,0] > 0.4).float()

I am casting to float, but you can change that as you wish, ofc. Or even to something like:

(y[0,:,:,0] > 0.4).to(dtype=y.dtype)

so that it will remain with the same data type.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you four your answer. I think it can work but there are more opencv functions in my code and with your way I have to change all of these methods. I think It would be better if I know how I can give tensor data to opencv methods.
@EmreBEGEN Without converting to numpy, you won't be able to do it, and you shouldn't need it. Walk us through your use case and we may be able to give you an advice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.