HELLO
from ultralytics import YOLO
import cv2
# Load YOLO model (pre-trained)
model = YOLO("yolov8n.pt") # lightweight model
# Read image
image = cv2.imread("images.jpg")
# Run detection
results = model(image)
car_count = 0
# Loop through detections
for r in results:
for box in r.boxes:
cls = int(box.cls[0])
label = model.names[cls]
# Count only cars
if label == "car":
car_count += 1
# Draw bounding box
x1, y1, x2, y2 = map(int, box.xyxy[0])
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(image, "Car", (x1, y1-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)
# Show result
print("Total Cars:", car_count)
cv2.imshow("Detected Cars", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
0 Comments