Home > Blockchain >  TypeError: 'numpy.uint8' object is not iterable and put csv file didn't work
TypeError: 'numpy.uint8' object is not iterable and put csv file didn't work

Time:01-04

im try to put my loop numpy data Modus_citra into csv file, but i've tried using numpy and using normal write csv didn't work

import glob
import cv2
from os import listdir
from os.path import isfile, join
import os
import numpy as np
from sklearn.utils.validation import check_array
import csv
import pandas as pd




def find_mode(np_array) :                                    
        vals,counts = np.unique(np_array, return_counts=True)    
        index = np.argmax(counts)                                
        return(vals[index])                                      

folder = ("C:/Users/ROG FLOW/Desktop/Untuk SIDANG TA/Sudah Aman/testbikincsv/folderdatacitra/*.jpg")
for file in glob.glob(folder):
    image = cv2.imread(file)
    rows = image.shape[0]
    cols = image.shape[1]
    middlex = cols/2                    
    middley = rows/2                     
    middle = [middlex,middley]
    titikawalx = middlex - 10             
    titikawaly = middley - 10
    titikakhirx = middlex   10           
    titikakhiry = middley   10
    crop = image[int(titikawaly):int(titikakhiry), int(titikawalx):int(titikakhirx)]
    c = cv2.cvtColor(crop, cv2.COLOR_BGR2HSV)
    H,S,V = cv2.split(c)
    Modus_citra = (find_mode(H))
    data = (Modus_citra)
    with open("foo.csv", 'w') as file:
        writer = csv.writer(file)
        writer.writerows(data)

error = 'numpy.uint8' object is not iterable

foo.csv = from pictures 60 40 19 11 please can someone help me ? i Appreciate every help

CodePudding user response:

According to the edit, you can try:

folder = "C:/Users/ROG FLOW/Desktop/Untuk SIDANG TA/Sudah Aman/testbikincsv/folderdatacitra/*.jpg"

all_data = []
for file in glob.glob(folder):
    # ...
    Modus_citra = find_mode(H)
    all_data.append(Modus_citra)  # <-- add the numpy.uint8 to the all_data list

# after the loop write the data to the CSV file:
with open("foo.csv", "w") as file:
    writer = csv.writer(file)
    for data in all_data:
        writer.writerow([data])
  • Related