목표

  • Flutter 앱에서 이미지를 업로드하면, Flask 서버가 파일을 받아 임시 저장
  • 추후 Gemini API 분석 단계에서 이 파일을 활용

라이브러리 준비

Flask와 함께 파일 업로드 처리를 위해 werkzeug가 필요합니다.
(Flask에 기본 포함되어 있어 추가 설치는 필요 없음)

코드 작성

app.py를 다음과 같이 수정합니다.

import os
from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename

# Flask 앱 초기화
app = Flask(__name__)

# 업로드 폴더 지정
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

# 허용 파일 확장자
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

# 홈 라우트
@app.route('/')
def home():
    return jsonify({"message": "IdeaSnap Backend is running!"})

# 이미지 업로드 API
@app.route('/api/upload-image', methods=['POST'])
def upload_image():
    if 'image' not in request.files:
        return jsonify({"error": "No file part"}), 400

    file = request.files['image']

    if file.filename == '':
        return jsonify({"error": "No selected file"}), 400

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(filepath)

        return jsonify({
            "message": "Image uploaded successfully",
            "file_path": filepath
        }), 200

    return jsonify({"error": "Invalid file type"}), 400


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

프로젝트 구조 (지금까지)

ideasnap-backend/
 ├─ uploads/            # 업로드된 파일이 저장될 폴더
 ├─ app.py
 ├─ sample.jpg          # 테스트할 이미지 파일
 └─ venv/

터미널에서 프로젝트 폴더로 이동

cd ideasnap-backend

테스트 방법

터미널에서 curl을 사용해 업로드를 테스트할 수 있습니다.

curl -X POST http://127.0.0.1:5000/api/upload-image \
  -F "image=@sample.jpg"

예상 응답

{
  "message": "Image uploaded successfully",
  "file_path": "uploads/sample.jpg"
}

Part 4에서는 업로드된 이미지를 Gemini API에 전달하여
👉 사진을 분석하고 키워드/아이디어(주제) 추출하는 기능을 구현합니다.


TechTinkerer's에서 더 알아보기

구독을 신청하면 최신 게시물을 이메일로 받아볼 수 있습니다.

댓글 남기기

  • Understanding Pointers and Memory Management in C++

    [Tutorial] · 2026-04-30 05:10 UTC Understanding Pointers and Memory Management in C++ 💡 TL;DR Mastering pointers in C++ is crucial for efficient memory management and writing effective code. 📚 Learning Objectives This tutorial covers the fundamentals of pointers in C++, including declaration, initialization, and memory management. Students will learn how to effectively use pointers to…

  • Building a Command-Line Calculator with C++

    [Tutorial] · 2026-04-30 04:08 UTC Building a Command-Line Calculator with C++ 💡 TL;DR Learn how to build a command-line calculator in C++ that takes user input and performs basic arithmetic operations. 📚 Learning Objectives This tutorial guides you through creating a basic command-line calculator in C++. You’ll learn how to take user input, perform arithmetic…

  • Mastering Python Data Structures for Efficient Coding

    [Tutorial] · 2026-04-30 03:05 UTC Mastering Python Data Structures for Efficient Coding 💡 TL;DR Learn about Python’s fundamental data structures – arrays, lists, tuples, and dictionaries – to write efficient and scalable code. 📚 Learning Objectives This tutorial covers the essential Python data structures – arrays, lists, tuples, and dictionaries. You’ll learn about their usage,…

  • Introduction to Object-Oriented Programming in Python

    [Tutorial] · 2026-04-30 02:02 UTC Introduction to Object-Oriented Programming in Python 💡 TL;DR Learn the fundamentals of object-oriented programming in Python, including classes and objects, inheritance, and polymorphism. 📚 Learning Objectives This tutorial introduces the basics of object-oriented programming in Python, covering classes, objects, inheritance, and polymorphism. By the end of this tutorial, beginners will…

  • Complete Guide to Python List Comprehensions

    [Tutorial] · 2026-04-30 01:00 UTC Complete Guide to Python List Comprehensions 💡 TL;DR Master Python list comprehensions to write concise and efficient code for data manipulation and transformation tasks. 📚 Learning Objectives This tutorial covers the basics of Python list comprehensions, including syntax, use cases, and execution results. You’ll learn how to write efficient and…

← 뒤로

응답해 주셔서 감사합니다. ✨

TechTinkerer's에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기

TechTinkerer's에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기