Skip to content

Instantly share code, notes, and snippets.

View miladfa7's full-sized avatar
🎯
Focusing

Milad Farzalizadeh miladfa7

🎯
Focusing
View GitHub Profile
@miladfa7
miladfa7 / nms_object_detection.py
Last active September 11, 2024 14:31
This code performs Non-Maximum Suppression (NMS) to filter object detection predictions and visualize the results. numpy_nms function: Implements NMS using NumPy. apply_nms function: Applies NMS using both the custom NumPy-based NMS and PyTorch’s built-in NMS (torchvision.ops.nms).
import numpy as np
import os
import cv2
import torchvision
import torch
import json
def numpy_nms(boxes, scores, iou_threshold):
@miladfa7
miladfa7 / reduce_video_size.sh
Created April 26, 2024 04:22
Reduce video size with ffmpeg
file="/mnt/disk2/users/milad/videos/final_match.mp4"
output_dir="/mnt/disk2/users/milad/output"
input_file="$file"
filename=$(basename -- "$input_file")
filename_noext="${filename%.*}"
output_file="$output_dir/${filename_noext}.mp4"
# Reduce video size using ffmpeg
@miladfa7
miladfa7 / rotate_text_cv2.py
Last active September 11, 2024 14:31
Rotate text (Put rotated text) on image with OpenCV
import cv2
improt numpy as np
def draw_rotated_text(image: np.ndarray, main_text: str) -> np.ndarray:
main_font_face = cv2.FONT_HERSHEY_SIMPLEX
main_font_scale = 0.4
main_thickness = 1
main_text_size, _ = cv2.getTextSize(main_text, main_font_face, main_font_scale, main_thickness)
main_baseline = main_text_size[1] + main_thickness
@miladfa7
miladfa7 / merge_coco.py
Created March 29, 2024 21:47
how to merge multiple coco json files in python
# pip install pycocotools
from pycocotools.coco import COCO
import json
def merge_coco_json(json_files, output_file):
merged_annotations = {
"info": {},
"licenses": [],
"images": [],
"annotations": [],
@miladfa7
miladfa7 / show_video_in_notebook.py
Created November 29, 2023 10:43
How to load and show video in Jupyter Notebook
import io
import base64
from IPython.display import HTML
video = io.open('21590891783.mp4', 'r+b').read()
encoded = base64.b64encode(video)
HTML(data='''
<video controls>
<source src="data:video/mp4;base64,{0}" type="video/mp4" />
@miladfa7
miladfa7 / image_augmentaion.py
Last active September 11, 2024 14:33
Image Augmentation Python Code
from PIL import Image
from PIL import ImageOps
import os
import random
# set the path to the directory containing the images
input_path = "/path/.../input"
output_path = "/path/.../output"
# loop through each image file in the directory
@miladfa7
miladfa7 / WikiText_Download.py
Last active September 25, 2021 09:01
Download Wikipedia articles with python
import tensorflow as tf
from gensim.corpora import WikiCorpus
import os
import argparse
# lang = 'fa' farsi
def store(corpus, lang):
base_path = os.getcwd()
store_path = os.path.join(base_path, '{}_corpus'.format(lang))
@miladfa7
miladfa7 / gpt2_from_scratch.py
Last active April 30, 2023 06:54
Train GPT-2 from Scratch on your own language(Persain) | GPT-2 Training on non-english text
from simpletransformers.language_modeling import LanguageModelingModel
import logging
logging.basicConfig(level=logging.INFO)
transformers_logger = logging.getLogger("transformers")
transformers_logger.setLevel(logging.WARNING)
train_args = {
@miladfa7
miladfa7 / inplaceAugmentation.py
Created December 28, 2019 20:36
ImageDataGenerator (in-place augmentation)
import tensorflow
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Create training ImageDataGenerator object
train_data_gen = ImageDataGenerator(rotation_range=50,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.3,
horizontal_flip=True,
vertical_flip=True,
@miladfa7
miladfa7 / Augmentation.py
Last active December 28, 2019 20:34
Image Augmentation for Increasing sample of dataset
import tensorflow
from PIL import Image
import glob
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
import numpy as np
aug = ImageDataGenerator(
rotation_range=30,