0

I'm trying to read a png file and output the numpy matrix of the image in terminal using imread function of opencv on the server like this

import cv2
from flask import Flask
import os

@application.route('/readImage',methods=['POST'])
def handleHTTPPostRequest():
    imagePath = f'{os.getcwd()}/input.png'
    print('image path is', imagePath)
    print(cv2.__version__)
    im = cv2.imread(imagePath,cv2.IMREAD_COLOR)
    print(im)
    return 'success'

This is giving expected output on my local machine(Ubuntu 18.04) no matter howmany times I execute it. I moved this to elastic beanstalk(CentOS) with necessary setup. The request runs fine(gives proper logs along with success) the very first time I make a post call. But when I make the post call second time, it's only outputting first two logs(imagepath and cv2 version) and is stuck there for a while. and after sometime, it's showing this error

End of script output before headers: application.py

I have added one more line just before cv2.imread just to make sure that the file exists

print('does the file exists',os.path.isfile(imagePath) )

This is returning true everytime. I have restarted the server multiple times, looks like it only works the very first time and cv2.imread() is stuck after the first post call.What am I missing

Saikiran
  • 756
  • 2
  • 11
  • 29

3 Answers3

0

When you print from a request handler, Flask tries to do something sensible, but print really isn't what you want to be doing, as it risks throwing the HTTP request/response bookkeeping off.

A fully-supported way of getting diagnostic info out of a handler is to use the logging module. It will require a small bit of configuration. See http://flask.pocoo.org/docs/1.0/logging/

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • Thanks for letting me know. Do you think removing all the print statements will prevent the issue – Saikiran Jul 01 '18 at 18:01
  • The reason I'm asking is because this is only affecting the opencv imread function and that too only after the first request – Saikiran Jul 01 '18 at 18:02
  • Try removing the print statements. It might appear to that `.imread()` is the problem, but it could be the next attempt to print. – Dave W. Smith Jul 01 '18 at 19:15
  • If you comment out the cv2.imread() line (and the print below it), you'll get some indication of whether cv2 has anything to do with the issue. – Dave W. Smith Jul 02 '18 at 16:34
0

To anyone facing this issue, I have found a solution. Add this to your ebextensions config file

container_commands:  
  AddGlobalWSGIGroupAccess: 
    command: "if ! grep -q 'WSGIApplicationGroup %{GLOBAL}' ../wsgi.conf ; then echo 'WSGIApplicationGroup %{GLOBAL}' >> ../wsgi.conf; fi;"
Saikiran
  • 756
  • 2
  • 11
  • 29
0

Saikiran's final solution worked for me. I was getting this issue when I tried calling methods from the opencv-python library. I'm running Ubuntu 18.04 locally and it works fine there. However, like Saikiran's original post, when deployed to Elastic Beanstalk the first request works and then the second one does not. For my EB environment, I'm using a Python3.6-based Amazon Linux server.

Matt
  • 13
  • 4