Skip to content

Commit

Permalink
image color process
Browse files Browse the repository at this point in the history
  • Loading branch information
entellaKa committed May 15, 2024
1 parent 7c041a0 commit 1d1641c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
4 changes: 2 additions & 2 deletions AI/app/controllers/example_controllers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import Blueprint, request, jsonify, Response
from app.services import auto_draw
import json, tensorflow as tf
import json

bp = Blueprint(name='example',
import_name=__name__,
Expand All @@ -12,7 +12,7 @@


@ai.route('/base64', methods=['POST'])
def draw() -> str:
def draw() -> Response:
return jsonify(auto_draw.AIbyBase64(json.loads(request.get_json())))


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import io, base64
import os
from PIL import Image
import boto3
import requests
Expand All @@ -18,8 +17,7 @@ def downloadFromS3(bucket, key):
client = boto3.client('s3',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_DEFAULT_REGION
)
region_name=AWS_DEFAULT_REGION)

file_name = 'downLoad.png' # 다운될 이미지 이름
# bucket = 'mufi-photo' # 버켓 주소
Expand Down
30 changes: 30 additions & 0 deletions AI/app/services/ImagePreprocessing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from PIL import Image
import numpy as np

# 이미지를 흑백으로 변환
def convertMono(img: Image):
fn = lambda x: 255 if x > 0 else 0
img = img.convert('L').point(fn, mode='1')
return img


# 이미지 크기를 128*128로 변경 후 이미지를 배열화
def resizing(img):
img = img.resize((128, 128))
img = img.convert('RGB')
return img


# 이미지를 배열화
def imageToArray(img):
img = np.array(img)
img = np.expand_dims(img, axis=0)
return img


# 이미지를 AI에 맞게 전환
def imageProcessing(img: Image):
img = convertMono(img)
img = resizing(img)
img = imageToArray(img)
return img
28 changes: 10 additions & 18 deletions AI/app/services/auto_draw.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
from keras.models import load_model
import numpy as np
import operator, os
from app.services.LoadImage import decodeFromJsonToImage, downloadFromS3, downloadFromURL
from app.services.ImageLoading import decodeFromJsonToImage, downloadFromS3, downloadFromURL
from app.services.ImagePreprocessing import imageProcessing

tags = 'airplane,apartment,apple,arm,arrow,axe,bag,baseball,basketball,bath,bed,bench,book,bottle,box,bread,broom,brush,bucket,bulb,cake,calendar,candle,candy,castle,chair,cherry,clock,cloud,coffee,coin,cookie,cup,cylinder,dice,dress,ear,earth,envelope,eraser,eye,finger,flag,flame,flask,flower,gamecontroller,ghost,gift,grape,hammer,heart,hospital,house,industry,injection,ladder,lake,leaf,leg,lightning,meat,megaphone,mic,money,monitor,mouse,mushroom,nail,nose,officebuilding,pants,peanut,pencil,police,pumpkin,rain,ribbon,rocket,ruler,school,shield,shirt,skirt,soccer,speaker,sprout,star,stethoscope,sun,swim,sword,television,tennis,tree,truck,umbrella,vehicle,volleyball,watch,wheel,windmill,zoom'.split(
',')


# 이미지 크기를 128*128로 변경
def resizing(image):
image = image.resize((128, 128))
image = image.convert('RGB')
image = np.array(image)
image = np.expand_dims(image, axis=0)
return image


# AI를 이용하여 유사한 이미지 출력
# AI를 이용하여 유사하다고 예상되는 이미지(이름) 출력
def getPredict(img):
saved_model = load_model(os.getcwd() + "/app/services/cnn1.h5")
pre = saved_model.predict(img)
return pre


# AI 결과를 정확도에 따라 오름차순으로 정렬
# AI 결과를 정확도에 따라 오름차 순으로 정렬
def resultByDesc(result):
x = {}
for i in range(len(result)):
Expand All @@ -34,15 +25,16 @@ def resultByDesc(result):
def getImageNameList(result):
names = []
for k, v in result:
print(k)
print(k,tags[k])
names.append(tags[k])
return names


def AI_process(image):
resized = resizing(image)
pre_result = getPredict(resized)
result2 = resultByDesc(pre_result[0])
return getImageNameList(result2)
imgData = imageProcessing(image)
predict = getPredict(imgData)
result = resultByDesc(predict[0])
return getImageNameList(result)


def AIbyBase64(json_data):
Expand Down

0 comments on commit 1d1641c

Please sign in to comment.