Skip to content

Instantly share code, notes, and snippets.

@juliussin
juliussin / mmma.py
Created April 25, 2023 01:40
Max Min for Multidimensional Array
from operator import itemgetter
x = [[1, 4], [3, 2]]
max(x, key=itemgetter(0))
# >> [3, 2]
max(x, key=itemgetter(1))
# >> [1, 4]
from typing import List
import pandas as pd
def one_hot_to_categorical(df: pd.DataFrame, one_hot_features: List[str], new_feature: str, rm_ori: bool = False) -> pd.DataFrame:
"""
One Hot Encoding to Categorical Feature
"""
return_df = df.copy()
one_hot_df = return_df.loc[:, one_hot_features].astype(bool)
for row in range(len(one_hot_df)):
#!/usr/bin/env python
from WebcamVideoStream import WebcamVideoStream
import cv2
import time
from flask import Flask, render_template, Response
app = Flask(__name__)
first_flag = True
vs = None
@juliussin
juliussin / point_capture.py
Created November 9, 2020 07:51
Capture point.
import cv2
import numpy as np
import argparse
def rescale_frame(frame, scale=2):
width = int(frame.shape[1] * scale)
height = int(frame.shape[0] * scale)
dim = (width, height)
return cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
@juliussin
juliussin / xyxy_xywh.py
Last active October 22, 2023 19:02
Convert XYXY format (x,y top left and x,y bottom right) to XYWH format (x,y center point and width, height) and vice versa.
import cv2
import numpy as np
def xyxy_to_xywh(xyxy):
"""
Convert XYXY format (x,y top left and x,y bottom right) to XYWH format (x,y center point and width, height).
:param xyxy: [X1, Y1, X2, Y2]
:return: [X, Y, W, H]
"""
if np.array(xyxy).ndim > 1 or len(xyxy) > 4:
@juliussin
juliussin / point_perspective_transform.py
Created November 9, 2020 03:22
Perspective transform a point given the transformation matrix.
import cv2
import numpy as np
def point_perspective_transform(point, matrix):
"""
Perspective transform a point given the transformation matrix
:param point: [X, Y] point to be transformed
:param matrix: 3x3 transformation matrix
:return: [X, Y] transformed point
@juliussin
juliussin / rescale_frame.py
Created November 9, 2020 03:15
Rescale frame OpenCV
import cv2
import numpy as np
def rescale_frame(frame, percent=75):
width = int(frame.shape[1] * percent / 100)
height = int(frame.shape[0] * percent / 100)
dim = (width, height)
return cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
@juliussin
juliussin / datasets_management.py
Created November 9, 2020 03:14
Datasets management.
import shutil
import os
import random
def move_ext(src, dst, ext):
"""
Move file based on its extension
:param src: file source path
:param dst: file destination path
@juliussin
juliussin / cuda_pytorch_check.py
Created November 9, 2020 03:12
Check CUDA device and availability on PyTorch.
import torch
print('Torch CUDA Current Device: {}'.format(torch.cuda.current_device()))
print('Torch CUDA Device: {}'.format(torch.cuda.device(torch.cuda.current_device())))
print('Torch CUDA Device Count: {}'.format(torch.cuda.device_count()))
print('Torch CUDA Device Name: {}'.format(torch.cuda.get_device_name(torch.cuda.current_device())))
print('Torch CUDA Availability: {}'.format(torch.cuda.is_available()))