Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,37 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# Filter out small contours and merge nearby ones
|
| 39 |
-
filtered_boxes = []
|
| 40 |
-
for x, y, w, h in bounding_boxes:
|
| 41 |
-
if h >= min_height and w >= min_width: # Filter small boxes
|
| 42 |
-
filtered_boxes.append((x, y, w, h))
|
| 43 |
-
|
| 44 |
-
# Extract individual lines as images
|
| 45 |
-
line_images = []
|
| 46 |
-
for (x, y, w, h) in filtered_boxes:
|
| 47 |
-
line = image_np[y:y+h, x:x+w]
|
| 48 |
-
line_images.append(line)
|
| 49 |
-
|
| 50 |
-
return line_images
|
| 51 |
-
|
| 52 |
-
# Streamlit app
|
| 53 |
-
st.title("OCR API Service with Multiline Support")
|
| 54 |
-
|
| 55 |
-
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
| 56 |
-
|
| 57 |
-
if uploaded_file is not None:
|
| 58 |
-
try:
|
| 59 |
-
# Load and display the uploaded image
|
| 60 |
-
image = Image.open(uploaded_file).convert("RGB")
|
| 61 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 62 |
-
|
| 63 |
-
# Detect lines in the image
|
| 64 |
-
st.write("Detecting lines...")
|
| 65 |
-
line_images = detect_lines(image, min_height=30, min_width=100)
|
| 66 |
-
st.write(f"Detected {len(line_images)} lines in the image.")
|
| 67 |
-
|
| 68 |
-
# Perform OCR on each detected line
|
| 69 |
-
extracted_text = ""
|
| 70 |
-
for idx, line_img in enumerate(line_images):
|
| 71 |
-
# Convert the line image to PIL format
|
| 72 |
-
line_pil = Image.fromarray(line_img)
|
| 73 |
-
|
| 74 |
-
# Prepare the image for OCR
|
| 75 |
-
pixel_values = processor(images=line_pil, return_tensors="pt").pixel_values
|
| 76 |
-
|
| 77 |
-
# Generate text from the line image
|
| 78 |
-
generated_ids = model.generate(pixel_values)
|
| 79 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 80 |
-
|
| 81 |
-
# Append the extracted text
|
| 82 |
-
extracted_text += f"{generated_text}\n"
|
| 83 |
-
|
| 84 |
-
# Display extracted text
|
| 85 |
-
st.subheader("Extracted Text:")
|
| 86 |
-
st.text_area("Output Text", extracted_text, height=300)
|
| 87 |
-
|
| 88 |
-
# Simulate API-like JSON response
|
| 89 |
-
json_response = {"extracted_text": extracted_text}
|
| 90 |
-
st.write("API Response:")
|
| 91 |
-
st.json(json_response)
|
| 92 |
-
|
| 93 |
-
except Exception as e:
|
| 94 |
-
st.error(f"An error occurred: {e}")
|
| 95 |
-
else:
|
| 96 |
-
st.info("Please upload an image to start the OCR process.")
|
|
|
|
| 1 |
+
<?php
|
| 2 |
+
defined('BASEPATH') or exit('No direct script access allowed');
|
| 3 |
+
|
| 4 |
+
class OcrClient extends CI_Controller
|
| 5 |
+
{
|
| 6 |
+
public function process_image()
|
| 7 |
+
{
|
| 8 |
+
$url = 'http://<your-streamlit-app-url>/'; // Update with your Streamlit app URL
|
| 9 |
+
$imagePath = '/path/to/image.jpg'; // Path to the image file on the server
|
| 10 |
+
|
| 11 |
+
$curl = curl_init();
|
| 12 |
+
curl_setopt($curl, CURLOPT_URL, $url);
|
| 13 |
+
curl_setopt($curl, CURLOPT_POST, 1);
|
| 14 |
+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
| 15 |
+
|
| 16 |
+
// Attach the image file
|
| 17 |
+
$cfile = curl_file_create($imagePath, 'image/jpeg', 'image.jpg');
|
| 18 |
+
curl_setopt($curl, CURLOPT_POSTFIELDS, ['file' => $cfile]);
|
| 19 |
+
|
| 20 |
+
// Execute the cURL request
|
| 21 |
+
$response = curl_exec($curl);
|
| 22 |
+
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
| 23 |
+
curl_close($curl);
|
| 24 |
+
|
| 25 |
+
// Handle the response
|
| 26 |
+
if ($httpCode === 200) {
|
| 27 |
+
$result = json_decode($response, true);
|
| 28 |
+
if (isset($result['extracted_text'])) {
|
| 29 |
+
echo "Extracted Text: " . $result['extracted_text'];
|
| 30 |
+
} else {
|
| 31 |
+
echo "Error: Unexpected response from the API.";
|
| 32 |
+
}
|
| 33 |
+
} else {
|
| 34 |
+
echo "Error: Unable to connect to the OCR service.";
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|