SmartWareHouseAI / src /vision_model.py
Pro-Coder's picture
Upload 43 files
2d9a408 verified
Raw History Blame Contribute Delete
6.64 kB
"""
vision_model.py
-----------------
Visual Inventory & Rack Occupancy Scanner.
A deliberately different computer-vision approach from a YOLO-style deep
detector: a small, fully deterministic **classical image-processing
pipeline** (OpenCV: color-based background/shelf segmentation ->
morphological cleanup -> connected components -> geometric filtering) that
(a) counts box/pallet-like objects on a warehouse rack and (b) estimates
what fraction of the rack's storage slots are occupied.
Why classical CV, deliberately: zero model downloads (self-contained and
reproducible), runs instantly on a free CPU Space, and every detection is a
traceable contour rather than an opaque activation. A production system
would likely fine-tune a detector; this module demonstrates the systems
thinking (synthetic ground truth, honest evaluation under noise) that would
carry over directly.
Synthetic scenes: rack photos can't be licensed/scraped for a portfolio
project, so -- like every other dataset here -- scenes are generated with
PIL with known ground truth, which is what allows honest metrics.
Development notes (kept because they're real):
* v1 used Canny edges + heavy dilation and merged every box and shelf bar
into one blob. Switching to per-pixel color-distance segmentation fixed it.
* On clean renders (noise_level=0) v2 hit a suspiciously perfect 100%, so
`noise_level` (Gaussian pixel noise, brightness/contrast jitter, per-box
color variation) was added; accuracy then degrades realistically.
* Occupancy was first computed from padded box pixel area vs full cell area
(a definitional mismatch that under-counted ~40%); it now counts occupied
grid cells, matching how ground truth is defined.
"""
import random
from dataclasses import dataclass
from typing import List, Optional, Tuple
import cv2
import numpy as np
from PIL import Image, ImageDraw
RACK_W, RACK_H = 640, 400
SHELF_ROWS, SHELF_COLS = 3, 6
MARGIN = 20
SHELF_BAR_H = 10
BOX_COLORS = [
(181, 101, 29), (139, 69, 19), (205, 133, 63), (160, 82, 45),
(222, 184, 135), (128, 0, 0), (46, 87, 46), (25, 65, 100),
]
BG_COLOR = (235, 235, 230)
SHELF_COLOR = (90, 90, 95)
@dataclass
class RackScene:
image: Image.Image
true_box_count: int
true_occupancy: float # fraction of cells filled, 0-1
def generate_rack_scene(fill_prob: float = 0.6, seed: Optional[int] = None, noise_level: float = 0.0) -> RackScene:
"""One synthetic rack image with known ground-truth box count/occupancy."""
rng = random.Random(seed)
img = Image.new("RGB", (RACK_W, RACK_H), BG_COLOR)
draw = ImageDraw.Draw(img)
cell_w = (RACK_W - 2 * MARGIN) / SHELF_COLS
cell_h = (RACK_H - 2 * MARGIN) / SHELF_ROWS
for row in range(SHELF_ROWS + 1):
y = MARGIN + row * cell_h
draw.rectangle([MARGIN - 5, y - SHELF_BAR_H / 2, RACK_W - MARGIN + 5, y + SHELF_BAR_H / 2], fill=SHELF_COLOR)
n_filled = 0
for row in range(SHELF_ROWS):
for col in range(SHELF_COLS):
if rng.random() < fill_prob:
x0, y0 = MARGIN + col * cell_w, MARGIN + row * cell_h
pad = min(cell_w, cell_h) * 0.12
bw, bh = cell_w - 2 * pad, cell_h - 2 * pad - 4
bx0, by0 = x0 + pad, y0 + pad + 4
base = rng.choice(BOX_COLORS)
if noise_level > 0:
j = int(25 * noise_level)
color = tuple(int(np.clip(c + rng.randint(-j, j), 0, 255)) for c in base)
else:
color = base
draw.rectangle([bx0, by0, bx0 + bw, by0 + bh], fill=color, outline=(30, 30, 30), width=2)
draw.line([bx0 + 4, by0 + bh * 0.4, bx0 + bw - 4, by0 + bh * 0.4], fill=(20, 20, 20), width=2)
n_filled += 1
if noise_level > 0:
arr = np.array(img).astype(np.float32)
brightness = rng.uniform(-20, 20) * noise_level
contrast = 1.0 + rng.uniform(-0.15, 0.15) * noise_level
arr = (arr - 127.5) * contrast + 127.5 + brightness
arr += np.random.RandomState(seed).normal(0, 10 * noise_level, arr.shape)
img = Image.fromarray(np.clip(arr, 0, 255).astype(np.uint8))
return RackScene(img, n_filled, n_filled / (SHELF_ROWS * SHELF_COLS))
@dataclass
class DetectionResult:
detected_count: int
estimated_occupancy: float
annotated_image: Image.Image
boxes: List[Tuple[int, int, int, int]]
def _iou(a, b) -> float:
ax, ay, aw, ah = a
bx, by, bw, bh = b
iw = max(0, min(ax + aw, bx + bw) - max(ax, bx))
ih = max(0, min(ay + ah, by + bh) - max(ay, by))
inter = iw * ih
union = aw * ah + bw * bh - inter
return inter / union if union > 0 else 0.0
def _nms(boxes, thr=0.3):
kept = []
for b in sorted(boxes, key=lambda b: b[2] * b[3], reverse=True):
if all(_iou(b, k) < thr for k in kept):
kept.append(b)
return kept
def detect_boxes(image: Image.Image, min_area_frac: float = 0.01, max_area_frac: float = 0.25) -> DetectionResult:
"""Color-distance segmentation -> morphology -> contours -> geometric filter."""
rgb = np.array(image.convert("RGB")).astype(np.int16)
dist = lambda c: np.sqrt(np.sum((rgb - np.array(c)) ** 2, axis=-1))
mask = ((dist(BG_COLOR) > 60) & (dist(SHELF_COLOR) > 60)).astype(np.uint8) * 255
k = np.ones((3, 3), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, k, iterations=2)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, k, iterations=1)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
h, w = rgb.shape[:2]
boxes = []
for c in contours:
area = cv2.contourArea(c)
if not (h * w * min_area_frac <= area <= h * w * max_area_frac):
continue
x, y, bw, bh = cv2.boundingRect(c)
aspect = bw / float(bh) if bh else 0
if 0.2 <= aspect <= 3.0:
boxes.append((x, y, bw, bh))
boxes = _nms(boxes)
cell_w = (RACK_W - 2 * MARGIN) / SHELF_COLS
cell_h = (RACK_H - 2 * MARGIN) / SHELF_ROWS
cells = set()
for (x, y, bw, bh) in boxes:
col = max(0, min(SHELF_COLS - 1, int((x + bw / 2 - MARGIN) // cell_w)))
row = max(0, min(SHELF_ROWS - 1, int((y + bh / 2 - MARGIN) // cell_h)))
cells.add((row, col))
occupancy = len(cells) / (SHELF_ROWS * SHELF_COLS)
annotated = image.convert("RGB").copy()
d = ImageDraw.Draw(annotated)
for (x, y, bw, bh) in boxes:
d.rectangle([x, y, x + bw, y + bh], outline=(0, 220, 90), width=3)
return DetectionResult(len(boxes), float(occupancy), annotated, boxes)