Kabilash10 commited on
Commit
c86c8e2
·
verified ·
1 Parent(s): b090a54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
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
- inputs = processor(images=image, return_tensors="pt")
24
-
25
- # Perform OCR
26
- with torch.no_grad():
27
- outputs = model.generate(**inputs)
28
-
29
- # Decode the generated text
30
- text = processor.decode(outputs[0], skip_special_tokens=True)
31
-
32
- # Display the OCR result
33
- st.write("Extracted Text:")
34
- st.text(text)
 
 
 
 
 
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)}")