views.py 910 B

123456789101112131415161718192021222324
  1. from cv2 import cv2
  2. from django.views import View
  3. from django import http
  4. from . import ocrModel
  5. import numpy as np
  6. class InvoiceOCRView(View):
  7. def post(self, request):
  8. images = request.FILES.getlist('images', '')
  9. if images == '':
  10. return http.JsonResponse({'code': 404, 'results': '请上传图片!!!'})
  11. invoice_type = int(request.POST.get('invoice_type', 1))
  12. if invoice_type not in (1, 2):
  13. return http.JsonResponse({'code': 404, 'results': '输入正确的参数!!!'})
  14. image_list = []
  15. for image in images:
  16. img = np.fromstring(image.read(), np.uint8)
  17. img = cv2.imdecode(img, cv2.IMREAD_COLOR)
  18. image_list.append(img)
  19. ocr = ocrModel
  20. res = ocr.predict(images=image_list, invoice_type=invoice_type)
  21. # print(res)
  22. return http.JsonResponse({'code': 200, 'results': res})