File size: 11,980 Bytes
50d6ddc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import cv2
import gradio as gr
import pandas as pd
import shortuuid
from ultralytics import YOLO
from utils.data_utils import clear_all
import torch
import numpy as np
import os
from utils.measure_utils import ContourAnalyzer
from PIL import Image
import utils.plot as pt


# Clear any previous data and configurations
clear_all()
model = YOLO('./weights/best.pt')
# Define the color scheme/theme for the website
theme = gr.themes.Soft(
    primary_hue="orange",
    secondary_hue="sky",
)
#Custom css for styling
css = """
    .size {
        min-height: 400px !important;
        max-height: 400px !important;
        overflow: auto !important;
    }
"""

# Create the Gradio interface using defined theme and CSS
with gr.Blocks(theme=theme, css=css) as demo:
    # Title and description for the app
    gr.Markdown("# Concrete Crack Detection and Segmentation")
    gr.Markdown("Upload concrete crack images and get segmented results.")
    with gr.Tab('Instructions'):
        gr.Markdown(
    """**Instructions for Concrete Crack Detection and Segmentation App:**

    **Input:**
    - Upload one or more concrete crack images using the "Image Input" section.
    - Adjust confidence level and distance sliders if needed.\n
    **Buttons:**
    - Click "Segment" to perform crack segmentation.
    - Click "Clear" to reset inputs and outputs.\n
    **Output:**
    - View segmented images in the "Image Output" gallery.
    - Check crack detection results in the "Results" table.
    - Download the PDF report file with detailed information..

    **Additional Information:**
    - The app uses a YOLOv8 trained model for crack detection with 86.8\% accuracy.
    - Results include orientation category, width of the crack (widest), number of cracks per photo.

    **Notes:**
    - Ensure uploaded images are in the supported formats: PNG, JPG, JPEG, WEBP.
    - Remarks and Reference Image must have data.

    **Enjoy detecting and segmenting concrete cracks with the app!**
    """)
    # Image tab
    with gr.Tab("Image"):
        
        with gr.Row():           
            with gr.Column():
                # Input section for uploading images
                image_input = gr.File(
                    file_count="multiple",
                    file_types=["image"],
                    label="Image Input",
                    elem_classes="size",
                )


                #Confidence Score for prediction
                conf = gr.Slider(value=20,step=5, label="Confidence", 
                                   interactive=True)
                distance = gr.Slider(value=10,step=1, label="Distance (cm)", 
                                   interactive=True)
                # Buttons for segmentation and clearing
                image_remark = gr.Textbox(label="Remark for the Batch",
                                          placeholder='Fifth floor: Wall facing the door')
                with gr.Row():
                    image_button = gr.Button("Segment", variant='primary')
                    image_clear = gr.ClearButton()

            with gr.Column():
                # Display section for segmented images
                image_output = gr.Gallery(
                    label="Image Output",
                    show_label=True,
                    elem_id="gallery",
                    columns=2,
                    object_fit="contain",
                    height=400,
                )
                md_result = gr.Markdown("**Results**", visible=False)
                csv_image = gr.File(label='Report', interactive=False, visible=False)
                df_image = gr.DataFrame(visible=False)

                
        image_reference = gr.File(
                    file_count="multiple",
                    file_types=["image"],
                    label="Reference Image",)


    def detect_pattern(image_path):
        """
        Detect concrete cracks in the binary image.

        Parameters:
            image_path (str): Path to the binary image.

        Returns:
            tuple: Principal orientation and orientation category.
        """
        image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
        skeleton = cv2.erode(image, np.ones((3, 3), dtype=np.uint8), iterations=1)
        contours, _ = cv2.findContours(skeleton, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        data_pts = np.vstack([contour.squeeze() for contour in contours])
        mean, eigenvectors = cv2.PCACompute(data_pts.astype(np.float32), mean=None)
        principal_orientation = np.arctan2(eigenvectors[0, 1], eigenvectors[0, 0])

        if -0.05 <= principal_orientation <= 0.05:
            orientation_category = "Horizontal"
        elif 1 <= principal_orientation <= 1.8:
            orientation_category = "Vertical"
        elif -0.99 <= principal_orientation <= 0.99:
            orientation_category = "Diagonal"
        else:
            orientation_category = "Other"

        return principal_orientation, orientation_category

    def load_model():
        """
        Load the YOLO model with pre-trained weights.
        
        Returns:
            model: Loaded YOLO model.
        """
        return YOLO('./weights/best.pt')
    
    def generate_uuid():
        """
        Generates a short unique identifier.
        
        Returns:
            str: Unique identifier string.
        """
        return str(shortuuid.uuid())


    def preprocess_image(image):
        """ 
        Preprocesses the input image.
        
        Parameters:
            image (numpy.array or PIL.Image): Image to preprocess.
        
        Returns:
            numpy.array: Resized and converted RGB version of the input image.
        """

        image = np.array(image)

        input_image = Image.fromarray(image)
        input_image = input_image.resize((640, 640))
        input_image = input_image.convert("RGB")

        return np.array(input_image)
    
    
    def predict_segmentation_im(image, conf, reference, remark):
        """
        Perform segmentation prediction on a list of images.
        
        Parameters:
            image (list): List of images for segmentation.
            conf (float): Confidence score for prediction.

        Returns:
            tuple: Paths of the processed images, CSV file, DataFrame, and Markdown.
        """
        # Check if reference or remark is empty
        if not reference:
            raise gr.Error("Reference Image cannot be empty.")
        if not remark:
            raise gr.Error("Batch Remark cannot be empty.")
        if not image:
            raise gr.Error("Image input cannot be empty.")
        
        print("THE REFERENCE IN APPPY", reference)
        uuid = generate_uuid()
        image_list = [preprocess_image(Image.open(file.name)) for file in image]
        filenames = [file.name for file in image]
        conf= conf * 0.01
        model = load_model()
        results = model.predict(image_list, conf=conf, save=True, project='output', name=uuid, stream=True)
        processed_image_paths = []
        output_image_paths = []
        result_list = []
        width_list = []
        orientation_list = []
        width_interpretations = []
        # Populate the dataframe with counts
        for i, r in enumerate(results):
            result_list.append(r)
            instance_count = len(r)
            if r.masks is not None and r.masks.data.numel() > 0:
                masks = r.masks.data
                boxes = r.boxes.data
                clss = boxes[:, 5]
                people_indices = torch.where(clss == 0)
                people_masks = masks[people_indices]
                people_mask = torch.any(people_masks, dim=0).int() * 255
                processed_image_path = str(model.predictor.save_dir / f'binarize{i}.jpg')
                cv2.imwrite(processed_image_path, people_mask.cpu().numpy())
                processed_image_paths.append(processed_image_path)
                
                crack_image_path = processed_image_path
                principal_orientation, orientation_category = detect_pattern(crack_image_path)
                
                # Print the results if needed
                print(f"Crack Detection Results for {crack_image_path}:")
                print("Principal Component Analysis Orientation:", principal_orientation)
                print("Orientation Category:", orientation_category)

                # Load the original image in color
                original_img = cv2.imread(f'output/{uuid}/image{i}.jpg')
                orig_image_path = str(model.predictor.save_dir / f'image{i}.jpg')
                processed_image_paths.append(orig_image_path)
                # Load and resize the binary image to match the dimensions of the original image
                binary_image = cv2.imread(f'output/{uuid}/binarize{i}.jpg', cv2.IMREAD_GRAYSCALE)
                binary_image = cv2.resize(binary_image, (original_img.shape[1], original_img.shape[0]))

                contour_analyzer = ContourAnalyzer()
                max_width, thickest_section, thickest_points, distance_transforms = contour_analyzer.find_contours(binary_image)

                visualized_image = original_img.copy()
                cv2.drawContours(visualized_image, [thickest_section], 0, (0, 255, 0), 1)

                contour_analyzer.draw_circle_on_image(visualized_image, (int(thickest_points[0]), int(thickest_points[1])), 5, (57, 255, 20), -1)
                print("Max Width in pixels: ", max_width)

                width = contour_analyzer.calculate_width(y=10, x=5, pixel_width=max_width, calibration_factor=0.001, distance=150)
                print("Max Width, converted: ", width)
                
                prets = pt.classify_wall_damage(width)
                width_interpretations.append(prets)
                
                visualized_image_path = f'output/{uuid}/visualized_image{i}.jpg'
                output_image_paths.append(visualized_image_path)
                cv2.imwrite(visualized_image_path, visualized_image)

                width_list.append(round(width, 2))
                orientation_list.append(orientation_category)
            else:
                original_img = cv2.imread(f'output/{uuid}/image{i}.jpg')
                visualized_image_path = f'output/{uuid}/visualized_image{i}.jpg'
                output_image_paths.append(visualized_image_path)
                cv2.imwrite(visualized_image_path, original_img)
                width_list.append('None')
                orientation_list.append('None')
                width_interpretations.append('None')

        # Delete binarized and initial segmented images after processing
        for path in processed_image_paths:
            if os.path.exists(path):
                os.remove(path)
                
        # results = gr.Textbox(res, visible=True)
        csv, df = pt.count_instance(result_list, filenames, uuid, width_list, orientation_list, output_image_paths, reference, remark, width_interpretations)

        csv = gr.File(value=csv, visible=True)
        df = gr.DataFrame(value=df, visible=True)
        md = gr.Markdown(visible=True)

        # return get_all_file_paths(f"output/{uuid}/"), csv, df, md
        return output_image_paths, csv, df, md


    # Connect the buttons to the prediction function and clear function
    image_button.click(
        predict_segmentation_im,
        inputs=[image_input, conf, image_reference, image_remark],
        outputs=[image_output, csv_image, df_image, md_result]
    )
    
    image_clear.click(
        lambda: [
            None,
            None,
            gr.Markdown(visible=False),
            gr.File(visible=False),
            gr.DataFrame(visible=False),
            gr.Slider(value=20),
            None,
            None
        ],
        outputs=[image_input, image_output, md_result, csv_image, df_image, conf, image_reference, image_remark]
    )

# Launch the Gradio app
demo.launch()