File size: 1,561 Bytes
2aee2df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import cloudinary
import cloudinary.uploader
from pymongo import MongoClient
from dotenv import load_dotenv
from datetime import datetime

# Load environment variables from .env file
load_dotenv()

# --- Cloudinary Configuration ---
cloudinary.config(
    cloud_name=os.getenv("CLOUDINARY_CLOUD_NAME"),
    api_key=os.getenv("CLOUDINARY_API_KEY"),
    api_secret=os.getenv("CLOUDINARY_API_SECRET")
)

# --- MongoDB Configuration ---
MONGO_URI = os.getenv("MONGO_URI")
client = MongoClient(MONGO_URI)
db = client.get_database("face_shape_db")  # You can name your database
analyses_collection = db.get_collection("analyses") # You can name your collection

def upload_image_to_cloudinary(image_file):
    """Uploads an image file to Cloudinary and returns the secure URL."""
    try:
        upload_result = cloudinary.uploader.upload(image_file)
        return upload_result.get("secure_url")
    except Exception as e:
        print(f"Error uploading to Cloudinary: {e}")
        return None

def save_analysis_to_db(image_url, face_shape, measurements):
    """Saves the analysis results to MongoDB."""
    try:
        analysis_data = {
            "image_url": image_url,
            "face_shape": face_shape,
            "measurements": measurements,
            "created_at": datetime.utcnow()
        }
        result = analyses_collection.insert_one(analysis_data)
        return str(result.inserted_id)
    except Exception as e:
        print(f"Error saving to MongoDB: {e}")
        return None