Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public api #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import streamlit as st
import numpy as np
from PIL import Image
from merged_model import CompletedModel
import time

st.set_page_config(layout="wide")
st.markdown('<center><h1 style="color: blue;">SCAN ID CARD</h1></center>',
unsafe_allow_html=True)

uploaded_file = st.file_uploader("Upload Files", type=['png','jpeg', 'jpg'])
if uploaded_file is not None:
title_container = st.beta_container()
col1, col2, col3, col4 = st.beta_columns((0.6, 0.8, 0.8, 1))

with title_container:
with col1:
image = Image.open(uploaded_file)
img_np = np.array(image)

st.image(image, caption='ID-Card for detection', width=200)
# st.write("")
# st.write("Running...")
model = CompletedModel()
start = time.time()
img_crop, img_text, result = model.predict(img_np)
end = time.time()
total_time = end - start

with col2:
st.image(Image.fromarray(img_crop), caption='Cropped image', width=250)

with col3:
st.image(Image.fromarray(img_text), caption='Text recognition', width=250)

with col4:
st.write(result)
st.write('Process time: ' + str(round(total_time, 2)) + 'seconds')
st.markdown('<h3 style="color: green;">Scan ID-Card done !!!</h3>',
unsafe_allow_html=True)
43 changes: 24 additions & 19 deletions complete/merged_model.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from complete.detector import Detector
from complete.recognition import TextRecognition
from detector import Detector
from recognition import TextRecognition
from utils.image_utils import align_image, sort_text
import cv2
import time
import numpy as np
from copy import deepcopy as dc


class CompletedModel(object):
def __init__(self):
self.corner_detection_model = Detector(path_to_model='./config_corner_detection/Corner/model.tflite',
path_to_labels='./config_corner_detection/label_map.pbtxt',
self.corner_detection_model = Detector(path_to_model='[root]/extract-information-from-identity-card/complete/config_corner_detection/model.tflite',
path_to_labels='[root]/extract-information-from-identity-card/complete/config_corner_detection/label_map.pbtxt',
nms_threshold=0.2, score_threshold=0.3)
self.text_detection_model = Detector(path_to_model='./config_text_detection/Text/model.tflite',
path_to_labels='./config_text_detection/label_map.pbtxt',
self.text_detection_model = Detector(path_to_model='[root]/extract-information-from-identity-card/complete/config_text_detection/model.tflite',
path_to_labels='[root]/extract-information-from-identity-card/complete/config_text_detection/label_map.pbtxt',
nms_threshold=0.2, score_threshold=0.2)
self.text_recognition_model = TextRecognition(path_to_checkpoint='./config_text_recognition/transformerocr.pth')
self.text_recognition_model = TextRecognition(path_to_checkpoint='[root]/transformerocr.pth') #download link: https://drive.google.com/file/d/1B7b52G0hL6SKpsxP70xfyKhaRPaoWTuJ/view?usp=sharing

# init boxes
self.id_boxes = None
Expand All @@ -39,7 +40,7 @@ def detect_corner(self, image):

# align image
cropped_img = align_image(image, coordinate_dict)
cv2.imwrite('./test.png', cropped_img)
# cv2.imwrite('./test.png', cropped_img)

return cropped_img

Expand All @@ -50,6 +51,8 @@ def detect_text(self, image):
# sort text boxes according to coordinate
self.id_boxes, self.name_boxes, self.birth_boxes, self.home_boxes, self.add_boxes = sort_text(detection_boxes, detection_classes)

return self.text_detection_model.draw(image)

def text_recognition(self, image):
field_dict = dict()

Expand Down Expand Up @@ -82,17 +85,19 @@ def crop_and_recog(boxes):
field_dict['birth'] = result[len(self.name_boxes) + 1]
field_dict['home'] = ' '.join(result[len(self.name_boxes) + 2: -len(self.home_boxes)])
field_dict['add'] = ' '.join(result[-len(self.home_boxes):])
print(field_dict)
return field_dict

def predict(self, image):
cropped_image = self.detect_corner(image)
self.detect_text(cropped_image)
self.text_recognition(cropped_image)


img = cv2.imread('./test_image/test.jpeg')
model = CompletedModel()
start = time.time()
model.predict(img)
end = time.time()
print("Required time: ", end - start)
cropped_image_cp = dc(cropped_image)
text_image = self.detect_text(cropped_image)
return cropped_image_cp, text_image, self.text_recognition(cropped_image)


if "__name__" == "__main__":
model = CompletedModel()
"""
image = request.args['image_url']
"""
np_image = np.array(image)
_, _, text = model.predict(np_image)
12 changes: 10 additions & 2 deletions complete/requirement.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
vietocr==0.19.0
torch
pillow
tensorflow==2.2.0
matplotlib
torchvision
torchvision
tf_slim
vietocr==0.3.5
uvicorn
fastapi
streamlit
transformers
chatterbot
pyngrok
imageio
6 changes: 3 additions & 3 deletions complete/serve_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from io import BytesIO
import numpy as np
from imageio import imread
from PIL import Image
from merged_model import CompletedModel

Expand All @@ -20,13 +21,12 @@ def predict(image: Image.Image):
model = load_model()

img = np.asarray(image)
result = model.predict(img)
_, _, result = model.predict(img)
print(result)

return result


def read_image_file(file) -> Image.Image:
image = Image.open(BytesIO(file))

image = imread(file)
return image
35 changes: 29 additions & 6 deletions complete/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,50 @@
from fastapi import FastAPI, File, UploadFile
from starlette.responses import RedirectResponse
from serve_model import *
from pydantic import BaseModel

app_desc = """<h2>Try this app by uploading any image with `predict/image`</h2>"""

app = FastAPI(title="Tensorflow FastAPI Start Pack", description=app_desc)


class Item(BaseModel):
file: str


@app.get("/", include_in_schema=False)
async def index():
return RedirectResponse(url="/docs")


@app.post("/predict/image/url")
async def predict_api(file: Item):
try:
extension = file.file.split(".")[-1] in ("jpg", "jpeg", "png")
if not extension:
return "Image must be jpg or png format!"

image = read_image_file(file.file)
prediction = predict(image)

return prediction
except Exception as e:
print("Payload Exception" + str(e))


@app.post("/predict/image")
async def predict_api(file: UploadFile = File(...)):
extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png")
if not extension:
return "Image must be jpg or png format!"
try:
extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png")
if not extension:
return "Image must be jpg or png format!"

image = read_image_file(await file.read())
prediction = predict(image)
image = read_image_file(await file.read())
prediction = predict(image)

return prediction
return prediction
except Exception as e:
print("Payload File Exception" + str(e))


if __name__ == "__main__":
Expand Down