Does not create pdf report?

import streamlit as st
import cv2
import numpy as np
from tensorflow.keras.models import load_model
import base64
from fpdf import FPDF

Load the trained model

model = load_model(“model.hdf5”)

Dictionary mapping class numbers to class names

class_names = {
0: “Plant: Apple\nDisease: Apple scab”,
1: “Plant: Apple\nDisease: Black rot”,
2: “Plant: Apple\nDisease: Cedar apple rust”,
3: “Plant: Apple\nDisease: healthy”,
4: “Plant: Blueberry\nDisease: healthy”,
5: “Plant: Cherry (including sour)\nDisease: healthy”,
6: “Plant: Cherry (including sour)\nDisease: Powdery mildew”,
7: “Plant: Corn (maize)\nDisease: Cercospora leaf spot Gray leaf spot”,
8: “Plant: Corn (maize)\nDisease: Common rust”,
9: “Plant: Corn (maize)\nDisease: healthy”,
10: “Plant: Corn (maize)\nDisease: Northern Leaf Blight”,
11: “Plant: Grape\nDisease: Black rot”,
12: “Plant: Grape\nDisease: Esca (Black Measles)”,
13: “Plant: Grape\nDisease: healthy”,
14: “Plant: Grape\nDisease: Leaf blight (Isariopsis Leaf Spot)”,
15: “Plant: Orange\nDisease: Haunglongbing (Citrus greening)”,
16: “Plant: Peach\nDisease: Bacterial spot”,
17: “Plant: Peach\nDisease: healthy”,
18: “Plant: Pepper\nDisease: bell Bacterial spot”,
19: “Plant: Pepper\nDisease: bell healthy”,
20: “Plant: Potato\nDisease: Early blight”,
21: “Plant: Potato\nDisease: healthy”,
22: “Plant: Potato\nDisease: Late blight”,
23: “Plant: Raspberry\nDisease: healthy”,
24: “Plant: Soybean\nDisease: healthy”,
25: “Plant: Squash\nDisease: Powdery mildew”,
26: “Plant: Strawberry\nDisease: healthy”,
27: “Plant: Strawberry\nDisease: Leaf scorch”,
28: “Plant: Tomato\nDisease: Bacterial spot”,
29: “Plant: Tomato\nDisease: Early blight”,
30: “Plant: Tomato\nDisease: healthy”,
31: “Plant: Tomato\nDisease: Late blight”,
32: “Plant: Tomato\nDisease: Leaf Mold”,
33: “Plant: Tomato\nDisease: Septoria leaf spot”,
34: “Plant: Tomato\nDisease: Spider mites Two-spotted spider mite”,
35: “Plant: Tomato\nDisease: Target Spot”,
36: “Plant: Tomato\nDisease: Tomato mosaic virus”,
37: “Plant: Tomato\nDisease: Tomato Yellow Leaf Curl Virus”
}

Function for preprocessing an uploaded image

def preprocess_image(image):
img = cv2.resize(image, (224, 224))
img = img / 255.0 # Normalize pixel values
return img

Function to create a PDF report

def create_pdf(image, predicted_class_name):
pdf = FPDF()
pdf.add_page()
pdf.set_font(‘Arial’, ‘B’, 16)
pdf.cell(200, 10, txt=“Plant Disease Detection Report”, ln=True, align=“C”)
pdf.ln(10)
pdf.cell(200, 10, txt=“Uploaded Image”, ln=True, align=“L”)
pdf.ln(10)
# Convert the image to JPEG format and encode as base64
_, buffer = cv2.imencode(‘.jpg’, image)
image_data = base64.b64encode(buffer).decode()
pdf.image(‘data:image/jpeg;base64,’ + image_data, x=10, y=None, w=180)
pdf.ln(10)
pdf.cell(200, 10, txt=“Prediction”, ln=True, align=“L”)
pdf.ln(10)
pdf.multi_cell(0, 10, predicted_class_name)
return pdf

Streamlit app

st.title(“Plant Disease Detection”)

File uploader widget

uploaded_file = st.file_uploader(“Upload an image”, type=[“jpg”, “jpeg”, “png”])

if uploaded_file is not None:
# Read the uploaded image
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
uploaded_image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)

# Display the uploaded image
st.image(uploaded_image, caption='Uploaded Image', use_column_width=True)

# Add a button to trigger prediction
if st.button('Predict'):
    # Preprocess the image
    processed_image = preprocess_image(uploaded_image)
    
    # Make prediction
    prediction = model.predict(np.expand_dims(processed_image, axis=0))
    predicted_class = np.argmax(prediction)
    
    # Get the class name corresponding to the predicted class number
    predicted_class_name = class_names.get(predicted_class, "Unknown")
    
    # Display the prediction
    st.write(f"Predicted Class: {predicted_class_name}")
    
    # Add button to generate PDF report
    export_as_pdf = st.button("Export Report")
    if export_as_pdf:
        # Create PDF report
        pdf_report = create_pdf(uploaded_image, predicted_class_name)
        
        # Generate a download link for the PDF
        pdf_bytes = pdf_report.output(dest="S").encode("latin-1")
        b64 = base64.b64encode(pdf_bytes).decode()
        href = f'<a href="data:application/octet-stream;base64,{b64}" download="plant_disease_report.pdf">Download Report</a>'
        st.markdown(href, unsafe_allow_html=True)

Footer

st.text(“Credits: Engr. Kaleem Ahmad | Engr. Abdul Rafay”)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.