opencvを用いたWEBアプリをエックスサーバーで稼働するとメモリエラーが発生する

実現したいこと

opencvで特徴量マッチングを行うflaskのWEBアプリをエックスサーバー上で作成したい

発生している問題・分からないこと

ローカルの仮想環境で問題なく動作するflaskコードをエックスサーバー上で公開すると、以下のエラーが発生してしまいます。

エックスサーバーのプランはプレミアムでメモリにはかなり余裕があるように思うのですが、なぜこのようなエラーが発生するのでしょうか。

An error occurred: OpenCV(4.9.0) /io/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 702032 bytes in function 'OutOfMemoryError'

よろしくお願いします。

該当のソースコード

@app.route('/') def home(): try: import cv2 #画像を読み込む #画像1, 2 img1 = cv2.imread('image_1.png') img2 = cv2.imread('image_2.png') #AKAZE検出器の生成 akaze = cv2.AKAZE_create() #画像を読み込んで特徴量を計算する #kp = keypoints(特徴点), des = descriptors(特徴点描画) #detectAndCompute() => (kp: 特徴点の一覧, des:獲得頂点の特徴量記述子)のタプル kp1, des1 = akaze.detectAndCompute(img1, None) kp2, des2 = akaze.detectAndCompute(img2, None) #特徴量のマッチングを行う #総当たりマッチング(Brute-Force Matcher)を生成 bf = cv2.BFMatcher() #特徴量ベクトル同士をBrute-ForceとKNNでマッチング matches = bf.knnMatch(des1, des2, k=2) #データをマッチングの精度が高いもののみ抽出する ratio = 0.6 good = [] for m, n in matches: if m.distance < ratio * n.distance: good.append([m]) #対応する特徴点同士を描画する img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, flags = 2) #画像表示 #cv2.imshow('img',img3) #画像保存 cv2.imwrite('img_3.jpg',img3) #キーを押したら終了する #cv2.waitKey(0) cv2.destroyAllWindows() #以下のコードでエラーの詳細が分かる!!!! except Exception as e: return f"An error occurred: {str(e)}" return render_template('home.html')

試したこと・調べたこと

上記の詳細・結果

単純に実行した場合は、以下の大まかなエラー表示だけだったため、
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

以下のコードを追加した結果、メモリのエラーであることが分かりました。
except Exception as e:
return f"An error occurred: {str(e)}"

補足

特になし

コメントを投稿

0 コメント