Skip to content

Instantly share code, notes, and snippets.

View kerenskybr's full-sized avatar
🤖
___

Roger Monteiro kerenskybr

🤖
___
View GitHub Profile
@kerenskybr
kerenskybr / remove_none.py
Last active May 11, 2023 14:38
Remove null keys and values from a nested structure of dicts, tuple and lists
def remove_none(json_file):
""" Remove null keys and values from
a nested structure of dicts, tuple and lists.
"""
if isinstance(json_file, (list, tuple, set)):
return type(json_file)(remove_none(x) for x in json_file if x is not None)
elif isinstance(json_file, dict):
return type(json_file)((remove_none(k), remove_none(v))
for k, v in json_file.items() if k is not None and v is not None)
else:
@kerenskybr
kerenskybr / gray_to_rgb.py
Created September 16, 2020 01:21
Function to convert the Grayscale data to RGB to match with an architecture
from keras import backend as K
def grayscale_to_rgb(images, channel_axis=-1):
'''Function to convert the Grayscale data
to RGB to match with an architecture
'''
images= K.expand_dims(images, axis=channel_axis)
tiling = [1] * 4 # 4 dimensions: B, H, W, C
tiling[channel_axis] *= 3
images= K.tile(images, tiling)
@kerenskybr
kerenskybr / four_lists_to_dict.py
Last active September 18, 2020 12:30
Creates dicts from list of lists
def merge_items(a_list):
"""Merge the 4 lists of items
and combine as values to keys
"""
#Same amount of lists of lists to receive a key
keys = ['key1','key2','key3','key4']
items = [list(i) for i in list(zip(*a_list))]
result = []
for i in items:
@kerenskybr
kerenskybr / concat_imgs.py
Last active September 19, 2020 13:48
Concat two images towards the height
from PIL import Image
def concat_images(im1, im2):
""" Concat two images towards the height
"""
dst_img = Image.new('RGB', (min(im1.width, im2.width), im1.height + im2.height))
dst_img.paste(im1, (0, 0))
dst_img.paste(im2, (0, im1.height))
return dst_img
@kerenskybr
kerenskybr / divide_chubks.py
Created September 22, 2020 01:18
Divide a list in chunks from given size
def divide_chunks(a_list, chunk_size):
"""Divide a list in chunks from given size
"""
for i in range(0, len(a_list), chunk_size):
yield a_list[i:i + chunk_size]
@kerenskybr
kerenskybr / merge_list.py
Last active September 22, 2020 01:54
Merge together items from list of lists sequentially
def merge_list(a_list):
"""Merge together items from list of lists sequentially
"""
return [list(i) for i in list(zip(*a_list))]
"""
i. e.
[[1,2,3], [4,5,6]]
result:
[[1,4], [2,5], [3,6]]
@kerenskybr
kerenskybr / load_img_opencv.py
Created December 8, 2020 11:50
Load images from a directory using opencv
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename))
if img is not None:
images.append(img)
return images
@kerenskybr
kerenskybr / clean_blocks.py
Created December 8, 2020 14:36
Function to clean all data created in runtime to free memory (blender script)
def clean_blocks():
for block in bpy.data.meshes:
if block.users == 0:
bpy.data.meshes.remove(block)
for block in bpy.data.materials:
if block.users == 0:
bpy.data.materials.remove(block)
for block in bpy.data.textures:
@kerenskybr
kerenskybr / intersection.py
Last active November 22, 2022 19:53
Function to find intersection between two lines
def intersection(horizontal, vertical):
# https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
# Horizontal x y x y
# Vertical x y x y
# x1, y1, x2, y2, x3, y3, x4, y4
# 0 1 2 3 0 1 2 3
result = []
x1, x2, x3, x4, y1, y2, y3, y4 = int(horizontal[0]), int(horizontal[2]), int(vertical[0]), int(vertical[2]), int(horizontal[1]), int(horizontal[3]), int(vertical[1]), int(vertical[3])
@kerenskybr
kerenskybr / websocket_client.py
Last active April 5, 2021 13:26
Script to test websockets with/out luminati proxy
import websocket
try:
import thread
except ImportError:
import _thread as thread
import time
import argparse
import socks
parser = argparse.ArgumentParser()