Skip to content

Commit

Permalink
Added JSON support with multiprocessing mode support
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkonrom committed Dec 1, 2017
1 parent aed488f commit 48b439b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
6 changes: 5 additions & 1 deletion api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from boundaries import BoundariesOperation
from semantic_segmentation import SemanticSegmentation
from saliency import Saliency
import serialization
from face_recognition_ import FaceRecognition

def createParser():
Expand All @@ -19,15 +20,18 @@ def createParser():
def begin_invoke(namespase):
if namespase.infld and namespase.exec:
tree = []
files = None
for d, dirs, files in os.walk(namespase.infld):
for f in files:
path = os.path.join(d,f)
tree.append(path)
func = None
settings = serialization.deserialize()
output_folder = ''

if namespase.exec == 'boundaries':
func = BoundariesOperation.execute

elif namespase.exec == 'semantic_segmentation':
func = SemanticSegmentation.execute
elif namespase.exec == 'saliency':
Expand All @@ -38,5 +42,5 @@ def begin_invoke(namespase):
if namespase.outfld:
output_folder = namespase.outfld

multithreading.execute_processing(tree, files, func, output_folder)
multithreading.execute_processing(tree, files, func, settings, output_folder)
return True
3 changes: 3 additions & 0 deletions face_recognition_.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import namedtuple

import cv2
import face_recognition
from operation import Operation
Expand All @@ -7,6 +9,7 @@
class FaceRecognition(Operation):
@staticmethod
def execute(input_image, settings):
settings = namedtuple("settings", settings.keys())(*settings.values())
colors = settings.face_settings[0]
width = settings.face_settings[1]
img = np.array(input_image)
Expand Down
24 changes: 12 additions & 12 deletions multithreading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@
from multiprocessing import Queue
from PIL import Image


class Worker(threading.Thread):
def __init__(self, work_queue, func, output):
def __init__(self, work_queue, func, output, settings):
super(Worker, self).__init__()
self.work_queue = work_queue
self.func = func
self.output = output
self.settings = settings

def run(self):

try:
filename, filename_without_path = self.work_queue.get()
self.process(filename, filename_without_path)
finally:
self.process(filename, filename_without_path, self.settings)
except Exception:
pass

def process(self, filename, filename_without_path):
savename = self.output + filename_without_path
def process(self, filename, filename_without_path, settings):
savename = self.output + '/ ' + filename_without_path
image = Image.open(filename)
out_image = self.func(image)
out_image = self.func(image, settings)
out_image.save(savename)


def execute_processing(filelist, files, func, output='.'):
def execute_processing(filelist, files, func, settings, output='.'):
work_queue = Queue()
for filename, file in zip(filelist, files):
work_queue.put((filename, file))

threads = []
for i in range(8):
worker = Worker(work_queue, func, output)
for i in range(4):
worker = Worker(work_queue, func, output, settings)
worker.start()
threads.append(worker)

for t in threads:
t.join()



8 changes: 3 additions & 5 deletions serialization.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import json
from json import JSONEncoder


class JSON(JSONEncoder):
def default(self, o):
return o.__dict__

class Settings(object):
def toJSON(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=True, indent=4)

def __init__(self, boundaries_settings, segmentation_settings, saliency_settings, face_settngs):
self.boundaries_settings = boundaries_settings
self.segmentation_settings = segmentation_settings
Expand All @@ -23,5 +21,5 @@ def serialize(settings):

def deserialize():
filename = 'settings.json'
return json.load(filename)
return json.load(fp=open(filename, mode='r'))

2 changes: 1 addition & 1 deletion settings.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"boundaries_settings": [1], "segmentation_settings": [], "saliency_settings": [], "face_settings": [[255, 0, 0], 0]}
{"boundaries_settings": [1], "segmentation_settings": [], "saliency_settings": [], "face_settings": [[255, 0, 0], 3]}

0 comments on commit 48b439b

Please sign in to comment.