Menu



Manage

Cord > Project_AI이미지 처리 전체 다운로드
Project_AI이미지 처리 > venv/app.py Lines 302 | 9.7 KB
다운로드

                        import install # 실행에 필요한 프로그램이 없는경우 자동 설치

# 설치 목록 파일의 경로
requirements_file = 'requirements.txt'

# 경로에 있는 패키지 설치
install.install_from_requirements(requirements_file)

# 이후 코드에서 패키지 사용 가능
from flask import Flask, render_template, request, redirect, url_for, jsonify, session, send_from_directory
from flask_wtf import FlaskForm
import pymysql
import os
import shutil

#아래는 파일 불러오기
from login import login, LoginForm, username, byebye
from join import signup_s, SignupForm
from nukki import process_image, generate_result_filename, get_result_image_path, save_uploaded_file, process_gray, process_color, process_sizer
from facetask import process_face

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key' # Flask-WTF 사용 시 필요

# MySQL 연결 설정
mydb = pymysql.connect(
  host="localhost",
  user="-",
  password="-",
  database="-",
  charset="-" #5.0버전으로 지원 안하는 utf8mb4 대신 utf8 로 변경
)

@app.route('/', methods=['GET', 'POST'])
def index():
    form = LoginForm()
    return login(form, mydb)

@app.route('/signup', methods=['GET', 'POST'])
def signup():
    form = SignupForm()
    return signup_s(form, mydb)

@app.route('/welcome')  
def welcome():
    if 'user_id' in session: # 세션에 사용자 ID가 있는지 확인하여 로그인 확인
        user_id = session['user_id']
        user_name = username(mydb)
        return render_template('welcome.html', user_name=user_name)
    else: #로그인이 안된 경우
        return redirect(url_for('index'))

@app.route('/nukki')
def nukki():
    if 'user_id' in session:
        user_name = username(mydb)
        return render_template('nukki.html', user_name=user_name)
    else:
        return redirect(url_for('index'))

@app.route('/gray')
def gray():
    if 'user_id' in session:
        user_name = username(mydb)
        return render_template('gray.html', user_name=user_name)
    else:
        return redirect(url_for('index'))

@app.route('/color')
def color():
    if 'user_id' in session:
        user_name = username(mydb)
        return render_template('color.html', user_name=user_name)
    else:
        return redirect(url_for('index'))

@app.route('/upscale')
def upscale():
    if 'user_id' in session:
        user_name = username(mydb)
        return render_template('upscale.html', user_name=user_name)
    else:
        return redirect(url_for('index'))

@app.route('/facechange')
def facechange():
    if 'user_id' in session:
        user_name = username(mydb)
        return render_template('facechange.html', user_name=user_name)
    else:
        return redirect(url_for('index'))

@app.route('/userout')
def userout():
    byebye(mydb)
    session.clear()
    return redirect(url_for('index'))

@app.route('/logout')
def logout():
    session.clear()
    return redirect(url_for('index'))

#파일 처리
UPLOAD_FOLDER = './venv/uploads'
RESULT_FOLDER = './venv/static/results'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['RESULT_FOLDER'] = RESULT_FOLDER

if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)
if not os.path.exists(RESULT_FOLDER):
    os.makedirs(RESULT_FOLDER)

@app.route('/nukki_task', methods=['GET', 'POST'])
def nukki_task():

    user_name = username(mydb)
    result_image = None

    #결과 폴더 초기화
    if os.path.exists(RESULT_FOLDER):
        shutil.rmtree(RESULT_FOLDER)
    os.makedirs(RESULT_FOLDER)

    if request.method == 'POST':
        if 'image' not in request.files:
            return '파일이 없습니다.'
        file = request.files['image']
        if file.filename == '':
            return '파일을 선택해주세요.'
        if file:
            upload_path = save_uploaded_file(app.config['UPLOAD_FOLDER'], file)

            result_filename = generate_result_filename(file.filename)
            result_path = get_result_image_path(app.config['RESULT_FOLDER'], result_filename)

            model_name = request.form.get('model', 'birefnet-hrsod')  # 정보가 없는경우 모델 설정
            print(f"{model_name} 모델명 사용중입니다.")
            process_image(upload_path, result_path, model_name)

            result_image = '../static/results/' + result_filename

        #업로드 폴더 초기화
        if os.path.exists(UPLOAD_FOLDER):
            shutil.rmtree(UPLOAD_FOLDER)
        os.makedirs(UPLOAD_FOLDER)

    return render_template('nukki.html', result_image=result_image, user_name=user_name)

@app.route('/results/<filename>')
def serve_result_image(filename):
    return send_from_directory(app.config['RESULT_FOLDER'], filename)


