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.

1 Answer
0

Based on the error, it seems like you ALB is unsure how to process the connection. How is your ALB configured?

While 5XX errors do appear to be commonplace for this library, I believe your issue is related to load balancer configuration more than anything.

AWS
Dan_F
answered 2 years ago

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