Skip to content

Commit eb7abf2

Browse files
committed
init
1 parent 2705828 commit eb7abf2

File tree

4 files changed

+145
-1
lines changed

4 files changed

+145
-1
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Alex
3+
Copyright (c) 2021 Alexander Spirin
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

faces.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
Code related to face detection and manipulation
3+
"""
4+
5+
#pip install facenet_pytorch
6+
7+
from facenet_pytorch import MTCNN
8+
mtcnn = MTCNN(image_size=256, margin=80)
9+
10+
# simplest ye olde trustworthy MTCNN for face detection with landmarks
11+
def detect(img):
12+
13+
# Detect faces
14+
batch_boxes, batch_probs, batch_points = mtcnn.detect(img, landmarks=True)
15+
# Select faces
16+
if not mtcnn.keep_all:
17+
batch_boxes, batch_probs, batch_points = mtcnn.select_boxes(
18+
batch_boxes, batch_probs, batch_points, img, method=mtcnn.selection_method
19+
)
20+
21+
return batch_boxes, batch_points
22+
23+
# my version of isOdd, should make a separate repo for it :D
24+
def makeEven(_x):
25+
return _x if (_x % 2 == 0) else _x+1
26+
27+
# the actual scaler function
28+
def scale(boxes, _img, max_res=1_500_000, target_face=256, fixed_ratio=0, max_upscale=2, VERBOSE=False):
29+
30+
x, y = _img.size
31+
32+
ratio = 2 #initial ratio
33+
34+
#scale to desired face size
35+
if (boxes is not None):
36+
if len(boxes)>0:
37+
ratio = target_face/max(boxes[0][2:]-boxes[0][:2]);
38+
ratio = min(ratio, max_upscale)
39+
if VERBOSE: print('up by', ratio)
40+
41+
if fixed_ratio>0:
42+
if VERBOSE: print('fixed ratio')
43+
ratio = fixed_ratio
44+
45+
x*=ratio
46+
y*=ratio
47+
48+
#downscale to fit into max res
49+
res = x*y
50+
if res > max_res:
51+
ratio = pow(res/max_res,1/2);
52+
if VERBOSE: print(ratio)
53+
x=int(x/ratio)
54+
y=int(y/ratio)
55+
56+
#make dimensions even, because usually NNs fail on uneven dimensions due skip connection size mismatch
57+
x = makeEven(int(x))
58+
y = makeEven(int(y))
59+
60+
size = (x, y)
61+
62+
return _img.resize(size)
63+
64+
"""
65+
A useful scaler algorithm, based on face detection.
66+
Takes PIL.Image, returns a uniformly scaled PIL.Image
67+
68+
boxes: a list of detected bboxes
69+
_img: PIL.Image
70+
max_res: maximum pixel area to fit into. Use to stay below the VRAM limits of your GPU.
71+
target_face: desired face size. Upscale or downscale the whole image to fit the detected face into that dimension.
72+
fixed_ratio: fixed scale. Ignores the face size, but doesn't ignore the max_res limit.
73+
max_upscale: maximum upscale ratio. Prevents from scaling images with tiny faces to a blurry mess.
74+
"""
75+
76+
def scale_by_face_size(_img, max_res=1_500_000, target_face=256, fix_ratio=0, max_upscale=2, VERBOSE=False):
77+
boxes = None
78+
boxes, _ = detect(_img)
79+
if VERBOSE: print('boxes',boxes)
80+
img_resized = scale(boxes, _img, max_res, target_face, fix_ratio, max_upscale, VERBOSE)
81+
return img_resized
82+

ffmpeg.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Combining frames into a video, then adding sound from the source video.
3+
4+
frames: a glob-like pattern, for example '/content/out_resized/frame*.jpg'
5+
vid_name: path to the input video
6+
fps: fps :D
7+
out_fname: output video name. Use mp4 container by default.
8+
"""
9+
10+
ffmpeg -f image2 -r {fps} -pattern_type glob -i "{frames}" -i "{vid_name}" -c:v copy -c:a aac -map 0:v:0 \
11+
-map 1:a:0 -shortest -vcodec libx264 -r {fps} -crf 18 -pix_fmt yuv420p "{out_fname}"
12+
13+
"""
14+
The simplest way to combine frames into video
15+
16+
frames: a glob-like pattern, for example '/content/out_resized/frame*.jpg'
17+
fps: fps :D
18+
out_fname: output video name. Use mp4 container by default.
19+
"""
20+
21+
ffmpeg -pattern_type glob -i "{frames}" -filter:v fps={fps} "{out_fname}"
22+
23+
"""
24+
The simplest way to split a video into frames
25+
26+
vid_name: input video'
27+
out_dir: output folder.
28+
29+
You can change the pattern if you need more digits (increase the number in %05d pattern), or change the filename.
30+
"""
31+
32+
ffmpeg -v quiet -i "{vid_name}" "{out_dir}/frame_%05d.jpg"

google_colab.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Some of the colab snippets I use on a daily basis.
3+
I've decided to store those as .py for faster access from github and easier imports.
4+
"""
5+
"""
6+
Zips a folder and downloads it. Can also copy the archive to a google drive path (or any other available to your colab instance)
7+
Use to automatically download results after some afk training or inference, before the instance resets.
8+
9+
path: path to a folder to download. Can contain wildcards
10+
prefix: a name of your new archive
11+
drive: a folder to copy the archive to
12+
"""
13+
from google.colab import files
14+
import time
15+
16+
def download(path, prefix, drive=''):
17+
print('\nCкачиваем...\n')
18+
timestamp = round(time.time())
19+
!zip -r /content/{prefix}-{timestamp}.zip {path}
20+
files.download(f"/content/{prefix}-{timestamp}.zip")
21+
if drive!='':
22+
!cp "/content/{prefix}-{timestamp}.zip" {drive}
23+
24+
25+
"""
26+
It beeps. Use to give yourself a signal.
27+
"""
28+
from google.colab import output
29+
def beep():
30+
output.eval_js('new Audio("https://upload.wikimedia.org/wikipedia/commons/0/05/Beep-09.ogg").play()')

0 commit comments

Comments
 (0)