-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #5, fixes #6, fixes #31. * Added upload functionality/image storage/UUID * Combined segmentation and backend functionality * Added download functionality * Bug fixes for test server * Fixed regex bug for image saving * Adds example images. * Uses Flask debug server instead of gunicorn for testing. * Adds backend testing script. * Fixes some backend bugs - user entries are now saved correctly - image conversion is now more robust - support for downloading jpeg, png and gif files - validation to match new endpoints * Got the segmentation process fixed. - turns out you cannot send a boolean array to Image.save() * Implements /list endpoint. - for getting the ids of the original and processed images * /download now uses uuid for choosing the file * Functional frontend. - all endpoints now implemented - both original and processed image are displayed * Added fix for converting RGBA images to JPEG. * Removes console.log. * Small fixes - fixes Icon classname -> className - adds logout notification
- Loading branch information
Showing
29 changed files
with
559 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,7 @@ ENV/ | |
# database | ||
db | ||
|
||
# backend files | ||
secret_key.py | ||
db_debug.py | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import requests | ||
import base64 | ||
|
||
baseURL = 'http://localhost:5000/' | ||
|
||
test_img = 'exampleImages/test.jpg' | ||
filetype = 'jpeg' | ||
|
||
un = 'test@mail.com' | ||
pw = '12345' | ||
|
||
|
||
def main(): | ||
headers = login(un, pw) | ||
print(_list(headers, un)) | ||
origID = upload(headers, un, test_img) | ||
procID = process(headers, un) | ||
download(headers, un, procID, filetype) | ||
print(_list(headers, un)) | ||
|
||
|
||
def read_image(path): | ||
with open(path, 'rb') as image_file: | ||
return base64.b64encode(image_file.read()).decode() | ||
|
||
|
||
def login(username, password): | ||
body = { | ||
'username': username, | ||
'password': password | ||
} | ||
r = requests.post(baseURL + 'login', json=body) | ||
jwt = r.json()['jwt'] | ||
|
||
__headers__ = { | ||
'authorization': 'Bearer ' + jwt, | ||
'content-type': 'application/json', | ||
} | ||
return __headers__ | ||
|
||
|
||
def _list(headers, username): | ||
r = requests.get(baseURL + 'list', headers=headers) | ||
return r.text | ||
|
||
|
||
def upload(headers, username, test_img): | ||
body = { | ||
'username': username, | ||
'file': 'data:image/png;base64,' + read_image(test_img) | ||
} | ||
r = requests.post(baseURL + 'upload', headers=headers, json=body) | ||
return r.json()['fileID'] | ||
|
||
|
||
def process(headers, username): | ||
body = { | ||
'username': username | ||
} | ||
r = requests.post(baseURL + 'process', headers=headers, json=body) | ||
return r.json()['fileID'] | ||
|
||
|
||
def download(headers, username, fileID, img_format): | ||
body = { | ||
'username': username, | ||
'fileID': fileID, | ||
'filetype': img_format | ||
} | ||
r = requests.post(baseURL + 'download', headers=headers, json=body) | ||
data = r.json()['file'] | ||
with open('exampleImages/temp.{}'.format(img_format), 'wb') as f: | ||
f.write(base64.decodebytes(data.encode())) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
temp* |
File renamed without changes.
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import base64 | ||
import numpy as np | ||
from PIL import Image | ||
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''' | ||
|
||
uuid = uuid4().hex | ||
str_parts = img_str.split(',') | ||
|
||
if(len(str_parts) == 2): | ||
img_type = str_parts[0] | ||
img_raw = str_parts[1] | ||
|
||
try: | ||
extension = re.search('data:image/(.+);', img_type).group(1) | ||
except AttributeError: # no match found | ||
return None | ||
|
||
with open('images/{}.png'.format(uuid, extension), 'wb') as f: | ||
f.write(base64.b64decode(img_raw)) | ||
return uuid | ||
|
||
return None | ||
|
||
|
||
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''' | ||
|
||
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 | ||
''' | ||
|
||
for f in os.listdir('images/'): | ||
if re.match(uuid, f): | ||
im = Image.open('images/' + f) | ||
im = im.convert('L') | ||
im_arr = np.asarray(im) | ||
return im_arr | ||
|
||
return None | ||
|
||
|
||
def get_image_as_b64(uuid, filetype='png'): | ||
'''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 | ||
''' | ||
|
||
filetype = filetype.lower() | ||
img_format = None | ||
if filetype == 'png': | ||
img_format = 'PNG' | ||
elif filetype == 'jpeg' or filetype == 'jpg': | ||
img_format = 'JPEG' | ||
elif filetype == 'gif': | ||
img_format = 'GIF' | ||
else: | ||
return None # error, incorrect file type | ||
|
||
# convert file to desired type | ||
|
||
for f in os.listdir('images/'): | ||
if f.startswith(uuid): | ||
output = BytesIO() | ||
image = Image.open('images/' + f.format(uuid)) | ||
|
||
# handle transparent images | ||
if image.mode in ('RGBA', 'LA'): | ||
background = Image.new(image.mode[:-1], image.size, '#fff') | ||
background.paste(image, image.split()[-1]) | ||
image = background | ||
|
||
image.save(output, format=img_format) | ||
contents = base64.b64encode(output.getvalue()).decode() | ||
output.close() | ||
return contents |
Oops, something went wrong.