#6 모자이크할 파일 upload 및 download 기능 구현

saiy17·2021년 9월 7일

Face Out

목록 보기
6/6
post-thumbnail

1. 파일 upload 기능 구현

# main.html과 mosaic_process.html에서 버튼 클릭 시 파일 업로드 처리
@app.route('/save_file', methods = ['GET', 'POST'])
def upload_file():
    d_path = request.args.get('path','경로')
    image_extenstion = ['ai', 'bmp', 'jpeg', 'jpg', 'jpe', 'jfif', 'jp2', 'j2c', 'pcx', 'psd', 'tga', 'taga',
                        'png', 'tif', 'tiff']
    video_extenstion = ['mp4', 'm4v', 'avi', 'wmv', 'mwa', 'asf', 'mpg', 'mpeg', 'ts', 'mkv', 'mov', '3gp', '3g2','gif',
                        'webm']
    if request.method == 'POST':
        # 모자이크에서 제외할 이미지
        f_input = request.files['file1']
        # 모자이크 할 파일 (단체사진)
        f_output = request.files['file2']

        if f_input.filename == '':
            # 모자이크에서 제외할 이미지가 없을 때 전체 인물 모자이크 처리코드
            downloads_path = './static/downloads'
            d_path = './downloads/'
            filename2 = f_output.filename

            file_extenstion = filename2.split(".")[-1]
            filename = filename2

            if file_extenstion in image_extenstion:  # 단체 사진이 이미지 파일인 경우,
                if session.get('u_id'):
                    info = db_session.query(User).filter_by(u_id=session['u_id']).first()
                    filename = info.u_id + '-' + filename2
                    downloads_path = './static/member_img_downloads'
                    d_path = './member_img_downloads/'

                f_output.save('./static/output_uploads/' + secure_filename(filename2))  # 모자이크 할 image file (단체사진)
                path2 = './static/output_uploads/' + filename2

                img=face_recog_imageOnly.input(path2)
                img=face_recog_imageOnly.process(img)
                face_recog_imageOnly.save(downloads_path, img, filename)
                d_path=d_path+filename
                return render_template('/user_templates/save_file.html', filename=filename, path=d_path)

            elif file_extenstion in video_extenstion:   # 단체 사진이 비디오 파일인 경우,
                if session.get('u_id'):
                    info = db_session.query(User).filter_by(u_id=session['u_id']).first()
                    filename = info.u_id + '-' + filename2
                    downloads_path = './static/member_video_downloads'

                f_output.save('./static/output_uploads/' + secure_filename(filename2))  # 모자이크 할 video file (단체사진)
                path2 = './static/output_uploads/' + filename2
                filename=filename.split(".")[0] + '.mp4'
                img = face_recog_videoOnly.input(path2)
                downloads_path = downloads_path + '/' + filename
                fn = filename.split(".")[0] + ".jpg"
                d_path = './thumnail/' + fn
                d_path2 = './static/thumnail/' + fn
                writer = face_recog_videoOnly.process(img, downloads_path, d_path2)
                face_recog_videoOnly.save(writer)

                return render_template('/user_templates/save_file.html', filename=filename, path=d_path)

        elif f_input.filename != '' and f_output.filename != '':    # 전체 이미지가 잘 들어온 경우, f_input 제외한 모든 대상 모자이크 처리
            downloads_path = './static/downloads'
            d_path = './downloads/'

            filename1 = f_input.filename    # 모자이크에서 제외할 이미지
            filename2 = f_output.filename   # 모자이크 할 image file (단체사진)

            file_extenstion = filename2.split(".")[-1]
            filename = filename2

            if file_extenstion in image_extenstion:     # 단체 사진이 이미지 파일인 경우,
                if session.get('u_id'):
                    info = db_session.query(User).filter_by(u_id=session['u_id']).first()
                    filename = info.u_id + '-' + filename2
                    downloads_path = './static/member_img_downloads'
                    d_path = './member_img_downloads/'

                f_input.save('./static/input_uploads/' + secure_filename(filename1))  # 모자이크에서 제외할 이미지
                f_output.save('./static/output_uploads/' + secure_filename(filename2))  # 모자이크 할 image file (단체사진)
                path1 = './static/input_uploads/' + filename1
                path2 = './static/output_uploads/' + filename2

                imgTrain = face_recog_image.input1(path1)  # 모자이크에서 제외할 이미지
                imgTest = face_recog_image.input2(path2)  # 모자이크 할 image file (단체사진)
                imgTest = face_recog_image.process(imgTest, imgTrain)
                face_recog_image.save(imgTest, downloads_path, filename)
                d_path = d_path + filename
                return render_template('/user_templates/save_file.html', filename=filename, path=d_path)

            elif file_extenstion in video_extenstion:   # 단체 사진이 비디오 파일인 경우,
                if session.get('u_id'):
                    info = db_session.query(User).filter_by(u_id=session['u_id']).first()
                    filename = info.u_id + '-' + filename2
                    downloads_path = './static/member_video_downloads'

                f_input.save('./static/input_uploads/' + secure_filename(filename1))  # 모자이크에서 제외할 이미지
                f_output.save('./static/output_uploads/' + secure_filename(filename2))  # 모자이크 할 video file (단체사진)
                path1 = './static/input_uploads/' + filename1
                path2 = './static/output_uploads/' + filename2
                filename=filename.split(".")[0] + '.mp4'

                img = face_recog_video.input1(path1)  # 모자이크에서 제외할 이미지
                cap = face_recog_video.input2(path2)  # 모자이크 할 video file (단체사진)
                downloads_path = downloads_path + '/' + filename
                fn = filename.split(".")[0] + ".jpg"
                d_path = './thumnail/' + fn
                d_path2 = './static/thumnail/'+fn
                writer = face_recog_video.process(cap, img, downloads_path, d_path2)
                face_recog_video.save(writer)
                return render_template('/user_templates/save_file.html', filename=filename, path=d_path)

            else:   # 지원하지 않는 형식
                flash('지원하지 않는 확장자입니다.')
                return render_template('/user_templates/main.html')
        else:
            # 그 이외의 모든 경우
            flash('모자이크 할 파일을 업로드 해주세요')
            return render_template('/user_templates/main.html')

