Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,19 +16,23 @@ uploaded_image = st.file_uploader("Upload an image for OCR", type=["jpg", "jpeg"
|
|
16 |
|
17 |
if uploaded_image is not None:
|
18 |
# Open and display the uploaded image
|
19 |
-
image = Image.open(uploaded_image)
|
20 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
21 |
|
22 |
-
# Convert image to suitable format
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
if uploaded_image is not None:
|
18 |
# Open and display the uploaded image
|
19 |
+
image = Image.open(uploaded_image).convert("RGB") # Ensure image is in RGB format
|
20 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
21 |
|
22 |
+
# Convert image to a suitable format and ensure it's a batch (list of images)
|
23 |
+
try:
|
24 |
+
# Convert image to the right format for the processor
|
25 |
+
inputs = processor(images=[image], return_tensors="pt") # Put image in a list
|
26 |
+
|
27 |
+
# Perform OCR
|
28 |
+
with torch.no_grad():
|
29 |
+
outputs = model.generate(**inputs)
|
30 |
+
|
31 |
+
# Decode the generated text
|
32 |
+
text = processor.decode(outputs[0], skip_special_tokens=True)
|
33 |
+
|
34 |
+
# Display the OCR result
|
35 |
+
st.write("Extracted Text:")
|
36 |
+
st.text(text)
|
37 |
+
except Exception as e:
|
38 |
+
st.error(f"An error occurred: {str(e)}")
|