Skip to content

Instantly share code, notes, and snippets.

View minooei's full-sized avatar

mohammad minouei minooei

View GitHub Profile
@minooei
minooei / conv.py
Created September 30, 2022 18:56
custom coco
import os
import cv2
import json
import string
import random
import numpy as np
def draw(img,poly,bbox):
cv2.polylines(img, [poly], True, (0,255,0), thickness=3)
#original: https://github.com/nmosto/kick-into-traction/blob/803bcfb96772d7d182622b98f9b1e7be3cc7435c/image-feature-engineering/dominant_color_clustering.py
import numpy as np
from PIL import Image as IMG
import cv2
from skimage.io import imread, imshow
def dominant_color(img):
# img should be the img object or path to the image
# read in image using openCV
@minooei
minooei / TFLiteConverter.py
Created April 29, 2021 13:51
TFLiteConverter
import tensorflow as tf
from tensorflow import keras
model = keras.models.load_model('model')
tf.saved_model.save(model, "new_out")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
@minooei
minooei / copy.sh
Created April 13, 2021 13:19
copy images with height greater than 100
for f in images/*.jpg; do _h=`identify -format '%h' $f`; if [[ $_h -gt 100 ]] cp "$f" 100/images/; done
@minooei
minooei / freeze.py
Created March 19, 2021 20:44
save the frozen model to one single .pb model file
# in this case, we save the frozen model to one single .pb model file
# and load it for forward inference
# for save model, first import meta graph and restore variables for checkpoint file
# and then send the GraphDef to graph_util.convert_variables_to_constants and get
# a new GraphDef object which is a simplified version of original one,
# and then serialize the GraphDef and write it to disk using tf.gfile
#
# for load model, first we read the .pb file from disk and deserialize it,as a return we
# get a GraphDef object, and then import it into the default graph use import_graph_def,
# Copyright 2020 Google. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
#original: https://github.com/lizhaoliu-Lec/DENet/blob/master/dataset/coco.py
from PIL import Image
import torch
from pycocotools.coco import COCO
import os
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
@minooei
minooei / get_stats.py
Last active February 11, 2021 15:40
custom mean and std calculation with parallel data loader
#original: https://github.com/hemanth346/EVA4_Phase2/blob/master/Session2/utils/get_stats.py
from torch.utils.data import Dataset, DataLoader
import os
import torch
import torch.utils.data
import torchvision
from PIL import Image
from skimage import io, transform
from pycocotools.coco import COCO
@minooei
minooei / CustomPasswordField.py
Created August 7, 2020 19:55
flask-admin customization
class CustomPasswordField(StringField):
#admin can change the user password. do not change if the form contains the old password hash.
def populate_obj(self, obj, name):
if obj.password != self.data:
setattr(obj, name, hash_password(self.data))
class UserView(AdminMixin, ModelView):
form_extra_fields = {
'password': CustomPasswordField('password', validators=[
validators.Regexp('(?=^.{6,}$)(?=.*\d)(?![.\n])(?=.*[a-z]).*$',
@minooei
minooei / app.py
Created June 2, 2020 09:47
flask-admin validate username and password
form_args = {
'username': {
'validators': [validators.Regexp('^(?=.{5,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$', message='نام کاربری حداقل به طول ۵')]
},
'password': {
'validators': [validators.Regexp('(?=^.{8,}$)(?=.*\d)(?=.*[!@#$%^&*]+)(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$', message='پسورد حداقل به طول ۸ و شامل حروف کوچک و بزرگ و عدد و علائم خاص باشد')]
}
}