Tyre tread analysis is a crucial task to identify wear and ensure safety, especially in vehicles that travel long distances. Using Artificial Intelligence (AI) and Python, we can automate this process quickly and accurately. Here, we show how a convolutional neural network (CNN) model, based on the VGG16 architecture, classifies tires into "new" or "used", while OpenCV helps analyze images to measure tread depth.
Technologies Used
Python:
Popular programming language for AI and Machine Learning, especially for its advanced libraries.
OpenCV:
Used to process images, detect contours and measure tire tread area.
TensorFlow and Keras:
Deep learning libraries. We use Keras to work with the VGG16 model, a pre-trained CNN for image recognition.
Matplotlib:
Library for data visualization and graph creation, making classification results more interpretable.
Code:
1. Load and Pre-process Images:
Tire images are uploaded and resized to a standard format (150x150 pixels) required for model input. This resizing maintains the aspect ratio and normalizes pixel values between 0 and 1 to make it easier for the model to process.
import cv2 import numpy as np from tensorflow.keras.applications.vgg16 import preprocess_input def process_image(image_path, target_size=(150, 150)): image = cv2.imread(image_path) if image is None: print(f"Erro ao carregar a imagem: {image_path}. Verifique o caminho e a integridade do arquivo.") return None, None image_resized = cv2.resize(image, target_size, interpolation=cv2.INTER_AREA) image_array = np.array(image_resized) / 255.0 image_array = np.expand_dims(image_array, axis=0) image_preprocessed = preprocess_input(image_array) return image_resized, image_preprocessed
2. Classification with the Trained Model:
We loaded a pre-trained convolutional neural network model, which was fine-tuned to classify tires as “new” or “used”. This model provides a confidence score that indicates the probability that a tire is new.
from tensorflow.keras.models import load_model model = load_model('pneu_classificador.keras') prediction = model.predict(image_preprocessed)
3. Contour Analysis for Groove Depth:
Groove depth detection is performed using computer vision techniques. The grayscale image goes through a blur filter and Canny edge detection, which helps identify groove contours. We then calculate the total area of the contours, which allows us to estimate wear.
def detect_tread_depth(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) edges = cv2.Canny(blurred, 30, 100) contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) total_area = sum(cv2.contourArea(c) for c in contours if cv2.contourArea(c) > 100) return total_area
4. Visualization and Analysis of Results:
After classifying and analyzing each tire, the results are displayed with Matplotlib. We compared the classification confidence score and the groove area detected in each image.
import matplotlib.pyplot as plt confidence_scores = [] total_area_green_values = [] predicted_classes = [] for image_file in os.listdir(ver_dir): image_path = os.path.join(ver_dir, image_file) image_resized, image_preprocessed = process_image(image_path) if image_preprocessed is not None: prediction = model.predict(image_preprocessed) confidence_score = prediction[0][0] total_area_green = detect_tread_depth(image_resized) predicted_class = "novo" if total_area_green > 500 else "usado" confidence_scores.append(confidence_score) total_area_green_values.append(total_area_green) predicted_classes.append(predicted_class) plt.imshow(cv2.cvtColor(image_resized, cv2.COLOR_BGR2RGB)) plt.title(f"Pneu {predicted_class} (Área: {total_area_green:.2f}, Confiança: {confidence_score:.2f})") plt.axis('off') plt.show() fig, axs = plt.subplots(2, 1, figsize=(10, 10)) axs[0].bar(os.listdir(ver_dir), confidence_scores, color='skyblue') axs[0].set_title('Confiança na Classificação') axs[0].set_ylim(0, 1) axs[0].tick_params(axis='x', rotation=45) axs[1].bar(os.listdir(ver_dir), total_area_green_values, color='lightgreen') axs[1].set_title('Área Verde Detectada') axs[1].tick_params(axis='x', rotation=45) plt.tight_layout() plt.show()
This project of mine demonstrates how it is possible to automate tire wear analysis using AI and computer vision, resulting in accurate and fast classification. The VGG16 architecture and use of OpenCV are key to combining neural network model accuracy with visual sulci analysis. This system can be expanded for continuous monitoring across vehicle fleets, helping to reduce accidents and optimize tire management.
The above is the detailed content of Tire Groove Analysis with Artificial Intelligence in Python!. For more information, please follow other related articles on the PHP Chinese website!