Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,53 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
|
|
|
| 5 |
from gtts import gTTS
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Mock context-aware filter function
|
| 15 |
def filter_relevant_objects(detected_objects, setting):
|
| 16 |
st.write(f"Filtering relevant objects for setting: {setting}")
|
| 17 |
-
# Simulated filtering based on setting
|
| 18 |
if setting == "indoor":
|
| 19 |
return [obj for obj in detected_objects if obj in ["table", "lamp"]]
|
| 20 |
return detected_objects
|
|
@@ -22,7 +55,6 @@ def filter_relevant_objects(detected_objects, setting):
|
|
| 22 |
# Mock summarization function
|
| 23 |
def generate_summary(relevant_objects):
|
| 24 |
st.write("Generating summary for relevant objects...")
|
| 25 |
-
# Simulated summary
|
| 26 |
summary = f"This is an {len(relevant_objects)}-item scene including: {', '.join(relevant_objects)}."
|
| 27 |
return summary
|
| 28 |
|
|
@@ -36,23 +68,27 @@ def text_to_speech(text):
|
|
| 36 |
# Mock GPS navigation function
|
| 37 |
def get_distance_to_object(address):
|
| 38 |
st.write(f"Calculating distance to address: {address}")
|
| 39 |
-
# Simulated output
|
| 40 |
return "5 km", "15 mins"
|
| 41 |
|
| 42 |
# Streamlit app main function
|
| 43 |
def main():
|
| 44 |
-
st.title("Context-Aware Object Detection")
|
| 45 |
-
|
| 46 |
-
#
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
if
|
| 50 |
-
# Open the
|
| 51 |
-
image = Image.open(
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
# Step 2: Detect Objects
|
| 55 |
-
|
|
|
|
| 56 |
st.write(f"Detected Objects: {detected_objects}")
|
| 57 |
|
| 58 |
# Step 3: Filter Relevant Objects
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
from gtts import gTTS
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# Load pre-trained model and classes
|
| 9 |
+
def load_model():
|
| 10 |
+
# Load YOLO model from OpenCV
|
| 11 |
+
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") # Ensure these files are in the working directory
|
| 12 |
+
layer_names = net.getLayerNames()
|
| 13 |
+
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
|
| 14 |
+
return net, output_layers
|
| 15 |
+
|
| 16 |
+
# Object detection function
|
| 17 |
+
def detect_objects(image, net, output_layers):
|
| 18 |
+
height, width, _ = image.shape
|
| 19 |
+
|
| 20 |
+
# Prepare the image for detection
|
| 21 |
+
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
|
| 22 |
+
net.setInput(blob)
|
| 23 |
+
outputs = net.forward(output_layers)
|
| 24 |
+
|
| 25 |
+
# Process the outputs
|
| 26 |
+
detected_objects = []
|
| 27 |
+
for out in outputs:
|
| 28 |
+
for detection in out:
|
| 29 |
+
scores = detection[5:] # Get the scores for detected objects
|
| 30 |
+
class_id = np.argmax(scores)
|
| 31 |
+
confidence = scores[class_id]
|
| 32 |
+
|
| 33 |
+
# Filter out weak predictions
|
| 34 |
+
if confidence > 0.5: # Adjust threshold as needed
|
| 35 |
+
detected_objects.append(class_id)
|
| 36 |
+
|
| 37 |
+
return detected_objects
|
| 38 |
+
|
| 39 |
+
# Mock function to convert class IDs to object names
|
| 40 |
+
def get_object_names(class_ids):
|
| 41 |
+
# Sample mapping (extend this according to your class IDs)
|
| 42 |
+
class_names = {0: "person", 1: "bicycle", 2: "car", 3: "motorcycle", 4: "airplane",
|
| 43 |
+
5: "bus", 6: "train", 7: "truck", 8: "boat", 9: "traffic light",
|
| 44 |
+
10: "fire hydrant", 11: "stop sign", 12: "parking meter", 13: "bench",
|
| 45 |
+
14: "bird", 15: "cat", 16: "dog", 17: "horse", 18: "sheep", 19: "cow"}
|
| 46 |
+
return [class_names[id] for id in class_ids if id in class_names]
|
| 47 |
|
| 48 |
# Mock context-aware filter function
|
| 49 |
def filter_relevant_objects(detected_objects, setting):
|
| 50 |
st.write(f"Filtering relevant objects for setting: {setting}")
|
|
|
|
| 51 |
if setting == "indoor":
|
| 52 |
return [obj for obj in detected_objects if obj in ["table", "lamp"]]
|
| 53 |
return detected_objects
|
|
|
|
| 55 |
# Mock summarization function
|
| 56 |
def generate_summary(relevant_objects):
|
| 57 |
st.write("Generating summary for relevant objects...")
|
|
|
|
| 58 |
summary = f"This is an {len(relevant_objects)}-item scene including: {', '.join(relevant_objects)}."
|
| 59 |
return summary
|
| 60 |
|
|
|
|
| 68 |
# Mock GPS navigation function
|
| 69 |
def get_distance_to_object(address):
|
| 70 |
st.write(f"Calculating distance to address: {address}")
|
|
|
|
| 71 |
return "5 km", "15 mins"
|
| 72 |
|
| 73 |
# Streamlit app main function
|
| 74 |
def main():
|
| 75 |
+
st.title("Context-Aware Object Detection App")
|
| 76 |
+
|
| 77 |
+
# Load the YOLO model
|
| 78 |
+
net, output_layers = load_model()
|
| 79 |
+
|
| 80 |
+
# Step 1: Capture Image from Camera
|
| 81 |
+
captured_image = st.camera_input("Take a picture")
|
| 82 |
|
| 83 |
+
if captured_image is not None:
|
| 84 |
+
# Open the captured image
|
| 85 |
+
image = Image.open(captured_image)
|
| 86 |
+
image_np = np.array(image) # Convert PIL image to numpy array
|
| 87 |
+
st.image(image, caption="Captured Image", use_column_width=True)
|
| 88 |
|
| 89 |
# Step 2: Detect Objects
|
| 90 |
+
detected_ids = detect_objects(image_np, net, output_layers)
|
| 91 |
+
detected_objects = get_object_names(detected_ids)
|
| 92 |
st.write(f"Detected Objects: {detected_objects}")
|
| 93 |
|
| 94 |
# Step 3: Filter Relevant Objects
|