Food safety is a major concern that has an impact on the national economy and people’s lives. The food industry has grown in quality and innovation in tandem with the rapid development of the economy and society. The emergence of new food technologies, as well as changes in dietary habits, has increased public concern about food safety. With the emergence of various counterfeit and substandard products, food quality and safety testing have become even more important. Traditional testing methods rely on sensory analysis and physical and chemical analysis. This approach is subjective and poorly adapted to the general public. It requires a high level of technical operation and is difficult to carry out on a large scale. To address this situation, this paper proposes an intelligent approach to food safety quality testing. The core idea is, first, to use sensors to collect data on the various components of the sample to be tested. Second, the random forest (RF) model used in this paper is trained. Third, the trained model is used to classify and identify the test samples. Based on the classification results, a conclusion is drawn as to whether the food product is a variant or a counterfeit. The advantage of this study is that the training model used is a weighted RF algorithm based on mutual information. The correlation between any two decision trees is calculated using mutual information, and for the more correlated decision trees, only the one with the highest evaluation accuracy is retained to form a new RF, and the evaluation accuracy is converted into voting weights, resulting in an RF model with less redundancy and higher evaluation accuracy. The experimental results show that the method used in this paper can successfully identify spoiled or counterfeit products and has good practicality.