63 lines
2.1 KiB
Python
63 lines
2.1 KiB
Python
# from django.shortcuts import render
|
|
from django.http import HttpResponse
|
|
from django.views.decorators.http import require_http_methods
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.http import JsonResponse
|
|
|
|
from PIL import Image
|
|
import cv2
|
|
import numpy as np
|
|
from ocr_module.main import img_to_products, img_to_products_debug
|
|
|
|
from .forms import UploadFileForm
|
|
|
|
|
|
# Create your views here.
|
|
def index(request):
|
|
return HttpResponse("Hello, world!")
|
|
|
|
|
|
@require_http_methods(["POST"])
|
|
@csrf_exempt
|
|
def upload_bill(request):
|
|
form = UploadFileForm(request.POST, request.FILES)
|
|
if not form.is_valid():
|
|
return HttpResponse("Invalid form...")
|
|
img = Image.open(request.FILES['image'])
|
|
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
|
product_list = img_to_products(img)
|
|
products = [{'name': prod, 'price': pri} for _, prod, pri in product_list]
|
|
response = JsonResponse({'products': products})
|
|
return response
|
|
|
|
|
|
@require_http_methods(["POST"])
|
|
@csrf_exempt
|
|
def upload_bill_debug_img(request):
|
|
form = UploadFileForm(request.POST, request.FILES)
|
|
if not form.is_valid():
|
|
return HttpResponse("Invalid form...")
|
|
img = Image.open(request.FILES['image'])
|
|
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
|
_, _, img_out_bgr = img_to_products_debug(img)
|
|
|
|
img_out_rgb = cv2.cvtColor(img_out_bgr, cv2.COLOR_BGR2RGB)
|
|
pil_img_out = Image.fromarray(img_out_rgb)
|
|
response = HttpResponse(content_type='image/jpg')
|
|
pil_img_out.save(response, "JPEG")
|
|
return response
|
|
|
|
|
|
@require_http_methods(["POST"])
|
|
@csrf_exempt
|
|
def upload_bill_debug_text(request):
|
|
form = UploadFileForm(request.POST, request.FILES)
|
|
if not form.is_valid():
|
|
return HttpResponse("Invalid form...")
|
|
img = Image.open(request.FILES['image'])
|
|
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
|
product_list, full_text, _ = img_to_products_debug(img)
|
|
products = [{'name': prod, 'price': pri} for _, prod, pri in product_list]
|
|
response = JsonResponse({'products': products, 'text': full_text})
|
|
return response
|