Skip to content

Commit

Permalink
img_test add & move
Browse files Browse the repository at this point in the history
  • Loading branch information
YongRokKim committed Jan 4, 2024
1 parent 38ee5ba commit 29a97db
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 48 deletions.
13 changes: 4 additions & 9 deletions BP_Api_File/BP.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,8 @@
"output_type": "stream",
"text": [
"Image already meets the minimum size requirements. No resizing needed.\n",
"<_io.BufferedReader name='/data/BP/BP_Api_File/food/test/test2.jpg'>\n",
"-------------- <class '_io.BufferedReader'>\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<_io.BufferedReader name='/data/BP/test2.jpg'>\n",
"-------------- <class '_io.BufferedReader'>\n",
"Code: 200\n",
"{'confidence': 0.6900922, 'food_name': '미소된장국', 'food_code': 'D000041', 'food_cal': 38.0, 'food_nat': 517.03, 'food_serving_size': 150.0, 'food_carbs': 2.8, 'food_protein': 2.8, 'food_fat': 1.8, 'food_sugar': 0.7, 'food_cholesterol': 0.0}\n",
"====================\n",
Expand Down Expand Up @@ -184,12 +178,13 @@
" return secrets[setting]\n",
" except KeyError:\n",
" error_msg = \"Set the {} environment variable\".format(setting)\n",
" print(\"error_msg\")\n",
" # raise ImproperlyConfigured(error_msg)\n",
" \n",
"client_id = get_secret(\"kt_client_id\")\n",
"client_secret = get_secret(\"kt_client_secret\")\n",
"\n",
"img_path = \"/data/BP/BP_Api_File/food/test/test2.jpg\"\n",
"img_path = \"/data/BP/test2.jpg\"\n",
"\n",
"# HMAC 기반 signature 생성\n",
"signature = hmac.new(\n",
Expand Down
19 changes: 14 additions & 5 deletions BP_Api_File/OCR_test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b'{\"version\":\"V2\",\"requestId\":\"0f02780b-1265-4f3f-b3c6-6354a4741eab\",\"timestamp\":1704177019686,\"images\":[{\"receipt\":{\"meta\":{\"estimatedLanguage\":\"ko\"},\"result\":{\"storeInfo\":{\"name\":{\"text\":\"\\xeb\\xa5\\xb4\\xed\\x95\\x98\\xec\\x9e\\x84\\xec\\x8a\\xa4\\xed\\x84\\xb0\\xeb\\x94\\x94\\xec\\xb9\\xb4\\xed\\x8e\\x98\",\"formatted\":{\"value\":\"\\xeb\\xa5\\xb4\\xed\\x95\\x98\\xec\\x9e\\x84\\xec\\x8a\\xa4\\xed\\x84\\xb0\\xeb\\x94\\x94\\xec\\xb9\\xb4\\xed\\x8e\\x98\"},\"keyText\":\"\\xea\\xb0\\x80\\xeb\\xa7\\xb9\\xec\\xa0\\x90\\xeb\\xaa\\x85\",\"confidenceScore\":0.95034,\"boundingPolys\":[{\"vertices\":[{\"x\":466.0,\"y\":348.0},{\"x\":663.0,\"y\":351.0},{\"x\":662.0,\"y\":384.0},{\"x\":466.0,\"y\":381.0}]}],\"maskingPolys\":[]},\"subName\":{\"text\":\"\\xeb\\xb6\\x80\\xec\\x82\\xb0\\xeb\\xa7\\x9d\\xeb\\xaf\\xb8\\xec\\xa0\\x90\",\"keyText\":\"\",\"confidenceScore\":0.92417,\"boundingPolys\":[{\"vertices\":[{\"x\":669.0,\"y\":351.0},{\"x\":794.0,\"y\":351.0},{\"x\":794.0,\"y\":385.0},{\"x\":669.0,\"y\":385.0}]}],\"maskingPolys\":[]},\"addresses\":[{\"text\":\"\\xeb\\xb6\\x80\\xec\\x82\\xb0\\xea\\xb4\\x91\\xec\\x97\\xad\\xec\\x8b\\x9c \\xec\\x88\\x98\\xec\\x98\\x81\\xea\\xb5\\xac \\xea\\xb3\\xbc\\xec\\xa0\\x95\\xeb\\xa1\\x9c 63 \\xeb\\xa7\\x9d \\xeb\\xaf\\xb8\\xec\\x84\\xbc\\xed\\x85\\x80 4\\xec\\xb8\\xb5\",\"formatted\":{\"value\":\"\\xeb\\xb6\\x80\\xec\\x82\\xb0\\xea\\xb4\\x91\\xec\\x97\\xad\\xec\\x8b\\x9c \\xec\\x88\\x98\\xec\\x98\\x81\\xea\\xb5\\xac \\xea\\xb3\\xbc\\xec\\xa0\\x95\\xeb\\xa1\\x9c 63 \\xeb\\xa7\\x9d \\xeb\\xaf\\xb8\\xec\\x84\\xbc\\xed\\x85\\x80 4\\xec\\xb8\\xb5\"},\"keyText\":\"\\xec\\xa3\\xbc \\xec\\x86\\x8c:\",\"confidenceScore\":0.92462,\"boundingPolys\":[{\"vertices\":[{\"x\":466.0,\"y\":410.0},{\"x\":590.0,\"y\":410.0},{\"x\":590.0,\"y\":442.0},{\"x\":466.0,\"y\":442.0}]},{\"vertices\":[{\"x\":596.0,\"y\":410.0},{\"x\":672.0,\"y\":410.0},{\"x\":672.0,\"y\":442.0},{\"x\":596.0,\"y\":442.0}]},{\"vertices\":[{\"x\":676.0,\"y\":411.0},{\"x\":755.0,\"y\":411.0},{\"x\":755.0,\"y\":444.0},{\"x\":676.0,\"y\":444.0}]},{\"vertices\":[{\"x\":756.0,\"y\":414.0},{\"x\":790.0,\"y\":414.0},{\"x\":790.0,\"y\":439.0},{\"x\":756.0,\"y\":439.0}]},{\"vertices\":[{\"x\":795.0,\"y\":412.0},{\"x\":825.0,\"y\":412.0},{\"x\":825.0,\"y\":444.0},{\"x\":795.0,\"y\":444.0}]},{\"vertices\":[{\"x\":265.0,\"y\":439.0},{\"x\":344.0,\"y\":439.0},{\"x\":344.0,\"y\":469.0},{\"x\":265.0,\"y\":469.0}]},{\"vertices\":[{\"x\":345.0,\"y\":439.0},{\"x\":390.0,\"y\":439.0},{\"x\":390.0,\"y\":468.0},{\"x\":345.0,\"y\":468.0}]}],\"maskingPolys\":[]}],\"tel\":[{\"text\":\"01057018642\",\"formatted\":{\"value\":\"01057018642\"},\"keyText\":\"\\xec\\xa0\\x84\\xed\\x99\\x94\\xeb\\xb2\\x88\\xed\\x98\\xb8\",\"confidenceScore\":0.9302,\"boundingPolys\":[{\"vertices\":[{\"x\":465.0,\"y\":380.0},{\"x\":602.0,\"y\":382.0},{\"x\":601.0,\"y\":407.0},{\"x\":465.0,\"y\":405.0}]}]}]},\"paymentInfo\":{\"cardInfo\":{\"company\":{\"text\":\"\\xeb\\xa1\\xaf\\xeb\\x8d\\xb0\\xec\\xb9\\xb4\\xeb\\x93\\x9c\\xec\\x82\\xac\",\"formatted\":{\"value\":\"\\xeb\\xa1\\xaf\\xeb\\x8d\\xb0\\xec\\xb9\\xb4\\xeb\\x93\\x9c\\xec\\x82\\xac\"},\"keyText\":\"\\xec\\xb9\\xb4 \\xeb\\x93\\x9c \\xec\\x82\\xac\",\"confidenceScore\":0.90842,\"boundingPolys\":[{\"vertices\":[{\"x\":468.0,\"y\":524.0},{\"x\":588.0,\"y\":524.0},{\"x\":588.0,\"y\":551.0},{\"x\":468.0,\"y\":551.0}]}],\"maskingPolys\":[]},\"number\":{\"text\":\"5137920000000000****\",\"formatted\":{\"value\":\"5137920000000000****\"},\"keyText\":\"\\xec\\xb9\\xb4\\xeb\\x93\\x9c\\xeb\\xb2\\x88\\xed\\x98\\xb8:\",\"confidenceScore\":0.88703,\"boundingPolys\":[{\"vertices\":[{\"x\":465.0,\"y\":552.0},{\"x\":705.0,\"y\":552.0},{\"x\":705.0,\"y\":574.0},{\"x\":465.0,\"y\":574.0}]}],\"maskingPolys\":[]}}},\"subResults\":[{\"items\":[{\"name\":{\"text\":\"(\\xec\\xa0\\x95\\xec\\x95\\xa1\\xea\\xb6\\x8c) 50: 00\",\"formatted\":{\"value\":\"(\\xec\\xa0\\x95\\xec\\x95\\xa1\\xea\\xb6\\x8c)50: 00\"},\"keyText\":\"\",\"confidenceScore\":0.47716,\"boundingPolys\":[{\"vertices\":[{\"x\":305.0,\"y\":660.0},{\"x\":408.0,\"y\":660.0},{\"x\":408.0,\"y\":691.0},{\"x\":305.0,\"y\":691.0}]},{\"vertices\":[{\"x\":415.0,\"y\":662.0},{\"x\":452.0,\"y\":662.0},{\"x\":452.0,\"y\":688.0},{\"x\":415.0,\"y\":688.0}]},{\"vertices\":[{\"x\":451.0,\"y\":662.0},{\"x\":482.0,\"y\":662.0},{\"x\":482.0,\"y\":689.0},{\"x\":451.0,\"y\":689.0}]}],\"maskingPolys\":[]},\"price\":{\"price\":{\"text\":\"60,000\",\"formatted\":{\"value\":\"60000\"},\"keyText\":\"\",\"confidenceScore\":0.85618,\"boundingPolys\":[{\"vertices\":[{\"x\":582.0,\"y\":662.0},{\"x\":664.0,\"y\":662.0},{\"x\":664.0,\"y\":691.0},{\"x\":582.0,\"y\":691.0}]}]}}}]}],\"totalPrice\":{\"price\":{\"text\":\"60,000\",\"formatted\":{\"value\":\"60000\"},\"keyText\":\"\\xed\\x95\\xa9\\xea\\xb3\\x84\\xea\\xb8\\x88\\xec\\x95\\xa1\",\"confidenceScore\":0.9458,\"boundingPolys\":[{\"vertices\":[{\"x\":726.0,\"y\":722.0},{\"x\":810.0,\"y\":722.0},{\"x\":810.0,\"y\":750.0},{\"x\":726.0,\"y\":750.0}]}]}},\"subTotal\":[]}},\"uid\":\"3cd663a927e84bba9d0b73d3bec22155\",\"name\":\"demo\",\"inferResult\":\"SUCCESS\",\"message\":\"SUCCESS\",\"validationResult\":{\"result\":\"NO_REQUESTED\"}}]}'\n",
"\n",
"(정액권) 50: 00\n"
"b'{\"version\":\"V2\",\"requestId\":\"09d439ae-b453-44c9-b034-7f5a51e87a86\",\"timestamp\":1704347028917,\"images\":[{\"uid\":\"cf457daf3ff4414a9a7eef5e8663f0e1\",\"name\":\"demo\",\"inferResult\":\"ERROR\",\"message\":\"Recognition failure.\",\"validationResult\":{\"result\":\"NO_REQUESTED\"},\"convertedImageInfo\":{\"width\":1440,\"height\":1440,\"pageIndex\":0,\"longImage\":false}}]}'\n"
]
},
{
"ename": "KeyError",
"evalue": "'receipt'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[1], line 54\u001b[0m\n\u001b[1;32m 51\u001b[0m json\u001b[38;5;241m.\u001b[39mdump(result, make_file, indent\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, ensure_ascii\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 53\u001b[0m text \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m---> 54\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m field \u001b[38;5;129;01min\u001b[39;00m result[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mimages\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;241m0\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mreceipt\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mresult\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msubResults\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;241m0\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mitems\u001b[39m\u001b[38;5;124m'\u001b[39m]:\n\u001b[1;32m 55\u001b[0m text \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m+\u001b[39mfield[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtext\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[1;32m 56\u001b[0m \u001b[38;5;28mprint\u001b[39m(text)\n",
"\u001b[0;31mKeyError\u001b[0m: 'receipt'"
]
}
],
Expand All @@ -37,7 +46,7 @@
"\n",
"api_url = get_secret('CLOVA_OCR_Invoke_URL')\n",
"secret_key = get_secret('naver_secret_key')\n",
"image_file = '/data/BP/BP_Api_File/not_food_img/not_food4.jpg'\n",
"image_file = '/data/BP/test2.jpg'\n",
"\n",
"request_json = {\n",
" 'images': [\n",
Expand Down
22 changes: 22 additions & 0 deletions BP_Api_File/result_notfood_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "V2",
"requestId": "09d439ae-b453-44c9-b034-7f5a51e87a86",
"timestamp": 1704347028917,
"images": [
{
"uid": "cf457daf3ff4414a9a7eef5e8663f0e1",
"name": "demo",
"inferResult": "ERROR",
"message": "Recognition failure.",
"validationResult": {
"result": "NO_REQUESTED"
},
"convertedImageInfo": {
"width": 1440,
"height": 1440,
"pageIndex": 0,
"longImage": false
}
}
]
}
98 changes: 64 additions & 34 deletions Server/model_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,56 @@ def img_resize(img):
# 이미지를 열고 크기 가져오기
with Image.open(io.BytesIO(img)) as img:
width, height = img.size
print("@hello14 :", width, height)

