-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogic.py
61 lines (42 loc) · 1.5 KB
/
logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import base64
import io
import logging
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from model import preprocess_image, postprocess_image
logging.basicConfig(level=logging.INFO)
def predict(opencv_image, ai_model):
# make prediction
model_output = predict_model(opencv_image, ai_model)
final_image = postprocess_image(model_output, opencv_image)
return final_image
def predict_model(img, classifier):
logging.debug("predict lesions with DNN model")
input_tensor = preprocess_image(img)
output = classifier(input_tensor)
return output
def matplotlib_viz(image):
def fig2img(fig):
"""Convert a Matplotlib figure to a PIL Image and return it"""
buf = io.BytesIO()
fig.savefig(buf)
buf.seek(0)
img = Image.open(buf)
return img
def image_to_byte_array(image: Image):
img_byte_array = io.BytesIO()
image.save(img_byte_array, format=image.format)
img_byte_array = img_byte_array.getvalue()
return img_byte_array
figure = plt.figure()
plot = figure.add_subplot(111)
plot.imshow(image)
matplotlib_plotted_image = image_to_byte_array(fig2img(figure))
return matplotlib_plotted_image
def ct_scan_image_to_rgb(image):
return np.stack([image] * 3, axis=2) + 50
def convert_to_base64(obj):
logging.debug("Convert to base64")
base64_data = base64.b64encode(obj) # convert to base64 as bytes
return base64_data.decode() # convert bytes to string