Skip to content

Commit

Permalink
Add csv and json sink
Browse files Browse the repository at this point in the history
  • Loading branch information
shwars committed Nov 28, 2018
1 parent 426796e commit 256179d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions mPyPl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .funcs import *
from .video import *
from .keras import *
from .sink import *
from .multiclass_datastream import *
from .xmlstream import *
from .utils.pipeutils import *
Expand Down
7 changes: 7 additions & 0 deletions mPyPl/mdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ def as_float(self,item):
def as_int(self,item):
return int(self[item])

def as_csv(self):
return ','.join(map(encode_csv,self.values()))

def as_csv_header(self):
return ','.join(map(encode_csv,self.keys()))


@staticmethod
def extract_from_object(x,fields):
"""
Expand Down
17 changes: 17 additions & 0 deletions mPyPl/sink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# mPyPl - Monadic Pipeline Library for Python
# http://github.com/shwars/mPyPl

# Different sinks to consume mdict streams

import csv
import json

def write_csv(l,filename):
with open(filename,'wb') as f:
w = csv.writer(f)
for x in l:
w.writerow(x)

def write_json(l,filename):
with open(filename,'w') as f:
f.write(json.dumps(l))
33 changes: 32 additions & 1 deletion mPyPl/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

# taken from https://stackoverflow.com/questions/44720580/resize-image-canvas-to-maintain-square-aspect-ratio-in-python-opencv
def im_resize_pad(img, size, pad_color=0):

"""
Resize an image with padding
:param img: Original image
:param size: Size in the form (Width,Height). Both should be present
:return: Resized image
"""
h, w = img.shape[:2]
sh, sw = size

Expand Down Expand Up @@ -48,6 +53,13 @@ def im_resize_pad(img, size, pad_color=0):
return scaled_img

def im_resize(frame,size):
"""
Resize an image, calculating one of the dimensions if needed
:param frame: Original image
:param size: Size in the form (Width,Height). If one of the parameters is None, it is calculated. If both are present,
image is stretched.
:return: Resized image
"""
width,height=size
if width or height:
width = width if width else int(height / frame.shape[0] * frame.shape[1])
Expand All @@ -57,6 +69,12 @@ def im_resize(frame,size):
return frame

def show_images(images, cols = 1, titles = None):
"""
Show a list of images using matplotlib
:param images: list of images (or any sequence with len and indexing defined)
:param cols: number of columns to use
:param titles: list of titles to use or None
"""
assert((titles is None)or (len(images) == len(titles)))
if not isinstance(images,list):
images = list(images)
Expand All @@ -73,3 +91,16 @@ def show_images(images, cols = 1, titles = None):
fig.set_size_inches(np.array(fig.get_size_inches()) * n_images)
plt.tight_layout()
plt.show()

# Taken from https://stackoverflow.com/questions/31400769/bounding-box-of-numpy-array
def calc_bounding_box(img):
"""
Calculate bounding box of an image or mask. Bounding box surrounds non-zero pictures
:param img: Original image
:return: Bounding box in a form (x1,y1,x2,y2)
"""
rows = np.any(img, axis=1)
cols = np.any(img, axis=0)
rmin, rmax = np.where(rows)[0][[0, -1]]
cmin, cmax = np.where(cols)[0][[0, -1]]
return rmin, cmin, rmax, cmax

0 comments on commit 256179d

Please sign in to comment.