├── README.md └── generate_html.py /README.md: -------------------------------------------------------------------------------- 1 | 1. Set Up Python on Your System 2 | - Install Python 3.x from the [official Python website](https://www.python.org/). 3 | - During installation, ensure the option Add Python to PATH is selected. 4 | 5 | 2. Install Required Libraries 6 | - After Python is installed, open the terminal or command prompt. 7 | - Install the necessary libraries using the following commands: 8 | - pip install numpy 9 | - pip install scikit-learn 10 | - pip install matplotlib 11 | - These libraries are essential for data handling, model training, and visualization. 12 | 13 | 3. Prepare the Project Files 14 | - Place all project files in a single folder for easy access. 15 | - Ensure the script file, Digit Recognition Training.py, is included in this folder. 16 | 17 | 4. Run the Training Script 18 | - Open the terminal or command prompt. 19 | - Use the cd command to navigate to the folder containing the project files. For example: 20 | bash 21 | cd path/to/your/project-folder 22 | 23 | - Run the training script using: 24 | bash 25 | python "Digit Recognition Training.py" 26 | 27 | - The script will perform the following: 28 | - Load the digits dataset from scikit-learn. 29 | - Split the data into training and testing sets. 30 | - Train a Support Vector Machine (SVM) classifier. 31 | - Predict outcomes on the test set. 32 | - Save example predictions as image files. 33 | - Generate an HTML file displaying the model's accuracy and prediction images. 34 | 35 | 5. View the Results 36 | - After execution, check the project folder for: 37 | - Several image files (digit_0.png, digit_1.png, etc.) showing test predictions. 38 | - An HTML file named digit_recognition.html. 39 | - Open the HTML file in any web browser to view: 40 | - The accuracy of the model. 41 | - Images of predicted digits along with their labels. 42 | 43 | 6. Verify and Adjust 44 | - Review the accuracy displayed in the browser. 45 | - If needed, modify the script parameters (e.g., kernel type in SVM) to optimize the model performance. 46 | - Re-run the script to test the changes. 47 | 48 | 7. Next Steps (Optional) 49 | - Save the trained SVM model for later use. 50 | - Modify the HTML to allow real-time user input for new digit predictions 51 | -------------------------------------------------------------------------------- /generate_html.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn import datasets 3 | from sklearn.model_selection import train_test_split 4 | from sklearn.preprocessing import StandardScaler 5 | from sklearn.svm import SVC 6 | from sklearn.metrics import accuracy_score 7 | import matplotlib.pyplot as plt 8 | 9 | # Load the digits dataset 10 | digits = datasets.load_digits() 11 | 12 | # Split data into training and testing sets 13 | X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.3, random_state=42) 14 | 15 | # Standardize the data 16 | scaler = StandardScaler() 17 | X_train_scaled = scaler.fit_transform(X_train) 18 | X_test_scaled = scaler.transform(X_test) 19 | 20 | # Train an SVM classifier 21 | svm_clf = SVC(kernel='linear') 22 | svm_clf.fit(X_train_scaled, y_train) 23 | 24 | # Make predictions on the test set 25 | y_pred = svm_clf.predict(X_test_scaled) 26 | 27 | # Calculate accuracy 28 | accuracy = accuracy_score(y_test, y_pred) 29 | 30 | # Save test images with predictions 31 | n_images = 5 32 | image_filenames = [] 33 | for i in range(n_images): 34 | filename = f"digit_{i}.png" 35 | image_filenames.append(filename) 36 | plt.figure() 37 | plt.imshow(X_test[i].reshape(8, 8), cmap="gray") 38 | plt.title(f"Pred: {y_pred[i]}") 39 | plt.axis("off") 40 | plt.savefig(filename) 41 | plt.close() 42 | 43 | # Generate the HTML content 44 | html_content = f""" 45 | 46 | 47 |
48 | 49 | 50 |Model Accuracy: {accuracy * 100:.2f}%
74 |