How to extract videos frames in a folder seperately? - python

I need to extract video frames in a folder. I have 90 videos in a folder and would like to extract each video frames in a seperate folder one time.
How could I do that?
Below is the code I tried so far but it works for one video only. I would like to do this for all the videos in a folder one time:
import os
import numpy as np
import cv2
from glob import glob
def create_dir(path):
try:
if not os.path.exists(path):
os.makedirs(path)
except OSError:
print(f"ERROR: creating directory with name {path}")
def save_frame(video_path, save_dir, gap=1):
name = video_path.split("/")[-1].split(".")[0]
save_path = os.path.join(save_dir, name)
create_dir(save_path)
cap = cv2.VideoCapture(video_path)
idx = 0
while True:
ret, frame = cap.read()
if ret == False:
cap.release()
break
if idx == 0:
cv2.imwrite(f"{save_path}/{idx}.png", frame)
else:
if idx % gap == 0:
cv2.imwrite(f"{save_path}/{idx}.png", frame)
idx += 1
if __name__ == "__main__":
video_paths = glob("D:/test_/crops/*")
save_dir = "save"
for path in video_paths:
save_frame(path, save_dir, gap=1)

import os
import numpy as np
import cv2
from glob import glob
def create_dir(path):
try:
if not os.path.exists(path):
os.makedirs(path)
except OSError:
print(f"ERROR: creating directory with name {path}")
def save_frame(video_path, save_dir, gap=1):
name = video_path.split("/")[-1].split(".")[0]
save_path = os.path.join(save_dir, name)
create_dir(save_path)
cap = cv2.VideoCapture(video_path)
idx = 0
while True:
ret, frame = cap.read()
if ret == False:
cap.release()
break
if idx == 0:
cv2.imwrite(f"{save_path}/{idx}.png", frame)
else:
if idx % gap == 0:
cv2.imwrite(f"{save_path}/{idx}.png", frame)
idx += 1
if __name__ == "__main__":
video_paths = glob("D:/test_/crops/*")
save_dir = "save"
for path in video_paths:
save_frame(path, save_dir, gap=1)

Related

Check if record exists in python. If exists then skip else process

