import streamlit as st import os from PIL import Image, ImageOps from PIL import ImageDraw, ImageFont import random from io import BytesIO from requests.auth import HTTPBasicAuth import boto3 import json from datetime import datetime import requests remote_url = os.environ.get("remote_url") base_url = os.environ.get("base_url") authEmail = os.environ.get("authEmail") authPass = os.environ.get("authPass") aws_access_key_id = os.environ.get("AWS_ACCESS_KEY_ID") aws_secret_access_key = os.environ.get("AWS_SECRET_ACCESS_KEY") aws_default_region = os.environ.get("AWS_DEFAULT_REGION") bucket_name = os.environ.get("bucket_name") user_id = os.environ.get("user_id") # Read values from the JSON file # with open('../config.json') as f: # config = json.load(f) # remote_url = config.get("remote_url") # base_url = config.get("base_url") # authEmail = config.get("authEmail") # authPass = config.get("authPass") # aws_access_key_id = config.get("AWS_ACCESS_KEY_ID") # aws_secret_access_key = config.get("AWS_SECRET_ACCESS_KEY") # aws_default_region = config.get("AWS_DEFAULT_REGION") # bucket_name = config.get("bucket_name") # user_id = config.get("user_id") # Create a Boto3 session session = boto3.Session( aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=aws_default_region ) # Create an S3 client s3 = session.client('s3') def calculate_font_size(box, max_font_size=30, min_font_size=10, font_path="NotoSansKR-Medium.otf"): width = box[2] - box[0] font_size = int(max_font_size * width / 100) # Limit font size to max and min values font_size = min(max_font_size, font_size) font_size = max(min_font_size, font_size) font = ImageFont.truetype(font_path, font_size) return font, font_size def estimate_text_size(text, font_size): # Estimate text width based on number of characters and average character width avg_character_width = font_size * 0.5 # Adjust this value based on your font and preferences text_width = len(text) * avg_character_width # Estimate text height based on font size text_height = font_size return text_width, text_height def draw_annotated_box(draw, box, text, font_path="NotoSansKR-Medium.otf"): x1, y1, x2, y2 = box draw.rectangle([x1, y1, x2, y2], outline="green", width=3) # Calculate font size and get font font, font_size = calculate_font_size(box) # Measure text size text_width, text_height = estimate_text_size(text, font_size) # Reduce height of the background box by a factor (for example, 0.8 for 80% of the original height) reduced_text_height = int(text_height * 0.8) # Draw white background for text background_box = [x1, y1 - reduced_text_height - 2, x1 + text_width + 2, y1] draw.rectangle(background_box, fill="green") # Draw text draw.text((x1, y1 - text_height), text, font=font, fill="white") def upload_to_s3(image_data, region): # Pre-configured bucket name and path region = region.upper() path_prefix = f"image_{region}" # S3 client setup s3 = boto3.client('s3') # Generating file name based on current date and time now = datetime.now() date_str = now.strftime("%Y%m%d") time_str = now.strftime("%H%M%S") file_name = f"{path_prefix}/{user_id}_{date_str}_{time_str}.jpeg" try: # Attempt to upload the image to S3 s3.put_object( Body=image_data, Bucket=bucket_name, Key=file_name, ContentType='image/jpeg' ) return True except Exception as e: # Log and return False if an error occurs print(f"Error uploading to S3: {e}") return False def main(): st.title("๐Ÿš™ ANPR - Ver.1.0.leon") country = st.radio("๐Ÿฆ‹ Type of License", options=['Korean', 'EU']) st.markdown(""" Korean : ๐Ÿ‡ฐ๐Ÿ‡ท
EU : ๐Ÿ‡ฆ๐Ÿ‡ฑ ๐Ÿ‡ฆ๐Ÿ‡ฉ ๐Ÿ‡ฆ๐Ÿ‡น ๐Ÿ‡ง๐Ÿ‡ช ๐Ÿ‡ง๐Ÿ‡ฆ ๐Ÿ‡ง๐Ÿ‡ฌ ๐Ÿ‡ญ๐Ÿ‡ท ๐Ÿ‡จ๐Ÿ‡พ ๐Ÿ‡จ๐Ÿ‡ฟ ๐Ÿ‡ฉ๐Ÿ‡ฐ ๐Ÿ‡ซ๐Ÿ‡ฎ ๐Ÿ‡ซ๐Ÿ‡ท ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿ‡ฌ๐Ÿ‡ท ๐Ÿ‡ญ๐Ÿ‡บ ๐Ÿ‡ฎ๐Ÿ‡ช ๐Ÿ‡ฎ๐Ÿ‡น ๐Ÿ‡ฑ๐Ÿ‡ฎ ๐Ÿ‡ฒ๐Ÿ‡น ๐Ÿ‡ฒ๐Ÿ‡จ ๐Ÿ‡ฒ๐Ÿ‡ช ๐Ÿ‡ณ๐Ÿ‡ฑ ๐Ÿ‡ฒ๐Ÿ‡ฐ ๐Ÿ‡ณ๐Ÿ‡ด ๐Ÿ‡ต๐Ÿ‡ฑ ๐Ÿ‡ต๐Ÿ‡น ๐Ÿ‡ท๐Ÿ‡ด ๐Ÿ‡ธ๐Ÿ‡ฒ ๐Ÿ‡ท๐Ÿ‡ธ ๐Ÿ‡ธ๐Ÿ‡ฐ ๐Ÿ‡ธ๐Ÿ‡ฎ ๐Ÿ‡ช๐Ÿ‡ธ ๐Ÿ‡ธ๐Ÿ‡ช ๐Ÿ‡จ๐Ÿ‡ญ ๐Ÿ‡ฌ๐Ÿ‡ง ๐Ÿ‡ฑ๐Ÿ‡บ """, unsafe_allow_html=True) st.text("") st.write("๐Ÿงช Options ") use_default_image = st.checkbox("Use default server image?", value=False) # New checkbox img_file_buffer = st.file_uploader("๐Ÿฆ‹ Upload your image", type=['jpg', 'jpeg', 'png']) if not use_default_image else None submit = st.button("Submit") show_image = True if show_image: font = ImageFont.truetype("NotoSansKR-Medium.otf", 30) if submit: if use_default_image or img_file_buffer is not None: gif_runner = st.image('wait-waiting.gif') if use_default_image: # Load the default image from your server here # Make sure to replace 'your_default_image_path' with the actual path if country == 'Korean': image_choice = random.choice(['kr1.jpg']) image_choice = os.path.join('./', image_choice) image = Image.open(image_choice) elif country == 'EU': image_choice = random.choice(['eu1.jpg']) image_choice = os.path.join('./', image_choice) image = Image.open(image_choice) else: image = Image.open(img_file_buffer) image = ImageOps.exif_transpose(image) img_byte_arr = BytesIO() if image.mode == 'RGBA': rgb_image = image.convert('RGB') rgb_image.save(img_byte_arr, format='JPEG') else: image.save(img_byte_arr, format='JPEG') img_byte_arr.seek(0) region = "kr" if country == 'Korean': region = "kr" elif country == 'EU': region = "eu" auth_values = HTTPBasicAuth(authEmail, authPass) # Make POST request url = remote_url form_data = {'base_url': base_url, 'region':region} files = {'file': ('image.jpg', img_byte_arr, 'image/jpeg')} res = requests.post(url, headers={'Accept': 'application/json'}, files=files, data=form_data, # Additional form data auth=auth_values) img_byte_arr.seek(0) if use_default_image == False: if upload_to_s3(img_byte_arr.getvalue(), region): print("Upload successful!") # Close the file img_byte_arr.close() if res.status_code == 200: output_res = res.json() if show_image: draw = ImageDraw.Draw(image) for item in output_res['results']: x1, y1, x2, y2 = item['ltrb'] ocr_text = item['ocr'] # print(x1,y1,x2,y2,ocr_text) if show_image: draw_annotated_box(draw, (x1, y1, x2, y2), ocr_text) if show_image: st.image(image, caption='Annotated Image', use_column_width=True) st.header('๐Ÿฆ‹ Result Json') st.code(json.dumps(output_res, indent=4, ensure_ascii=False), language="json") else: error_detail = res.json().get("detail", "Unknown error") st.code(f"Failed to get result, Status Code: {res.status_code}, Detail: {error_detail}") gif_runner.empty() st.write("") if __name__ == "__main__": #main function main()