Skip to content

Commit cc1487b

Browse files
Merge pull request #77 from wowzoo/master
python version specific code and image cropper
2 parents b8c9334 + 12deca2 commit cc1487b

File tree

4 files changed

+88
-24
lines changed

4 files changed

+88
-24
lines changed

genai/genai-app-demo/01-prompt-engineering-demo/frontUI/app/pages/4_Product_Identify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@
4545
for name, desc in similar.items():
4646
temp.append(f'{name}: {desc}')
4747

48-
st.info(f'상품\n\n {result['goods']}\n\n브랜드: {result['brand']}\n\n추천\n\n {'\n'.join(temp)}')
48+
st.info(f"상품\n\n {result['goods']}\n\n브랜드: {result['brand']}\n\n추천\n\n {'\n'.join(temp)}")
4949
elif 'etc' in result and result['etc']:
50-
st.info(f'기타: {result['etc']}')
50+
st.info(f"기타: {result['etc']}")

genai/genai-app-demo/01-prompt-engineering-demo/frontUI/app/pages/9_Image_Replace.py

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import requests
33
import boto3
44
import os
5+
import io
6+
7+
from streamlit_cropper import st_cropper
8+
from PIL import Image, ImageDraw
59

610

711
ALB_URL = os.environ.get('ALB_URL')
@@ -12,48 +16,104 @@
1216
s3 = boto3.client('s3')
1317

1418
st.set_page_config(
15-
page_title='Gen AI - Image Variation',
19+
page_title='Gen AI - Image Replace',
1620
page_icon = 'images/aws_favi.png',
1721
# layout = 'wide'
1822
)
1923
st.title('이미지 교체')
2024
st.write('주변 배경과 일치하도록 변경하여 이미지를 수정합니다.')
2125

26+
if "mask_enable" not in st.session_state:
27+
st.session_state.mask_enable = False
28+
2229
uploaded_file = st.file_uploader("파일을 선택하세요", type=['png', 'jpg'])
2330
if uploaded_file is not None:
24-
bytes_data = uploaded_file.getvalue()
25-
st.image(bytes_data)
31+
# print(st.session_state.mask_enable)
32+
st.checkbox("이미지 마스크 지정", key="mask_enable")
33+
if st.session_state.mask_enable:
34+
img = Image.open(uploaded_file)
35+
width, height = img.size
2636

27-
m_input = st.text_area(
28-
'이미지에서 남기고 싶은 오브젝트를 서술합니다 예) car, phone, bag',
29-
'',
30-
# height=100
31-
)
32-
q_input = st.text_area(
33-
'남기고 싶은 오프젝트 이외에 배경에 대해서 정의합니다',
34-
'',
35-
# height=100
36-
)
37+
cropped_box = st_cropper(
38+
img,
39+
realtime_update=True,
40+
box_color='#0000FF',
41+
aspect_ratio=None,
42+
return_type='box'
43+
)
44+
45+
left = cropped_box['left']
46+
top = cropped_box['top']
47+
right = left + cropped_box['width']
48+
bottom = top + cropped_box['height']
49+
shape = (left, top, right, bottom)
50+
51+
masked_image = Image.new('RGB', (width, height), color=(255, 255, 255))
52+
temp_image = ImageDraw.Draw(masked_image)
53+
temp_image.rectangle(shape, fill=(0, 0, 0))
54+
55+
# st.image(masked_image)
56+
57+
st.write('이미지에서 남기고 싶은 오브젝트')
58+
cropped_image = img.crop(shape)
59+
_ = cropped_image.thumbnail((150,150))
60+
st.image(cropped_image)
61+
else:
62+
bytes_data = uploaded_file.getvalue()
63+
st.image(bytes_data)
64+
65+
m_input = st.text_area(
66+
'이미지에서 남기고 싶은 오브젝트를 지정합니다 예) car, phone, bag',
67+
'',
68+
# height=100
69+
)
70+
71+
q_input = st.text_area(
72+
'지정한 오프젝트가 보일 배경에 대해서 정의합니다',
73+
'',
74+
# height=100
75+
)
3776

3877
with st.form('submit_form', clear_on_submit=True):
3978
submitted = st.form_submit_button('Submit')
4079
if submitted:
4180
with st.spinner('Loading...'):
81+
uploaded_file.seek(0)
82+
s3.upload_fileobj(
83+
uploaded_file,
84+
BUCKET_NAME,
85+
f'images/{uploaded_file.name}'
86+
)
87+
88+
if st.session_state.mask_enable:
89+
in_mem_file = io.BytesIO()
90+
masked_image.save(in_mem_file, format='PNG')
91+
in_mem_file.seek(0)
92+
93+
file_name, ext = os.path.splitext(uploaded_file.name)
94+
masked_image_name = f'images/masked_{file_name}.png'
4295
s3.upload_fileobj(
43-
uploaded_file,
96+
in_mem_file,
4497
BUCKET_NAME,
45-
f'images/{uploaded_file.name}'
98+
masked_image_name
4699
)
47100

101+
data = {
102+
'name': f'images/{uploaded_file.name}',
103+
'prompt': q_input,
104+
'mask_image': masked_image_name
105+
}
106+
else:
48107
data = {
49108
'name': f'images/{uploaded_file.name}',
50109
'prompt': q_input,
51110
'mask_prompt': m_input
52111
}
53-
response = requests.post(API_URL, json=data)
54-
result = response.text
112+
113+
response = requests.post(API_URL, json=data)
114+
result = response.text
55115

56-
print(result)
57-
image_object = s3.get_object(Bucket=BUCKET_NAME, Key=f'images/{result}')
58-
st.image(image_object['Body'].read())
116+
print(result)
117+
image_object = s3.get_object(Bucket=BUCKET_NAME, Key=f'images/{result}')
118+
st.image(image_object['Body'].read())
59119

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
boto3==1.34.59
22
streamlit==1.32.2
3-
requests==2.31.0
3+
requests==2.31.0
4+
streamlit-cropper==0.2.2
5+
pillow==10.2.0

genai/genai-app-demo/01-prompt-engineering-demo/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ cdk-ecr-deployment==3.0.40
55
boto3==1.34.59
66
langchain==0.1.12
77
streamlit==1.32.2
8-
requests==2.31.0
8+
requests==2.31.0
9+
streamlit-cropper==0.2.2
10+
pillow==10.2.0

0 commit comments

Comments
 (0)