I want to create a csv file which should have the coordinates of each object as i am getting in my python shell window.

import cv2
import pandas as pd


# capture frames from a video
cap = cv2.VideoCapture('video.avi')

# Trained XML classifiers describes some features of some object we want 
to detect
car_cascade = cv2.CascadeClassifier('cars.xml')

no_obj_det=0
frames_got_processed = 0
frame_processed = []
number_of_object_detected= []

# loop runs if capturing has been initialized.
while True:
# reads frames from a video
  try:
    ret, frames = cap.read()
    frames_got_processed += 1
    # convert to gray scale of each frames
    gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
    # Detects cars of different sizes in the input image
    cars = car_cascade.detectMultiScale(gray, 1.1, 1)
    # To draw a rectangle in each cars
    for (x,y,w,h) in cars:
        cv2.rectangle(frames,(x,y),(x+w,y+h),(0,0,255),2)

    cv2.imshow('video2', frames)
    if cv2.waitKey(33) == 27:
        break

    # loop to count the number of objects detected at every 5th frame
    if frames_got_processed % 5 == 0:

        print "appended in frame 
        number",frames_got_processed,len(cars),cars
        frame_processed.append(frames_got_processed)
        number_of_object_detected.append(len(cars))

df.to_csv('example.csv')


# De-allocate any associated memory usage
cv2.destroyAllWindows()

Output on python shell same output i want in my csv file [1]: https://i.stack.imgur.com/vfPEP.png

1 Answers

0
Martin Evans On

As you probably need to write data to the CSV continuously, it is probably better to do this as you go rather than trying to append all of the data and then write it at the end. Doing it this way would avoid you eventually running out of memory.

The Python csv library could be used to do this as follows:

import cv2
import csv

# capture frames from a video
cap = cv2.VideoCapture('video.avi')

# Trained XML classifiers describes some features of some object we want to detect
car_cascade = cv2.CascadeClassifier('cars.xml')

frames_got_processed = 0

with open('example.csv', 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)

    # loop runs if capturing has been initialized.
    while True:
    # reads frames from a video
        try:
            ret, frames = cap.read()
            frames_got_processed += 1
            # convert to gray scale of each frames
            gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
            # Detects cars of different sizes in the input image
            cars = car_cascade.detectMultiScale(gray, 1.1, 1)
            # To draw a rectangle in each cars
            for (x,y,w,h) in cars:
                cv2.rectangle(frames,(x,y),(x+w,y+h),(0,0,255),2)

            cv2.imshow('video2', frames)
            if cv2.waitKey(33) == 27:
                break

            # loop to count the number of objects detected at every 5th frame
            if frames_got_processed % 5 == 0:
                print "appended in frame number", frames_got_processed, len(cars), cars
                csv_output.writerow([frames_got_processed, len(cars)] + list(cars))
        except:
            pass

# De-allocate any associated memory usage
cv2.destroyAllWindows()

This should give you a row containing the frame number, the number of cars, followed by the co-ordinates for each car.