Compare commits

..

5 Commits

Author SHA1 Message Date
cdf994fd54
clean up 2024-08-03 05:28:37 +05:00
4b196c578f
added web api and web ui 2024-08-03 05:27:59 +05:00
0f58bbc330
added more docker stuff 2024-08-03 05:22:44 +05:00
e1e69a85f6
added docker stuff 2024-08-03 05:22:17 +05:00
35c802bf7a
Revert "cleaner json... i think"
This reverts commit 69f687790590d5cfeef8b28658687dddf42307aa.
2024-08-03 02:28:51 +05:00
6 changed files with 149 additions and 6 deletions

21
.build/Dockerfile Normal file
View File

@ -0,0 +1,21 @@
FROM python:3.9.16-slim-bullseye
# Set build shell to bash, default has has some issues sometimes
SHELL ["/bin/bash", "-c"]
# Install packges
RUN apt update \
&& apt install --no-install-recommends -y imagemagick jq curl tesseract-ocr nano
WORKDIR /var/www/html
COPY . /var/www/html/
RUN chmod 777 .
# del usesless files
RUN rm -rf \
/var/lib/{apt,dpkg} \
/var/{cache,log,spool} \
/var/www/html/{.git,.build,README.md,env.example,docker-compose.yml,.gitignore}
RUN mkdir -p logs
CMD python3 -m http.server --cgi 8000 --directory public

View File

@ -0,0 +1,7 @@
services:
reciptocr:
build:
context: ..
dockerfile: .build/Dockerfile
hostname: reciptocr
image: reciptocr

7
compose.yml Normal file
View File

@ -0,0 +1,7 @@
services:
#########################
reciptocr:
hostname: reciptocr
image: reciptocr
ports:
- 8000:8000

26
ocr.sh
View File

@ -1,8 +1,9 @@
#!/bin/bash
INPUT_IMAGE=$1
MAGICK_ARGS="$INPUT_IMAGE -resize 150% -type Grayscale -threshold 95% "
raw_text=$(tesseract $INPUT_IMAGE stdout --psm 6)
raw_text=$(convert $MAGICK_ARGS - | tesseract stdin stdout| grep -v '^$')
referece=$(echo "$raw_text" | grep -o 'BLAZ[0-9]*')
to_number=$(echo "$raw_text" | grep -oE '\b9[0-9]{16}\b|\b7[0-9]{12}\b')
@ -15,10 +16,23 @@ amount=$(echo "$raw_text" | grep -oP '(?<=Amount\s[A-Z]{3}\s)[0-9]{1,3}(?:,[0-9]
status=$(echo "$raw_text" | grep -oP '(?<=Status\s)[A-Z]+')
remarks=$(echo "$raw_text" | grep Remarks | sed 's/Remarks //')
from=$(echo "$raw_text" | grep From | sed 's/From //')
to_name=$(echo "$raw_text"| grep -B1 $to_number | grep -v $to_number)
to_name=$(echo "$raw_text"| grep -B1 $to_number 2>/dev/null| grep -v $to_number 2>/dev/null)
json='{"Amount":"$amount","Currency":"$currency","Date":"$date $time","From":"$from","referece":"$referece","Remarks":"$remarks","Status":"$status","To":{"Account":"$to_number","Name":"$to_name"}}'
json=$(cat <<EOF
{
"Amount": "$amount",
"Currency": "$currency",
"Date": "$date $time",
"From": "$from",
"referece": "$referece",
"Remarks": "$remarks",
"Status": "$status",
"To": {
"Account": "$to_number",
"Name": "$to_name"
}
}
EOF
)
echo $json #| jq
#echo $raw_text
#tesseract $INPUT_IMAGE stdout --psm 6
echo $json

35
public/cgi-bin/api.cgi Executable file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env python3
import cgi
import os
import subprocess
UPLOAD_DIR = './uploads/'
def main():
print("Content-Type: application/json\n")
form = cgi.FieldStorage()
fileitem = form['file']
if fileitem.filename:
fn = os.path.basename(fileitem.filename)
file_path = os.path.join(UPLOAD_DIR, fn)
open(file_path, 'wb').write(fileitem.file.read())
result = run_ocr_script(file_path)
print(result)
os.remove(file_path)
else:
print("No file was uploaded.")
def run_ocr_script(file_path):
try:
completed_process = subprocess.run(['./ocr.sh', file_path], check=True, text=True, capture_output=True)
return completed_process.stdout
except subprocess.CalledProcessError as e:
return f"An error occurred: {e}"
except Exception as e:
return f"Unexpected error: {e}"
if __name__ == '__main__':
main()

59
public/index.html Normal file
View File

@ -0,0 +1,59 @@
<!DOCTYPE html>
<html>
<head>
<title>Recipt OCR API</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
color: #333;
margin: 40px;
display: flex;
justify-content: center;
align-items: center;
height: 90vh;
}
form {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h1 {
color: #5d5d5d;
text-align: center;
}
label {
margin-bottom: 10px;
display: block;
font-size: 16px;
}
input[type="file"] {
display: block;
margin-top: 5px;
margin-bottom: 20px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>Upload Recipt</h1>
<form action="/cgi-bin/api.cgi" method="post" enctype="multipart/form-data">
<label for="file">Select an image (PNG or JPEG):</label>
<input type="file" name="file" id="file" accept=".png, .jpeg, .jpg">
<input type="submit" value="Upload">
</form>
</body>
</html>