Research has shown that cognitive engagement plays a key role in effective learning, resulting in extensive efforts have been devoted to measuring it. Whereas most of the literature explores manual methods to measure cognitive engagement, the research on automatic detection of cognitive engagement levels in real classrooms is very limited. Automatic detection of cognitive engagement has been a problem for a long time due to the lack of behavior annotation guidance and effective detection algorithms. For the first challenge, a theory of cognitive engagement called Interactive-Constructive-Active-Passive-Disengage (ICAPD) was proposed in this paper. ICAPD links visual behaviors with cognitive engagement in the classroom. According to the ICAPD framework, a cognitive engagement dataset was constructed to train the detection model. To tackle the second challenge, the simAM-based You Only Look Once version 8 Nano (simAM-YOLOv8n) model, which uses simAM attention module to strengthen feature extraction, was designed to detect different levels of cognitive engagement precisely and efficiently. The experimental results on the self-build dataset have demonstrated the effectiveness of the proposed theory framework and detection algorithm, indicating that the proposed methodology could be used to detect real-time cognitive engagement in the classroom scenario. This work has the potential to help teachers to carry out learning analysis and instructional adjustments.