python TypeError:“numpy.uint8”对象不可迭代,并且放置csv文件不起作用

sr4lhrrt  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(270)

我尝试将循环numpy数据Modus_citra放入csv文件中,但我尝试使用numpy和使用正常写入csv不起作用

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)

错误= 'numpy.uint8'对象不可迭代
foo.csv =从图片60 40 19 11请有人能帮助我吗?我感谢每一个帮助

ar5n3qh5

ar5n3qh51#

根据编辑,您可以尝试:

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])

相关问题