# 최소 크기 정의
minWidth = 1080 if width > height else 720
minHeight = 720 if width > height else 1080
# 최소 및 최대 크기 정의
minWidth, maxWidth = (720, 2560) if width > height else (720, 1440)
minHeight, maxHeight = (1080, 1440) if width > height else (1080, 2560)
print("@hello14 :", minWidth, minHeight, maxWidth, maxHeight)

# 이미지가 이미 최소 크기 요건을 충족하는지 확인
if width >= minWidth and height >= minHeight:
print("Image already meets the minimum size requirements. No resizing needed.")
return
if minWidth <= width <= maxWidth and minHeight <= height <= maxHeight:
print("Image meets size requirements. No resizing needed.")
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='JPEG')
return img_byte_arr.getvalue()

# 새 크기 계산 및 비율 유지
if width / height > 1:
# 넓은 이미지
newHeight = max(height, minHeight)
newWidth = round(newHeight * (width / height))
if newWidth < minWidth:
newWidth = minWidth
newHeight = round(newWidth / (width / height))
else:
# 높은 이미지
newWidth = max(width, minWidth)
newHeight = round(newWidth / (width / height))
if newHeight < minHeight:
newHeight = minHeight
# 이미지 크기 조정
newWidth, newHeight = width, height
if width < minWidth or height < minHeight:
# 이미지 확대
if width / height > 1:
# 넓은 이미지
newHeight = max(height, minHeight)
newWidth = round(newHeight * (width / height))
if newWidth < minWidth:
newWidth = minWidth
newHeight = round(newWidth / (width / height))
else:
# 높은 이미지
newWidth = max(width, minWidth)
newHeight = round(newWidth / (width / height))
if newHeight < minHeight:
newHeight = minHeight
newWidth = round(newHeight * (width / height))
elif width > maxWidth or height > maxHeight:
# 이미지 축소
if width / height > 1:
# 넓은 이미지
newWidth = min(width, maxWidth)
newHeight = round(newWidth / (width / height))
if newHeight > maxHeight:
newHeight = maxHeight
newWidth = round(newHeight * (width / height))
else:
# 높은 이미지
newHeight = min(height, maxHeight)
newWidth = round(newHeight / (width / height))
if newWidth > maxWidth:
newWidth = maxWidth
newHeight = round(newWidth / (width / height))

