Hello!
Just having some issues with the prediction response from the Roboflow API. Not getting the predictions to come through which I want to deploy on Streamlit.
I attached my a screenshot and my code. Double checked my API key, project name, version but there must be something I am missing.
Thank you so much in advance.
Screenshot:
Code:
import streamlit as st
from roboflow import Roboflow
from PIL import Image
from io import BytesIO
Function to get predictions from Roboflow model
def get_prediction(image_bytes):
try:
# eventually use streamlit secrets for api
api_key = âXXXXXXâ
project_name = âmaize-catergoriesâ
version = â1â # Replace with version number
# Initialize Roboflow
rf = Roboflow(api_key=api_key)
project = rf.workspace().project(project_name)
model = project.version(version).model
# Save the uploaded image to a temporary file for prediction
image = Image.open(BytesIO(image_bytes))
image.save("temp_image.jpg")
# Perform prediction and handle possible exceptions
prediction = model.predict("temp_image.jpg").json()
return prediction
except Exception as e:
return {"error": str(e)}
Streamlit app interface
st.title(âImage Classification with Roboflowâ)
uploaded_file = st.file_uploader(âChoose an imageâŠâ, type=[âjpgâ, âjpegâ, âpngâ])
if uploaded_file is not None:
try:
# Display the uploaded image
st.image(uploaded_file, caption=âUploaded Imageâ, use_column_width=True)
# Get predictions
with st.spinner('Analyzing the image...'):
prediction = get_prediction(uploaded_file.getvalue())
# Check for errors in the prediction
if "error" in prediction:
st.error(f"Error in prediction: {prediction['error']}")
else:
st.success('Analysis complete.')
# Display predictions
st.write("Prediction Time:", prediction.get("time", "N/A"))
st.write("Predictions:")
for pred in prediction.get("predictions", []):
st.write(f"Class: {pred.get('class', 'N/A')}, Confidence: {pred.get('confidence', 'N/A'):.2f}")
st.write("Top Prediction:", prediction.get("top", "N/A"), "with confidence", prediction.get("confidence", "N/A"))
except Exception as e:
st.error(f"Error: {str(e)}")