How to fix TypeError: expected str, bytes or os.PathLike object, not UploadedFile

2.3k views Asked by At

I'm trying to make OCR-platform using streamlit and easyocr. I already managed to do text conversions from images, but I can’t convert PDF to JPG in order to continue further processing.

I tried downloading the pdf, then converting it to jpg, then converting the image to numpy.ndarray, then extracting the text from there. However, for some reason streamlit won't let me upload the PDF, I get this TypeError: expected str, bytes or os.PathLike object, not UploadedFile. It occurs even at the moment convert_from_path(uploaded_file). Does someone know how to fix this error? I will be very grateful for your answers!

uploaded_file = st.file_uploader("Download PDF:", type=['pdf'])
if uploaded_file is not None:
    st.write('File downloaded')
    pdf2img = convert_from_path(uploaded_file)
    pdf2img.save('out.jpg', 'JPEG')
    imgcv=cv2.imread('out.jpg')
    imgcv=cv2.cvtColor(imgcv, cv2.COLOR_BGR2RGB)
    file_bytes = np.asarray(bytearray(imgcv.read()), dtype=np.uint8)
    bytearray_img1 = cv2.imdecode(file_bytes, 1)

1

There are 1 answers

0
ben On

This is probably because you are passing an instance of uploaded_file from the UploadedFile class from Streamlit.

To fix, you will need to save the file to a temporary location and then provide the path to the convert_from_path() function.