Skip to content

Instantly share code, notes, and snippets.

View olegkhomenko's full-sized avatar
🪩
--

oleg olegkhomenko

🪩
--
View GitHub Profile
def predict(self, img_path: str):
"""Predicts how attractive is the person on the image"""
img = DeepFace.functions.load_image(img_path)
pil_to_predict = self.detect_face_with_padding(img, 0.2)
if pil_to_predict is None:
return (None,) * 3
gender = self.predict_clip(pil_to_predict, ["man", "woman"]).argmax()
score = self.predict_clip(pil_to_predict, self.captions[gender])[0][0]
@olegkhomenko
olegkhomenko / precitor_clip_01.py
Created May 3, 2022 09:46
predictor_clip_01_predict_clip
@torch.no_grad()
def predict_clip(self, image: Image.Image, text: List[str]):
text = clip.tokenize(text).to(self.device)
text_features = self.model.encode_text(text)
text_features /= text_features.norm(dim=1, keepdim=True)
image = self.preprocess(image).unsqueeze(0).to(self.device)
image_features = self.model.encode_image(image)
image_features /= image_features.norm(dim=1, keepdim=True)
@olegkhomenko
olegkhomenko / predictor_clip_00.py
Created May 3, 2022 09:37
medium_predictor_clip_00
class PredictorCLIP(nn.Module):
def __init__(self, device="cpu"):
super().__init__()
self.device = device
# Loading CLIP
self.model, self.preprocess = clip.load("ViT-B/32", device=device)
# Loading BlazeFace detector from Mediapipe
self.detector = MediapipeWrapper.build_model()
self.captions = [["handsome", "ugly"], ["beautiful", "ugly"]]
@olegkhomenko
olegkhomenko / detect_face_with_padding.py
Last active May 3, 2022 09:42
detect_face_with_padding
def detect_face_with_padding(self, img, padding=0.1):
"""Detects faces in an image. Returns first detected face. Returns None if no face detected."""
try:
w, h, _ = img.shape
padding_coef_v = 1 + padding
padding_coef_h = 1 + padding
bbox = MediapipeWrapper.detect_face(self.detector, img)[0][1]
@olegkhomenko
olegkhomenko / vis_gpustat.py
Last active December 8, 2021 17:40
GPU Utilization visualization using gpustat
import gpustat
import tqdm
import time
import seaborn as sns
import numpy as np
import pandas as pd
# Query for 6 seconds (step is 0.1 second)
gpu_stats = []
for i in range(60):
@olegkhomenko
olegkhomenko / vai.py
Created September 15, 2021 17:38
vast.ai INIT
"""Vast.ai script"""
import argparse
import subprocess
from pathlib import Path
import sysrsync
folders_to_sync = [
("/root/data", "/root/data"),
]
@olegkhomenko
olegkhomenko / diff_two_folders.sh
Last active July 13, 2021 12:04
diff two folders
diff -ur %folder1% %folder2% | ydiff -s
@olegkhomenko
olegkhomenko / extract_frames_from_videos.sh
Last active July 6, 2021 14:16
Simple Bash script to extract each Nth frame from videos using .txt file with the list of video files
FILES=/data/datasets/files.txt
COUNTER=0
LARGEFILES_CNT=0
SMALLFILES_CNT=0
start=`date +%s`
for f in `cat $FILES`; do
# INFO: IF ENDS AT:
# if [[ $COUNTER -gt 50 ]];
@olegkhomenko
olegkhomenko / blender_local_and_global_vertex_coordinates.py
Created January 7, 2021 15:55
blender_local_and_global_vertex_coordinates.py
def get_local_and_global_vertex_coordinates():
"""First you need to select a vertex in 'Edit Mode'"""
obj = bpy.context.selected_objects[0]
coords_local = obj.data.vertices[0].co
coords_global = obj.matrix_world @ coords_local
return coords_local, coords_global
@olegkhomenko
olegkhomenko / helpers.swift
Created December 17, 2020 10:35
Swift Helpers (Images)
// Convert cvPixelBuffer or cvPixelBufferRef to UIImage
let capturedUIImage = UIImage(ciImage: CIImage(cvPixelBuffer: capturedImage))