AI-based calculation of building materials based on photos and videos
Manually counting materials on a construction site is a source of errors and waste. Rebar, bricks, bags of cement, pipes, and plywood sheets can all be counted automatically using photos from a phone or drone camera.
Counting methods depending on the type of material
import cv2
import numpy as np
from ultralytics import YOLO
import torch
class ConstructionMaterialCounter:
def __init__(self, config: dict):
# YOLOv8m дообученный на строительных материалах
self.detector = YOLO(config['model_path'])
# SAM для точной сегментации при плотном расположении
self.segmentor = self._load_sam(config.get('sam_checkpoint'))
def count_by_detection(self, image: np.ndarray,
material_class: str) -> dict:
"""
Подходит для: кирпичи в поддонах, мешки, листы OSB/фанеры.
Не подходит для: длинные трубы, арматура в пучках.
"""
results = self.detector(image, conf=0.4)
class_counts = {}
for box in results[0].boxes:
cls = self.detector.model.names[int(box.cls)]
class_counts[cls] = class_counts.get(cls, 0) + 1
return {
'method': 'detection',
'counts': class_counts,
'target_count': class_counts.get(material_class, 0)
}
def count_rebar_by_cross_section(self,
end_view_image: np.ndarray) -> dict:
"""
Арматура в пачке: фотографируем торец.
Детектируем круглые сечения через Hough circles или Watershed.
"""
gray = cv2.cvtColor(end_view_image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (9, 9), 2)
# Hough Circle Transform для круглых сечений арматуры
circles = cv2.HoughCircles(
blurred,
cv2.HOUGH_GRADIENT,
dp=1.2,
minDist=15,
param1=50,
param2=30,
minRadius=8,
maxRadius=40
)
count = 0
if circles is not None:
circles = np.uint16(np.around(circles))
count = len(circles[0])
return {
'method': 'hough_circles',
'rebar_count': count,
'circles': circles[0].tolist() if circles is not None else []
}
def count_pipes_stacked(self, side_view: np.ndarray) -> dict:
"""
Трубы в штабеле: детектируем через ellipse fitting (вид сбоку)
или circle detection (вид спереди).
"""
gray = cv2.cvtColor(side_view, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
# Контуры → эллипсы
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
pipe_count = 0
for cnt in contours:
if len(cnt) < 5:
continue
area = cv2.contourArea(cnt)
if area < 200:
continue
ellipse = cv2.fitEllipse(cnt)
# Проверяем, что это круглое сечение (a ≈ b)
a, b = ellipse[1]
if b > 0 and 0.7 < a/b < 1.3: # близко к кругу
pipe_count += 1
return {'method': 'ellipse_fitting', 'pipe_count': pipe_count}
Counting bags: density estimation method
For bulk materials and objects in dense rows, the classic detector fails: IoU between adjacent objects > 0.6, NMS suppresses correct detections. We use the density map approach:
class DensityBasedCounter:
"""
CSRNet или CrowdCounting подход, адаптированный для материалов.
Вместо детекции каждого объекта предсказываем карту плотности.
Сумма пикселей карты плотности ≈ количество объектов.
"""
def __init__(self, model_path: str):
import torchvision.models as models
# VGG16 backbone + density head
self.model = self._build_csrnet()
self.model.load_state_dict(torch.load(model_path))
self.model.eval()
@torch.no_grad()
def count(self, image: np.ndarray) -> dict:
from torchvision import transforms
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
])
tensor = transform(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
tensor = tensor.unsqueeze(0)
density_map = self.model(tensor)[0, 0].numpy()
# Сумма карты плотности = количество объектов
count = float(density_map.sum())
return {
'method': 'density_estimation',
'count': round(count),
'count_float': count,
'density_map': density_map
}
Case: Inventory of a reinforcement warehouse
Warehouse: 800 tons of rebar in bundles, 6 standard sizes (d8 to d32). Monthly manual inventory: 2 people, 1 working day.
After implementation: the operator photographs the ends of the bundles on a tablet (20–30 photos per hour). The AI calculates the number of rods in each bundle using hough circles (for d12–d25) and density estimation (for d8—small, dense).
- Counting accuracy: ±2% of manual recounting
- Inventory time: 1.5 hours vs. 8 hours manually
- Integration with 1C: automatic updating of balances
| Material type | Method | Accuracy |
|---|---|---|
| Bricks on a pallet | YOLO detection | 93–97% |
| Reinforcement (end) | Hough circles | 95–98% |
| Bags in a stack | Density estimation | 91–95% |
| OSB sheets/plywood | YOLO + segmentation | 96–99% |
| Pipes (end) | Ellipse fitting | 94–97% |
| Project type | Term |
|---|---|
| Counter of one type of material | 2–4 weeks |
| Multi-material system (5+ types) | 5–9 weeks |
| With integration into WMS/1C | 7–12 weeks |







