forked from Stalin-143/Ai-Basic-programes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmart-Tour-Guide.py
More file actions
50 lines (41 loc) · 1.72 KB
/
Smart-Tour-Guide.py
File metadata and controls
50 lines (41 loc) · 1.72 KB
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
import requests
from transformers import CLIPProcessor, CLIPModel
from PIL import Image
import wikipedia
# Load the CLIP model and processor
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
def recognize_object(image_path):
"""Recognize objects in an image using CLIP."""
image = Image.open(image_path)
text_inputs = [
"a painting", "a building", "a sculpture", "a park",
"an animal", "a historic site", "a famous landmark"
]
inputs = processor(text=text_inputs, images=image, return_tensors="pt", padding=True)
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=1)
# Get the most probable label
recognized_index = probs.argmax().item()
recognized_label = text_inputs[recognized_index]
confidence = probs[0, recognized_index].item()
return recognized_label, confidence
def fetch_information(query):
"""Fetch a summary of information from Wikipedia."""
try:
summary = wikipedia.summary(query, sentences=2)
except wikipedia.DisambiguationError as e:
summary = f"Multiple results found: {e.options[:3]}..."
except wikipedia.PageError:
summary = "No relevant information found."
return summary
if __name__ == "__main__":
# Example image
image_path = "example.jpg" # Replace with your image file
print("Recognizing object in the image...")
label, confidence = recognize_object(image_path)
print(f"Recognized as: {label} (Confidence: {confidence:.2f})")
print("Fetching information...")
description = fetch_information(label)
print(f"Description: {description}")