-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
184 lines (151 loc) · 8.22 KB
/
Copy pathapp2.py
File metadata and controls
184 lines (151 loc) · 8.22 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import streamlit as st # for creating webapp
import cv2 # for computer vision tasks
from PIL import Image, ImageEnhance
# PIL - Python Image Library, imported Image for image editing
# imported imageEnhance for morphological processing operations
# like contrast,brightness,blurriness etc
import numpy as np # to deal with arrays
# image contains pixel values 0-255 stores in arrays
import os # for os related operations
import streamlit as st
from keras.models import Model
from keras.models import load_model
from keras.preprocessing import image
from keras.applications.efficientnet import preprocess_input
import numpy as np
import tensorflow as tf
# from tensorflow.keras.models import BatchNormalization
from tensorflow.keras.layers import BatchNormalization
def main():
st.title('LOP or Porosity Detection')
# st.markdown('For us, Safety is not a priority, it is a precondition')
tasks = ['Detection','Visualization']
choice = st.sidebar.selectbox('Select Task',tasks, key="a")
if choice == 'Visualization':
st.subheader('Image Visualization')
image_file = st.file_uploader('Upload', type=['jpg', 'png', 'jpeg'], key='n')
st.text("")
if image_file is not None:
our_image = Image.open(image_file)
# Create two columns
col1, col2 = st.columns(2)
# Display original image in the first column
col1.markdown('Original Image')
col1.image(our_image, width=325)
# Display modified image in the second column
col2.markdown('Modified Image')
enhance_type = st.sidebar.radio("Enhance type", ['Original', 'Gray-scale', 'Contrast', 'Brightness', 'Blurring', 'Sharpness'], key="c")
if enhance_type == 'Gray-scale':
img = np.array(our_image.convert('RGB'))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
col2.image(gray, width=325)
elif enhance_type == 'Contrast':
rate = st.sidebar.slider("Contrast", 0.5, 6.0)
enhancer = ImageEnhance.Contrast(our_image)
enhanced_img = enhancer.enhance(rate)
col2.image(enhanced_img, width=325)
elif enhance_type == 'Brightness':
rate = st.sidebar.slider("Brightness", 0.0, 8.0)
enhancer = ImageEnhance.Brightness(our_image)
enhanced_img = enhancer.enhance(rate)
col2.image(enhanced_img, width=325)
elif enhance_type == 'Blurring':
rate = st.sidebar.slider("Blurring", 0.0, 7.0)
blurred_img = cv2.GaussianBlur(np.array(our_image), (15, 15), rate)
col2.image(blurred_img, width=325)
elif enhance_type == 'Sharpness':
rate = st.sidebar.slider("Sharpness", 0.0, 14.0)
enhancer = ImageEnhance.Sharpness(our_image)
enhanced_img = enhancer.enhance(rate)
col2.image(enhanced_img, width=325)
elif enhance_type == "Original":
col2.image(our_image, width=325)
else:
col2.image(our_image, width=325)
# Add space between lines
st.text("") # or st.markdown("") for Markdown
process_choice = st.sidebar.selectbox("Choose a process", ["None", "Crop", "Flip Horizontal", "Flip Vertical", "Add Noise", "Apply Filter", "Edge Detection", "Segmentation"])
if process_choice == "Crop":
st.sidebar.text("Crop the image")
left = st.sidebar.slider("Left", 0, our_image.width, 0)
top = st.sidebar.slider("Top", 0, our_image.height, 0)
right = st.sidebar.slider("Right", 0, our_image.width, our_image.width)
bottom = st.sidebar.slider("Bottom", 0, our_image.height, our_image.height)
cropped_image = crop_image(our_image, left, top, right, bottom)
col2.image(cropped_image, width=325)
elif process_choice == "Flip Horizontal":
flipped_image = our_image.transpose(Image.FLIP_LEFT_RIGHT)
col2.image(flipped_image, width=325)
elif process_choice == "Flip Vertical":
flipped_image = our_image.transpose(Image.FLIP_TOP_BOTTOM)
col2.image(flipped_image, width=325)
elif process_choice == "Add Noise":
noise_type = st.sidebar.radio("Noise type", ["Salt and Pepper", "Gaussian"])
if noise_type == "Salt and Pepper":
noise_amount = st.sidebar.slider("Amount", 0.01, 0.1, 0.05)
noisy_image = add_salt_and_pepper(np.array(our_image.convert('RGB')), noise_amount)
col2.image(noisy_image, width=325)
elif noise_type == "Gaussian":
noisy_image = add_gaussian_noise(np.array(our_image.convert('RGB')))
col2.image(noisy_image, width=325)
elif process_choice == "Apply Filter":
filter_type = st.sidebar.radio("Filter type", ["Average", "Median", "Min", "Max", "Mode", "Gaussian"])
if filter_type == "Average":
filtered_image = apply_filter(our_image, ImageFilter.BoxBlur(3))
elif filter_type == "Median":
filtered_image = apply_filter(our_image, ImageFilter.MedianFilter(size=3))
elif filter_type == "Min":
filtered_image = apply_filter(our_image, ImageFilter.MinFilter(size=3))
elif filter_type == "Max":
filtered_image = apply_filter(our_image, ImageFilter.MaxFilter(size=3))
elif filter_type == "Mode":
filtered_image = apply_filter(our_image, ImageFilter.ModeFilter(size=3))
elif filter_type == "Gaussian":
filtered_image = apply_filter(our_image, ImageFilter.GaussianBlur(radius=3))
col2.image(filtered_image, width=325)
elif process_choice == "Edge Detection":
edge_type = st.sidebar.radio("Edge detection type", ["Canny", "Sobel"])
if edge_type == "Canny":
edge_image = apply_canny(our_image)
elif edge_type == "Sobel":
edge_image = apply_sobel(our_image)
col2.image(edge_image, width=325)
elif process_choice == "Segmentation":
seg_type = st.sidebar.radio("Segmentation type", ["Felzenszwalb", "Gaussian Adaptive"])
if seg_type == "Felzenszwalb":
scale = st.sidebar.slider("Scale", 100, 1000, 300)
seg_image = apply_felzenszwalb(our_image, scale)
elif seg_type == "Gaussian Adaptive":
seg_image = apply_gaussian_adaptive_thresholding(our_image)
col2.image(seg_image, width=325)
elif choice == 'Detection':
tasks=["LOP or Porosity"]
disease_type = st.sidebar.radio("Enhance type",tasks, key="d")
if disease_type == 'LOP or Porosity':
# Load the saved model
loaded_model = load_model("model.h5")
# loaded_model.save("efficientnet_model_updated.h5")
def predict_image_class(img_path):
img = image.load_img(img_path, target_size=(640, 640))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
predictions = loaded_model.predict(img_array)
predicted_class = np.argmax(predictions, axis=1)[0]
return predicted_class
# Streamlit App
st.subheader('LOP or Porosity detection')
uploaded_file = st.file_uploader("Choose an image...", type=['jpg', 'png', 'jpeg'], key='m')
if uploaded_file is not None:
st.image(uploaded_file, caption="Uploaded Image.", use_column_width=True)
st.write("")
st.write("Classifying...")
# Get predictions
predicted_class = predict_image_class(uploaded_file)
# Display the predicted class
num_classes = loaded_model.layers[-1].output_shape[1]
classes = [str(i) for i in range(num_classes)]
class_names = ["lop", "porosity"]
st.write(f"Predicted State : {class_names[predicted_class]}")
if __name__ == 'main':
main()