2. 파일 download 기능 구현

# save_file.html에서 파일 다운로드 버튼 눌렀을 때 처리
@app.route('/download_file', methods=['GET', 'POST'])
def download_file():
    image_extenstion = ['ai', 'bmp', 'jpeg', 'jpg', 'jpe', 'jfif', 'jp2', 'j2c', 'pcx', 'psd', 'tga', 'taga',
                        'png', 'tif', 'tiff']
    video_extenstion = ['mp4', 'm4v', 'avi', 'wmv', 'mwa', 'asf', 'mpg', 'mpeg', 'ts', 'mkv', 'mov', '3gp', '3g2','gif',
                        'webm']

    # 파일 이름 받아옴
    filename = request.args.get('filename', '파일이름')
    file_extenstion = filename.split(".")[-1]
    downloads_path = './static/downloads/'

    if file_extenstion in image_extenstion:  # 단체 사진이 이미지 파일인 경우,
        if session.get('u_id'):
            downloads_path = './static/member_img_downloads/'

    elif file_extenstion in video_extenstion:  # 단체 사진이 비디오 파일인 경우,
        if session.get('u_id'):
            downloads_path = './static/member_video_downloads/'
    else:
        flash('예기치 못한 오류가 발생했습니다.')
        return render_template('/user_templates/save_file.html')

    return send_file(downloads_path + filename, attachment_filename=filename, as_attachment=True)

3. 모자이크 기능 구현

https://github.com/Sunyoung-Song/Mosaic-Processing-Web

👉 github에 모자이크 처리 기능과 그 외 모든 기능 upload

0개의 댓글