Skip to content

Commit

Permalink
chore(code): Format Python code with psf/black
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryVasanth committed Nov 12, 2022
1 parent d29bcf8 commit d4aeb06
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 61 deletions.
18 changes: 10 additions & 8 deletions celery_conf.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from celery import Celery

app = Celery('Distributed-Image-Processing-System',
broker='amqp://dist:dist@localhost/imageproc', # The IP shoul$
backend='redis://localhost:6379/0', # The IP should be change$
include=['client'])
app.conf.task_serializer = 'pickle'
app.conf.result_serializer = 'pickle'
app.conf.accept_content = ['pickle']
app.conf.worker_send_task_events = 'true'
app = Celery(
"Distributed-Image-Processing-System",
broker="amqp://dist:dist@localhost/imageproc", # The IP shoul$
backend="redis://localhost:6379/0", # The IP should be change$
include=["client"],
)
app.conf.task_serializer = "pickle"
app.conf.result_serializer = "pickle"
app.conf.accept_content = ["pickle"]
app.conf.worker_send_task_events = "true"
154 changes: 112 additions & 42 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import requests
import datetime

class File():

class File:
"""
Implements all file operation Open Close and Save Files
"""
Expand All @@ -28,17 +29,17 @@ def openFile(self, loadType):
"""
self.im = cv2.imread(self.path, loadType)
if self.im == None or self.im.size == 0:
print('Image loaded is empty')
print("Image loaded is empty")
sys.exit(1)
else:
return self.im;
return self.im

def showFile(self):
"""
Does not work in linux need to install some libs
:return:
"""
cv2.imshow('image', image)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Expand All @@ -64,6 +65,7 @@ class Handler:
results = {}
filename = None
testNumber = 0

def restartData(self):
self.task_ids = {}
self.results = {}
Expand All @@ -80,34 +82,50 @@ def splitAndSend(self, image, path, algorithm, parameters):
if len(parameters) == 0:
self.task_ids[count] = algorithm.delay(chunk)
elif len(parameters) == 1:
self.task_ids[count] = algorithm.delay(chunk, float(parameters.get("parameter1")))
self.task_ids[count] = algorithm.delay(
chunk, float(parameters.get("parameter1"))
)
elif len(parameters) == 2:
self.task_ids[count] = \
algorithm.delay(chunk, float(parameters.get("parameter1")), float(parameters.get("parameter2")))
self.task_ids[count] = algorithm.delay(
chunk,
float(parameters.get("parameter1")),
float(parameters.get("parameter2")),
)
elif len(parameters) == 3:
self.task_ids[count] = \
algorithm.delay(chunk, float(parameters.get("parameter1")), float(parameters.get("parameter2")),
float(parameters.get("parameter3")))
self.task_ids[count] = algorithm.delay(
chunk,
float(parameters.get("parameter1")),
float(parameters.get("parameter2")),
float(parameters.get("parameter3")),
)
self.results[count] = None
count = count + 1

def applyToCompleteImage(self, image, path, algorithm, parameters):
if len(parameters) == 0:
self.task_ids[path] = algorithm.delay(image)
elif len(parameters) == 1:
self.task_ids[path] = algorithm.delay(image, float(parameters.get("parameter1")))
self.task_ids[path] = algorithm.delay(
image, float(parameters.get("parameter1"))
)
elif len(parameters) == 2:
self.task_ids[path] = \
algorithm.delay(image, float(parameters.get("parameter1")), float(parameters.get("parameter2")))
self.task_ids[path] = algorithm.delay(
image,
float(parameters.get("parameter1")),
float(parameters.get("parameter2")),
)
elif len(parameters) == 3:
self.task_ids[path] = \
algorithm.delay(image, float(parameters.get("parameter1")), float(parameters.get("parameter2")),
float(parameters.get("parameter3")))
self.task_ids[path] = algorithm.delay(
image,
float(parameters.get("parameter1")),
float(parameters.get("parameter2")),
float(parameters.get("parameter3")),
)
self.results[path] = None

def checkProcessingState(self):
self.filename = self.filename + "_"+str(self.testNumber)
self.testNumber+=1
self.filename = self.filename + "_" + str(self.testNumber)
self.testNumber += 1
newCSV = GenerateCSV(self.filename)
newCSV.openCSVFile(self.filename)
while self.checkForNoneResults(self.results):
Expand All @@ -116,14 +134,24 @@ def checkProcessingState(self):
r = requests.get("http://localhost:5555/api/task/info/" + result.id)
jsonFile = r.json()
if jsonFile["state"] == "SUCCESS":
#print(jsonFile['succeeded'])
# print(jsonFile['succeeded'])
# def writeRow(self,taskUID, taskname,receivedTime, startTime, endTime,runtime, whichworker):
newCSV.writeRow(jsonFile['task-id'],jsonFile['name'],jsonFile['received'],jsonFile['started'],jsonFile['succeeded'],jsonFile['runtime'],jsonFile['worker'])
newCSV.writeRow(
jsonFile["task-id"],
jsonFile["name"],
jsonFile["received"],
jsonFile["started"],
jsonFile["succeeded"],
jsonFile["runtime"],
jsonFile["worker"],
)
self.results[key] = result.get()

def getSplitResults(self, imageOpen, path):
self.checkProcessingState()
self.results = np.array([value for (key, value) in sorted(self.results.items())])
self.results = np.array(
[value for (key, value) in sorted(self.results.items())]
)
imageOpen.saveFile(self.results, path)

def getFolderResults(self, imageOpen):
Expand All @@ -139,7 +167,7 @@ def algorithmApplier(self, algorithm, path, folder, loadType=0, **parameters):
imageOpen = File(path)
image = imageOpen.openFile(loadType)

if (folder):
if folder:
self.filename = self.filename + "_allImage"
self.applyToCompleteImage(image, path, algorithm, parameters)
else:
Expand All @@ -149,7 +177,7 @@ def algorithmApplier(self, algorithm, path, folder, loadType=0, **parameters):


def mainMenu():
""" Function to display Main Menu"""
"""Function to display Main Menu"""
ans = True
while ans:
print("1. Select spliting algorithm")
Expand Down Expand Up @@ -283,7 +311,9 @@ def singleImage(function, par1, par2, loadType, times):
handler = Handler()
for i in range(times):
begin_time = time.time()
handler.algorithmApplier(function, imagePath, False, loadType, parameter1=par1, parameter2=par2)
handler.algorithmApplier(
function, imagePath, False, loadType, parameter1=par1, parameter2=par2
)
handler.restartData()
finish_time = time.time()
time_elapsed.append(finish_time - begin_time)
Expand Down Expand Up @@ -311,10 +341,21 @@ def multiImage(function, par1, par2, loadType, times):
for filename in os.listdir(fullPathToImage):
if filename.endswith(".png") or filename.endswith(".jpg"):
if par1 is None and par2 is None and function is not None:
handler.algorithmApplier(function, fullPathToImage + "/" + filename, True, loadType)
handler.algorithmApplier(
function,
fullPathToImage + "/" + filename,
True,
loadType,
)
else:
handler.algorithmApplier(function, fullPathToImage + "/" + filename, True, loadType,
parameter1=par1, parameter2=par2)
handler.algorithmApplier(
function,
fullPathToImage + "/" + filename,
True,
loadType,
parameter1=par1,
parameter2=par2,
)
aFile = fullPathToImage + "/" + filename
else:
print("Can't use this file")
Expand All @@ -330,7 +371,8 @@ def multiImage(function, par1, par2, loadType, times):

print("The Image Processing has finished")

class GenerateCSV():

class GenerateCSV:
"""
Generates a csv file with all the data from the tests
"""
Expand All @@ -340,35 +382,63 @@ def __init__(self, filename):
Creates the csv and writes the header (Preparing File)
:param name:
"""
with open("./csvFile/" + filename + ".csv", 'a', newline='') as fp:
with open("./csvFile/" + filename + ".csv", "a", newline="") as fp:
self.timeConversion = timeCls()
self.a = csv.writer(fp, delimiter=',')
self.a = csv.writer(fp, delimiter=",")
self.a.writerow(
['TaskUID','Task', 'Task Received', 'Task Started', 'Task Succeed', 'Runtime', 'Time Between Reception And Start Processing (s)','Processing Time (s)','Total Time(s)', 'WorkerName'])

