how to get this print data in csv file - python

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

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.

Related

Is there a way for me to crop in the middle of a video to run my vehicle detection model on that part

I want to run inference of my vehicle detection model only on the center of a video. like you see in this picture. The red zone is only where I want my model to run. I wanted to know if there's a way for me to do that. to specify a zone for my model to work.
This is the code that I have used for the crop which kinda works. I just need to implement it with my vehicle detection model which I will post right after I finish
# Import packages
import cv2
import numpy as np
# Open the video
cap = cv2.VideoCapture('test.mp4')
# Initialize frame counter
cnt = 0
# Some characteristics from the original video
w_frame, h_frame = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps, frames = cap.get(cv2.CAP_PROP_FPS), cap.get(cv2.CAP_PROP_FRAME_COUNT)
# Here you can define your croping values
x,y,h,w = 0,0,600,1000
# output
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
#fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter('result.mp4', fourcc, fps, (w, h))
# Now we start
while(cap.isOpened()):
ret, frame = cap.read()
cnt += 1 # Counting frames
# Avoid problems when video finish
if ret==True:
# Croping the frame
crop_frame = frame[y:y+h, x:x+w]
# Percentage
xx = cnt *100/frames
print(int(xx),'%')
# Saving from the desired frames
#if 15 <= cnt <= 90:
# out.write(crop_frame)
# I see the answer now. Here you save all the video
out.write(crop_frame)
# Just to see the video in real time
cv2.imshow('frame',frame)
cv2.imshow('croped',crop_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()

Frame Extraction at Fixed FPS Value

While extracting frames from a video using OpenCV, how can I set a particular value at which the frame extraction will occur?
I have seen many available sample codes for image extraction, but they are not shown any option for the frame rate.
There are many ways of frame extraction, one is to use ffmg for frame extraction.
other is, You can try this code, but we can't use any random value that you will understand while trying at different values.
change directories ap per your system.
import math
count = 0
videoFile = "train/train.mp4"
cap = cv2.VideoCapture(videoFile)
frameRate = cap.get(5) #frame rate
x=1
while(cap.isOpened()):
frameId = cap.get(1)
ret, frame = cap.read()
if (ret != True):
break
else (frameId % math.floor(frameRate) == 0):
filename ="train/frame2/frame%d.jpg" % count;count+=1
cv2.imwrite(filename, frame)
cap.release()
print ("Done!")

Real time taking pictures with different exposure times and automatically with OpenCV

I'm trying to make a python program with OpenCV, which opens the webcam and takes several images with different exposures in real time (40ms,95ms,150ms) and averages them in the end.
I tried to create a loop in which I change the exposure time, update the rendering (frame) and save it in a list, but the problem is that the display remains static and the rendering hardly changes (which gives after merging the images an image whose exposure time is almost 40)
I supposed that after setting exposure time, the frame update needs some time so I added the method time.sleep to suspend the execution for 3 seconds, but it was in vain.
Here is my code
import numpy as np
import cv2
import os
import time
capture = cv2.VideoCapture(0, cv2.CAP_V4L2)
while True:
(grabbed, frame) = capture.read()
if not grabbed:
break
# Resize frame
width = 1500
height = 1000
dim = (width, height)
frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
cv2.imshow('RGB', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) == ord('h') or cv2.waitKey(1) == ord('H'):
#repertory = input("Enter the name of the directory: ")
if not os.path.exists(repertory):
os.mkdir(repertory)
exposure = [40,95,150]
ims = []
for i in exposure:
capture.set(cv2.CAP_PROP_EXPOSURE, i) # Setting Exposure
(grabbed, frame) = capture.read() # Updating frame
if grabbed:
cv2.imshow('RGB', frame) #Display
ims.append(frame)
# Convert to numpy
ims = np.array([np.array(im) for im in ims])
# average et conversion en uint8
imave = np.average(ims, axis=0)
imave = imave.astype(np.uint8)
# image HDR
cv2.imwrite(repertory + '/' + repertory + '_HDR8.jpg', imave)
capture.release()
cv2.destroyAllWindows()
Is there an optimal solution that allows to take pictures with differents exposure time in real time and in an automatic way?

How to crop a video (such as a zoom call) into multiple files?

I have conferance call video with different people's tiles arranged on a grid.
Example:
gallery view zoom
Can I crop every video tile to a separate file using python or nodejs?
Yes, you can achieve that using OpenCV library
Read the video in OpenCV using VideoCapture API. Note down framerate while reading.
Parse through each frame and crop the frame:
Write the frame in a video using OpenCV VideoWriter
Here is the example code using (640,480) to be the new dimensions:
cap = cv2.VideoCapture(<video_file_name>)
fps = cap.get(cv2.CAP_PROP_FPS)
out = cv2.VideoWriter('<output video file name>, -1, fps, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
crop_frame = frame[y:y+h, x:x+w]
# write the crooped frame
out.write(crop_frame)
# Release reader wand writer after parsing all frames
cap.release()
out.release()
Here's the code (tested). It works by initialising a number of video outputs, then for each frame of the input video: cropping the region of interest (roi) and assigning each to the relevent output video. You might need to make tweaks depending on input video dimensions, number of times, offsets etc.
import numpy as np
import cv2
import time
cap = cv2.VideoCapture('in.mp4')
ret, frame = cap.read()
(h, w, d) = np.shape(frame)
horiz_divisions = 5 # Number of tiles stacked horizontally
vert_divisions = 5 # Number of tiles stacked vertically
divisions = horiz_divisions*vert_divisions # Total number of tiles
seg_h = int(h/vert_divisions) # Tile height
seg_w = int(w/horiz_divisions) # Tile width
# Initialise the output videos
outvideos = [0] * divisions
for i in range(divisions):
outvideos[i] = cv2.VideoWriter('out{}.avi'.format(str(i)),cv2.VideoWriter_fourcc('M','J','P','G'), 10, (seg_w,seg_h))
# main code
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
vid = 0 # video counter
for i in range(vert_divisions):
for j in range(horiz_divisions):
# Get the coordinates (top left corner) of the current tile
row = i * seg_h
col = j * seg_w
roi = frame[row:row+seg_h,col:col+seg_w,0:3] # Copy the region of interest
outvideos[vid].write(roi)
vid += 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release all the objects
cap.release()
for i in range(divisions):
outvideos[i].release()
# Release everything if job is finished
cv2.destroyAllWindows()
Hope this helps!

how to automatically select the best frame in a video using python?

We are doing a project on license plate recognition using python(2.7.12). We have divided the video into frames using the following code:
import cv2 #importing opencv library
import numpy #importing numpy
cap = cv2.VideoCapture('C:/Python27/project/license.avi') #read the video
success,image = cap.read() #divide into frames
count = 0
success = True
while (cap.isOpened()):
success,image = cap.read()
print 'Read a new frame: ', success #when frame has been read successfully
if (type(image) == type(None)): #check for invalid frame
break
else:
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #convert frame into grey
cv2.imwrite("frame%d.jpg" % count, gray_image) # save frame as JPEG file
count += 1 #repeat for all the frames
cap.release()
We are trying to get the best frame(with high quality).Is it possible to automatically select a frame that has the complete license plate?
Any suggestions would be helpful.

Categories