Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from PIL import Image
3
4model = AutoModelForCausalLM.from_pretrained(
5 "vikhyatk/moondream2",
6 revision="2025-01-09",
7 trust_remote_code=True,
8 # Uncomment to run on GPU.
9 # device_map={"": "cuda"}
10)
11
12# Captioning
13print("Short caption:")
14print(model.caption(image, length="short")["caption"])
15
16print("\nNormal caption:")
17for t in model.caption(image, length="normal", stream=True)["caption"]:
18 # Streaming generation example, supported for caption() and detect()
19 print(t, end="", flush=True)
20print(model.caption(image, length="normal"))
21
22# Visual Querying
23print("\nVisual query: 'How many people are in the image?'")
24print(model.query(image, "How many people are in the image?")["answer"])
25
26# Object Detection
27print("\nObject detection: 'face'")
28objects = model.detect(image, "face")["objects"]
29print(f"Found {len(objects)} face(s)")
30
31# Pointing
32print("\nPointing: 'person'")
33points = model.point(image, "person")["points"]
34print(f"Found {len(points)} person(s)")