badru commited on
Commit
a4ad892
·
verified ·
1 Parent(s): 8c349b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -37
app.py CHANGED
@@ -1,37 +1,80 @@
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
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ from PIL import Image
4
+ import cv2
5
+ import numpy as np
6
+
7
+ # Load the model and processor
8
+ @st.cache_resource
9
+ def load_model():
10
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
11
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
12
+ return processor, model
13
+
14
+ processor, model = load_model()
15
+
16
+ # Helper function to preprocess the image and detect lines
17
+ def detect_lines(image, min_height=20, min_width=100):
18
+ # Convert the PIL image to a NumPy array
19
+ image_np = np.array(image)
20
+
21
+ # Convert to grayscale
22
+ gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
23
+
24
+ # Apply binary thresholding
25
+ _, binary = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
26
+
27
+ # Dilate to merge nearby text
28
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
29
+ dilated = cv2.dilate(binary, kernel, iterations=1)
30
+
31
+ # Find contours
32
+ contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
33
+
34
+ # Sort contours top-to-bottom
35
+ bounding_boxes = [cv2.boundingRect(c) for c in contours]
36
+ bounding_boxes = sorted(bounding_boxes, key=lambda b: b[1]) # Sort by y-coordinate
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
+ # Handle image upload
56
+ uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
57
+
58
+ if uploaded_file is not None:
59
+ try:
60
+ # Load and process the uploaded image
61
+ image = Image.open(uploaded_file).convert("RGB")
62
+ line_images = detect_lines(image, min_height=30, min_width=100)
63
+
64
+ # Perform OCR on each detected line
65
+ extracted_text = ""
66
+ for line_img in line_images:
67
+ line_pil = Image.fromarray(line_img)
68
+ pixel_values = processor(images=line_pil, return_tensors="pt").pixel_values
69
+ generated_ids = model.generate(pixel_values)
70
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
71
+ extracted_text += f"{generated_text}\n"
72
+
73
+ # Simulate API-like JSON response
74
+ json_response = {"extracted_text": extracted_text}
75
+
76
+ # Return JSON response
77
+ st.write(json_response) # This is the response to your CodeIgniter client
78
+ except Exception as e:
79
+ # Return an error response
80
+ st.write({"error": str(e)})