Python Flask: OpenCV library does not work, produces HTTP code 502 Bad Gateway when trying to compare images

0

Hello,

I'm having trouble with Python OpenCV library, running on AWS Lightsail container instance.

Some information:

  • It is running on python:3.7 Docker image.
  • Python Flask app
  • AWS Lightsail container instance
  • Using following packages: link
  • Uses opencv-contrib-python-headless==4.5.4.60 for OpenCV.
  • Error image: link

When trying to compare two images, I'm receiving HTTP status code of 502 Bad Gateway, which is very strange. Seems to work perfectly on my Windows machine locally, but on this Linux image it does not work.

`from cv2 import cv2 import logging logger = logging.getLogger()

def compare_two_images(image_to_compare_file, image_to_compare_against_file): # Image imports # Features logger.warning("image_to_compare_file " + image_to_compare_file) logger.warning("image_to_compare_against_file " + image_to_compare_against_file)

sift = cv2.SIFT_create() 
logger.warning("SIFT created " + str(sift is None))

# QueryImage
img1 = cv2.imread(image_to_compare_file, cv2.IMREAD_GRAYSCALE)   
logger.warning("IMG1 read created " + str(img1 is None))

# Find the key points and descriptors with SIFT
kp1, desc1 = sift.detectAndCompute(img1, None)
logger.warning("DETECT AND COMPUTE " + str(kp1 is None) + " " + str(desc1 is None))
    
img2 = cv2.imread(image_to_compare_against_file, cv2.IMREAD_GRAYSCALE)
logger.warning("IMG2 read created " + str(img2 is None))

kp2, desc2 = sift.detectAndCompute(img2, None)
logger.warning("DETECT AND COMPUTE " + str(kp2 == None) + " " + str(desc2 is None))

# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(desc1, desc2, k=2)

# Apply ratio test
good = []
for m, n in matches:
    if m.distance < 0.55 * n.distance:
        good.append([m])`

It crashes on kp1, desc1 = sift.detectAndCompute(img1, None)

and produces 502 Bad Gateway. Then, on some other endpoints I have in my Python Flask app, it produces 503 Service Temporarily Unavailable for a very times. After that, I can see that images were deleted.

Any help is appreciated.

  • Hi AWS-User-4298801, could you share the logs from the container? How do you know that is crashing on the sift.detectAndCompute(...) line? OpenCV is compute intensive, what is your container size on Lightsail?

  • Thank you for your reply. Here are the steps of how I can reproduce it:

    1. I have created user and a city in my Postgres DB.
    2. I have created a GIS marker.
    3. I have uploaded 1st image. 3.1 I have successfully found 1st image on my Linux instance.
    4. I have uploaded 2nd image. 5.1 I have successfully found 2nd image on my Linux instance.
    5. Last log was on logger.warning("IMG1 read created " + str(img1 is None)), then I received 500 Bad Gateway
    6. Now, GET GIS marker fails with 503 Service Temporarily Unavailable. This persists for 30 seconds or so and then I receive data again.
    7. At the end, I can see both image files deleted (from step 3 and 4) somehow.
  • Hi @AWS-User-4298801, please try to run your flask app in debug mode and kindly provide more detail around the error output. You could simply start your python app from an interactive terminal and view the logs while trying to compare the images.

  • Hi, I took your advice and pushed the project in dev mode to AWS, but it is the same result, but no extra logs were added. I can't use interactive terminal, since I'm using Lightsail container services, can't log in to the machine.

  • No logs after logger.warning("IMG1 read created " + str(img1 is None)). So I assume it crashes on detectAndCompute.

1 Answer
1
Accepted Answer

It is relevant to determine if the error 5XX originated from your load balancer or the instance: https://lightsail.aws.amazon.com/ls/docs/en_us/articles/understanding-instance-health-metrics-in-amazon-lightsail#understanding-load-balancer-metrics

To gather more information as to what is happening, I recommend you try the following:

  1. Ensure the relevant flask environment flags are set to output debug level logging:

You should be able to see an error traceback as shown here: https://flask.palletsprojects.com/en/2.0.x/debugging

You can also review the recorded logs in Lightsail as explained here: https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-viewing-container-service-container-logs

  1. As a test run your application with more RAM to see if an out-of-memory error crashes your application. https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-viewing-container-services-metrics

  2. Investigate the health status reported by your container. The container service may have specific health requirements that are monitored (e.g. response time). If this metric determines that the service is unhealthy, I expect it will restart the service for you. You can find more information about the health monitoring options here: https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-container-services-deployments

AWS
Chris
answered 2 years ago
  • I'm accepting either way because you took a long time with me, will try to see what the underlying issues is. Maybe even Amazon Rekognition is better, since I don't really want to spend much money on this school project.

  • Hi Chris, I have tried your suggestions 1. I have created a new flask app with the debug option now properly set up, like in your 1. https://flask.palletsprojects.com/en/2.0.x/debugging , but it didn't returned anything, I got 502 bad gateway instantly. 2. Will try to check this out. 3. I have updated health options, but no noticable impact. Link: https://ibb.co/xYmtjWX

  • Hey, I have also tried 2. option, but nothing has changed, same bad gateway error.

  • If you can't get flask to display more debug output here, it's likely that the error 502/503 you are seeing is not generated flask, but a load balancer that sits before your application and can't route traffic to the desired service. Therefore option 3 and the configuration around the network connectivity and health monitoring of your service should be evaluated. Often short time-outs in combination with synchronous request handling lead to services being considered stale.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions