File size: 2,516 Bytes
f065c3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from flask import Flask, render_template, request
from model.got_model import perform_ocr  # Import the OCR function
import os
import re

app.config['UPLOAD_FOLDER'] = 'uploads/'

# Create the uploads folder if it doesn't exist
if not os.path.exists(app.config['UPLOAD_FOLDER']):
    os.makedirs(app.config['UPLOAD_FOLDER'])

# Global variable to store extracted text
extracted_text_global = ""

@app.route("/", methods=["GET", "POST"])
def upload_image():
    global extracted_text_global
    if request.method == "POST":
        file = request.files.get("file")  # Get the uploaded file
        
        if file:
            file_path = os.path.join(app.config["UPLOAD_FOLDER"], file.filename)
            file.save(file_path)  # Save the file to the uploads folder

            # Run the OCR model on the uploaded image
            extracted_text_global = perform_ocr(file_path)

            # Render the result template, passing the extracted text
            return render_template("result.html", extracted_text=extracted_text_global)
        else:
            # If no file was uploaded, display an error message
            return render_template("upload.html", error="Please upload a valid image file.")
    
    return render_template("upload.html")

@app.route("/search", methods=["POST"])
def search_text():
    global extracted_text_global
    keyword = request.form.get("keyword")
    
    if keyword:
        # Escape special characters in the keyword to avoid conflicts in HTML
        keyword = keyword.strip()

        # Check if the keyword is found in the extracted text
        if re.search(keyword, extracted_text_global, re.IGNORECASE):
            # Use re.sub to replace the keyword with <mark> for case-insensitive highlighting
            highlighted_text = re.sub(f"({re.escape(keyword)})", r"<mark>\1</mark>", extracted_text_global, flags=re.IGNORECASE)
            result_message = f"The keyword '{keyword}' was found and highlighted in the text."
        else:
            highlighted_text = extracted_text_global
            result_message = f"The keyword '{keyword}' was not found in the extracted text."
        
        # Render the result page with the highlighted text and search result message
        return render_template("result.html", extracted_text=highlighted_text, search_result=result_message)
    
    return render_template("result.html", extracted_text=extracted_text_global, search_result="Please enter a keyword.")
if __name__ == "__main__":
    app.run(debug=True)