我目前正在使用OpenCV和PyTesseract从指定的ROI读取图像。这是我经历过的事情:

import pytesseract
import urllib
import math
import cv2
import numpy as np
from unidecode import unidecode
from PIL import Image

def process_image(path=None):
    if path != None:
        original_image = cv2.imread(path)
        original_height, original_width = original_image.shape[:2]
        image = cv2.resize(original_image, (640, int(math.floor(original_height*640/original_width))))
        cv2.imwrite('./uploads/resized_image.jpg', image)
        height, width = image.shape[:2]

    else:
        return "Terjadi kesalahan, harap hubungi administrator."

    print(height)
    print(width)
    coord = [
        [height - 84, 34, height, 624] #Barcode
        # [378, 415, 364, 583], #Tanggal Lahir
        # [605, 656, 32, 992]
    ]

    result = None

    print ("Recognizing...")
    for x in coord:
        roi = image[x[0]:x[1],x[2]:x[3]]
        print(x[0])
        print(x[1])
        print(x[2])
        print(x[3])
        gray = cv2.cvtColor(roi,cv2.COLOR_RGB2GRAY)
        ret2,th2 = cv2.threshold(gray,128,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
        dst = cv2.fastNlMeansDenoising(th2,10,10,7)
        cv2.imwrite('./uploads/tmp.jpg',dst)
        cao = Image.open('./uploads/tmp.jpg')
        rec_string = pytesseract.image_to_string(cao,lang='deu')
        if result != None:
            result += "\n" + rec_string
        else:
            result = rec_string

    print ("the result is {}".format(result))
    return result

在这里,您可以看到它得到一个图像,将其调整为一定大小,然后将图像写入名为resized_image.jpg的新图像中。然后,我尝试根据基于高度的坐标创建ROI。看一下坐标数组,您可以看到我正在尝试通过从高-84到高,以及从34到624的宽度获取坐标来获取底部组件。

事实证明,如果值正确,我会打印出“正在确认...”文本。是的,但是图像不会被处理,并且会弹出此错误:
File ./uploads/24-07-2018/aenkjtWIBYuTZsaU.jpg telah diupload
903
640
Recognizing...
819
34
903
624
OpenCV(3.4.1) Error: Assertion failed (!_src.empty()) in fastNlMeansDenoising, file /opt/conda/conda-bld/opencv-suite_1527005194613/work/modules/photo/src/denoising.cpp, line 119
[2018-07-24 13:51:29,900] ERROR in app: Exception on /api/upload [POST]
Traceback (most recent call last):
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/dennydap/Projects/flask-ocr/server.py", line 55, in upload_file
    rec_string = process_image(path=path)
  File "/home/dennydap/Projects/flask-ocr/pre_img.py", line 39, in process_image
    dst = cv2.fastNlMeansDenoising(th2,10,10,7)
cv2.error: OpenCV(3.4.1) /opt/conda/conda-bld/opencv-suite_1527005194613/work/modules/photo/src/denoising.cpp:119: error: (-215) !_src.empty() in function fastNlMeansDenoising

OpenCV(3.4.1) /opt/conda/conda-bld/opencv-suite_1527005194613/work/modules/photo/src/denoising.cpp:119: error: (-215) !_src.empty() in function fastNlMeansDenoising

127.0.0.1 - - [24/Jul/2018 13:51:29] "POST /api/upload HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/werkzeug/serving.py", line 270, in run_wsgi
    execute(self.server.app)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/werkzeug/serving.py", line 258, in execute
    application_iter = app(environ, start_response)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1748, in handle_exception
    return self.finalize_request(handler(e), from_error_handler=True)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1831, in finalize_request
    response = self.make_response(rv)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1957, in make_response
    'The view function did not return a valid response. The'
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

如果我使用的是精确数字,而不是高度-84,则不会显示错误。我做错什么了?

最佳答案

事实证明,定义ROI是错误的。我以为是 roi = image [y1:x1,y2:x2] ,但快速搜索后,它是 roi = image [y1:y2,x1:x2] 。应该已经入睡了。

关于python - 在数组中使用计算值时,Python cv2.error:OpenCV(3.4.1)错误:(-215)!_src.empty()在fastNlMeansDenoising函数中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51492385/

10-13 00:28