# 이미지 크기 조정 및 저장
# 조정된 크기 출력 및 이미지 크기 조정
print("@hello15 : Resized Image Size:", newWidth, newHeight)
resized_img = img.resize((newWidth, newHeight))
img_byte_arr = io.BytesIO()
resized_img.save(img_byte_arr, format='JPEG')
Expand Down Expand Up @@ -104,9 +127,10 @@ def food_api(img,secret_file):

if response.ok:
json_data = json.loads(response.text)
code = json_data['code']
print("@hello13 :",json_data)
# code = json_data['code']
data = json_data['data']
print(f"Code: {code}")
# print(f"Code: {code}")
return data
#.json 형식 출력
# print(f"Data: {data}")
Expand Down Expand Up @@ -194,22 +218,24 @@ def predict():
"foodNames": [],
# kt predict food info
"ktFoodsInfo": {}
}
},
# "image":[file]
}

# API키 연결 .json 파일
# =====================================================
secret_file = "secrets.json"
# ====================================================
# print("@hello2")
print("@hello2")
try:
# print("@helloimg",type(img))
print("@helloimg",type(img))
resized_img = img_resize(img)
ocr_result = OCR_api(img, secret_file)
# print("@hello3")
print("@hello10 :",ocr_result)
print("@hello3")
if ocr_result['images'][0]['inferResult'] == 'ERROR':
# print("@hello6",ocr_result['images'][0]['inferResult'] )
resized_img = img_resize(img)
# print('@hello7')
print("@hello6",ocr_result['images'][0]['inferResult'] )
print('@hello7')
food_result = food_api(resized_img, secret_file)
od_result = get_prediction(img)
print('+++'*20)
Expand All @@ -223,13 +249,17 @@ def predict():
# print(food_result)
# print("="*20)
for region_num in food_result[0]:
if food_result[0][region_num]['prediction_top1']['confidence'] >=0.5:
# res['predict']['ktFoodsInfo'][region_num] = food_result[0][region_num]['prediction_top1']
# res['predict']['foodNames'].append(food_result[0][region_num]['prediction_top1']["food_name"])
if food_result[0][region_num]['prediction_top1']['confidence'] >=0.3:
res['predict']['ktFoodsInfo'][region_num] = food_result[0][region_num]['prediction_top1']
res['predict']['foodNames'].append(food_result[0][region_num]['prediction_top1']["food_name"])

for item in od_result:
if od_result[item]['Food_name'] not in res['predict']['foodNames'] and od_result[item]['highest_confidence'] >=0.3:
res['predict']['foodNames'].append(od_result[item]['Food_name'])
# res['predict']['foodNames'].append(od_result[item]['Food_name'])
if od_result[item]['highest_confidence'] >=0.5:
if od_result[item]['Food_name'] not in res['predict']['foodNames']:
res['predict']['foodNames'].append(od_result[item]['Food_name'])
else:
# print('@hello4')
if ocr_result['images'][0]['receipt']['result']['subResults']:
Expand Down
File renamed without changes
Binary file added img_file/KakaoTalk_20240104_142034141.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img_file/KakaoTalk_20240104_142034141_01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img_file/KakaoTalk_20240104_142034141_02.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img_file/KakaoTalk_20240104_142034141_03.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img_file/KakaoTalk_20240104_142034141_04.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img_file/KakaoTalk_20240104_142034141_05.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img_file/KakaoTalk_20240104_142034141_06.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img_file/Test_gokbob.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img_file/Test_gokbob2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes

0 comments on commit 29a97db

Please sign in to comment.