Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert cv2.umat to numpy array

Processed_image() function returns a cv2.Umat type value which is to be reshaped from 3 dimensions(h, ch, w) to 4 dimensions(h, ch, w, 1) so i need it to be converted to numpy array or also if possible help me to directally rehshape cv2.umat type variable to be directally reshaped and converted to a pytorch tensor and can be assigned to reshaped_image_tensor.

img_w=640
img_h=640
img_ch=3
umat_img = cv2.UMat(img)
display_one(umat_img, "RESPONSE")    #function created by me to display image
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
with torch.no_grad():
    processed_img = preprocess_image(umat_img, model_image_size = (img_h, img_ch, img_w))
    #___________write YOUR CODE here________
    reshaped_images_tensor = torch.from_numpy(processed_img.reshape(img_h, img_ch, img_w, 1)).float().to(device)      #images_tensor.reshape(img_h, img_ch, img_w, 1)
    outputs = model(reshaped_images_tensor)
    _, predicted = torch.max(outputs, 1)
    c = predicted.squeeze()
    output_probability(predicted, processed_img, umat_img)
if ord('q')==cv2.waitKey(10):
    exit(0)
like image 791
Aayush Sahu Avatar asked Mar 13 '26 04:03

Aayush Sahu


1 Answers

I didn't quite catch your question, but you can get numpy data of an opencv's umat with "get()" like this

and you should probably permute your tensor before feeding it into your model.

like image 154
Separius Avatar answered Mar 14 '26 16:03

Separius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!