항상 좋은 강의 감사합니다.
열씸히 따라 하다 웹앱 웹서비스 테스트 마지막 부분에서 flask main.py실행 하였으나 제목 처럼 오류가 발생했습니다.
여기저기 구글링 검색해도 명확한 답이 나오지 않아 이렇게 질문 드립니다.
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] - 1] for i in net.getUnconnectedOutLayers()] #yolo model적용시 사용
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416),
swapRB=True, crop=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=True, host='127.0.0.1')