2

I want to write in csv file 3 columns. One with a number, one with a purpose (training/testing) and the third one with a numpy array.

I tried this code

with open('resources/new_file.csv', 'w', newline='') as csvfile:
    row_writer = csv.writer(csvfile, delimiter=',')
    for i, row in enumerate(data):
        image = np.fromstring(row, dtype='uint8', sep=' ')
        image = np.reshape(image, (48, 48))
        x_train[i] = image
        img = image
        face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
        faces = face_cascade.detectMultiScale(img, 1.1, 2)
        for (x, y, w, h) in faces:
            new_img = cv2.resize(img[y: y + h, x:x + w], dsize=(48, 48), interpolation=cv2.INTER_CUBIC)
            image_str = new_img.flatten()
            row_writer.writerow([emotion[i], purpose[i], image_str])
csvfile.close()

but after opening the csv file every row is like that (exactly like that with the ...)

3,Training,[ 49  72  85 ... 192 188 185]

Also notice that between some numbers, there is a double space.

How can i write the whole thing to be like

3,Training,49 72 85 12 0 [here every other number from array] 192 188 185
4
  • Right after image_str = new_img.flatten(), please add print(image_str); what does that give you? Commented May 25, 2020 at 19:24
  • 3
    ' '.join(map(str,image_str))? Commented May 25, 2020 at 19:36
  • Yes, because image_str is a numpy.ndarray, and then you simply write the string representation of the array Commented May 25, 2020 at 20:03
  • @wwii that was it. If you post your comment as an answer, I could set it as a solution. Commented May 25, 2020 at 20:12

1 Answer 1

1

To get every number that you have in array, you can try setting

import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)

I think double spaces are because to account the 3 digit numbers also present in that array because you used flatten. If not used flatten then in truncated form it shows with spaces to account for readability.

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

Comments

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.