Skip to content

Commit

Permalink
Merge pull request #44 from rvshi/docs
Browse files Browse the repository at this point in the history
Docs
  • Loading branch information
michellewei04 committed May 1, 2018
2 parents 30e6887 + 8f55096 commit f9c5a8b
Show file tree
Hide file tree
Showing 17 changed files with 386 additions and 36 deletions.
27 changes: 16 additions & 11 deletions backend/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
from images import save_image, get_image_by_uuid, get_image_as_b64


def act_login(req):
def act_login(request):
"""Authenticates login with email and password
: param req: json request from client
:param request: json request from client
:returns: json of jwt
"""
params = request.get_json()
username = params.get('username', None)
Expand All @@ -34,18 +35,20 @@ def act_login(req):
def act_list(username):
"""Lists the original and processed images for a user
:param username: client username
:param username: client username
:returns: json including uuid's of original and processed images
"""
return(jsonify({
'originalID': get_original_image(username),
'processedID': get_processed_image(username)
}), 200)


def act_upload(req):
def act_upload(request):
"""Uploads original user image
:param req: request from client
:param request: request from client
:returns: uuid of uploaded image
"""
params = request.get_json()
username = params.get('username', None)
Expand All @@ -60,10 +63,11 @@ def act_upload(req):
return (jsonify({'fileID': uuid}), 200)


def act_process(req):
def act_process(request):
"""Processes the original image that has been uploaded
:param req: request from client
:param request: request from client
:returns: uuid of processed image
"""

username = request.get_json().get('username', None)
Expand All @@ -76,11 +80,12 @@ def act_process(req):
return (jsonify({'fileID': newUuid}), 200)


def act_download(req):
'''Handles download request for images
def act_download(request):
"""Handles download request for images
:param req: json request from client
'''
:param request: json request from client
:returns: b64 image string of processed image
"""
params = request.get_json()
fileID = params.get('fileID', None)
filetype = params.get('filetype', None)
Expand Down
42 changes: 42 additions & 0 deletions backend/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class User(MongoModel):


def add_user(username, password):
"""Creates new user if user does not exist in the mongo database
:param username: user email as string type which serves as user id
:param password: user password as string type
:returns: updates user information in mongo database
"""
try:
user = User.objects.raw({'_id': username}).first()
except DoesNotExist:
Expand All @@ -22,6 +28,10 @@ def add_user(username, password):


def get_user(username):
"""Gets user by unique username
:param username: user email as string type which serves as user id
:returns: user information
"""
try:
user = User.objects.raw({'_id': username}).first()
return user
Expand All @@ -30,6 +40,9 @@ def get_user(username):


def delete_user(username):
"""Deletes user from mongo database
:param username: user email as string type which serves as user id
"""
try:
user = User.objects.raw({'_id': username}).first()
user.delete()
Expand All @@ -40,6 +53,11 @@ def delete_user(username):


def login_user(username, password):
"""Returns true if user exists and has the correct password
:param username: user email as string type which serves as user id
:param password: user password as string type
:returns: True if password is correct, False if incorrect
"""
try:
user = User.objects.raw({'_id': username}).first()
if user.password and pbkdf2_sha256.verify(password, user.password):
Expand All @@ -51,6 +69,11 @@ def login_user(username, password):


def save_original_image_uuid(username, uuid):
"""Updates existing user by adding the uuid of a user-uploaded image
:param username: user email as string type which serves as user id
:param uuid: UUID4 of user-uploaded image
:returns: adds uuid of user-uploaded image to mongo database
"""
try:
user = User.objects.raw({'_id': username}).first()
user.original_image = uuid
Expand All @@ -60,6 +83,11 @@ def save_original_image_uuid(username, uuid):


def save_processed_image_uuid(username, uuid):
"""Updates existing user by adding the uuid of the processed image
:param username: user email as string type which serves as user id
:param uuid: UUID4 of processed image
:returns: adds uuid of processed image to mongo database
"""
try:
user = User.objects.raw({'_id': username}).first()
user.processed_image = uuid
Expand All @@ -69,6 +97,10 @@ def save_processed_image_uuid(username, uuid):


def get_original_image(username):
"""Gets the original image uuid for a user
:param username: user email as string type which serves as user id
:returns: uuid of user's original image as a string
"""
try:
user = User.objects.raw({'_id': username}).first()
return user.original_image
Expand All @@ -77,6 +109,10 @@ def get_original_image(username):


def get_processed_image(username):
"""Gets the processed image uuid for a user
:param username: user email as string type which serves as user id
:returns: uuid (UUID4) of user's processed image as a string
"""
try:
user = User.objects.raw({'_id': username}).first()
return user.processed_image
Expand All @@ -85,13 +121,19 @@ def get_processed_image(username):


def delete_image(name):
"""Deletes image stored in server
:param name: name (uuid) of an image stored in the VM server
"""
for f in os.listdir('images/'):
if f.startswith(name):
os.remove('images/' + f)
return


def remove_images(username):
"""Removes all images associated with a user
:param username: user email as string type which serves as user id
"""
try:
user = User.objects.raw({'_id': username}).first()
if user.original_image is not None:
Expand Down
39 changes: 18 additions & 21 deletions backend/images.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import base64
import numpy as np
from PIL import Image
from mimetypes import guess_extension
# from mimetypes import guess_extension
from uuid import uuid4
import re
import os
from io import BytesIO


def save_image(img_str):
'''Converts image string to binary and saves onto drive
:param img_str: base64 image string
:return uuid: UUID4 of image'''
"""Converts image string to binary and saves onto drive
:param img_str: base64 image string
:returns: uuid of image
"""
uuid = uuid4().hex
str_parts = img_str.split(',')

Expand All @@ -34,26 +34,23 @@ def save_image(img_str):


def save_image_from_arr(img_arr):
'''Converts uint array to png file (intermediary format stored on server)
:param img_arr: uint array
:return uuid: uuid of converted image'''
"""Converts uint array to png file (intermediary format stored on server)
:param img_arr: uint array of image
:returns: uuid of image
"""
uuid = uuid4().hex
img = Image.fromarray(img_arr)
img.save('images/{}.png'.format(uuid), 'PNG')
return uuid


def get_image_by_uuid(uuid):
'''Converts image base64 string to uint array, saves intermediate image
file to server
:param img_str: base64 image string
:param uuid: UUID of image to save
:return data: grayscale image array
'''
"""Retrieves uint array of image by its uuid
:param uuid: UUID of image as string
:returns: grayscale image array
"""
for f in os.listdir('images/'):
if re.match(uuid, f):
im = Image.open('images/' + f)
Expand All @@ -65,12 +62,12 @@ def get_image_by_uuid(uuid):


def get_image_as_b64(uuid, filetype='png'):
'''Gets b64 image string by uuid
"""Gets b64 image string by uuid
:param uuid: uuid of image
:param filetype: file type to output, options are jpeg, png, or gif
:return: b64 string of image
'''
:param uuid: uuid of image
:param filetype: file type to output, options are jpeg, png, or gif
:returns: b64 string of image
"""

filetype = filetype.lower()
img_format = None
Expand Down
8 changes: 4 additions & 4 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
def handler(input, validator, action):
"""Handles API endpoints
:param input: input data from request
:param validator: validator function to use
:param action: database function to apply to data
:return: jsonified response
:param input: input data from request
:param validator: validator function to use
:param action: database function to apply to data
:return: jsonified response
"""

if not request.is_json:
Expand Down
6 changes: 6 additions & 0 deletions backend/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@


def segment(uuid):
"""Segments image with input uuid, saves processed image to server
and returns its uuid
:param uuid: uuid of original image
:returns: uuid of processed image, saves b64 string of image on server
"""
uintfile = get_image_by_uuid(uuid)
val = filters.threshold_otsu(uintfile)
hist, bins_center = exposure.histogram(uintfile)
Expand Down
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
SPHINXPROJ = ImageProcessorS18
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
7 changes: 7 additions & 0 deletions docs/actions.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
actions module
==============

.. automodule:: actions
:members:
:undoc-members:
:show-inheritance:

0 comments on commit f9c5a8b

Please sign in to comment.