Inspection of thermal fusion joints is essential for in-service high-density polyethylene (HDPE) gas pipe operating safety. A directivity-compensated circular coherence factor weighted total focusing method (D-CCF-TFM) algorithm is presented to solve the problem of low imaging quality of phased array ultrasonic testing during inspecting thermal fusion joints of HDPE pipes. The D-CCF-TFM leverages the circular coherence factor (CCF) to reduce noise interference during calculating the phase of defect and employs a directivity function to compensate sound field intensities in different directions, leading to obvious average signal-to-noise ratio (SNR) improvement compared to TFM, as confirmed through experiments on thermal fusion joint and test block. To automate defect detection in D-CCF-TFM images, an improved YOLOX algorithm is proposed, incorporating a convolutional block attention module (CBAM) and adopting the complete intersection over union (CIoU) as the regression loss. Evaluation of this improved YOLOX algorithm on a dataset of 2504 images yielded a mean average precision (mAP) of 99.15%, demonstrating its effect in detecting small defects. Thus, it provides theoretical and technical support for quality detection of thermal fusion joint welding.