First test in fine tuning via images, increased BBox detection accuracy a bit (not skewed too much).
HOW TO RUN
Start your llama.cpp server with model:
llama-server -m "Qwen3.5-4B.Q4_K_M.gguf" --mmproj "Qwen3.5-4B.F16-mmproj.gguf.gguf" --port 8080
EXAMPLE (may be buggy), THE GOLD IS IN NORMALIZATION BECOZ QWEN USE 1000-1000 COORDS CAN BE USED TO ALL QWEN3 IMAGES STUFF
import requests
from io import BytesIO
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
import base64
import re
import json
# ==========================================
# LLAMA.CPP CONFIGURATION & PLACEHOLDERS
# ==========================================
LLAMA_ENDPOINT = "http://localhost:8080/v1/chat/completions"
# 5 random high-quality room URLs from Unsplash
urls = [
"https://images.unsplash.com/photo-1616486338812-3dadae4b4ace?auto=format&fit=crop&w=800&q=80", # Living room
"https://images.unsplash.com/photo-1505691938895-1758d7feb511?auto=format&fit=crop&w=800&q=80", # Bedroom
"https://images.unsplash.com/photo-1556911220-e15b29be8c8f?auto=format&fit=crop&w=800&q=80", # Kitchen
"https://images.unsplash.com/photo-1584622650111-993a426fbf0a?auto=format&fit=crop&w=800&q=80", # Bathroom
"https://images.unsplash.com/photo-1524758631624-e2822e304c36?auto=format&fit=crop&w=800&q=80" # Workspace/Office
]
def fetch_image(url):
try:
res = requests.get(url, timeout=10)
return Image.open(BytesIO(res.content)).convert("RGB")
except Exception as e:
print(f"Error fetching image: {e}")
return None
def pil_image_to_base64_data_uri(img, format="JPEG"):
buffered = BytesIO()
img.save(buffered, format=format)
img_bytes = buffered.getvalue()
encoded = base64.b64encode(img_bytes).decode("utf-8")
return f"data:image/{format.lower()};base64,{encoded}"
def run_inference(img):
prompt = 'Detect everything in this room and output their labels and bbox coordinates in JSON format.'
image_data_uri = pil_image_to_base64_data_uri(img, format="JPEG")
payload = {
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": image_data_uri}}
]
}
],
"max_tokens": 256,
"temperature": 0.0
}
try:
response = requests.post(LLAMA_ENDPOINT, json=payload, timeout=60)
response.raise_for_status()
data = response.json()
return data["choices"][0]["message"]["content"]
except Exception as e:
print(f"Inference error: {e}")
return ""
# DA GOLD
def parse_all_boxes(text):
boxes_found = []
num_re = r"-?\d+(?:\.\d+)?"
pattern = re.compile(r'"bbox_2d"\s*:\s*\[\s*(' + num_re + r')\s*,\s*(' + num_re + r')\s*,\s*(' + num_re + r')\s*,\s*(' + num_re + r')\s*\]')
labels = re.findall(r'"label"\s*:\s*"([^"]+)"', text)
matches = list(pattern.finditer(text))
for idx, match in enumerate(matches):
coords = [float(v) for v in match.groups()]
label = labels[idx] if idx < len(labels) else "object"
boxes_found.append((coords, label))
return boxes_found
fig, axes = plt.subplots(1, 5, figsize=(25, 5))
for idx, url in enumerate(urls):
img = fetch_image(url)
if img is None:
continue
raw_ans = run_inference(img)
print(f"\n--- Room {idx+1} Raw Output ---")
print(raw_ans)
detections = parse_all_boxes(raw_ans)
W, H = img.size
draw_img = img.copy()
d = ImageDraw.Draw(draw_img)
print(f"\n--- Room {idx+1} Detections ---")
for box, label in detections:
x1, y1, x2, y2 = int(box[0]/1000*W), int(box[1]/1000*H), int(box[2]/1000*W), int(box[3]/1000*H) #this baby
d.rectangle([x1, y1, x2, y2], outline="green", width=3)
d.text((x1 + 4, max(0, y1 - 15)), label, fill="red")
print(f"Detected: {label} at {box}")
axes[idx].imshow(draw_img)
axes[idx].axis("off")
axes[idx].set_title(f"Room {idx+1} (All)", fontsize=12)
plt.tight_layout()
plt.show()