Add NotoSansKR-Medium.otf font file using Git LFS
Browse files- app.py +204 -0
- eu1.jpg +0 -0
- kr1.jpg +0 -0
- requirements.txt +3 -0
- wait-waiting.gif +0 -0
app.py
CHANGED
@@ -1,4 +1,208 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
|
4 |
if __name__ == "__main__":
|
|
|
1 |
import streamlit as st
|
2 |
+
import os
|
3 |
+
from PIL import Image, ImageOps
|
4 |
+
from PIL import ImageDraw, ImageFont
|
5 |
+
import random
|
6 |
+
from io import BytesIO
|
7 |
+
from requests.auth import HTTPBasicAuth
|
8 |
+
import boto3
|
9 |
+
import json
|
10 |
+
from datetime import datetime
|
11 |
+
import requests
|
12 |
+
|
13 |
+
remote_url = os.environ.get("remote_url")
|
14 |
+
base_url = os.environ.get("base_url")
|
15 |
+
authEmail = os.environ.get("authEmail")
|
16 |
+
authPass = os.environ.get("authPass")
|
17 |
+
aws_access_key_id = os.environ.get("AWS_ACCESS_KEY_ID")
|
18 |
+
aws_secret_access_key = os.environ.get("AWS_SECRET_ACCESS_KEY")
|
19 |
+
aws_default_region = os.environ.get("AWS_DEFAULT_REGION")
|
20 |
+
bucket_name = os.environ.get("bucket_name")
|
21 |
+
user_id = os.environ.get("user_id")
|
22 |
+
|
23 |
+
# Read values from the JSON file
|
24 |
+
# with open('../config.json') as f:
|
25 |
+
# config = json.load(f)
|
26 |
+
# remote_url = config.get("remote_url")
|
27 |
+
# base_url = config.get("base_url")
|
28 |
+
# authEmail = config.get("authEmail")
|
29 |
+
# authPass = config.get("authPass")
|
30 |
+
# aws_access_key_id = config.get("AWS_ACCESS_KEY_ID")
|
31 |
+
# aws_secret_access_key = config.get("AWS_SECRET_ACCESS_KEY")
|
32 |
+
# aws_default_region = config.get("AWS_DEFAULT_REGION")
|
33 |
+
# bucket_name = config.get("bucket_name")
|
34 |
+
# user_id = config.get("user_id")
|
35 |
+
|
36 |
+
|
37 |
+
# Create a Boto3 session
|
38 |
+
session = boto3.Session(
|
39 |
+
aws_access_key_id=aws_access_key_id,
|
40 |
+
aws_secret_access_key=aws_secret_access_key,
|
41 |
+
region_name=aws_default_region
|
42 |
+
)
|
43 |
+
|
44 |
+
# Create an S3 client
|
45 |
+
s3 = session.client('s3')
|
46 |
+
|
47 |
+
def calculate_font_size(box, max_font_size=30, min_font_size=10, font_path="NotoSansKR-Medium.otf"):
|
48 |
+
width = box[2] - box[0]
|
49 |
+
font_size = int(max_font_size * width / 100)
|
50 |
+
|
51 |
+
# Limit font size to max and min values
|
52 |
+
font_size = min(max_font_size, font_size)
|
53 |
+
font_size = max(min_font_size, font_size)
|
54 |
+
|
55 |
+
font = ImageFont.truetype(font_path, font_size)
|
56 |
+
return font, font_size
|
57 |
+
|
58 |
+
def estimate_text_size(text, font_size):
|
59 |
+
# Estimate text width based on number of characters and average character width
|
60 |
+
avg_character_width = font_size * 0.5 # Adjust this value based on your font and preferences
|
61 |
+
text_width = len(text) * avg_character_width
|
62 |
+
|
63 |
+
# Estimate text height based on font size
|
64 |
+
text_height = font_size
|
65 |
+
|
66 |
+
return text_width, text_height
|
67 |
+
|
68 |
+
def draw_annotated_box(draw, box, text, font_path="NotoSansKR-Medium.otf"):
|
69 |
+
x1, y1, x2, y2 = box
|
70 |
+
draw.rectangle([x1, y1, x2, y2], outline="green", width=3)
|
71 |
+
|
72 |
+
# Calculate font size and get font
|
73 |
+
font, font_size = calculate_font_size(box)
|
74 |
+
|
75 |
+
# Measure text size
|
76 |
+
text_width, text_height = estimate_text_size(text, font_size)
|
77 |
+
|
78 |
+
# Reduce height of the background box by a factor (for example, 0.8 for 80% of the original height)
|
79 |
+
reduced_text_height = int(text_height * 0.8)
|
80 |
+
|
81 |
+
# Draw white background for text
|
82 |
+
background_box = [x1, y1 - reduced_text_height - 2, x1 + text_width + 2, y1]
|
83 |
+
draw.rectangle(background_box, fill="green")
|
84 |
+
|
85 |
+
# Draw text
|
86 |
+
draw.text((x1, y1 - text_height), text, font=font, fill="white")
|
87 |
+
|
88 |
+
|
89 |
+
def upload_to_s3(image_data, region):
|
90 |
+
# Pre-configured bucket name and path
|
91 |
+
region = region.upper()
|
92 |
+
path_prefix = f"image_{region}"
|
93 |
+
|
94 |
+
# S3 client setup
|
95 |
+
s3 = boto3.client('s3')
|
96 |
+
|
97 |
+
# Generating file name based on current date and time
|
98 |
+
now = datetime.now()
|
99 |
+
date_str = now.strftime("%Y%m%d")
|
100 |
+
time_str = now.strftime("%H%M%S")
|
101 |
+
file_name = f"{path_prefix}/{user_id}_{date_str}_{time_str}.jpeg"
|
102 |
+
|
103 |
+
try:
|
104 |
+
# Attempt to upload the image to S3
|
105 |
+
s3.put_object(
|
106 |
+
Body=image_data,
|
107 |
+
Bucket=bucket_name,
|
108 |
+
Key=file_name,
|
109 |
+
ContentType='image/jpeg'
|
110 |
+
)
|
111 |
+
return True
|
112 |
+
except Exception as e:
|
113 |
+
# Log and return False if an error occurs
|
114 |
+
print(f"Error uploading to S3: {e}")
|
115 |
+
return False
|
116 |
+
|
117 |
+
|
118 |
+
def main():
|
119 |
+
st.title("π ANPR - Ver.1.0.leon")
|
120 |
+
country = st.radio("π¦ Type of License", options=['Korean', 'EU'])
|
121 |
+
st.write("π§ͺ Options")
|
122 |
+
use_default_image = st.checkbox("Use default server image?", value=False) # New checkbox
|
123 |
+
img_file_buffer = st.file_uploader("π¦ Upload your image", type=['jpg', 'jpeg', 'png']) if not use_default_image else None
|
124 |
+
submit = st.button("Submit")
|
125 |
+
show_image = True
|
126 |
+
|
127 |
+
if show_image:
|
128 |
+
font = ImageFont.truetype("NotoSansKR-Medium.otf", 30)
|
129 |
+
|
130 |
+
if submit:
|
131 |
+
if use_default_image or img_file_buffer is not None:
|
132 |
+
gif_runner = st.image('wait-waiting.gif')
|
133 |
+
|
134 |
+
if use_default_image:
|
135 |
+
# Load the default image from your server here
|
136 |
+
# Make sure to replace 'your_default_image_path' with the actual path
|
137 |
+
if country == 'Korean':
|
138 |
+
image_choice = random.choice(['kr1.jpg'])
|
139 |
+
image_choice = os.path.join('./', image_choice)
|
140 |
+
image = Image.open(image_choice)
|
141 |
+
elif country == 'EU':
|
142 |
+
image_choice = random.choice(['eu1.jpg'])
|
143 |
+
image_choice = os.path.join('./', image_choice)
|
144 |
+
image = Image.open(image_choice)
|
145 |
+
else:
|
146 |
+
image = Image.open(img_file_buffer)
|
147 |
+
image = ImageOps.exif_transpose(image)
|
148 |
+
|
149 |
+
img_byte_arr = BytesIO()
|
150 |
+
if image.mode == 'RGBA':
|
151 |
+
rgb_image = image.convert('RGB')
|
152 |
+
rgb_image.save(img_byte_arr, format='JPEG')
|
153 |
+
else:
|
154 |
+
image.save(img_byte_arr, format='JPEG')
|
155 |
+
img_byte_arr.seek(0)
|
156 |
+
|
157 |
+
|
158 |
+
region = "kr"
|
159 |
+
if country == 'Korean':
|
160 |
+
region = "kr"
|
161 |
+
elif country == 'EU':
|
162 |
+
region = "eu"
|
163 |
+
|
164 |
+
auth_values = HTTPBasicAuth(authEmail, authPass)
|
165 |
+
|
166 |
+
# Make POST request
|
167 |
+
url = remote_url
|
168 |
+
form_data = {'base_url': base_url, 'region':region}
|
169 |
+
files = {'file': ('image.jpg', img_byte_arr, 'image/jpeg')}
|
170 |
+
res = requests.post(url, headers={'Accept': 'application/json'},
|
171 |
+
files=files,
|
172 |
+
data=form_data, # Additional form data
|
173 |
+
auth=auth_values)
|
174 |
+
|
175 |
+
img_byte_arr.seek(0)
|
176 |
+
if use_default_image == False:
|
177 |
+
if upload_to_s3(img_byte_arr.getvalue(), region):
|
178 |
+
print("Upload successful!")
|
179 |
+
|
180 |
+
# Close the file
|
181 |
+
img_byte_arr.close()
|
182 |
+
|
183 |
+
if res.status_code == 200:
|
184 |
+
output_res = res.json()
|
185 |
+
if show_image:
|
186 |
+
draw = ImageDraw.Draw(image)
|
187 |
+
|
188 |
+
for item in output_res['results']:
|
189 |
+
x1, y1, x2, y2 = item['ltrb']
|
190 |
+
ocr_text = item['ocr']
|
191 |
+
# print(x1,y1,x2,y2,ocr_text)
|
192 |
+
if show_image:
|
193 |
+
draw_annotated_box(draw, (x1, y1, x2, y2), ocr_text)
|
194 |
+
|
195 |
+
if show_image:
|
196 |
+
st.image(image, caption='Annotated Image', use_column_width=True)
|
197 |
+
|
198 |
+
st.header('π¦ Result Json')
|
199 |
+
st.code(json.dumps(output_res, indent=4, ensure_ascii=False), language="json")
|
200 |
+
|
201 |
+
else:
|
202 |
+
error_detail = res.json().get("detail", "Unknown error")
|
203 |
+
st.code(f"Failed to get result, Status Code: {res.status_code}, Detail: {error_detail}")
|
204 |
+
gif_runner.empty()
|
205 |
+
st.write("")
|
206 |
|
207 |
|
208 |
if __name__ == "__main__":
|
eu1.jpg
ADDED
![]() |
kr1.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
boto3
|
2 |
+
pillow
|
3 |
+
cffi
|
wait-waiting.gif
ADDED
![]() |