PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 28, 2022

[FIXED] How to convert a csv dataset to a numpy array readable by opencv?

 August 28, 2022     csv, image-processing, numpy, opencv, python     No comments   

Issue

I have a csv dataset namely sign language mnist dataset. It contains information of 28x28 images with its pixel information. I would like to convert this dataset into a numpy array that can be read by opencv3. A numpy array where i can manipulate through opencv. I would like to apply histogram of oriented gradients into this dataset through opencv.

I have already successfully converted it into a numpy array and was able to isolate a row and reshape it into a 28x28 array. The row has a label at the beginning and i have also split it with only the 28x28 pixel data. I have used matplotlib to successfully plot it however I can't seem to use cv2.imshow() on the numpy array. I also know that opencv can only read certain datatypes and i have tried converting my data numpy array into both int32 and float but it still didnt work.

Here is how the CSV file looks like: The CSV file showing the first 4 rows

Here is the file: Sign Language CSV dataset

Site Where I got the Dataset[Kaggle.com - Sign Language Mnist]

The 1st column is the label for the image and the rest are the pixels. It goes up to the pixel 784 column.

Here is my code:

import cv2

data = np.genfromtxt('sign_mnist_test.csv', delimiter=',', skip_header=1)

labels = data[1:, 0].astype(np.float)
value1 = data[0, 1:].astype(np.float) #the 1st row of the dataset

reshaped = np.reshape(value1, (28, 28))

cv2.imshow('image', reshaped)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here is how my numpy array for the 1st row looks like:

Numpy array of the 1st row

Here is the output:

Output Image

I expect it to show a 28x28 training image(a hand image) however it only shows a plain white 28x28 image with no features.

plt.imshow(reshaped, cmap="Greys")
plt.show()

Output using matplotlib

I am using PyCharm as IDE.

I am also looking for alternative options so that i can use my dataset for openCV if there is any solution that is better.


Solution

The problem is that you have made it a float on this line:

value1 = data[0, 1:].astype(np.float)

You need to preferably pass an np.uint8 to cv2.imshow().



Answered By - Mark Setchell
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing