Home > Software engineering >  How to append data from opencv to csv using python?
How to append data from opencv to csv using python?

Time:11-23

can you help me in solving my code problem, where I want to append the data that I have created using opencv into the csv file. I followed several tutorials on the internet, but the results were not what I wanted, here is the complete code

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

the problem is when i run the script to add data to csv the result is not what i want, here is the example:

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

supposedly the result I want is the following:

268,268,96,96,50

258,258,60,60,50

260,260,102,102,50

what should i do to solve my problem? really appreciate your help.

CodePudding user response:

I'm assuming you are trying to write data into your CSV file? If so, move the file creation above the for loop as follows:

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

You do not need to explicitly close the file as the with() will handle this automatically. So for the code you have provided it would be:

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

CodePudding user response:

hi thanks to Mr Evans for the answer here is code:

import cv2
import os
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import csv
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

sample_image = 5
counter = 1
distance = 30
dir_name = 'C:/MyDrive'

video_capture = cv2.VideoCapture(0)
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}{distance}.{counter}.jpg', frame_copy)
        print(f'capture image->{counter}, with distance: {distance} cm')
        counter  = 1
        if counter > sample_image:
            break

    # Display the resulting frame
    cv2.imshow('Take Image', 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)))

# with open('test.csv', 'w', newline='') as f_output:
#csv_output = csv.writer(f_output)
with open('test.csv', 'a', newline='') 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)

        data = str(w) ',' str(h) ',' str(ew) ',' str(eh) ',' str(distance)

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

        print(data)
  • Related