Downloads · 30 days
0
Piumi-Udeshika29/caption-generate
caption-generate is a machine learning model from Piumi-Udeshika29. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
!pip install ultralytics transformers pillow torch torchvision
Downloads · 30 days
0
Access
Public
Updated Feb 15, 2026
Repo size
—
Likes
0
Public
Click a slice to open those files.
.md2 KB · 57%
From the Hugging Face model README
!pip install ultralytics transformers pillow torch torchvision
import torch from ultralytics import YOLO from transformers import BlipProcessor, BlipForConditionalGeneration from PIL import Image
device = "cuda" if torch.cuda.is_available() else "cpu"
yolo = YOLO("yolov8n.pt")
processor = BlipProcessor.from_pretrained( "Salesforce/blip-image-captioning-base" ) blip = BlipForConditionalGeneration.from_pretrained( "Salesforce/blip-image-captioning-base" ).to(device)
def detect_objects(image_path, conf_threshold=0.6): results = yolo(image_path) detected = set()
for r in results:
for box in r.boxes:
conf = float(box.conf[0])
cls = int(box.cls[0])
if conf >= conf_threshold:
label = yolo.names[cls]
detected.add(label)
return list(detected)
def generate_caption(image_path): image = Image.open(image_path).convert("RGB")
inputs = processor(image, return_tensors="pt").to(device)
output = blip.generate(
**inputs,
max_length=30
)
caption = processor.decode(
output[0],
skip_special_tokens=True
)
return caption
def refine_caption(caption, objects): caption = caption.lower()
# Force object correctness
if "laptop" in objects and "laptop" not in caption:
caption += " using a laptop"
if "person" in objects and not caption.startswith("a person"):
caption = "a person " + caption
return caption.capitalize()
def generate_smart_caption(image_path): objects = detect_objects(image_path) caption = generate_caption(image_path) final_caption = refine_caption(caption, objects)
return final_caption, objects
image_path = "/content/drive/MyDrive/Research/06.png"
caption, objects = generate_smart_caption(image_path)
print("Detected objects:", objects) print("Final caption:", caption)