I have a function for extracting frames from videos. I have a csv file which has the names of the videos already processed. I want to check if the name of a newly added video file is present in the csv file. If present then exit the code else process the function to extract frames from the new video
def extractFrames(m):
global vid_name
vid_files=glob(m)
for v_f in range(len(vid_files)):
print("path of video========>>>>.",vid_files[v_f])
#latest_file=max(vid_files, key=os.path.getctime)
#print(latest_file)
v1=os.path.basename(vid_files[v_f])
try:
vid_name = os.path.splitext(v1)[0]
vidcap = cv2.VideoCapture(vid_files[v_f])
except cv2.error as e:
print(e)
except:
print('error')
#condition
fsize=os.stat(vid_files[v_f])
print('=============size of video ===================:' , fsize.st_size)
try:
if (fsize.st_size > 1000):
fps = vidcap.get(cv2.CAP_PROP_FPS) # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
frameCount = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frameCount/fps
minutes = int(duration/60)
print('fps = ' + str(fps))
print('number of frames = ' + str(frameCount))
print('duration (S) = ' + str(duration))
if (duration > 1):
success,image = vidcap.read()
count=0
success=True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count % 10 == 0 or count ==0:
target_non_target(img_name, image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
except:
print("error")
print('finished processing video ', vid_files[v_f])
with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs"+'/video_info.csv', 'a') as csv_file:
fieldnames = ['Video_Name','Process']
file_is_empty = os.stat("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs"+'/video_info.csv').st_size == 0
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
if file_is_empty:
writer.writeheader()
writer.writerow({'Video_Name':vid_name,'Process':'done'})
if __name__ == "__main__":
x="C:\\Python36\\videos\\*.mp4"
extractFrames(x)
Suppose a folder has 2 videos V1 and V2 from which frames have already been extracted and the names V1 and V2 are added in the csv file. Now when i add video V3 the code should check if V3 exists already in the csv. If it exists then the code should be skipped else frames from V3 should be processed and V3 should be added in the csv file after extraction of the frames
Without the details you have code like this
def extractFrames(m):
# do stuff
vid_files=glob(m)
for v_f in range(len(vid_files)):
#find vid_name
#do stuff
save_as_done(vid_name)
if __name == '__main__':
x="C:\\Python36\\videos\\*.mp4"
extractFrames(x)
If you pass in a list of things that have been done, something like
done = ['first.mp4', 'second.mp4']
you can check if a filename has been done like this:
>>> 'first.mp4' in done
True
So, if you save the filenames (fully pathed) of what you've done to a file and load them into a list, like this
def load_done_list():
with open('video_info.csv') as f: #or full path, maybe pass in the file name?
return f.readlines()
you can check the list
def extractFrames(m, done):
# do stuff
vid_files=glob(m)
for v_f in range(len(vid_files)):
#find vid_name
if vid_name not in done: #,--- check if done already
#do stuff
save_as_done(vid_name)
if __name == '__main__':
x="C:\\Python36\\videos\\*.mp4"
done = load_done_list() #<--- You need to load this into a list
extractFrames(x, done) #<--- and pass it in to your function
This need something that just saves the filenames as they are done:
def save_as_done(vid_name):
with open('video_info.csv', 'a') as f: #maybe pass in the file name so you only define it once?
f.write(vid_name + '\n')
I haven't filled in all the details, but have shown where you can do loading and saving and checking.
The written file only has the filenames in - there doesn't seem much point in having "done" on the end of each line.
This will keep opening and closing the file as the files are processed. This may slow thing down but might not matter: you could pass in a file handle to write to, to keep it open. You have options.
I think you might need a function to get the list completed/done videos from the csv file.
Something like this, might need to tweak a bit for the header row.
def get_completed_videos():
completed_videos = []
with open(".../video_info.csv") as csv_file:
for row in csv.reader(csv_file):
completed_videos.append(row[0])
return completed_videos
Then exclude them in the extracting func
def extractFrames(m):
global vid_name
vid_files=glob(m)
complete_videos = get_completed_videos()
new_vid_files = [x for x in vid_files if x not in complete_videos]
...
def extractFrames(m,done):
global vid_name
vid_files=glob(m)
for v_f in range(len(vid_files)):
print("path of video========>>>>.",vid_files[v_f])
v1=os.path.basename(vid_files[v_f])
vid_name = os.path.splitext(v1)[0]
if vid_name not in done:
try:
vidcap = cv2.VideoCapture(vid_files[v_f])
except cv2.error as e:
print(e)
except:
print('error')
#condition
fsize=os.stat(vid_files[v_f])
print('=============size of video ===================:' , fsize.st_size)
try:
if (fsize.st_size > 1000):
fps = vidcap.get(cv2.CAP_PROP_FPS) # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
frameCount = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frameCount/fps
minutes = int(duration/60)
print('fps = ' + str(fps))
print('number of frames = ' + str(frameCount))
print('duration (S) = ' + str(duration))
if (duration > 1):
success,image = vidcap.read()
count=0
success=True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count % 10 == 0 or count ==0:
target_non_target(img_name, image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
except:
print("error")
print('finished processing video ', vid_files[v_f])
with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv', 'a') as csv_file:
fieldnames = ['Video_Name','Process']
file_is_empty = os.stat("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv').st_size == 0
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
if file_is_empty:
writer.writeheader()
writer.writerow({'Video_Name':vid_name,'Process':'done'})
if __name__ == "__main__":
x="C:\\Python36\\videos\\*.mp4"
y="C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\video_info.csv"
done=list(y)
extractFrames(x,done)

Extract frames from video into specific folder

I want to extract frames from 3 videos into 3 different folder. Each folder has the frames of their corresponding video file. I am able to access my objective for only the 3rd video. How can I extract the frames for the first 2 videos as well
I have made the folders having names as per the video files till now. Developed the code for frame extraction but can extract only from the last video. Below is my code
import cv2
import glob
from glob import glob
import os
import shutil
def extractFrames(m,n):
if not os.path.exists:
os.makedirs(n)
vid_files=glob(m)
print(vid_files)
for v_f in range(len(vid_files)):
v1=os.path.basename(vid_files[v_f])
print(v1)
vid_name = os.path.splitext(v1)[0]
print(vid_name)
output = n +'\\video_' + vid_name
os.makedirs(output)
print(output)
vidcap = cv2.VideoCapture(vid_files[v_f])
print(vidcap)
success,image = vidcap.read()
seconds = 2
fps = vidcap.get(cv2.CAP_PROP_FPS) # Gets the frames per second
multiplier = fps * seconds
count=0
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
image_path = output + "/" + img_name
frameId = int(round(vidcap.get(1)))
success,image = vidcap.read()
if frameId % multiplier == 0:
cv2.imwrite(filename = image_path, img = image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
print('finished processing video {0} with frames {1}'.format(vid_files[v_f], count))
return output
x=("C:\\Python36\\videos\\*.mp4")
y=("C:\\Python36\\videos\\videos_new")
z=extractFrames(x,y)
If there are 3 videos namely video1,video2,video3. I want to extract the corresponding frames into their specific folders i.e video1 folder,video2 folder, video3 folder. Currently I am able to extract the frames for only the 3rd video into folder video3. How can I do it for video1 and video2 as well
Your indentation on the part from vidcap = ... down is off. Therefor only the last file in the for-loop is used.
import cv2
import glob
from glob import glob
import os
import shutil
def extractFrames(m,n):
if not os.path.exists:
os.makedirs(n)
vid_files=glob(m)
print(vid_files)
for v_f in range(len(vid_files)):
v1=os.path.basename(vid_files[v_f])
print(v1)
vid_name = os.path.splitext(v1)[0]
print(vid_name)
output = n +'\\video_' + vid_name
os.makedirs(output)
print(output)
vidcap = cv2.VideoCapture(vid_files[v_f])
print(vidcap)
success,image = vidcap.read()
seconds = 2
fps = vidcap.get(cv2.CAP_PROP_FPS) # Gets the frames per second
multiplier = fps * seconds
count=0
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
image_path = output + "/" + img_name
frameId = int(round(vidcap.get(1)))
success,image = vidcap.read()
if frameId % multiplier == 0:
cv2.imwrite(filename = image_path, img = image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
print('finished processing video {0} with frames {1}'.format(vid_files[v_f], count))
return output # indent this less
x=("C:\\Python36\\videos\\*.mp4")
y=("C:\\Python36\\videos\\videos_new")
z=extractFrames(x,y)

Converting images in a folder to grayscale using python and opencv and writing it to a specific folder

import glob
import cv2
import os
import numpy as np
from PIL import Image
images=[]
images=np.array(images)
path='C:\Users\Quantum\Desktop\test'
count=0
images = [cv2.imread(file,0) for file in glob.glob("E:\homework\Computer vision\Faces\*.jpg")]
for i in range(len(images)):
# im = Image.fromarray(images[i])
# cv2.imwrite(str(path) + '.jpg', images[count])
cv2.imwrite(os.path.join(path, 'pic.jpg'), images[count])
count+=1
Trying to select all the images from a folder and the images are getting selected and are converted to grayscale although I dont know how to write those images to a specific folder.Kindly help
#multiple image conversions
import cv2
import os,glob
from os import listdir,makedirs
from os.path import isfile,join
path = '/root/Desktop/Anil' # Source Folder
dstpath = '/root/Desktop/Anil2' # Destination Folder
try:
makedirs(dstpath)
except:
print ("Directory already exist, images will be written in same folder")
# Folder won't used
files = list(filter(lambda f: isfile(join(path,f)), listdir(path)))
for image in files:
try:
img = cv2.imread(os.path.join(path,image))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dstPath = join(dstpath,image)
cv2.imwrite(dstPath,gray)
except:
print ("{} is not converted".format(image))
for fil in glob.glob("*.jpg"):
try:
image = cv2.imread(fil)
gray_image = cv2.cvtColor(os.path.join(path,image), cv2.COLOR_BGR2GRAY) # convert to greyscale
cv2.imwrite(os.path.join(dstpath,fil),gray_image)
except:
print('{} is not converted')
import cv2
import glob, os, errno
# Replace mydir with the directory you want
mydir = r'C:\Users\Quantum\Desktop\testoutput'
#check if directory exist, if not create it
try:
os.makedirs(mydir)
except OSError as e:
if e.errno == errno.EEXIST:
raise
for fil in glob.glob("*.jpg"):
image = cv2.imread(fil)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to greyscale
cv2.imwrite(os.path.join(mydir,fil),gray_image) # write to location with same name
import os,cv2
path = r'C:\Users\me\Desktop\folder' # Source Folder
dstpath = r'C:\Users\me\Desktop\desfolder' # Destination Folder
try:
makedirs(dstpath)
except:
print ("Directory already exist, images will be written in asme folder")
# Folder won't used
files = os.listdir(path)
for image in files:
img = cv2.imread(os.path.join(path,image))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imwrite(os.path.join(dstpath,image),gray)
import cv2
from os import listdir,makedirs
from os.path import isfile,join
path = r'C:\Users\fakabbir.amin\Desktop\pdfop' # Source Folder
dstpath = r'C:\Users\fakabbir.amin\Desktop\testfolder' # Destination Folder
try:
makedirs(dstpath)
except:
print ("Directory already exist, images will be written in asme folder")
# Folder won't used
files = [f for f in listdir(path) if isfile(join(path,f))]
for image in files:
try:
img = cv2.imread(os.path.join(path,image))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dstPath = join(dstpath,image)
cv2.imwrite(dstPath,gray)
except:
print ("{} is not converted".format(image))
This code snippet will take all the images from path and write into another folder mentioned in dstpath.

Python Resize Multiple images ask user to continue

I'm trying to make a script that resize multiple or a single image based on a data pulled from XML.
My question is if i have multiple images how can I print out a qusetion like "There are more than 1 image do you wish to resize image 2 also?... than maybe " Would you liek to resize image 3 also ?"
My script so far is as follow,the only problem is taht it resizez all the images at start :
import os, glob
import sys
import xml.etree.cElementTree as ET
import re
from PIL import Image
pathNow ='C:\\'
items = []
textPath = []
imgPath = []
attribValue = []
#append system argument to list for later use
for item in sys.argv:
items.append(item)
#change path directory
newPath = pathNow + items[1]
os.chdir(newPath)
#end
#get first agrument for doc ref
for item in items:
docxml = items[2]
#search for file
for file in glob.glob(docxml + ".xml"):
tree = ET.parse(file)
rootFile = tree.getroot()
for rootChild in rootFile.iter('TextElement'):
if "svg" or "pdf" in rootChild.text:
try:
textPath = re.search('svg="(.+?)"', str(rootChild.text)).group(1)
attribValue.append(rootChild.get('elementId'))
imgPath.append(textPath)
except:
continue
for image in imgPath:
new_img_path = image[:-4] + '.png'
new_image = Image.open(new_img_path)
new_size=int(sys.argv[3]), int(sys.argv[4])
try:
new_image.thumbnail(new_size, Image.ANTIALIAS)
new_image.save(new_img_path, 'png')
except IOError:
print("Cannot resize picture '%s'" % new_img_path)
finally:
new_image.close()
print("Done resizeing image: %s " % new_img_path)
Thank you in advance.
Zapo
Change your final loop to:
for idx, image in enumerate(imgPath):
#img resizing goes here
count_remaining = len(imgPath) - (idx+1)
if count_remaining > 0:
print("There are {} images left to resize.".format(count_remaining))
response = input("Resize image #{}? (Y/N)".format(idx+2))
#use `raw_input` in place of `input` for Python 2.7 and below
if response.lower() != "y":
break

Loading all images using imread from a given folder

Loading and saving images in OpenCV is quite limited, so... what is the preferred ways to load all images from a given folder? Should I search for files in that folder with .png or .jpg extensions, store the names and use imread with every file? Or is there a better way?
Why not just try loading all the files in the folder? If OpenCV can't open it, oh well. Move on to the next. cv2.imread() returns None if the image can't be opened. Kind of weird that it doesn't raise an exception.
import cv2
import os
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename))
if img is not None:
images.append(img)
return images
I used skimage. You can create a collection and access elements the standard way, i.e. col[index]. This will give you the RGB values.
from skimage.io import imread_collection
#your path
col_dir = 'cats/*.jpg'
#creating a collection with the available images
col = imread_collection(col_dir)
import glob
cv_img = []
for img in glob.glob("Path/to/dir/*.jpg"):
n= cv2.imread(img)
cv_img.append(n)`
If all images are of the same format:
import cv2
import glob
images = [cv2.imread(file) for file in glob.glob('path/to/files/*.jpg')]
For reading images of different formats:
import cv2
import glob
imdir = 'path/to/files/'
ext = ['png', 'jpg', 'gif'] # Add image formats here
files = []
[files.extend(glob.glob(imdir + '*.' + e)) for e in ext]
images = [cv2.imread(file) for file in files]
you can use glob function to do this. see the example
import cv2
import glob
for img in glob.glob("path/to/folder/*.png"):
cv_img = cv2.imread(img)
You can also use matplotlib for this, try this out:
import matplotlib.image as mpimg
def load_images(folder):
images = []
for filename in os.listdir(folder):
img = mpimg.imread(os.path.join(folder, filename))
if img is not None:
images.append(img)
return images
import os
import cv2
rootdir = "directory path"
for subdir, dirs, files in os.walk(rootdir):
for file in files:
frame = cv2.imread(os.path.join(subdir, file))
To add onto the answer from Rishabh and make it able to handle files that are not images that are found in the folder.
import matplotlib.image as mpimg
images = []
folder = './your/folder/'
for filename in os.listdir(folder):
try:
img = mpimg.imread(os.path.join(folder, filename))
if img is not None:
images.append(img)
except:
print('Cant import ' + filename)
images = np.asarray(images)
Here is a simple script that feature opencv, scikit image, and glob
#!C:\Users\test\anaconda3\envs\data_aquisition\python.exe
import glob
import argparse
from timeit import default_timer as timer
import skimage
from skimage.io import imread_collection
import cv2
def get_args():
parser = argparse.ArgumentParser(
description='script that test the fastest image loading methods')
parser.add_argument('src_path', help = "diractorry that contains the ims")
parser.add_argument('extension', help = "extension of the images",choices=['jpg','png','webp'])
return parser.parse_args()
def load_imgs_scikit_image_collection(path:str):
#creating a collection with the available images
col = imread_collection(path)
print('loaded: ',len(col),' imgs')
return col
def load_imgs_scikit_image_glob(path):
imgs = []
for img in glob.glob(path):
imgs.append(skimage.io.imread(img))
return imgs
def load_image_opencv(path:str):
imgs = []
for f in glob.glob(path):
imgs.extend(cv2.imread(f))
return imgs
def load_image_opencv_glob(path:str):
filenames = glob.glob(path)
filenames.sort()
images = [cv2.imread(img) for img in filenames]
return images
def laod_images_opencv_extisions(path):
ext = [".jpg",".gif",".png",".tga",".webp"] # Add image formats here
files = []
images = []
[files.extend(glob.glob(path + '/*' + e)) for e in ext]
images.extend([cv2.imread(file) for file in files])
return images
def laod_images_ski_extisions(path):
ext = [".jpg",".gif",".png",".tga",".webp"] # Add image formats here
files = []
images = []
[files.extend(glob.glob(path + '/*' + e)) for e in ext]
images.extend([skimage.io.imread(file) for file in files])
return images
def show_image(img):
window_name = 'image'
cv2.imshow(window_name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def main():
args = get_args()
dir = args.src_path+'/*.'+args.extension
start = timer()
imgs=load_imgs_scikit_image_collection(dir)
end = timer()
print('scikit_image image collection',end - start) #time 0.08381089999999991
show_image(imgs[2])
start = timer()
load_imgs_scikit_image_glob(dir)
end = timer()
print('scikit_image and glob',end - start) #time 16.627431599999998
# dir = args.src_path+'\\.*'+args.extension
start = timer()
imgs_opencv = load_image_opencv_glob(dir) #time 10.9856656
end = timer()
print('opencv glob',end - start)
show_image(imgs_opencv[2])
start = timer()
valid_imgs_opencv = laod_images_opencv_extisions(args.src_path) #time 11.318516700000004
end = timer()
print('opencv glob extensions',end - start)
show_image(valid_imgs_opencv[2])
start = timer()
valid_imgs_opencv = laod_images_ski_extisions(args.src_path) #time 15.939870800000001
end = timer()
print('scikit_image glob extensions',end - start)
show_image(valid_imgs_opencv[2])
main()
Command to run script: python best_image_loader.py D:\data\dataset\radar_dome\manual png
png is used to load only png files.
Output
loaded: 876 imgs
scikit_image image collection 0.08248239999999996
scikit_image and glob 14.939381200000001
opencv glob 10.9708085
opencv glob extensions 10.974014100000005
scikit_image glob extensions 14.877048600000002
your_path = 'your_path'
ext = ['*.jpg', '*.png', '*.gif'] # Add image formats here
images = []
not_copy = 0
for item in [your_path + '/' + e for e in ext]:
images += glob(item)

Categories

Resources