[PYTHON] Code snippets often used when processing videos with Google Colaboratory

Mount Google Drive

gdrive_path = "/content/gdrive" #Mounted path

from google.colab import drive
drive.mount(gdrive_path)

Iteration

Loop any range at any interval

for a in range(0, 1280, 10):
  print(a)

List image files under a specific directory

import glob
files = glob.glob("/content/gdrive/My Drive/ColabExp/*.png ") #Only files with the extension png
files.sort() #Sort the files found

Create a temporary directory and work

import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
  #Write the file to tmpdir

Or

import tempfile
tmpdir = tempfile.TemporaryDirectory().name

Get the file name from the file path

import os
os.path.basename(files[i])

Display the progress of processing

from tqdm import tqdm
for i, file in enumerate(tqdm(src_files)):
  #processing

Image processing

Make a canvas with PIL

from PIL import Image
img = Image.new('RGB', (1280, 720), (255, 255, 255)) # 1280 x 720px

Draw a shape with PIL

from PIL import ImageDraw

draw = ImageDraw.Draw(img)

draw.rectangle(((x0, y0), (x1, y1)), fill=(255, 0, 0)) #Rectangle

Save images with OpenCV

import cv2
cv2.imwrite(save_path, image)

Save images with PIL / Pillow

img.save(path)

Make an array of images into a video

!ffmpeg -framerate 30 -i "$dst_img_dir/%06d.png " -vcodec h264 -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" "$dst_mov_dir/out.mp4"

CSV

Read CSV from file (DataFrame)

df = pd.read_csv(filepath)

Embed and read CSV (DataFrame)

CSV_DATA = StringIO("""x,y
10.0, 20.0
15.0, 30.0
20.0, 45.0
35.0, 80.0
""")

df = pd.read_csv(CSV_DATA, sep=",")

Save the contents of the DataFrame to a CSV file

df.to_csv("out.csv")

Times of Day

Timestamp of the current time in seconds

from datetime import datetime
now_ts = int(datetime.now().timestamp())

Recommended Posts

Code snippets often used when processing videos with Google Colaboratory
Code snippets often used when using BigQuery with Google Colab
Usual processing notes when using Google Colaboratory
I tried simple image processing with Google Colaboratory.
Cheat sheet when scraping with Google Colaboratory (Colab)
Easy learning of 100 language processing knock 2020 with "Google Colaboratory"
Study Python with Google Colaboratory
A memo when executing the deep learning sample code created from scratch with Google Colaboratory
Python frequently used code snippets
Sample code summary when working with Google Spreadsheets from Google Colab
Try OpenCV with Google Colaboratory
Snippets (scraping) registered in Google Colaboratory
OpenCV feature detection with Google Colaboratory
100 language processing knock 2020 "for Google Colaboratory"
How to search Google Drive with Google Colaboratory
Make a cascade classifier with google colaboratory
Summary of snippets when developing with Go
Manage deals with Trello + Google Colaboratory (Part 1)
Using Java's Jupyter Kernel with Google Colaboratory
Use TPU and Keras with Google Colaboratory
Processing memos often used in pandas (beginners)
A memorandum of method often used when analyzing data with pandas (for beginners)