import gradio as gr import cv2 import numpy as np import plotly.graph_objects as go import pandas as pd from math import ceil # Solar-themed CSS CSS = """ .gradio-container { background: linear-gradient(to right, #f5f7fa, #e4e8f0); } .header { color: #ff7e36; text-align: center; } .panel { border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); } """ def analyze_roof(image, roof_length, roof_width, roof_angle, panel_type): # Image processing for roof detection if image is not None: gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 100, 200) contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(image, contours, -1, (0, 255, 0), 2) # Solar panel calculations panel_sizes = { "Standard (1.6m x 1m)": (1.6, 1.0), "Large (2m x 1.2m)": (2.0, 1.2), "Commercial (2.5m x 1.3m)": (2.5, 1.3) } panel_length, panel_width = panel_sizes[panel_type] # Calculate maximum panels that can fit panels_length = ceil(roof_length / panel_length) panels_width = ceil(roof_width / panel_width) total_panels = panels_length * panels_width # 3D Visualization fig = create_3d_model(roof_length, roof_width, roof_angle, panel_length, panel_width) # Generate report report = generate_report(total_panels, roof_length, roof_width, panel_type) return image, fig, report def create_3d_model(roof_l, roof_w, angle, panel_l, panel_w): # Create I-beam structure beam_thickness = 0.2 beams = [] # Main beams along length for i in np.linspace(-roof_w/2, roof_w/2, 5): beams.append(go.Scatter3d( x=[-roof_l/2, roof_l/2], y=[i, i], z=[0, 0], mode='lines', line=dict(width=beam_thickness*10, color='#555555'), name=f'Beam {i}' )) # Solar panels panels = [] for x in np.arange(-roof_l/2, roof_l/2, panel_l): for y in np.arange(-roof_w/2, roof_w/2, panel_w): panels.append(go.Mesh3d( x=[x, x+panel_l, x+panel_l, x], y=[y, y, y+panel_w, y+panel_w], z=[0, 0, 0, 0], color='#1e88e5', opacity=0.8 )) fig = go.Figure(data=beams + panels) fig.update_layout( scene=dict( xaxis_title='Length (m)', yaxis_title='Width (m)', zaxis_title='Height', aspectmode='manual', aspectratio=dict(x=1, y=roof_w/roof_l, z=0.2) ), margin=dict(l=0, r=0, b=0, t=0), height=500 ) return fig def generate_report(panels, length, width, panel_type): energy_per_panel = 300 # watts total_energy = panels * energy_per_panel / 1000 # kW report = f""" ## Solar Installation Report - **Roof Dimensions**: {length}m x {width}m - **Panel Type**: {panel_type} - **Total Panels**: {panels} - **Estimated Capacity**: {total_energy:.1f} kW - **Annual Production**: {total_energy * 1500:.0f} kWh - **I-Beams Required**: {ceil(length/2) * 2} - **Mounting Hardware**: {panels * 4} units """ return report with gr.Blocks(css=CSS) as demo: gr.Markdown(""" # ☀️ Solar Panel Design Assistant *Optimize your rooftop solar installation with AI* """, elem_classes="header") with gr.Row(): with gr.Column(): input_image = gr.Image(label="Upload Roof Image", type="numpy") roof_length = gr.Slider(1, 50, value=10, label="Roof Length (meters)") roof_width = gr.Slider(1, 30, value=5, label="Roof Width (meters)") roof_angle = gr.Slider(0, 60, value=30, label="Roof Angle (degrees)") panel_type = gr.Dropdown( choices=["Standard (1.6m x 1m)", "Large (2m x 1.2m)", "Commercial (2.5m x 1.3m)"], value="Standard (1.6m x 1m)", label="Solar Panel Type" ) submit_btn = gr.Button("Design Solar System", variant="primary") with gr.Column(): output_image = gr.Image(label="Roof Analysis", interactive=False) plot_output = gr.Plot(label="3D Solar Panel Layout") report_output = gr.Markdown() submit_btn.click( fn=analyze_roof, inputs=[input_image, roof_length, roof_width, roof_angle, panel_type], outputs=[output_image, plot_output, report_output] ) gr.Examples( examples=[["sample_roof.jpg", 12, 6, 25, "Standard (1.6m x 1m)"]], inputs=[input_image, roof_length, roof_width, roof_angle, panel_type] ) demo.launch()