@app.route('/gray_task', methods=['GET', 'POST'])
def gray_task():

    user_name = username(mydb)
    result_image = None

    #결과 폴더 초기화
    if os.path.exists(RESULT_FOLDER):
        shutil.rmtree(RESULT_FOLDER)
    os.makedirs(RESULT_FOLDER)

    if request.method == 'POST':
        if 'image' not in request.files:
            return '파일이 없습니다.'
        file = request.files['image']
        if file.filename == '':
            return '파일을 선택해주세요.'
        if file:
            upload_path = save_uploaded_file(app.config['UPLOAD_FOLDER'], file)

            result_filename = generate_result_filename(file.filename)
            result_path = get_result_image_path(app.config['RESULT_FOLDER'], result_filename)

            process_gray(upload_path, result_path)

            result_image = '../static/results/' + result_filename

        #업로드 폴더 초기화
        if os.path.exists(UPLOAD_FOLDER):
            shutil.rmtree(UPLOAD_FOLDER)
        os.makedirs(UPLOAD_FOLDER)

    return render_template('gray.html', result_image=result_image, user_name=user_name)


@app.route('/color_task', methods=['GET', 'POST'])
def color_task():

    user_name = username(mydb)
    result_image = None

    #결과 폴더 초기화
    if os.path.exists(RESULT_FOLDER):
        shutil.rmtree(RESULT_FOLDER)
    os.makedirs(RESULT_FOLDER)

    if request.method == 'POST':
        if 'image' not in request.files:
            return '파일이 없습니다.'
        file = request.files['image']
        if file.filename == '':
            return '파일을 선택해주세요.'
        if file:
            upload_path = save_uploaded_file(app.config['UPLOAD_FOLDER'], file)

            result_filename = generate_result_filename(file.filename)
            result_path = get_result_image_path(app.config['RESULT_FOLDER'], result_filename)

            process_color(upload_path, result_path)

            result_image = '../static/results/' + result_filename

        #업로드 폴더 초기화
        if os.path.exists(UPLOAD_FOLDER):
            shutil.rmtree(UPLOAD_FOLDER)
        os.makedirs(UPLOAD_FOLDER)

    return render_template('color.html', result_image=result_image, user_name=user_name)

@app.route('/sizer_task', methods=['GET', 'POST'])
def sizer_task():

    user_name = username(mydb)
    result_image = None

    #결과 폴더 초기화
    if os.path.exists(RESULT_FOLDER):
        shutil.rmtree(RESULT_FOLDER)
    os.makedirs(RESULT_FOLDER)

    if request.method == 'POST':
        if 'image' not in request.files:
            return '파일이 없습니다.'
        file = request.files['image']
        if file.filename == '':
            return '파일을 선택해주세요.'
        if file:
            upload_path = save_uploaded_file(app.config['UPLOAD_FOLDER'], file)

            result_filename = generate_result_filename(file.filename)
            result_path = get_result_image_path(app.config['RESULT_FOLDER'], result_filename)

            process_sizer(upload_path, result_path)

            result_image = '../static/results/' + result_filename

        #업로드 폴더 초기화
        if os.path.exists(UPLOAD_FOLDER):
            shutil.rmtree(UPLOAD_FOLDER)
        os.makedirs(UPLOAD_FOLDER)

    return render_template('upscale.html', result_image=result_image, user_name=user_name)

@app.route('/face_task', methods=['GET', 'POST'])
def face_task():

    user_name = username(mydb)
    result_image = None

    #결과 폴더 초기화
    if os.path.exists(RESULT_FOLDER):
        shutil.rmtree(RESULT_FOLDER)
    os.makedirs(RESULT_FOLDER)

    if request.method == 'POST':
        if 'image1' not in request.files:
            return "1파일이 없습니다."
        if 'image2' not in request.files:
            return "2파일이 없습니다."

        file1 = request.files['image1']
        file2 = request.files['image2']

        if file1.filename == '' or file2.filename == '':
            return "파일을 선택해주세요."
        
        if file1 and file2:
            img1_path = save_uploaded_file(app.config['UPLOAD_FOLDER'], file1)
            img2_path = save_uploaded_file(app.config['UPLOAD_FOLDER'], file2)
            
            result_filename = generate_result_filename(file1.filename)
            result_path = get_result_image_path(app.config['RESULT_FOLDER'], result_filename)

            if process_face(img1_path, img2_path, result_path):
                result_image = '../static/results/' + result_filename
            else:
                return "얼굴 검출에 실패했습니다."

        #업로드 폴더 초기화
        if os.path.exists(UPLOAD_FOLDER):
            shutil.rmtree(UPLOAD_FOLDER)
        os.makedirs(UPLOAD_FOLDER)

    return render_template('facechange.html', result_image=result_image, user_name=user_name)

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