def openCSVFile(self,filename):
[
"TaskUID",
"Task",
"Task Received",
"Task Started",
"Task Succeed",
"Runtime",
"Time Between Reception And Start Processing (s)",
"Processing Time (s)",
"Total Time(s)",
"WorkerName",
]
)

def openCSVFile(self, filename):
"""
Opens a File in append Mode
:param filename:
:return:
"""
self.a = csv.writer(open("./csvFile/" + filename + ".csv", 'a'))
self.a = csv.writer(open("./csvFile/" + filename + ".csv", "a"))


def writeRow(self,taskUID, taskname,receivedTime, startTime, endTime,runtime, whichworker):
def writeRow(
self, taskUID, taskname, receivedTime, startTime, endTime, runtime, whichworker
):
"""
Write a row in the CSV file
"""


self.a.writerow([taskUID,taskname,receivedTime, startTime, endTime, runtime
, startTime-receivedTime, endTime-startTime,endTime - receivedTime, whichworker])
class timeCls():
self.a.writerow(
[
taskUID,
taskname,
receivedTime,
startTime,
endTime,
runtime,
startTime - receivedTime,
endTime - startTime,
endTime - receivedTime,
whichworker,
]
)


class timeCls:
"""
Time conversion class
"""
def convertTimeStamp(self,timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')

def convertTimeStamp(self, timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")


if __name__ == "__main__":
mainMenu()
30 changes: 19 additions & 11 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sys

sys.path.append("~/.virtualenvs/venv/lib/python3.4/site-packages")
import cv2 #THis will import open cv 2
import cv2 # THis will import open cv 2
import numpy as np
from celery_conf import app


@app.task()
def edgeDetection(image, sigma=0.33):
"""
Expand All @@ -22,6 +24,7 @@ def edgeDetection(image, sigma=0.33):
return cv2.Canny(image, lower, upper)
# plt.subplot()


@app.task()
def imageThresholding(image, thresholdValue=127, maxVal=122):
"""
Expand All @@ -34,17 +37,20 @@ def imageThresholding(image, thresholdValue=127, maxVal=122):
:param maxVal: represents the value to be given if pixel value is more than (sometimes less than) the threshold value (0 - black, 255 - White)
:return:
"""
ret, th1 = cv2.threshold(image, thresholdValue, maxVal,cv2.THRESH_BINARY)
ret, th1 = cv2.threshold(image, thresholdValue, maxVal, cv2.THRESH_BINARY)
return th1


@app.task()
def scaling():
pass


@app.task()
def translation():
pass


@app.task()
def rotation(image, angle, scale):
"""
Expand All @@ -55,31 +61,33 @@ def rotation(image, angle, scale):
:param scale: Isotropic scale factor.
:return:
"""
#img = cv2.imread(image,0)
rows,cols = image.shape
# img = cv2.imread(image,0)
rows, cols = image.shape

M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,scale)
dst = cv2.warpAffine(image,M,(cols,rows))
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, scale)
dst = cv2.warpAffine(image, M, (cols, rows))
return dst


@app.task()
def prespectiveTransformation():
pass


@app.task()
def smoothBy_Averaging(image, kernelX, kernelY):
"""https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_filtering/py_filtering.html """
blur = cv2.blur(image,(int(kernelX),int(kernelY)))
"""https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_filtering/py_filtering.html"""
blur = cv2.blur(image, (int(kernelX), int(kernelY)))
return blur


@app.task()
def smoothBy_Blur():
pass


@app.task()
def laplacianDerivative(image):
"""https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_gradients/py_gradients.html"""
laplacian = cv2.Laplacian(image,cv2.CV_64F)
laplacian = cv2.Laplacian(image, cv2.CV_64F)
return laplacian

0 comments on commit d4aeb06

Please sign in to comment.