import streamlit as st from transformers import DetrImageProcessor, DetrForObjectDetection, pipeline import torch from PIL import Image # Load the DETR model for object detection processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm") detr_model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm") # Load an NLP model for summarization (T5-small used as an example) summarizer = pipeline("summarization", model="t5-small") st.title("Hassan's Project") st.title("Object Detection with a Summary") st.write("Upload an image to detect objects and get a summary of what is detected.") # File uploader in Streamlit uploaded_file = st.file_uploader("Choose an image...", type="jpg") if uploaded_file is not None: # Load and display the image image = Image.open(uploaded_file) st.image(image, caption='Uploaded Image', use_column_width=True) # Process the image and perform object detection inputs = processor(images=image, return_tensors="pt") outputs = detr_model(**inputs) # Post-process the results to get bounding boxes and labels with a lower confidence threshold target_sizes = torch.tensor([image.size[::-1]]) results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.5)[0] # Generate descriptions for detected objects descriptions = [] st.write("Detected objects:") for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): box = [round(i, 2) for i in box.tolist()] label_text = detr_model.config.id2label[label.item()] description = f"Detected {label_text} with confidence {round(score.item(), 2)} at location {box}." descriptions.append(description) st.write(description) # Display each detected object # Combine descriptions into a single text input for the summarizer description_text = " ".join(descriptions) # Generate a summary using the NLP model summary = summarizer(description_text, max_length=50, min_length=10, do_sample=False)[0]['summary_text'] # Display the summary st.subheader("Summary") st.write(summary)