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|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
|
@@ -6,8 +506,8 @@ from sahi import AutoDetectionModel
|
|
| 6 |
from PIL import Image
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
import torch
|
| 9 |
-
|
| 10 |
-
|
| 11 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 12 |
|
| 13 |
|
|
@@ -146,7 +646,6 @@ class Detection:
|
|
| 146 |
return individual_graphs['Nicks'], individual_graphs['Dents'], individual_graphs['Scratches'], individual_graphs['Pittings']
|
| 147 |
|
| 148 |
|
| 149 |
-
|
| 150 |
|
| 151 |
|
| 152 |
|
|
@@ -210,7 +709,7 @@ def upload_image(image):
|
|
| 210 |
"""Process the uploaded image (if needed) and display it."""
|
| 211 |
return image
|
| 212 |
|
| 213 |
-
|
| 214 |
def apply_detection(image):
|
| 215 |
"""Run object detection on the uploaded image and return the annotated image."""
|
| 216 |
# Convert image from PIL to NumPy array
|
|
@@ -352,37 +851,54 @@ function refresh() {
|
|
| 352 |
"""
|
| 353 |
|
| 354 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
# Gradio interface components
|
| 357 |
with gr.Blocks(css = css,js=js_func) as demo:
|
| 358 |
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
<div class="left">
|
| 363 |
-
<h1><span>OIS</span><br></h1>
|
| 364 |
-
<span class="second-line">AI Detection Model</span>
|
| 365 |
-
<p>
|
| 366 |
-
The OIS AI Detection Model enhances manufacturing by using the powerful YOLOv11 algorithm on
|
| 367 |
-
a Raspberry Pi for real-time, on-device defect detection. It automates quality control,
|
| 368 |
-
reduces human error, and minimizes downtime. With a user-friendly web interface,
|
| 369 |
-
the model enables offline swift defect identification, seamless integration into
|
| 370 |
-
production, and improving both efficiency and product quality.
|
| 371 |
-
</p>
|
| 372 |
-
</div>
|
| 373 |
-
|
| 374 |
-
</header>
|
| 375 |
-
|
| 376 |
-
<section class="container">
|
| 377 |
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
# Image Upload and Display in two columns
|
| 387 |
with gr.Column():
|
| 388 |
gr.Markdown("### Input")
|
|
@@ -396,34 +912,97 @@ with gr.Blocks(css = css,js=js_func) as demo:
|
|
| 396 |
apply_detection_btn.click(apply_detection, inputs=upload_image_component, outputs=[output_image_component, output_annotations])
|
| 397 |
|
| 398 |
|
|
|
|
|
|
|
| 399 |
# Row for the graphs
|
| 400 |
-
with gr.Row():
|
| 401 |
# Individual graphs for each defect category
|
| 402 |
nicks_graph_component = gr.Plot(label="Nicks Area Distribution")
|
| 403 |
dents_graph_component = gr.Plot(label="Dents Area Distribution")
|
| 404 |
scratches_graph_component = gr.Plot(label="Scratches Area Distribution")
|
| 405 |
pittings_graph_component = gr.Plot(label="Pittings Area Distribution")
|
| 406 |
|
|
|
|
|
|
|
|
|
|
| 407 |
# Button to generate graphs
|
| 408 |
-
with gr.Row():
|
| 409 |
graph_btn = gr.Button("Generate Area Distribution Graphs")
|
| 410 |
graph_btn.click(generate_graphs_btn, inputs=output_annotations, outputs=[
|
| 411 |
nicks_graph_component, dents_graph_component,
|
| 412 |
scratches_graph_component, pittings_graph_component
|
| 413 |
])
|
| 414 |
|
|
|
|
|
|
|
| 415 |
# Row for frequency graph
|
| 416 |
-
with gr.Row():
|
| 417 |
frequency_graph_component = gr.Plot(label="Defect Frequency Distribution") # Frequency Graph
|
| 418 |
|
|
|
|
|
|
|
|
|
|
| 419 |
# Row for frequency graph btn
|
| 420 |
-
with gr.Row():
|
| 421 |
freq_graph_btn = gr.Button("Generate Frequency Graph")
|
| 422 |
freq_graph_btn.click(detection.generate_frequency_graph,
|
| 423 |
inputs=output_annotations,
|
| 424 |
outputs=frequency_graph_component)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
# Launch the Gradio interface
|
|
|
|
| 427 |
demo.launch(share=True)
|
| 428 |
|
| 429 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
# import numpy as np
|
| 3 |
+
# import cv2
|
| 4 |
+
# from sahi.predict import get_sliced_prediction
|
| 5 |
+
# from sahi import AutoDetectionModel
|
| 6 |
+
# from PIL import Image
|
| 7 |
+
# import plotly.graph_objects as go
|
| 8 |
+
# import torch
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# class Detection:
|
| 15 |
+
|
| 16 |
+
# def __init__(self):
|
| 17 |
+
# # Set the model path and confidence threshold
|
| 18 |
+
# yolov8_model_path = "./model/best.pt" # Update to your model path
|
| 19 |
+
|
| 20 |
+
# # Initialize the AutoDetectionModel
|
| 21 |
+
# self.model = AutoDetectionModel.from_pretrained(
|
| 22 |
+
# model_type='yolov8',
|
| 23 |
+
# model_path=yolov8_model_path,
|
| 24 |
+
# confidence_threshold=0.3,
|
| 25 |
+
# device='cpu' # Change to 'cuda:0' if you are using a GPU
|
| 26 |
+
# )
|
| 27 |
+
|
| 28 |
+
# def detect_from_image(self, image):
|
| 29 |
+
# # Perform sliced prediction with SAHI
|
| 30 |
+
# results = get_sliced_prediction(
|
| 31 |
+
# image=image,
|
| 32 |
+
# detection_model=self.model,
|
| 33 |
+
# slice_height=256,
|
| 34 |
+
# slice_width=256,
|
| 35 |
+
# overlap_height_ratio=0.2,
|
| 36 |
+
# overlap_width_ratio=0.2,
|
| 37 |
+
# postprocess_type='NMS',
|
| 38 |
+
# postprocess_match_metric='IOU',
|
| 39 |
+
# postprocess_match_threshold=0.1,
|
| 40 |
+
# postprocess_class_agnostic=True,
|
| 41 |
+
# )
|
| 42 |
+
|
| 43 |
+
# # Retrieve COCO annotations
|
| 44 |
+
# coco_annotations = results.to_coco_annotations()
|
| 45 |
+
# return coco_annotations
|
| 46 |
+
|
| 47 |
+
# def draw_annotations(self, image, annotations):
|
| 48 |
+
# """Draw bounding boxes on the image based on COCO annotations using OpenCV."""
|
| 49 |
+
# # Define colors for each category in BGR (OpenCV uses BGR format)
|
| 50 |
+
# category_styles = {
|
| 51 |
+
# 'Nicks': {'color': (255, 60, 60), 'thickness': 2}, # Nicks (Red)
|
| 52 |
+
# 'Dents': {'color': (255, 148, 156), 'thickness': 2}, # Dents (Light Red)
|
| 53 |
+
# 'Scratches': {'color': (255, 116, 28), 'thickness': 2}, # Scratches (Orange)
|
| 54 |
+
# 'Pittings': {'color': (255, 180, 28), 'thickness': 2} # Pittings (Yellow)
|
| 55 |
+
# }
|
| 56 |
+
|
| 57 |
+
# for annotation in annotations:
|
| 58 |
+
# bbox = annotation['bbox'] # Extract the bounding box
|
| 59 |
+
# category_name = annotation['category_name']
|
| 60 |
+
# score = annotation.get('score', 0) # Extract confidence score, default to 0 if not present
|
| 61 |
+
|
| 62 |
+
# # Get color and thickness for the current category
|
| 63 |
+
# style = category_styles.get(category_name, {'color': (255, 0, 0), 'thickness': 2}) # Default to red if not found
|
| 64 |
+
|
| 65 |
+
# # Draw rectangle
|
| 66 |
+
# cv2.rectangle(image,
|
| 67 |
+
# (int(bbox[0]), int(bbox[1])),
|
| 68 |
+
# (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])),
|
| 69 |
+
# style['color'],
|
| 70 |
+
# style['thickness'])
|
| 71 |
+
|
| 72 |
+
# # Prepare text with category and confidence score
|
| 73 |
+
# text = f"{category_name}: {score:.2f}" # Format the score to two decimal places
|
| 74 |
+
|
| 75 |
+
# # Put category text with score
|
| 76 |
+
# cv2.putText(image,
|
| 77 |
+
# text,
|
| 78 |
+
# (int(bbox[0]), int(bbox[1] - 10)), # Position above the rectangle
|
| 79 |
+
# cv2.FONT_HERSHEY_SIMPLEX,
|
| 80 |
+
# 0.5,
|
| 81 |
+
# style['color'],
|
| 82 |
+
# 2)
|
| 83 |
+
|
| 84 |
+
# return image
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# def generate_individual_graphs(self, annotations):
|
| 88 |
+
# """Generate individual area distribution histograms for each defect category."""
|
| 89 |
+
# # Dictionary to hold areas for each category
|
| 90 |
+
# category_areas = {
|
| 91 |
+
# 'Nicks': [],
|
| 92 |
+
# 'Dents': [],
|
| 93 |
+
# 'Scratches': [],
|
| 94 |
+
# 'Pittings': []
|
| 95 |
+
# }
|
| 96 |
+
|
| 97 |
+
# # Populate the category_areas dictionary
|
| 98 |
+
# for annotation in annotations:
|
| 99 |
+
# category_name = annotation['category_name']
|
| 100 |
+
# area = annotation['bbox'][2] * annotation['bbox'][3] # Width * Height
|
| 101 |
+
# if category_name in category_areas:
|
| 102 |
+
# category_areas[category_name].append(area)
|
| 103 |
+
|
| 104 |
+
# # Create individual area distribution histograms for each ctegory
|
| 105 |
+
# individual_graphs = {}
|
| 106 |
+
# for category in ['Nicks', 'Dents', 'Scratches', 'Pittings']:
|
| 107 |
+
# areas = category_areas[category]
|
| 108 |
+
# fig = go.Figure()
|
| 109 |
+
# if areas: # Check if there are areas to plot
|
| 110 |
+
# # Create a histogram and store the frequencies
|
| 111 |
+
# histogram_data = go.Histogram(
|
| 112 |
+
# x=areas,
|
| 113 |
+
# name=category,
|
| 114 |
+
# marker_color=self.get_color(category), # Use associated color
|
| 115 |
+
# opacity=1,
|
| 116 |
+
# nbinsx=10 # Number of bins
|
| 117 |
+
# )
|
| 118 |
+
# fig.add_trace(histogram_data)
|
| 119 |
+
|
| 120 |
+
# # Get the frequencies and edges for swapping axes
|
| 121 |
+
# frequencies = histogram_data.y
|
| 122 |
+
# edges = histogram_data.x
|
| 123 |
+
|
| 124 |
+
# # Create a bar chart to swap the axes
|
| 125 |
+
# fig = go.Figure(data=[
|
| 126 |
+
# go.Bar(
|
| 127 |
+
# x=frequencies, # Frequencies on x-axis
|
| 128 |
+
# y=edges, # Edges on y-axis
|
| 129 |
+
# name=category,
|
| 130 |
+
# marker_color=self.get_color(category), # Use associated color
|
| 131 |
+
# opacity=1
|
| 132 |
+
# )
|
| 133 |
+
# ])
|
| 134 |
+
# else: # Generate an empty graph if no areas
|
| 135 |
+
# fig.add_trace(go.Bar(x=[], y=[], name=category)) # Empty graph
|
| 136 |
+
|
| 137 |
+
# # Update layout with swapped axes
|
| 138 |
+
# fig.update_layout(
|
| 139 |
+
# title=f'Area Distribution of {category}',
|
| 140 |
+
# xaxis_title='Frequency', # Frequency on x-axis
|
| 141 |
+
# yaxis_title='Area', # Area on y-axis
|
| 142 |
+
# showlegend=True
|
| 143 |
+
# )
|
| 144 |
+
# individual_graphs[category] = fig
|
| 145 |
+
|
| 146 |
+
# return individual_graphs['Nicks'], individual_graphs['Dents'], individual_graphs['Scratches'], individual_graphs['Pittings']
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
# def generate_frequency_graph(self, annotations):
|
| 153 |
+
# """Generate a frequency bar chart for defect categories."""
|
| 154 |
+
# category_counts = {
|
| 155 |
+
# 'Nicks': 0,
|
| 156 |
+
# 'Dents': 0,
|
| 157 |
+
# 'Scratches': 0,
|
| 158 |
+
# 'Pittings': 0
|
| 159 |
+
# }
|
| 160 |
+
|
| 161 |
+
# # Count occurrences of each defect category
|
| 162 |
+
# for annotation in annotations:
|
| 163 |
+
# category_name = annotation['category_name']
|
| 164 |
+
# if category_name in category_counts:
|
| 165 |
+
# category_counts[category_name] += 1
|
| 166 |
+
|
| 167 |
+
# # Create a bar chart for frequency
|
| 168 |
+
# freq_chart = go.Figure()
|
| 169 |
+
# category_colors = {
|
| 170 |
+
# 'Nicks': 'rgba(255, 60, 60, 0.7)', # Red
|
| 171 |
+
# 'Dents': 'rgba(255, 148, 156, 0.7)', # Light Red
|
| 172 |
+
# 'Scratches': 'rgba(255, 116, 28, 0.7)', # Orange
|
| 173 |
+
# 'Pittings': 'rgba(255, 180, 28, 0.7)' # Yellow
|
| 174 |
+
# }
|
| 175 |
+
|
| 176 |
+
# for category, count in category_counts.items():
|
| 177 |
+
# freq_chart.add_trace(go.Bar(
|
| 178 |
+
# x=[category],
|
| 179 |
+
# y=[count],
|
| 180 |
+
# name=category,
|
| 181 |
+
# marker_color=category_colors.get(category, 'blue') # Default to blue if not found
|
| 182 |
+
# ))
|
| 183 |
+
|
| 184 |
+
# freq_chart.update_layout(
|
| 185 |
+
# title='Frequency of Defects',
|
| 186 |
+
# xaxis_title='Defect Category',
|
| 187 |
+
# yaxis_title='Count',
|
| 188 |
+
# barmode='group'
|
| 189 |
+
# )
|
| 190 |
+
|
| 191 |
+
# return freq_chart
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
# def get_color(self, category_name):
|
| 195 |
+
# """Get the color associated with a category name."""
|
| 196 |
+
# category_styles = {
|
| 197 |
+
# 'Nicks': 'rgba(255, 60, 60, 0.7)', # Red
|
| 198 |
+
# 'Dents': 'rgba(255, 148, 156, 0.7)', # Light Red
|
| 199 |
+
# 'Scratches': 'rgba(255, 116, 28, 0.7)', # Orange
|
| 200 |
+
# 'Pittings': 'rgba(255, 180, 28, 0.7)' # Yellow
|
| 201 |
+
# }
|
| 202 |
+
# return category_styles.get(category_name, (255, 0, 0)) # Default to red if not found
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# detection = Detection()
|
| 207 |
+
|
| 208 |
+
# def upload_image(image):
|
| 209 |
+
# """Process the uploaded image (if needed) and display it."""
|
| 210 |
+
# return image
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
# def apply_detection(image):
|
| 214 |
+
# """Run object detection on the uploaded image and return the annotated image."""
|
| 215 |
+
# # Convert image from PIL to NumPy array
|
| 216 |
+
# img = np.array(image)
|
| 217 |
+
|
| 218 |
+
# # Perform detection and get COCO annotations
|
| 219 |
+
# annotations = detection.detect_from_image(img)
|
| 220 |
+
|
| 221 |
+
# # Draw the annotations on the image using OpenCV
|
| 222 |
+
# annotated_image = detection.draw_annotations(img, annotations)
|
| 223 |
+
|
| 224 |
+
# # Convert back to PIL format for Gradio output
|
| 225 |
+
# return Image.fromarray(annotated_image), annotations
|
| 226 |
+
|
| 227 |
+
# def generate_graphs_btn(annotations):
|
| 228 |
+
# """Generate interactive graphs from the annotations."""
|
| 229 |
+
# # Generate individual graphs for each defect category
|
| 230 |
+
# individual_graphs = detection.generate_individual_graphs(annotations)
|
| 231 |
+
# frequency_graph = detection.generate_frequency_graph(annotations)
|
| 232 |
+
# return individual_graphs
|
| 233 |
+
|
| 234 |
+
# css = """
|
| 235 |
+
|
| 236 |
+
# @import url('https://fonts.googleapis.com/css2?family=Ubuntu:wght@300;400;500;700&family=Montserrat:wght@700&family=Open+Sans&family=Poppins:wght@300;400;500;600;700;800&display=swap');
|
| 237 |
+
|
| 238 |
+
# *{
|
| 239 |
+
# margin: 0;
|
| 240 |
+
# padding: 0;
|
| 241 |
+
# box-sizing: border-box;
|
| 242 |
+
# font-family: 'Ubuntu',sans-serif;
|
| 243 |
+
# }
|
| 244 |
+
|
| 245 |
+
# a{
|
| 246 |
+
# text-decoration: none;
|
| 247 |
+
# color: #000;
|
| 248 |
+
# }
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
# body{
|
| 252 |
+
# background-color: #fff;
|
| 253 |
+
# }
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
# header{
|
| 258 |
+
# padding: 0 80px;
|
| 259 |
+
# height: calc(100vh-80px);
|
| 260 |
+
# display: flex;
|
| 261 |
+
# align-items: center;
|
| 262 |
+
# justify-content: space-between;
|
| 263 |
+
# }
|
| 264 |
+
|
| 265 |
+
# header .left h1 {
|
| 266 |
+
# font-size: 80px;
|
| 267 |
+
# display: flex;
|
| 268 |
+
# justify-content: center;
|
| 269 |
+
# margin-top: 17rem;
|
| 270 |
+
|
| 271 |
+
# }
|
| 272 |
+
|
| 273 |
+
# header .left span{
|
| 274 |
+
# font-size: 80px;
|
| 275 |
+
# color: #083484;
|
| 276 |
+
# display: flex;
|
| 277 |
+
# justify-content: center;
|
| 278 |
+
|
| 279 |
+
# }
|
| 280 |
+
# header .left .second-line{
|
| 281 |
+
# font-size: 80px;
|
| 282 |
+
# color: #083484;
|
| 283 |
+
# display: flex;
|
| 284 |
+
# justify-content: center;
|
| 285 |
+
# font-weight: 400;
|
| 286 |
+
|
| 287 |
+
# }
|
| 288 |
+
|
| 289 |
+
# header .left p{
|
| 290 |
+
# margin-top: 35px;
|
| 291 |
+
# font-stretch: ultra-condensed;
|
| 292 |
+
# color: #777;
|
| 293 |
+
# display: flex;
|
| 294 |
+
# justify-content: center;
|
| 295 |
+
# text-align: center;
|
| 296 |
+
# margin-bottom: 10px;
|
| 297 |
+
# }
|
| 298 |
+
|
| 299 |
+
# header .left a{
|
| 300 |
+
# display: flex;
|
| 301 |
+
# align-items: center;
|
| 302 |
+
# background: #083484;
|
| 303 |
+
# width: 150px;
|
| 304 |
+
# padding: 8px;
|
| 305 |
+
# border-radius: 60px;
|
| 306 |
+
# }
|
| 307 |
+
|
| 308 |
+
# header .left a i{
|
| 309 |
+
# background-color: #fff;
|
| 310 |
+
# font-size: 24px;
|
| 311 |
+
# border-radius: 50%;
|
| 312 |
+
# padding: 8px;
|
| 313 |
+
# }
|
| 314 |
+
|
| 315 |
+
# header .left a span{
|
| 316 |
+
# color: #fff;
|
| 317 |
+
# margin-left: 22px;
|
| 318 |
+
# }
|
| 319 |
+
|
| 320 |
+
# .container {
|
| 321 |
+
# padding:30px;
|
| 322 |
+
# text-align: center;
|
| 323 |
+
# overflow: auto;
|
| 324 |
+
# margin-top: 500px;
|
| 325 |
+
# }
|
| 326 |
+
|
| 327 |
+
# .sub-header {
|
| 328 |
+
# font-size: 4em;
|
| 329 |
+
# text-align: center;
|
| 330 |
+
# color: #083484;
|
| 331 |
+
# font-family: 'Montserrat',sans-serif;
|
| 332 |
+
# }
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
# """
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
# js_func = """
|
| 342 |
+
# function refresh() {
|
| 343 |
+
# const url = new URL(window.location);
|
| 344 |
+
|
| 345 |
+
# if (url.searchParams.get('__theme') !== 'light') {
|
| 346 |
+
# url.searchParams.set('__theme', 'light');
|
| 347 |
+
# window.location.href = url.href;
|
| 348 |
+
# }
|
| 349 |
+
# }
|
| 350 |
+
|
| 351 |
+
# """
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
# # Gradio interface components
|
| 356 |
+
# with gr.Blocks(css = css,js=js_func) as demo:
|
| 357 |
+
|
| 358 |
+
# gr.HTML("""
|
| 359 |
+
|
| 360 |
+
# <header>
|
| 361 |
+
# <div class="left">
|
| 362 |
+
# <h1><span>OIS</span><br></h1>
|
| 363 |
+
# <span class="second-line">AI Detection Model</span>
|
| 364 |
+
# <p>
|
| 365 |
+
# The OIS AI Detection Model enhances manufacturing by using the powerful YOLOv11 algorithm on
|
| 366 |
+
# a Raspberry Pi for real-time, on-device defect detection. It automates quality control,
|
| 367 |
+
# reduces human error, and minimizes downtime. With a user-friendly web interface,
|
| 368 |
+
# the model enables offline swift defect identification, seamless integration into
|
| 369 |
+
# production, and improving both efficiency and product quality.
|
| 370 |
+
# </p>
|
| 371 |
+
# </div>
|
| 372 |
+
|
| 373 |
+
# </header>
|
| 374 |
+
|
| 375 |
+
# <section class="container">
|
| 376 |
+
|
| 377 |
+
# <p class="sub-header">OFFLINE DETECTION</p>
|
| 378 |
+
|
| 379 |
+
# </section>
|
| 380 |
+
|
| 381 |
+
# """)
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
# with gr.Row():
|
| 385 |
+
# # Image Upload and Display in two columns
|
| 386 |
+
# with gr.Column():
|
| 387 |
+
# gr.Markdown("### Input")
|
| 388 |
+
# upload_image_component = gr.Image(type="pil", label="Select Image")
|
| 389 |
+
|
| 390 |
+
# with gr.Column():
|
| 391 |
+
# gr.Markdown("### Output")
|
| 392 |
+
# output_image_component = gr.Image(type="pil", label="Annotated Image")
|
| 393 |
+
# apply_detection_btn = gr.Button("Apply Detection")
|
| 394 |
+
# output_annotations = gr.State() # Store annotations
|
| 395 |
+
# apply_detection_btn.click(apply_detection, inputs=upload_image_component, outputs=[output_image_component, output_annotations])
|
| 396 |
+
|
| 397 |
+
|
| 398 |
+
# # Row for the graphs
|
| 399 |
+
# with gr.Row():
|
| 400 |
+
# # Individual graphs for each defect category
|
| 401 |
+
# nicks_graph_component = gr.Plot(label="Nicks Area Distribution")
|
| 402 |
+
# dents_graph_component = gr.Plot(label="Dents Area Distribution")
|
| 403 |
+
# scratches_graph_component = gr.Plot(label="Scratches Area Distribution")
|
| 404 |
+
# pittings_graph_component = gr.Plot(label="Pittings Area Distribution")
|
| 405 |
+
|
| 406 |
+
# # Button to generate graphs
|
| 407 |
+
# with gr.Row():
|
| 408 |
+
# graph_btn = gr.Button("Generate Area Distribution Graphs")
|
| 409 |
+
# graph_btn.click(generate_graphs_btn, inputs=output_annotations, outputs=[
|
| 410 |
+
# nicks_graph_component, dents_graph_component,
|
| 411 |
+
# scratches_graph_component, pittings_graph_component
|
| 412 |
+
# ])
|
| 413 |
+
|
| 414 |
+
# # Row for frequency graph
|
| 415 |
+
# with gr.Row():
|
| 416 |
+
# frequency_graph_component = gr.Plot(label="Defect Frequency Distribution") # Frequency Graph
|
| 417 |
+
|
| 418 |
+
# # Row for frequency graph btn
|
| 419 |
+
# with gr.Row():
|
| 420 |
+
# freq_graph_btn = gr.Button("Generate Frequency Graph")
|
| 421 |
+
# freq_graph_btn.click(detection.generate_frequency_graph,
|
| 422 |
+
# inputs=output_annotations,
|
| 423 |
+
# outputs=frequency_graph_component)
|
| 424 |
+
|
| 425 |
+
# # Launch the Gradio interface
|
| 426 |
+
# demo.launch(share=True)
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
|
| 462 |
+
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
|
| 480 |
+
|
| 481 |
+
|
| 482 |
+
|
| 483 |
+
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
|
| 491 |
+
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
|
| 496 |
+
|
| 497 |
+
|
| 498 |
+
|
| 499 |
+
|
| 500 |
+
|
| 501 |
import gradio as gr
|
| 502 |
import numpy as np
|
| 503 |
import cv2
|
|
|
|
| 506 |
from PIL import Image
|
| 507 |
import plotly.graph_objects as go
|
| 508 |
import torch
|
| 509 |
+
|
| 510 |
+
|
| 511 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 512 |
|
| 513 |
|
|
|
|
| 646 |
return individual_graphs['Nicks'], individual_graphs['Dents'], individual_graphs['Scratches'], individual_graphs['Pittings']
|
| 647 |
|
| 648 |
|
|
|
|
| 649 |
|
| 650 |
|
| 651 |
|
|
|
|
| 709 |
"""Process the uploaded image (if needed) and display it."""
|
| 710 |
return image
|
| 711 |
|
| 712 |
+
|
| 713 |
def apply_detection(image):
|
| 714 |
"""Run object detection on the uploaded image and return the annotated image."""
|
| 715 |
# Convert image from PIL to NumPy array
|
|
|
|
| 851 |
"""
|
| 852 |
|
| 853 |
|
| 854 |
+
# Function to handle login authentication
|
| 855 |
+
def login_auth(username, password):
|
| 856 |
+
if username != password:
|
| 857 |
+
raise gr.Error("Username or Password is wrong") # Raise an error on failed login
|
| 858 |
+
return True # Return True if authentication is successful
|
| 859 |
+
|
| 860 |
+
|
| 861 |
|
| 862 |
# Gradio interface components
|
| 863 |
with gr.Blocks(css = css,js=js_func) as demo:
|
| 864 |
|
| 865 |
+
# State variable to track login status
|
| 866 |
+
login_successful = gr.State(value=False)
|
| 867 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 868 |
|
| 869 |
+
|
| 870 |
+
with gr.Row(visible=False) as header_row:
|
| 871 |
+
gr.HTML("""
|
| 872 |
+
|
| 873 |
+
<header>
|
| 874 |
+
<div class="left">
|
| 875 |
+
<h1><span>OIS</span><br></h1>
|
| 876 |
+
<span class="second-line">AI Detection Model</span>
|
| 877 |
+
<p>
|
| 878 |
+
The OIS AI Detection Model enhances manufacturing by using the powerful YOLOv11 algorithm on
|
| 879 |
+
a Raspberry Pi for real-time, on-device defect detection. It automates quality control,
|
| 880 |
+
reduces human error, and minimizes downtime. With a user-friendly web interface,
|
| 881 |
+
the model enables offline swift defect identification, seamless integration into
|
| 882 |
+
production, and improving both efficiency and product quality.
|
| 883 |
+
</p>
|
| 884 |
+
</div>
|
| 885 |
+
|
| 886 |
+
</header>
|
| 887 |
|
| 888 |
+
<section class="container">
|
| 889 |
+
|
| 890 |
+
<p class="sub-header">OFFLINE DETECTION</p>
|
| 891 |
+
|
| 892 |
+
</section>
|
| 893 |
+
|
| 894 |
+
""")
|
| 895 |
|
| 896 |
+
|
| 897 |
+
|
| 898 |
+
|
| 899 |
+
|
| 900 |
+
|
| 901 |
+
with gr.Row(visible=False) as input_row:
|
| 902 |
# Image Upload and Display in two columns
|
| 903 |
with gr.Column():
|
| 904 |
gr.Markdown("### Input")
|
|
|
|
| 912 |
apply_detection_btn.click(apply_detection, inputs=upload_image_component, outputs=[output_image_component, output_annotations])
|
| 913 |
|
| 914 |
|
| 915 |
+
|
| 916 |
+
|
| 917 |
# Row for the graphs
|
| 918 |
+
with gr.Row(visible=False) as area_graph_row:
|
| 919 |
# Individual graphs for each defect category
|
| 920 |
nicks_graph_component = gr.Plot(label="Nicks Area Distribution")
|
| 921 |
dents_graph_component = gr.Plot(label="Dents Area Distribution")
|
| 922 |
scratches_graph_component = gr.Plot(label="Scratches Area Distribution")
|
| 923 |
pittings_graph_component = gr.Plot(label="Pittings Area Distribution")
|
| 924 |
|
| 925 |
+
|
| 926 |
+
|
| 927 |
+
|
| 928 |
# Button to generate graphs
|
| 929 |
+
with gr.Row(visible=False) as area_btn_row:
|
| 930 |
graph_btn = gr.Button("Generate Area Distribution Graphs")
|
| 931 |
graph_btn.click(generate_graphs_btn, inputs=output_annotations, outputs=[
|
| 932 |
nicks_graph_component, dents_graph_component,
|
| 933 |
scratches_graph_component, pittings_graph_component
|
| 934 |
])
|
| 935 |
|
| 936 |
+
|
| 937 |
+
|
| 938 |
# Row for frequency graph
|
| 939 |
+
with gr.Row(visible=False) as frequency_graph_row:
|
| 940 |
frequency_graph_component = gr.Plot(label="Defect Frequency Distribution") # Frequency Graph
|
| 941 |
|
| 942 |
+
|
| 943 |
+
|
| 944 |
+
|
| 945 |
# Row for frequency graph btn
|
| 946 |
+
with gr.Row(visible=False) as frequency_btn_row:
|
| 947 |
freq_graph_btn = gr.Button("Generate Frequency Graph")
|
| 948 |
freq_graph_btn.click(detection.generate_frequency_graph,
|
| 949 |
inputs=output_annotations,
|
| 950 |
outputs=frequency_graph_component)
|
| 951 |
+
|
| 952 |
+
|
| 953 |
+
|
| 954 |
+
# Login row, initially visible
|
| 955 |
+
with gr.Row(visible=True) as login_row:
|
| 956 |
+
with gr.Column():
|
| 957 |
+
gr.Markdown(value="<H2 style='text-align: center;'>NILI Login</h2>")
|
| 958 |
+
with gr.Row():
|
| 959 |
+
with gr.Column(scale=2):
|
| 960 |
+
gr.Markdown("")
|
| 961 |
+
with gr.Column(scale=1, variant='panel'):
|
| 962 |
+
username_tbox = gr.Textbox(label="User Name", interactive=True)
|
| 963 |
+
password_tbox = gr.Textbox(label="Password", interactive=True, type='password')
|
| 964 |
+
submit_btn = gr.Button(value='Submit', variant='primary', size='sm')
|
| 965 |
+
|
| 966 |
+
# On clicking the submit button
|
| 967 |
+
submit_btn.click(
|
| 968 |
+
login_auth,
|
| 969 |
+
inputs=[username_tbox, password_tbox],
|
| 970 |
+
outputs=login_successful # Set state variable on successful login
|
| 971 |
+
).then(
|
| 972 |
+
lambda login_state: (
|
| 973 |
+
gr.update(visible=login_state), # Show header_row
|
| 974 |
+
gr.update(visible=login_state), # Show input_row
|
| 975 |
+
gr.update(visible=login_state), # Show area_graph_row
|
| 976 |
+
gr.update(visible=login_state), # Show area_btn_row
|
| 977 |
+
gr.update(visible=login_state), # Show frequency_graph_row
|
| 978 |
+
gr.update(visible=login_state) # Show frequency_btn_row
|
| 979 |
+
),
|
| 980 |
+
inputs=login_successful,
|
| 981 |
+
outputs=[header_row, input_row, area_graph_row, area_btn_row, frequency_graph_row, frequency_btn_row]
|
| 982 |
+
)
|
| 983 |
+
|
| 984 |
+
with gr.Column(scale=2):
|
| 985 |
+
gr.Markdown("")
|
| 986 |
+
|
| 987 |
+
# Footer Row
|
| 988 |
+
with gr.Row():
|
| 989 |
+
with gr.Column(scale=4):
|
| 990 |
+
gr.HTML('<center><i>© 2024 OIS AI Defect Detection Model.</center>')
|
| 991 |
+
|
| 992 |
+
|
| 993 |
|
| 994 |
# Launch the Gradio interface
|
| 995 |
+
demo.queue()
|
| 996 |
demo.launch(share=True)
|
| 997 |
|
| 998 |
|
| 999 |
+
|
| 1000 |
+
|
| 1001 |
+
|
| 1002 |
+
|
| 1003 |
+
|
| 1004 |
+
|
| 1005 |
+
|
| 1006 |
+
|
| 1007 |
+
|
| 1008 |
+
|