File size: 3,330 Bytes
8e6acb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import streamlit as st
import cv2
import numpy as np
from PIL import Image
from ultralytics import YOLO
from groq import Groq

# Load API key from Streamlit secrets
groq_api_key = st.secrets["GROQ_API_KEY"]

# Initialize Groq client
client = Groq(api_key=groq_api_key)

# Load YOLOv8 model (pretrained on COCO dataset)
model = YOLO("yolov8n.pt")

# Function to get AI-powered recommendations
def get_ai_recommendation(detected_objects):
    if not detected_objects:
        return "No objects detected. Please try again with a clearer image."
    
    prompt = f"Suggest optimal placement and design improvements for: {', '.join(detected_objects)} in a room."
    
    try:
        chat_completion = client.chat.completions.create(
            messages=[{"role": "user", "content": prompt}], 
            model="llama-3.3-70b-versatile"
        )
        return chat_completion.choices[0].message.content
    except Exception as e:
        return f"Error fetching AI recommendation: {e}"

# Streamlit UI
st.title("🏡 AI-Powered Home Setup Chatbot")
st.write("Upload an image of your room or use your camera for live capture.")

# Upload image option
uploaded_file = st.file_uploader("Upload an image of your room:", type=["jpg", "jpeg", "png"])

# Live camera capture
use_camera = st.checkbox("Use Live Camera")

if use_camera:
    st.write("Click 'Capture Image' to take a photo.")
    camera = cv2.VideoCapture(0)  # Open the webcam
    
    if st.button("Capture Image"):
        ret, frame = camera.read()
        if ret:
            image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            captured_image = Image.fromarray(image)
            st.image(captured_image, caption="Captured Image", use_column_width=True)
            
            # Run YOLOv8 for object detection
            results = model(image)
            detected_objects = [model.names[int(box.cls)] for box in results[0].boxes]
            
            # Display detected objects
            st.subheader("Detected Objects:")
            st.write(", ".join(set(detected_objects)))

            # Get AI-powered recommendations
            st.subheader("AI-Powered Placement Suggestions:")
            ai_recommendations = get_ai_recommendation(detected_objects)
            st.write(ai_recommendations)
        else:
            st.error("Failed to capture image. Please check your camera and try again.")

    camera.release()
    cv2.destroyAllWindows()

elif uploaded_file:
    image = Image.open(uploaded_file)
    image = np.array(image)
    
    # Run YOLOv8 for object detection
    results = model(image)
    detected_objects = [model.names[int(box.cls)] for box in results[0].boxes]
    
    # Display uploaded image and detected objects
    st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
    st.subheader("Detected Objects:")
    st.write(", ".join(set(detected_objects)))

    # Get AI-powered recommendations
    st.subheader("AI-Powered Placement Suggestions:")
    ai_recommendations = get_ai_recommendation(detected_objects)
    st.write(ai_recommendations)

# Text-based recommendations
user_input = st.text_input("Or enter an object name:")
if user_input:
    ai_recommendation = get_ai_recommendation([user_input.lower()])
    st.subheader("AI-Powered Suggestion:")
    st.write(ai_recommendation)