Urban traffic congestion is a persistent issue in medium-sized cities like Demak, leading to delays and potential accidents. This study presents the development of a real-time vehicle density and anomaly detection system using YOLOv8, combined with OpenCV for video analysis, to monitor traffic flow at strategic entry points of Demak City. The system classifies vehicles into four categories (cars, motorcycles, trucks, buses) and determines their direction by detecting crossing lines. A key feature is the recognition of vehicle patterns, particularly the detection of stopped vehicles, flagging anomalies after 30 seconds of stoppage, with tolerance for temporary detection losses. Traffic data is stored in CSV format, enabling periodic analysis and visualization via an interactive graphical user interface (GUI). Evaluation results show the YOLOv8n model achieves 92.5% precision, 88.3% recall, and 89.7% mean average precision (mAP@0.5), demonstrating improved accuracy and speed over previous YOLO versions. Additionally, the vehicle counting accuracy reaches 94.2% when compared with manual annotations. The proposed system provides a reliable solution for real-time traffic monitoring and early anomaly detection, supporting intelligent transportation systems (ITS) and enabling data-driven traffic management decisions. This research contributes to the advancement of real-time video analytics and pattern recognition for urban traffic control and serves as a scientific reference for the development of smart city infrastructures. Furthermore, this study strengthens the application of pattern recognition in intelligent anomaly detection, providing new insights for researchers in the fields of computer science and informatics.