import streamlit as st import base64 import openai import numpy as np import cv2 from tensorflow.keras.models import load_model from keras.preprocessing.image import img_to_array from keras.applications.inception_v3 import preprocess_input import requests import json_repair import json from PIL import Image import io from datetime import datetime import pandas as pd import re from json_repair import repair_json from dotenv import load_dotenv import os # Load environment variables load_dotenv() # Class labels for defect prediction class_labels = [ "algae", "bubbles", "Cracks", "Fungus", "peeling", ] # Set OpenAI API key openai.api_key = os.getenv("OPENAI_API_KEY") # Page configuration st.set_page_config( page_title="Painting Defect Detection System", page_icon="🔍", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS for better styling st.markdown(""" """, unsafe_allow_html=True) # Cache the model loading to improve performance @st.cache_resource def load_trained_model(): try: return load_model('painting.keras') except Exception as e: st.error(f"Error loading model: {str(e)}") return None def compress_image(image_bytes, max_size_kb=500): img = Image.open(io.BytesIO(image_bytes)) quality = 95 output_bytes = io.BytesIO() while True: output_bytes.seek(0) output_bytes.truncate() img.save(output_bytes, format='JPEG', quality=quality) if len(output_bytes.getvalue()) <= max_size_kb * 1024 or quality <= 5: break quality -= 5 return output_bytes.getvalue() def get_direct_drive_url(share_url): if "drive.google.com/file/d/" in share_url: file_id = share_url.split("/file/d/")[1].split("/")[0] return f"https://drive.google.com/uc?export=download&id={file_id}" return share_url def process_image(url): try: file_id = re.search(r'/d/(.*?)/', url) if file_id: # Google Drive share link url = f'https://drive.google.com/uc?export=download&id={file_id.group(1)}' resp = requests.get(url, timeout=15) resp.raise_for_status() if 'image' not in resp.headers.get('Content-Type', ''): raise ValueError('URL does not point to an image') img = cv2.imdecode(np.frombuffer(resp.content, np.uint8), cv2.IMREAD_COLOR) input_img_resized = cv2.resize(img, (256, 256)) x = img_to_array(input_img_resized) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x, resp.content except Exception as e: st.error(f"Error processing image from URL: {e}") return None, None def process_uploaded_image(uploaded_file): try: image_bytes = uploaded_file.read() img = Image.open(io.BytesIO(image_bytes)) img_array = np.array(img) # Convert RGB to BGR for OpenCV if len(img_array.shape) == 3: img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) input_img_resized = cv2.resize(img_array, (256, 256)) x = img_to_array(input_img_resized) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x, image_bytes except Exception as e: st.error(f"Error processing uploaded image: {e}") return None, None def predict_defect(processed_image, model): try: preds = model.predict(processed_image)[0] class_index = int(np.argmax(preds)) confidence = float(preds[class_index]) class_name = class_labels[class_index] return { "type": class_name, "severity": confidence }, class_index except Exception as e: return {"error": str(e)}, [] def compress_image_to_base64(image_bytes, max_size_kb=500): img = Image.open(io.BytesIO(image_bytes)).convert("RGB") output_bytes = io.BytesIO() quality = 95 while True: output_bytes.seek(0) output_bytes.truncate() img.save(output_bytes, format='JPEG', quality=quality) if len(output_bytes.getvalue()) <= max_size_kb * 1024 or quality <= 5: break quality -= 5 return base64.b64encode(output_bytes.getvalue()).decode('utf-8') def generate_openai_description(classification, image_bytes): try: if isinstance(classification, dict) and 'type' in classification: defect_type = classification['type'] elif isinstance(classification, dict) and 'error' in classification: defect_type = "Unknown defect" else: defect_type = str(classification) # Compress and encode image to base64 compressed_base64 = compress_image_to_base64(image_bytes) ai_prompt = ( f"You are an expert painting inspector analyzing surface defects." f"DETECTED DEFECTS: {defect_type}" f"TASK: Examine the painted surface image and provide a technical description of the visible defects." f"REQUIREMENTS:" f"- Write 200 characters describing what you observe" f"- Focus on visible paint failures, surface irregularities, or coating issues" f"- Use professional painting and coating terminology" f"- Be specific about location, extent, and characteristics of defects" f"- If multiple defects are present, describe each one" f"- Maintain an objective, technical tone" f"Dont use \\n" f"IMPORTANT: Always provide a description based on what you can see in the image. " f"Even if the detected defect types seem unclear, describe the actual visible conditions " f"in the painted surface. Only respond with 'Unable to generate description due to " f"image quality issues' if the image is genuinely too blurry, dark, or corrupted to analyze." f"Begin your description now:") # Call OpenAI with image and text response = openai.ChatCompletion.create( model="gpt-4o", messages=[ { "role": "user", "content": [ {"type": "text", "text": ai_prompt}, {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{compressed_base64}"}} ] } ] ) result_text = response.choices[0].message.content.strip() return result_text except Exception as e: return f"Error generating description: {str(e)}" def get_sps(defect_type, flooring_type,defect_category="flooring"): ai_content = """ You are an expert in identifying and resolving painting and wall surface defects. Your task is to accept a defect name and surface type as input and return applicable solutions using the structured dataset below. IMPORTANT: You MUST respond with valid JSON only. Do not include any explanatory text before or after the JSON. Instructions: Return TWO separate categories of solutions: MANDATORY SPS: Solutions specifically matching the input defect OPTIONAL SPS: Solutions from 'All Defects' records (universal solutions) For MANDATORY SPS: Match the input defect against the 'Defects' column using: Exact match Partial match (e.g. 'Cracks' matches all 'Cracks' records) EXCLUDE any 'All Defects' records from this section Only return records where the 'Category' matches the specified surface type (NonWaterproofing or Waterproofing) For OPTIONAL SPS: ONLY include records where 'Defects' is 'All Defects', 'ALL Defects', or 'All Defect' Filter by surface type: If surface_type is "NonWaterproofing": Return records with Category="NonWaterproofing" If surface_type is "Waterproofing": Return records with Category="Waterproofing" Return only those matching the specified surface type For each valid record in both categories: Use the id from the dataset. If not available, generate a unique numeric ID. Use the title from the dataset. If not available, use appropriate fallback title based on surface type Use the description from the dataset exactly (verbatim). If not found, generate fallback content based on best practices. CRITICAL FOR PRODUCT EXTRACTION: Carefully analyze the description content for ANY product names, brand names, or material references Extract ALL product names mentioned in the description, including: Brand names (e.g., "Asian Paints SmartCare Crack Seal") Generic product names (e.g., "Economy Primer", "Ace Suprema") Material names (e.g., "white cement", "fine sand") Tool names (e.g., "Putty Knife", "Paint Scraper") Chemical names (e.g., "oxygen bleach", "chlorinated bleach") List them in products array, assigning a unique ID per product NEVER leave products array empty - if no specific products are mentioned, include generic alternatives like "Primer", "Paint", "Cleaning Solution", etc. Include all records where Category matches the filtering condition IMPORTANT: Remove all HTML tags from the description content. Convert HTML lists to plain text format. Product Extraction Examples: "Use Asian Paints SmartCare Crack Seal" → Extract: "Asian Paints SmartCare Crack Seal" "Economy Primer, Ace Suprema" → Extract: "Economy Primer", "Ace Suprema" "Use boiling water, pressure washers, oxygen bleach" → Extract: "Pressure Washer", "Oxygen Bleach" "Sand with medium-grit sandpaper" → Extract: "Medium-grit Sandpaper" If no matching dataset entry is found for MANDATORY SPS, return one fallback solution following industry-standard practices for the specified surface type (without stating it is a fallback). Each solution must follow standard practices such as: Surface preparation Cleaning and washing Priming Crack filling Sanding Paint application Sealing Moisture treatment Finishing Each solution must follow standard practices such as: - Surface preparation - Cleaning and washing - Priming - Crack filling - Sanding - Paint application - Sealing - Moisture treatment - Finishing Return ONLY this JSON format (no other text): { "mandatory_sps": [ { "id": 181, "title": "Surface Preparation for Cracks - NonWaterproofing", "content": "New masonry surfaces must be allowed to cure completely. It is recommended to allow 28 days as the curing time for new masonry surfaces. Opening the cracks with the help of a grinder/cutter and cleaning the surface with pressure washers for further treatment. FILLING FOR CRACKS For filling cracks up to 3mm, use Asian Paints SmartCare Crack Seal. For filling cracks more than 3mm, use Asian Paints SmartCare Textured Crack Filler. FILLING FOR HOLES & DENTS In case of dents and holes, use TruCare Wall Putty Suprema or a mix of white cement and fine sand in the ratio 1:3.", "products": [ { "id": 1, "product_name": "Asian Paints SmartCare Crack Seal" }, { "id": 2, "product_name": "Asian Paints SmartCare Textured Crack Filler" }, { "id": 3, "product_name": "TruCare Wall Putty Suprema" } ] } ], "optional_sps": [ { "id": 211, "title": "4 Year Paint Warranty System - NonWaterproofing", "content": "Economy Primer, Ace Suprema", "products": [ { "id": 4, "product_name": "Economy Primer" }, { "id": 5, "product_name": "Ace Suprema" } ] } ] } Dataset: id: 181 Defects: Cracks Category: NonWaterproofing title: Surface Preparation for Cracks - NonWaterproofing description: New masonry surfaces must be allowed to cure completely. It is recommended to allow 28 days as the curing time for new masonry surfaces. Opening the cracks with the help of a grinder/cutter and cleaning the surface with pressure washers for further treatment. FILLING FOR CRACKS For filling cracks up to 3mm, use Asian Paints SmartCare Crack Seal. For filling cracks more than 3mm, use Asian Paints SmartCare Textured Crack Filler. FILLING FOR HOLES & DENTS In case of dents and holes, use TruCare Wall Putty Suprema or a mix of white cement and fine sand in the ratio 1:3. id: 182 Defects: Cracks Category: Waterproofing title: Surface Preparation for Cracks - Waterproofing description: New masonry surfaces must be allowed to cure completely. It is recommended to allow 28 days as the curing time for new masonry surfaces. Opening the cracks with the help of a grinder/cutter and cleaning the surface with pressure washers for further treatment. FILLING FOR CRACKS For filling cracks up to 3mm, use Asian Paints SmartCare Crack Seal. For filling cracks more than 3mm, use Asian Paints SmartCare Textured Crack Filler. FILLING FOR HOLES & DENTS In case of dents and holes, use TruCare Wall Putty Suprema or a mix of white cement and fine sand in the ratio 1:3. id: 183 Defects: Patchiness Category: NonWaterproofing title: Surface Preparation for Patchiness - NonWaterproofing description: Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatment id: 184 Defects: Patchiness Category: Waterproofing title: Surface Preparation for Patchiness - Waterproofing description: Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatment id: 185 Defects: Peeling Category: NonWaterproofing title: Surface Preparation for Peeling - NonWaterproofing description: Use Putty Knife or Paint Scraper for removing loose and peeling paint without damaging the underlying surface.Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface for further treatment Surface should be free from any loose paint, dust or grease.Growths of fungus, algae or moss should be removed by wire brushing and water. id: 186 Defects: Peeling Category: Waterproofing title: Surface Preparation for Peeling - Waterproofing description: Use Putty Knife or Paint Scraper for removing loose and peeling paint without damaging the underlying surface.Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface for further treatment Surface should be free from any loose paint, dust or grease.Growths of fungus, algae or moss should be removed by wire brushing and water. id: 187 Defects: Algae Category: NonWaterproofing title: Surface Preparation for Algae - NonWaterproofing description: Use boiling water, pressure washers, oxygen bleach, chlorinated bleach, and commercial algae removers to get rid of green algae on concrete. Pressure wash and scrape the wall with a soft brush id: 188 Defects: Algae Category: Waterproofing title: Surface Preparation for Algae - Waterproofing description: Use boiling water, pressure washers, oxygen bleach, chlorinated bleach, and commercial algae removers to get rid of green algae on concrete. Pressure wash and scrape the wall with a soft brush id: 189 Defects: Bubble Category: NonWaterproofing title: Surface Preparation for Bubble - NonWaterproofing description: If moisture is the issue, fix any leaks or dampness before proceeding. Moisture must be fully resolved to prevent new bubbling. Remove the buubled paint by using the scraper to peel off the loose and raised areas.Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatment id: 190 Defects: Bubble Category: Waterproofing title: Surface Preparation for Bubble - Waterproofing description: If moisture is the issue, fix any leaks or dampness before proceeding. Moisture must be fully resolved to prevent new bubbling. Remove the buubled paint by using the scraper to peel off the loose and raised areas.Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatment id: 191 Defects: Blisters Category: NonWaterproofing title: Surface Preparation for Blisters - NonWaterproofing description: Remove blisters by scraping, sanding or pressure-washing down to underlying coats of paint or primer.Repair loose caulking and improve ventilation of the building to prevent a recurring problem id: 192 Defects: Blisters Category: Waterproofing title: Surface Preparation for Blisters - Waterproofing description: Remove blisters by scraping, sanding or pressure-washing down to underlying coats of paint or primer.Repair loose caulking and improve ventilation of the building to prevent a recurring problem id: 193 Defects: Efflorescense Category: NonWaterproofing title: Surface Preparation for Efflorescense - NonWaterproofing description: Stiff brushes are used to sweep away efflorescence from smoother surfaces.Apply a forceful water rinse to efflorescence with a pressure washer to clean it up id: 194 Defects: Efflorescense Category: Waterproofing title: Surface Preparation for Efflorescense - Waterproofing description: Stiff brushes are used to sweep away efflorescence from smoother surfaces.Apply a forceful water rinse to efflorescence with a pressure washer to clean it up id: 195 Defects: Fungus Category: NonWaterproofing title: Surface Preparation for Fungus - NonWaterproofing description: Use boiling water, pressure washers, oxygen bleach, chlorinated bleach, and commercial fungus removers to get rid of fungus on concrete. Pressure wash and scrape the wall with a soft brush id: 196 Defects: Fungus Category: Waterproofing title: Surface Preparation for Fungus - Waterproofing description: Use boiling water, pressure washers, oxygen bleach, chlorinated bleach, and commercial fungus removers to get rid of fungus on concrete. Pressure wash and scrape the wall with a soft brush id: 197 Defects: Poor Hiding Category: NonWaterproofing title: Surface Preparation for Poor Hiding - NonWaterproofing description: Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatment id: 198 Defects: Poor Hiding Category: Waterproofing title: Surface Preparation for Poor Hiding - Waterproofing description: Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatment id: 199 Defects: Shade variation Category: NonWaterproofing title: Surface Preparation for Shade variation - NonWaterproofing description: Remove Loose Material: Carefully strip away loose or blistered paint to expose a firm surface.Spot Priming: Apply an appropriate primer to the affected area, ensuring adhesion. id: 200 Defects: Shade variation Category: Waterproofing title: Surface Preparation for Shade variation - Waterproofing description: Remove Loose Material: Carefully strip away loose or blistered paint to expose a firm surface.Spot Priming: Apply an appropriate primer to the affected area, ensuring adhesion. id: 211 Defects: All Defects Category: NonWaterproofing title: 4 Year Paint Warranty System - NonWaterproofing description: Economy Primer, Ace Suprema id: 212 Defects: All Defects Category: NonWaterproofing title: 7 year Paint Warranty System - NonWaterproofing description: Economy Primer, Apex Suprema id: 213 Defects: All Defects Category: NonWaterproofing title: 8 year Paint Warranty System - NonWaterproofing description: Exterior Primer, Ultima Strech Suprema id: 214 Defects: All Defects Category: NonWaterproofing title: 9 year Paint Warranty System - NonWaterproofing description: Exterior Primer, Ultima Suprema id: 215 Defects: All Defects Category: NonWaterproofing title: 12 year Paint Warranty System - NonWaterproofing description: Exterior Primer, Protek Topcoat id: 216 Defects: All Defects Category: NonWaterproofing title: 15 year Paint Warranty System - NonWaterproofing description: Exterior Primer, Duralife Topcoat id: 217 Defects: All Defects Category: Waterproofing title: 4 year Complete Paint Warranty System - Waterproofing description: Damp Sheath suprema, Ace Suprema id: 218 Defects: All Defects Category: Waterproofing title: 6 year Complete Paint Warranty System - Waterproofing description: Damp Proof Suprema, Apex Suprema id: 219 Defects: All Defects Category: Waterproofing title: 8 year Complete Paint Warranty System - Waterproofing description: Damp Proof Suprema, Ultima Strech Suprema id: 220 Defects: All Defects Category: Waterproofing title: 10 year Complete Paint Warranty System - Waterproofing description: Damp Prime Xtreme, Ultima Stretch Suprema id: 221 Defects: All Defects Category: Waterproofing title: 12 year Complete Paint Warranty System - Waterproofing description: Protek Basecoat, Protek Topcoat id: 222 Defects: All Defects Category: Waterproofing title: 15 year Complete Paint Warranty System - Waterproofing description: Duralife Basecoat, Duralife Topcoat """ def remove_html_tags(text): """Remove HTML tags and convert lists to plain text format""" if not text: return text text = re.sub(r'