Last active
January 8, 2019 07:05
-
-
Save bombs-kim/e71ca6cbfdb84fe3f8eb5dd7749e9e4a to your computer and use it in GitHub Desktop.
jupyter misc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to open an image file | |
from PIL import Image | |
img = Image.open(img_path) | |
# How to draw a bounding box with PIL | |
from PIL import Image, ImageDraw | |
def draw_bbox(img: Image, label: tuple): | |
# Starting from upper left corner | |
x1, y1, x2, y2 = label | |
d = ImageDraw.Draw(img) | |
d.rectangle((x1, y1, x2, y2), outline=(0, 255, 0)) | |
# How to visualize torch.tensor in Jupyter | |
import matplotlib.pyplot as plt | |
import numpy as np | |
def show(img): | |
img -= torch.min(img) | |
img /= torch.max(img) | |
if len(img.shape) == 4: | |
assert img.shape[0] == 1 | |
img = img[0] | |
if len(img.shape) == 3 and img.shape[0] == 1: | |
img = torch.cat((img, img, img), 0) | |
npimg = img.numpy() | |
plt.imshow(np.transpose(npimg, (1,2,0)), interpolation='nearest') | |
# More examples can be found here https://github.com/pytorch/vision/blob/master/test/sanity_checks.ipynb | |
# How to make a movie out of images in Python | |
# https://stackoverflow.com/questions/44947505/how-to-make-a-movie-out-of-images-in-python | |
import cv2 | |
import os | |
image_folder = 'images' | |
video_name = 'video.avi' | |
images = [img for img in os.listdir(image_folder) if img.endswith(".png")] | |
frame = cv2.imread(os.path.join(image_folder, images[0])) | |
height, width, layers = frame.shape | |
video = cv2.VideoWriter(video_name, -1, 1, (width,height)) | |
for image in images: | |
video.write(cv2.imread(os.path.join(image_folder, image))) | |
cv2.destroyAllWindows() | |
video.release() | |
# In ubuntu you may need to replace one line | |
video = cv2.VideoWriter(video_name, | |
cv2.VideoWriter_fourcc(*'XVID'), | |
30, | |
(width,height)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment