본문 바로가기
Python/Machine Learning

Til - 26day

by K-밍키 2022. 5. 21.

이어서 파일을 업로드 해보자.


main.py : index.html 에서 받은 파일을 서버로 올리기

# detector 폴더의 detect.py의 detect_run 임포트
from detector.detect import detect_run

bp = Blueprint("main", __name__, url_prefix="/main")


@bp.route('/api/img/upload', methods=['POST'])
def file_upload():
    # 업로드 파일 받아오기.
    file = request.files['file'] # werkzeug.datastructures.FileStorage, name
    extension = secure_filename(file.filename).split('.')[-1] # file.filename /
    f_name = file.filename.replace('.' + extension, '') # test1, 확장자 제거

    # 파일 이름 , Local에 Upload 한 이미지 저장
    today = datetime.now()
    today = today.strftime('%Y-%m-%d-%H-%M-%S')
    filename = f_name + '-' + today # test1-2022-05-19-14-43-23.jpg
    upload_path = 'static/upload_data/' + filename + '.' + extension
    file.save(upload_path) # 'static/upload_data/test1.jpg
    
    # detect_run의 결과를 각각에 배정
    predict_path, results = detect_run(WEIGHTS_PATH, upload_path, False)
    predict_path = predict_path + '/' + filename + '.' + extension
    
    # DB에 Image Path 저장
    doc = {
        'id': 'id',
        'company': 'samsung',
        'helmet': int(results['helmet']),
        'head': int(results['head']),
        'score': float(results['score']),
        'isPass': bool(results['isPass']),
        'date': today,
        'upload_path': upload_path,
        'predict_path': predict_path
    }
    db.result.insert_one(doc)
    
    return jsonify({'result':'success', 'predict_path': predict_path, 'results':results})

질문할 내용

# Directories
    save_dir = increment_path(Path(project) / name, exist_ok=exist_ok)  # increment run
    (save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True)  # make dir

'Python > Machine Learning' 카테고리의 다른 글

Til - 27day  (0) 2022.05.23
Wil - 5week  (0) 2022.05.22
Til - 25day  (0) 2022.05.21
Til - 24day  (0) 2022.05.19
Til - 23day  (0) 2022.05.17