如何使用 python 将数据从 opencv 附加到 csv?

How to append data from opencv to csv using python?

你能帮我解决我的代码问题吗,我想将我使用 opencv 创建的数据附加到 csv 文件中。看了网上的几个教程,结果都不是我想要的,这里是完整代码

import cv2
import os
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from csv import writer


faceCascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eyeCascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + 'haarcascade_eye.xml')

font = cv2.FONT_HERSHEY_SIMPLEX

sampel_foto = 5
counter = 1
jarak = 50
dir_name = 'C:/MyDrive'

video_capture = cv2.VideoCapture(1)
while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    frame_copy = frame.copy()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray, scaleFactor=1.2, flags=cv2.CASCADE_SCALE_IMAGE)
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 3)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        cv2.putText(frame, 'face: '+str(w)+','+str(h),
                    (10, 30), font, 0.6, (255, 255, 255), 2)
        eyes = eyeCascade.detectMultiScale(roi_gray)
        # Draw a rectangle around the eyes
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
            cv2.putText(frame, 'eyes: '+str(ew)+','+str(eh),
                        (10, 50), font, 0.6, (255, 255, 255), 2)

    if cv2.waitKey(1) & 0xFF == ord('c'):

        cv2.imwrite(f'{dir_name}{jarak}.{counter}.jpg', frame_copy)
        print(f'simpan foto ke-{counter} dengan jarak: {jarak} cm!')
        counter += 1
        if counter > sampel_foto:
            break

    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

# Get list of all files only in the given directory
imfilelist = [os.path.join(dir_name, f)
              for f in os.listdir(dir_name) if f.endswith(".jpg")]
#list_of_files = filter( lambda x: os.path.isfile(os.path.join(dir_name, x)), os.listdir(dir_name) )

# Sort list of files based on last modification time in ascending order
list_of_files = sorted(
    imfilelist, key=lambda x: os.path.getmtime(os.path.join(dir_name, x)))

for el in list_of_files:
    imagen = cv2.imread(el)
    gray = cv2.cvtColor(imagen, cv2.COLOR_BGR2GRAY)

    # Detect faces
    faces = faceCascade.detectMultiScale(
        gray, scaleFactor=1.2, flags=cv2.CASCADE_SCALE_IMAGE)
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(imagen, (x, y), (x + w, y + h), (0, 255, 0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = imagen[y:y+h, x:x+w]
        roi_color = imagen[y:y + h, x:x + w]

    # Detect eyes
    eyes = eyeCascade.detectMultiScale(roi_gray)
    # Draw a rectangle around the eyes
    for (ex, ey, ew, eh) in eyes:
        cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
        cv2.putText(imagen, '', (x + ex, y + ey), 1, 1, (0, 255, 0), 1)

    data = str(w)+','+str(h)+','+str(ew)+','+str(eh)+','+str(jarak)
    print(data)



# Create a file object for this file
with open('test.csv', 'a') as f_object:
  
    # Pass this file object to csv.writer()
    writer_object = writer(f_object)
  
    writer_object.writerow(data_pixel)
  
    #Close the file object
    f_object.close()

问题是当我 运行 将数据添加到 csv 的脚本时结果不是我想要的,这里是示例:

2,5,3,",",2,5,3,",",5,0,",",5,0,",",5,0

我想要的结果应该是这样的:

268,268,96,96,50

258,258,60,60,50

260,260,102,102,50

我应该怎么做才能解决我的问题?非常感谢您的帮助。

我假设您正在尝试将 data 写入您的 CSV 文件?如果是这样,请将文件创建移动到 for 循环之上,如下所示:

with open('test.csv', 'a', newline='') as f_output:
    csv_output = csv.writer(f_output)
    
    for el in list_of_files:
        .
        .
        .
        
        csv_output.writerow([w, h, ew, eh, jarak])
        

您不需要明确关闭文件,因为 with() 会自动处理。因此,对于您提供的代码,它将是:

import cv2
import os
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from csv import writer


faceCascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eyeCascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + 'haarcascade_eye.xml')

font = cv2.FONT_HERSHEY_SIMPLEX

sampel_foto = 5
counter = 1
jarak = 50
dir_name = 'E:/OneDrive - Institut Teknologi Sepuluh Nopember/Kuliah Teknik Elektro/Semester 3/SEC/Tugas_SEC_Devis/dataset_foto/50/'

video_capture = cv2.VideoCapture(1)
while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    frame_copy = frame.copy()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray, scaleFactor=1.2, flags=cv2.CASCADE_SCALE_IMAGE)
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 3)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        cv2.putText(frame, 'face: '+str(w)+','+str(h),
                    (10, 30), font, 0.6, (255, 255, 255), 2)
        eyes = eyeCascade.detectMultiScale(roi_gray)
        # Draw a rectangle around the eyes
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
            cv2.putText(frame, 'eyes: '+str(ew)+','+str(eh),
                        (10, 50), font, 0.6, (255, 255, 255), 2)

    if cv2.waitKey(1) & 0xFF == ord('c'):

        cv2.imwrite(f'{dir_name}{jarak}.{counter}.jpg', frame_copy)
        print(f'simpan foto ke-{counter} dengan jarak: {jarak} cm!')
        counter += 1
        if counter > sampel_foto:
            break

    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

# Get list of all files only in the given directory
imfilelist = [os.path.join(dir_name, f)
              for f in os.listdir(dir_name) if f.endswith(".jpg")]
#list_of_files = filter( lambda x: os.path.isfile(os.path.join(dir_name, x)), os.listdir(dir_name) )

# Sort list of files based on last modification time in ascending order
list_of_files = sorted(
    imfilelist, key=lambda x: os.path.getmtime(os.path.join(dir_name, x)))

# Create a file object for this file

with open('test.csv', 'a') as f_object:
    # Pass this file object to csv.writer()
    writer_object = writer(f_object)
    
    for el in list_of_files:
        imagen = cv2.imread(el)
        gray = cv2.cvtColor(imagen, cv2.COLOR_BGR2GRAY)

        # Detect faces
        faces = faceCascade.detectMultiScale(
            gray, scaleFactor=1.2, flags=cv2.CASCADE_SCALE_IMAGE)
        # Draw a rectangle around the faces
        for (x, y, w, h) in faces:
            cv2.rectangle(imagen, (x, y), (x + w, y + h), (0, 255, 0), 2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = imagen[y:y+h, x:x+w]
            roi_color = imagen[y:y + h, x:x + w]

        # Detect eyes
        eyes = eyeCascade.detectMultiScale(roi_gray)
        # Draw a rectangle around the eyes
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
            cv2.putText(imagen, '', (x + ex, y + ey), 1, 1, (0, 255, 0), 1)

        writer_object.writerow([w, h, ew, eh, jarak])