Applcation IpyWidgets & Appmode
import sys
import os
import io
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
import PIL
import ipysheet as sh
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from PIL import ImageStat
import ipywidgets as widgets
from ipywidgets import GridspecLayout, Layout
from ipywidgets import Button
from ipywidgets import HBox, VBox, Box, HTML, Image
from IPython.display import display
from bqplot import pyplot as bqplt
Be sure to use tensorflow 2.X
assert hasattr(tf, "function") # Be sure to use tensorflow 2.X
print('tensorflow :',tf.__version__)
tensorflow : 2.3.1
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
tf.executing_eagerly()
True
#Empty Data Frame test_df
test_df = pd.DataFrame()
#List targets_names - Labels of Classes
targets_names = ['Cat', 'Dog']
#Images Properties
Images_With = 128; Images_Height = 128
Images_Size = (Images_With, Images_Height)
global model
model = tf.keras.models.load_model('./results/fit1_vgg16_F_Adam&lr_cb2.h5')
file_no_img = open("img/no_image.gif", "rb")
no_image = file_no_img.read()
uploaded_file = widgets.FileUpload(
accept= '.png, .jpg, .bmp', # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'
layout=Layout(height='30px', width='auto',top='60%'),
disabled=False,
multiple = False # True to accept multiple files upload else False
)
def on_change(change):
cell_Class.value = ''
cell_name.value = ''
cell_State.value = ''
cell_Accuracy.value = ''
if len(uploaded_file.value) == 0 :
button_predict.disabled=True
else:
#Load Image & save tmp_file
img.value = load_img()
# Validation images with mean value on 3 channels
res = valid_image()
if res == True:
uploaded_file.disabled=True
button_predict.disabled=False
label_msg = 'You can make a prediction.'
label_cust.value = label_header + label_msg + label_footer
else:
label_msg = 'Not enough information to make prediction.'
label_cust.value = label_header + label_msg + label_footer
uploaded_file.observe(on_change,'value')
from PIL import Image
def load_img():
img_content = ''
uploaded_filename = next(iter(uploaded_file.value))
img_content = uploaded_file.value[uploaded_filename]['content']
label_msg = 'UpLoaded Image'
label_cust.value = label_header + label_msg + label_footer
#Save dict image [content] img_content to tmp_file.png
ext = uploaded_filename[-3:]
with open('data/tmp/tmp_file.' + ext ,'wb') as f: f.write(img_content)
#Convert to only png
Image.open(r'data/tmp/tmp_file.' + ext).save(r'data/tmp/tmp_file.png')
#DataFrame test_df
test_df['filename'] = ['tmp_file.png']
test_df['category'] = ['none']
uploaded_file.value.clear()
#uploaded_file._counter = 0
return img_content
def valid_image():
image_val = Image.open('data/tmp/tmp_file.png')
stat = ImageStat.Stat(image_val)
image_val_mean = (stat.mean[0]+stat.mean[1]+stat.mean[2])//3
if ((image_val_mean > 200) or (image_val_mean < 10)):
val = False
else:
val = True
return val
def make_predict():
button_cancel.disabled = False
#images_generator
images_generator = ImageDataGenerator(rescale = 1.0/255)
images_generator = images_generator.flow_from_dataframe(test_df,
"./data/tmp/",
x_col = 'filename',
y_col = 'category',
target_size = Images_Size,
class_mode = 'categorical',
batch_size = 1)
predictions = model.predict(images_generator, steps=None, verbose=False)
return predictions
def result_predict(predictions):
#predict class
predict_cl = np.argmax(predictions[0])
#index
global class_p
class_p = np.argmax(predictions[0])
#name class
global name_class
name_class = targets_names[np.argmax(predictions[0])]
#accuracy of the prediction in %
acc_predict = np.max(predictions[0]*100)
val = ('{t:3.2f} %'.format(t=acc_predict))
# Validate or Not
a = np.max(predictions[0])
if a > 0.60:
is_valid = 'Valid'
button_predict.disabled=True
button_infos.disabled=False
label_msg = label_msg = 'You can take infos - validated >= 60% (Wait) '
label_cust.value = label_header + label_msg + label_footer
else:
is_valid = 'Not Valid'
button_predict.disabled=True
button_infos.disabled=True
uploaded_file.disabled=False
label_msg = 'Dogs vs. Cats Load Test Image - Not Validated < 60%'
label_cust.value = label_header + label_msg + label_footer
cell_Class.value = str(predict_cl)
cell_name.value = name_class
cell_State.value = is_valid
cell_Accuracy.value = val
def screen_infos_labels(labels):
text = labels
label_f.value = text
def screen_infos(class_p):
path_file_infos = "./infos/" + str(class_p) + "/" + str(class_p) + ".txt"
f = open(path_file_infos, 'rt', encoding="latin-1")
text = f.read()
text_infos.value = text
def screen_infos_img(class_p):
path_file_infos = "./infos/" + str(class_p) + "/" + str(class_p) + ".jpg"
file = open(path_file_infos, "rb")
img = file.read()
return img
button_predict = widgets.Button(
description='Predict',
disabled=True,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
layout=Layout(height='30px', width='auto',top='60%'),
tooltip='Click me',
icon='check' # (FontAwesome names without the `fa-` prefix)
)
button_infos = widgets.Button(
description='Infos',
disabled=True,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
layout=Layout(height='30px', width='auto',top='60%'),
tooltip='Click me',
icon='check' # (FontAwesome names without the `fa-` prefix)
)
button_next = widgets.Button(
description='Next',
disabled=True,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
layout=Layout(height='30px', width='auto',top='60%'),
tooltip='Click me',
icon='check' # (FontAwesome names without the `fa-` prefix)
)
button_cancel = widgets.Button(
description='Cancel',
disabled=True,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
layout=Layout(height='30px', width='auto',top='60%'),
tooltip='Click me',
icon='check' # (FontAwesome names without the `fa-` prefix)
)
output = widgets.Output()
label_f = widgets.Text(
value='',
layout=Layout(height='auto', width='auto',bottom='20%'),
description='',
disabled=True
)
label_f.value = ''
label_msg = 'Dogs vs. Cats Load Test Image'
global label_header
label_header = '<font size=3>   '
global label_footer
label_footer = '</font>'
label_msg_html = label_header + label_msg + label_footer
label = widgets.HTML(
value = label_msg_html,
layout=Layout(height='30px', width='99,9%',top='30%'),
)
label_cust = label.add_class("mylabel")
%%html
<style>
.mylabel > .widget-html-content {
background-color: silver;
}
</style>
def label_screen(msg):
label_cust.value = label_header + label_msg + label_footer
img = widgets.Image(
layout=Layout(height='100%', width='100%',bottom='30%',object_fit='contain'),
format='',
)
img.value = no_image
img1 = widgets.Image(
layout=Layout(height='100%', width='100%',bottom='30%'),
)
img1.value = no_image
file = open("img/cerveau-neuronal.jpg", "rb")
image2 = file.read()
img2 = widgets.Image(
layout=Layout(height='100%', width='100%',bottom='30%'),
)
img2.value = image2
file = open("img/phil.jpg", "rb")
image3 = file.read()
img3 = widgets.Image(
layout=Layout(height='100%', width='100%',bottom='30%'),
)
img3.value = image3
msg_l = '    Dogs vs. Cats Kaggle - Network CNN - Convolution Neural Network - Phil-Dev ©'
Title = widgets.HTML(
value="<font size=4><b>"+ msg_l + "</b></font>",
layout=Layout(height='30px', width='99,9%'),
)
Title_cust = Title.add_class("mytitle")
%%html
<style>
.mytitle > .widget-html-content {
background-color: silver;
}
</style>
text_infos = widgets.Textarea(
value = '',
layout=Layout(height='200px', width='100%',bottom='30%'),
disabled=True
)
#import ipysheet as sh
sheet = sh.sheet(rows=2, columns=4, row_headers=False, column_headers=False,
layout=Layout(height='auto', width='auto'))
cell1 = sh.row(0,[0,1,2,3],background_color="silver")
cell2 = sh.cell(0,0,'Class')
cell3 = sh.cell(0,1,'Name')
cell4 = sh.cell(0,2,'State')
cell5 = sh.cell(0,3,'Accuracy')
cell_Class = sh.cell(1,0,'')
cell_name = sh.cell(1,1,'')
cell_State = sh.cell(1,2,'')
cell_Accuracy = sh.cell(1,3,'')
box_layount = widgets.Layout(border='solid 1px black', width='970px', height='600px')
grid = GridspecLayout(12, 15)
grid.layout = box_layount
grid[0, :] = Title
grid[1, 11:14] = label_f
grid[7:10, 11:14] = text_infos
grid[3:7, 1:5] = img
grid[3:7, 11:14] = img1
grid[2:5, 6:10] = img2
grid[6:10, 6:10] = img3
grid[9, 1:5] = sheet
grid[10, 0:3] = uploaded_file
grid[10, 3:6] = button_predict
grid[10, 6:9] = button_infos
grid[10, 9:12] = button_next
grid[10, 12:15] = button_cancel
grid[11, :] = label_cust
grid
Found 1 validated image filenames belonging to 1 classes. Found 1 validated image filenames belonging to 1 classes.
def on_button_predict_clicked(b):
predictions = make_predict()
result_predict(predictions)
with output:
button_predict.disabled=True
button_predict.on_click(on_button_predict_clicked)
def on_button_infos_clicked(b):
screen_infos_labels(name_class)
screen_infos(class_p)
image_infos = screen_infos_img(class_p)
with output:
uploaded_file.disabled=True
button_infos.disabled=True
button_next.disabled=False
img1.value = image_infos
button_infos.on_click(on_button_infos_clicked)
def on_button_next_clicked(b):
with output:
uploaded_file.disabled=False
button_predict.disabled=True
button_infos.disabled=True
button_next.disabled=True
cell_Class.value = ''
cell_name.value = ''
cell_State.value = ''
cell_Accuracy.value = ''
label_f.value = ''
text_infos.value = ''
img.value = no_image
img1.value = no_image
label_msg = 'Dogs vs. Cats Load Test Image'
label_cust.value = label_header + label_msg + label_footer
button_next.on_click(on_button_next_clicked)
def on_button_cancel_clicked(b):
with output:
uploaded_file.disabled=False
button_predict.disabled=True
button_infos.disabled=True
button_next.disabled=True
button_cancel.disabled=True
cell_Class.value = ''
cell_name.value = ''
cell_State.value = ''
cell_Accuracy.value = ''
label_f.value = ''
text_infos.value = ''
img.value = no_image
img1.value = no_image
label_msg = 'Dogs vs. Cats Load Test Image'
label_cust.value = label_header + label_msg + label_footer
button_cancel.on_click(on_button_cancel_clicked)