Автоматическая обрезка изображений в Python на сайте - PullRequest
0 голосов
/ 28 октября 2019

Я использую следующий исходный код для автоматической обрезки изображения. Я получаю эту ошибку: AttributeError: у объекта 'NoneType' нет атрибута 'shape'

Сервер обнаружил внутреннюю ошибку и не смог выполнить ваш запрос. Либо сервер перегружен, либо в приложении произошла ошибка.

Я новичок в python, Может ли кто-нибудь помочь мне выполнить эту задачу

    @app.route('/crop', methods=['GET', 'POST'])
    def crop_page():
        if request.method == 'POST':
            # check if the post request has the file part
            if 'file' not in request.files:
                return render_template('crop.html', msg='No file selected')
            file = request.files['file']
            # if user does not select file, browser also
            # submit a empty part without filename
            if file.filename == '':
                return render_template('crop.html', msg='No file selected')

            if file and allowed_file(file.filename):
                # file.save(os.path.join(os.getcwd() + UPLOAD_FOLDER, file.filename))

                # call the OCR function on it

                # Load the image in black and white (0 - b/w, 1 - color).
                img = cv2.imread(file.filename)

                # Get the height and width of the image.
                h, w = img.shape[:2]

                # Invert the image to be white on black for compatibility with findContours function.
                imgray = 255 - img

                # Binarize the image and call it thresh.
                ret, thresh = cv2.threshold(imgray, 127, 255, cv2.THRESH_BINARY)

                # Find all the contours in thresh. In your case the 3 and the additional strike
                contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

                # Calculate bounding rectangles for each contour.
                rects = [cv2.boundingRect(cnt) for cnt in contours]

                # Calculate the combined bounding rectangle points.
                top_x = min([x for (x, y, w, h) in rects])
                top_y = min([y for (x, y, w, h) in rects])
                bottom_x = max([x + w for (x, y, w, h) in rects])
                bottom_y = max([y + h for (x, y, w, h) in rects])

                # Draw the rectangle on the image
                out = cv2.rectangle(img, (top_x, top_y), (bottom_x, bottom_y), (0, 255, 0), 2)

                # get the thresholded crop
                crop = out[top_y:top_y + h, top_x:top_x + w]
                ret, thresh = cv2.threshold(crop, 127, 255, cv2.THRESH_BINARY)
                cv2.imwrite(file.save(os.path.join(os.getcwd() + UPLOAD_FOLDER, file.filename)), thresh)

                # extract the text and display it
                return render_template('crop.html',
                                       msg='Successfully processed',
                                       imgsrc = UPLOAD_FOLDER + file.filename)
        elif request.method == 'GET':
            return render_template('crop.html')

<!DOCTYPE html>
<html>
 <head>
   <title>QuikCapture</title>
       <!-- add icon link -->
     <link rel = "icon" href =
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png"
        type = "image/x-icon">
 </head>
 <body>

   {% if msg %}
   <h1>{{ msg }}</h1>
   {% endif %}

   <h1>Upload new File</h1>

   <form method=post enctype=multipart/form-data>
     <p><input type=file name=file>
        <input type=submit value=Upload>
   </form>

   <h1>Result:</h1>
   {% if img_src %}
     <img height="500" width="1000" src="{{ img_src }}">
   {% endif %}

 </body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...