• 카테고리

    질문 & 답변
  • 세부 분야

    웹 개발

  • 해결 여부

    미해결

flask Method Not Allowed The method is not allowed for the requested URL오류발생

20.11.25 21:40 작성 조회수 519

0

항상 좋은 강의 감사합니다.

열씸히 따라 하다 웹앱 웹서비스 테스트 마지막 부분에서 flask main.py실행 하였으나 제목 처럼 오류가 발생했습니다.

main.py에서 methods는 ['POST']이고 host:127.0.0.1입니다. 

여기저기 구글링 검색해도 명확한 답이 나오지 않아 이렇게 질문 드립니다.

main.py 코드는 다음과 같습니다. 감사합니다

from flask import Flask, request, Response, jsonify
import base64
from flask_cors import CORS
import numpy as np
import cv2
from PIL import Image
from io import BytesIO

confthres=0.5 #confidence값 0.5이상 값만 찾음
nmsthres=0.1

app = Flask(__name__)
CORS(app)

@app.route('/mobileweb/yolo'methods=['POST']) #web주소 세팅
def main():
    labelsPath="./model/coco.names"
    configpath="./model/yolov3.cfg"
    weightspath="./model/yolov3.weights"
    print("[INFO] loading YOLO models...")
    LABELS = open(labelsPath).read().strip().split("\n")
    net = cv2.dnn.readNetFromDarknet(configpath, weightspath) #yolo file loading

  #image file -> ascii converter
    file = request.form['image']
    starter = file.find(',')
    image_data = file[starter+1:]
    image_data = bytes(image_data, encoding="ascii")
    img = Image.open(BytesIO(base64.b64decode(image_data)))

    #img = cv2.imread('./railworker.jpg')

    npimg=np.array(img)
    image=npimg.copy()
    image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
    (H, W) = image.shape[:2]

    ln = net.getLayerNames()
    ln = [ln[i[0] - 1for i in net.getUnconnectedOutLayers()] #yolo model적용시 사용

    blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416416),
                                 swapRB=Truecrop=False)
    net.setInput(blob)
    layerOutputs = net.forward(ln)

    boxes = []
    confidences = []
    classes = []
    results = []

    for output in layerOutputs:
        for detection in output:
            scores = detection[5:]
            classID = np.argmax(scores)
            confidence = scores[classID]

            if confidence > confthres: #50%이상값만 찾음
                box = detection[0:4] * np.array([W, H, W, H])
                (centerX, centerY, width, height) = box.astype("int")

                x = int(centerX - (width / 2))
                y = int(centerY - (height / 2))

                boxes.append([x, y, int(width), int(height)])
                confidences.append(float(confidence))
                classes.append({'id'int(classID), 'name': LABELS[classID]})

    idxs = cv2.dnn.NMSBoxes(boxes, confidences, confthres, nmsthres)

    if len(idxs) > 0:
        for i in idxs.flatten():
            results.append({'class': classes[i], 'confidence': confidences[i], 'bbox': boxes[i]}) 

    return jsonify(results)

if __name__ == '__main__':
    app.run(debug=Truehost='127.0.0.1')

답변 2

·

답변을 작성해보세요.

0

네~~감사합니다

0

안녕하세요?

코드만 봐서는 문제 없어보이는데 Client에서 POST방식으로 Request를 하신 것인지 플라스크에서 확인해 보세요.

가끔 Client에서 PUT방식을 사용하면 유사한 에러가 생깁니다.

감사합니다.