I am trying to use Streamlit as an API to return json data. However, when I make the API request, it returns HTML content instead of json data.
import streamlit as st
from io import BytesIO
from PIL import Image
import numpy as np
import easyocr
from ultralytics import YOLO
import base64
import json
# Load pretrained YOLOv8s models
model_path = 'best.pt'
model = YOLO(model_path)
model_path_cropped = 'best-cropped.pt'
model_cropped = YOLO(model_path_cropped)
def ocr_to_string(image):
image_PIL_form = image
image = np.array(image)
reader = easyocr.Reader(['en'])
result = reader.readtext(image)
result_list = [text_tuple[1] for text_tuple in result]
result_string = ' '.join(result_list)
list_of_bus_numbers = ['A1', 'A2', 'D1', 'D2', 'K', 'E', 'BTC', '96', '95', '151',
'L', '153', '154', '156', '170', '186', '48', '67', '183',
'188', '33', '10', '200', '201']
results_cropped = model_cropped(image_PIL_form, conf=0.40)
image_cropped = None
bus_number = 'Bus not found'
for result_cropped in results_cropped:
orig_img = result_cropped.orig_img
for i, bbox in enumerate(result_cropped.boxes.xyxy):
xmin, ymin, xmax, ymax = map(int, bbox)
image_cropped = Image.fromarray(orig_img).crop((xmin, ymin, xmax, ymax))
image_cropped = np.array(image_cropped)
reader_cropped = easyocr.Reader(['en'])
result_cropped = reader_cropped.readtext(image_cropped)
result_list_cropped = [text_tuple[1] for text_tuple in result_cropped]
result_string_cropped = ' '.join(result_list_cropped)
bus_number = find_substring(result_string_cropped, list_of_bus_numbers)
if bus_number != 'Bus not found':
return bus_number
return find_substring(result_string, list_of_bus_numbers)
def find_substring(main_string, substrings):
for substring in substrings:
if substring in main_string:
return substring
return 'Bus not found'
def process_image(image):
results = model(image, conf=0.70)
for result in results:
img = result.orig_img
for i, bbox in enumerate(result.boxes.xyxy):
xmin, ymin, xmax, ymax = map(int, bbox)
ymax -= (1 / 4) * (ymax - ymin)
cropped_img = Image.fromarray(img).crop((xmin, ymin, xmax, ymax))
return ocr_to_string(cropped_img)
return 'Bus not found'
# Streamlit UI
st.title('Bus Number Detection')
# File upload component
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
# API simulation via file upload or sidebar input
if uploaded_file:
image = Image.open(uploaded_file)
bus_result = process_image(image)
st.json({"result": bus_result})
# API simulation via sidebar
st.sidebar.header('API Input Simulation')
base64_data = st.sidebar.text_area('Base64 Image Data', '')
if base64_data:
try:
frame_data = base64.b64decode(base64_data)
image = Image.open(BytesIO(frame_data))
bus_result = process_image(image)
st.sidebar.json({"result": bus_result})
except Exception as e:
st.sidebar.json({"error": str(e)})
this is how i make the api request:
const base64Image = await getBase64(uri);
const endpointUrl = `https://betterbus-backend-rmgrv35qukyryhbtpu3tsh.streamlit.app/?api=true`;
const response = await axios.post(endpointUrl, { data: base64Image }, {
headers: {
'Content-Type': 'application/json',
},
});
Thank you.