Skip to content

Commit

Permalink
Merge pull request #45 from kookmin-sw/feat/flask_server
Browse files Browse the repository at this point in the history
Feat/flask server
  • Loading branch information
lkl4502 authored May 23, 2024
2 parents ea1eb5a + d97aec8 commit 139b734
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ predict_image/*

__pycache__
.vscode
*.idea
*.idea
.env
Binary file added gan/input/286225_RfNB.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 17 additions & 2 deletions gan/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from models.Embedding import Embedding

import os
from dotenv import load_dotenv
from smtp import sendEmail

current_directory = os.getcwd()
print("현재 작업 디렉토리:", current_directory)
Expand Down Expand Up @@ -47,7 +49,10 @@ def get_im_paths_not_embedded(im_paths: Set[str]) -> List[str]:


def main(args):

if not len(args.email):
print("E-mail address required")
return

set_seed(42)

ii2s = Embedding(args)
Expand Down Expand Up @@ -76,12 +81,22 @@ def main(args):
align = Alignment(args)
align.align_images(im_path1, im_path2, sign=args.sign, align_more_region=False, smooth=args.smooth)

# Step 3 : Send result image to client!
load_dotenv()

sender = os.environ.get("sender")
sender_pw = os.environ.get("sender_pw")

sendEmail(args.email, "style_your_hair_output/wo_round_wo_square.png", sender, sender_pw)




if __name__ == "__main__":

parser = argparse.ArgumentParser(description='Style Your Hair')

#client email
parser.add_argument('--email', type=str, default="", help='client email')
# flip
parser.add_argument('--flip_check', action='store_true', help='image2 might be flipped')

Expand Down
31 changes: 31 additions & 0 deletions gan/resize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from PIL import Image
import os

def resize_img(path):
image_path = path
image = Image.open(image_path)
file_name = os.path.basename(path)

# 이미지 회전 상태 확인
orientation = 1 # 기본적으로 회전되지 않은 상태
if hasattr(image, '_getexif'): # Exif 정보가 있는지 확인
exif = image._getexif() # Exif 정보 가져오기
if exif is not None:
orientation = exif.get(0x0112, 1)

# 이미지 크기 조정
new_resolution = (1024, 1024)
resized_image = image.resize(new_resolution)

# 회전된 이미지 원래대로 돌리기
if orientation in [3, 6, 8]:
if orientation == 3:
resized_image = resized_image.transpose(Image.ROTATE_180)
elif orientation == 6:
resized_image = resized_image.transpose(Image.ROTATE_270)
elif orientation == 8:
resized_image = resized_image.transpose(Image.ROTATE_90)

resized_image.save(f"./gan/input/{file_name}")
print("조정된 이미지 경로:", f"./gan/input/{file_name}" )
print("조정된 이미지 크기:", resized_image.size)
51 changes: 51 additions & 0 deletions gan/smtp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import smtplib # SMTP 사용을 위한 모듈
import re # Regular Expression을 활용하기 위한 모듈
from email.mime.multipart import MIMEMultipart # 메일의 Data 영역의 메시지를 만드는 모듈
from email.mime.text import MIMEText # 메일의 본문 내용을 만드는 모듈
from email.mime.image import MIMEImage # 메일의 이미지 파일을 base64 형식으로 변환하기 위한 모듈

def sendEmail(receiver, image_path, sender, sender_pw):
# 유효성 검사를 위한 정규표현식
reg = "^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$"
if re.match(reg, receiver):
print("정상적인 메일 형식 확인 완료 --> 메일 전송 중")
else:
print("받으실 메일 주소가 정확하지 않습니다.")
return False

# 이메일 형식이 맞을 경우만 진행됨.
# smpt 서버와 연결
smtp_server = "smtp.gmail.com"
port = 587
smtp = smtplib.SMTP(smtp_server, port)
smtp.starttls()

# 로그인
smtp.login(sender, sender_pw)

# 메일 내용 구성
msg = MIMEMultipart()
msg["Subject"] = "Only You"
msg["From"] = sender
msg["To"] = receiver

content = '''\
안녕하세요. Capstone 11조 Only You입니다.
요청하신 이미지 생성이 완료되어 이메일로 전송해드립니다.
좋은 하루 보내세요~!\
'''

content_part = MIMEText(content, "plain")
msg.attach(content_part)

# image 추가
with open(image_path, 'rb') as file:
img = MIMEImage(file.read())
img.add_header('Content-Disposition', 'attachment', filename = "image.jpg")
msg.attach(img)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()
return True

3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def test():
return str(pc_model.test([[0] * 13]))

from shape_detect.controller import get_shape
from gan.resize import resize_img
@app.route('/predict_shape', methods =['GET'])
def predict_shape():
global current_image_path
Expand All @@ -131,6 +132,8 @@ def predict_shape():

res['probability'] = probability
print(res)

resize_img(current_image_path)

return res

Expand Down

0 comments on commit 139b734

Please sign in to comment.