Skip to content

Commit d66ecb3

Browse files
committed
merge
2 parents 9491fca + cf7d0c1 commit d66ecb3

File tree

8 files changed

+213
-227
lines changed

8 files changed

+213
-227
lines changed

Backend/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Created by https://www.gitignore.io/api/django,visualstudiocode
33
# Edit at https://www.gitignore.io/?templates=django,visualstudiocode
44

5+
.env
56
### Django ###
67
*.log
78
*.pot
@@ -120,7 +121,7 @@ dmypy.json
120121
.pyre/
121122

122123
### VisualStudioCode ###
123-
.vscode
124+
.vscode/
124125
!.vscode/settings.json
125126
!.vscode/tasks.json
126127
!.vscode/launch.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 3.0.3 on 2020-02-14 02:44
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('accounts', '0001_initial'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='receipt',
16+
name='schedule_name',
17+
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='accounts.Schedule'),
18+
),
19+
]

Backend/accounts/naverAPI.py

+73-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# import secret
21
import os
32
import sys
43
import urllib.request
@@ -7,8 +6,6 @@
76
from IPython import embed
87
# OCR API 요청
98
def image_NAVER_AI(img_64):
10-
with open('test.txt', 'w', encoding='utf-8-sig') as f:
11-
f.write(img_64)
129
comma_idx = img_64.find(',')
1310
img_64 = img_64[comma_idx+1:]
1411
TEMPLATE = {
@@ -25,20 +22,21 @@ def image_NAVER_AI(img_64):
2522
"version": "V1"
2623
}
2724
transmit = json.dumps(TEMPLATE)
28-
client_secret = 'anZ3ZkRQU05CR2VHa1VjSVdmRGFoeFdzUUJyS0h1WnU='
25+
client_secret = 'aUtKVFpZS3NQVFhDU3RZSUJPVEttZnZubHNzWFJjcks='
2926
data = transmit
30-
url = "https://4ezihkm520.apigw.ntruss.com/custom/v1/916/682ff265d8365600d74e4adcca80dc6a59c41e236784633af6c3c37438030d33/infer"
27+
url = "https://4ezihkm520.apigw.ntruss.com/custom/v1/927/f6002cbceb8c0327d974714b8f7a4ba28e25f0b7cbe216a719c35ced1fe9647f/infer"
3128
request = urllib.request.Request(url)
3229
request.add_header("X-OCR-SECRET",client_secret)
3330
request.add_header("Content-Type", "application/json")
34-
# embed()
31+
# requests.get(request)
3532
response = urllib.request.urlopen(request, data=data.encode("utf-8"))
3633
rescode = response.getcode()
3734
if(rescode==200):
3835
response_body = response.read()
3936
fix = response_body.decode('utf-8')
4037
fix = json.loads(fix)
4138
# print(fix)
39+
4240
# 결과 값 후 처리
4341
total = 0
4442
total_words = ['Total', 'total', 'TOTAL', 'AMOUNT', 'Amount', 'AMT', 'Payment', 'payment']
@@ -56,69 +54,112 @@ def image_NAVER_AI(img_64):
5654
real.append(k['inferText'])
5755
if '$' in k['inferText'] or '.' in k['inferText'] or '・' in k['inferText'] or k['inferText'][-1] == 'T' or k['inferText'][-1] == '0':
5856
empty.append(k['inferText'])
57+
5958
# 후 처리 한 결과 값과 그렇지 않은 결과 값의 차이 비교
6059
# print(real)
6160
if len(real) - len(empty) > 6:
6261
print('안 들어온 값이 많습니다. 계속 진행하시겠습니까?')
62+
6363
# Key 값 후 처리
6464
receipt = {}
65-
receipt[empty[0]] = empty[1]
65+
receipt[empty[0]] = {'value': empty[1]}
6666
for i in range(2, len(empty)):
6767
if list(empty[i].split()[0])[0] in nums:
6868
s = empty[i].split()[0][1:]
6969
if list(empty[i].split()[-1])[-1] in nums or list(empty[i].split()[-1])[-1] == 'T':
70-
receipt[s + ' '.join(empty[i].split()[1:-1])] = empty[i].split()[-1]
70+
receipt[s + ' '.join(empty[i].split()[1:-1])] = {'value': empty[i].split()[-1]}
7171
else:
7272
if list(empty[i].split()[-1])[-1] in nums or list(empty[i].split()[-1])[-1] == 'T':
73-
receipt[' '.join(empty[i].split()[0:-1])] = empty[i].split()[-1]
74-
print(receipt)
73+
receipt[' '.join(empty[i].split()[0:-1])] = {'value': empty[i].split()[-1]}
74+
75+
7576
else:
7677
print("Error Code:" + rescode)
78+
79+
80+
# print(receipt)
81+
82+
7783
# 파파고 번역 시작
84+
7885
maerong = []
79-
receipt_trans = {}
80-
client_id = "NYfX1D8nruuEQC6_20Dk" # 개발자센터에서 발급받은 Client ID 값
81-
client_secret = "fN0cDTYHh8" # 개발자센터에서 발급받은 Client Secret 값
86+
87+
client_id = "AUXR0gR5NNTjtmnyWdDD" # 개발자센터에서 발급받은 Client ID 값
88+
client_secret = "u6hHsOvN5g" # 개발자센터에서 발급받은 Client Secret 값
8289
for key, val in receipt.items():
8390
if key == '':
84-
key = 'None'
85-
if val == '':
86-
val = 'None'
87-
if key == '3:33TOTAL':
88-
key = 'TOTAL'
91+
continue
8992
encText = urllib.parse.quote(key)
90-
encText2 = urllib.parse.quote(val)
9193
data = "source=en&target=ko&text=" + encText
92-
data2 = "source=en&target=ko&text=" + encText2
9394
url = "https://openapi.naver.com/v1/papago/n2mt"
9495
request = urllib.request.Request(url)
9596
request.add_header("X-Naver-Client-Id",client_id)
9697
request.add_header("X-Naver-Client-Secret",client_secret)
9798
response = urllib.request.urlopen(request, data=data.encode("utf-8"))
98-
response2 = urllib.request.urlopen(request, data=data2.encode("utf-8"))
9999
rescode = response.getcode()
100-
rescode2 = response2.getcode()
101100
if(rescode==200):
102101
response_body = response.read()
103102
fix2 = response_body.decode('utf-8')
104103
fix2 = json.loads(fix2)
105104
translated = fix2["message"]["result"]["translatedText"]
105+
106106
# 예외 처리
107107
if translated == '바꾸다':
108108
translated = '거스름 돈'
109+
109110
if translated == 'AMT':
110111
translated = '총'
112+
111113
if translated == '1/015:57p전체:':
112114
translated = '총'
113115
if translated == '지불':
114116
translated = '총'
115-
if(rescode2==200):
116-
response_body2 = response2.read()
117-
fix3 = response_body2.decode('utf-8')
118-
fix3 = json.loads(fix3)
119-
translated1 = fix3["message"]["result"]["translatedText"]
120-
receipt_trans[translated] = translated1
121-
else:
122-
print("Error Code:" + rescode)
123-
return (receipt, receipt_trans)
124-
# print(receipt_trans)
117+
if translated == '합계:':
118+
translated = '총'
119+
if translated == '전체:':
120+
translated = '총'
121+
122+
if key == '3:33TOTAL':
123+
translated = '총'
124+
125+
receipt[key]['ko'] = translated
126+
127+
128+
# print(receipt)
129+
130+
result = {
131+
132+
}
133+
134+
useless = ['Cash', 'Change', 'SF * Healthy Surcharge', 'Subtotal', 'Net TTL', '30VAT TTL', 'Credit/Debit', 'Tax', 'Sub-Total', '%GST', 'VISA', 'SUBTOTAL', 'TAX:', 'SUBTOTAL:', 'Subtotal:', 'Tax:']
135+
result['items'] = []
136+
for k, v in receipt.items():
137+
if k == 'place':
138+
result['place']= receipt[k]['value']
139+
140+
elif k == '':
141+
continue
142+
143+
elif v['ko'] == '총':
144+
result['total_price'] = receipt[k]['value']
145+
146+
elif k == '3:33TOTAL':
147+
result['total_price'] = receipt[k]['value']
148+
149+
elif k not in useless:
150+
bb = {}
151+
bb['ko'] = v['ko']
152+
if '$' in v['value']:
153+
v['value'] = v['value'].replace("$", "")
154+
v['value'] = float(v['value'])
155+
bb['value'] = v['value']
156+
else:
157+
bb['value'] = float(v['value'])
158+
result['items'].append(bb)
159+
160+
161+
162+
if '$' in result['total_price']:
163+
result['total_price'] = float(result['total_price'].replace("$", ""))
164+
165+
return result

Backend/accounts/urls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# path('login/', views.login),
1515
path('v1/details/<int:pk>/', views.save_expenditure), # 세부 항목 저장
1616
path('v1/receipt/', views.save_receipt), # 영수증 하나 저장
17-
path('v1/receipt/<int:pk>', views.decide_receipt),
17+
path('v1/receipt/<int:pk>/', views.decide_receipt),
1818
path('v1/receipts/<int:pk>/', views.get_receipts), # 영수증 전체 가져오기
1919
path('v1/schedule/<int:pk>/', views.get_schedule),
2020
path('v1/schedules/', views.get_schedules),

0 commit comments

Comments
 (0)