Package: qmodel
QModelWelcome to the QModel Documentation, here are every details you may need to understand how to use QModel.What does this library:Q-model is a library that contains everything you need to create a Random Forest Classifer (RFC) to read 7 digits display from an image.What can you do with that library ?Create a training dataset of images to train a RFC modelCreate a testing dataset of images to test a trained RFC modelCreate and train a RFC model with a training datasetTest a RFC model with a testing dataset of imagesUse a RFC model to read the values of a display imageCreate a training dataset :1st step :First you have to take pictures of your 7-segment dipslay (try to take several photos to have at least every digit 0,9 with a good image quality).
Place them into a folder.2nd step :Then, for each photo open the photo editor and crop the image around the display to have a nice rectangle around all the digits you try to detect, as below.Device image:Display image:Then place all theese images into a folder that you can name "display" for exemple.3rd step :Now you have a folder full of display images.
Use theTestingsetclass from thetesting_set.pyfile to extract digits images from display images and save them into a folder.Example :fromtesting_setimportTestingsettesting_set=(display_images_path,destination_path)You should now have a folder at the destination_path full of digits images.4th step :You now must select 10 images for every token you used.
1 image per digits per token.Name every images by the digit they are representatingFor example this image:is going to be saved as "0.jpg".Take them and place them into a folder named by the name of the token their are from.For example if you have took images of the "A" token, place them into a folder named "A".5th step :Now you must call theTrainingSetclass from thetraining_set.pyfile, as below.fromtraining_set.pyimportTrainingSettraining_set=TrainingSet(name,path,original_10_img,storeImages)With :name(str): name of the set that will be createpath_to_save(str): root of the path where will be save the setoriginal_10_img(list of str): path where are located the 10 digit images that you created. ( if you want to create a training dataset from multiple device, you have to add the path of the folder that contains theese images to the list)storeImages(boolean): set as False by default, if you set it at True, the images before renaming and resizing will be saveConclusionYou now should have a training dataset at thepath_to_saveCreate a testing dataset :To create a testing dataset follow the 3rd first step of the Training dataset guide.You need now to sort every digits image and place each one of them in a folder named by the digit they are representing.For example put this imageinto a folder called "0"You shoud have a testing dataset of digits images.Create and training a Random Forest Model Classifier  :First you will need a training dataset so be sure you have one already created by following the guide aboveYou need to call aModelTrainingobject from themodel_training.pyfile, and use thetrain()method from that class.Example :frommodel_trainingimportModelTrainingmodel_training=ModelTraining(training_set_path,model_path)model_training.train()You now have created and trained a Random Forest Classifer model.
If you want to test it or use it you can use theget_model_path()method of theModelTrainingobject to get the path of that just created model.Test a trained Random Forest Model Classifier  :To test the model you need to first have a testing set created.
Ensure you have created a testing dataset by following the guide above.
To test a model, you need to create aModelTestingobject from themodel_testing.pyfile.
Then callprediction()method to get the accuracy of the model.Example:frommodel_testingimportModelTestingmodel_test=ModelTesting(testing_set_path,model_path)inverted_resulted,accuracy=model_test.prediction()testing_set_pathis the path of the testing datasetmodel_pathis the path of the modelaccuracyis the accuracy of the modelinverted_resultis the list of the predictions made by the modelMake prediction with a Random Forest Model Classifier  :The main goal of building a Random Forest Classifier with Q-model is to read the value of a 7 segment display.
To do that you have to make prediction with a Random Forest Classifer.To make a prediction you need to use a list of images.
For that you will use thePredictionImagesclass from theprediction_images.pyfile.Example of list of images from a PredictionImages object :fromprediction_imagesimportPredictionImagesprediction_images=PredictionImages(display_path)image_list=prediction_images.getdigitlist()display_pathis the path where are located all the display images.So, now you should have a trained RFC model and tested it.You now need to call aModelUsingobject from themodel_using.pyfile.
A ModelUsing object need an image list to work so you need to use the image_list got above from thePredictionImagesobject.Now create aModelUsingobject by givinig it the path of the RFC model you want to use:frommodel_usingimportModelUsingfromprediction_imagesimportPredictionImagesprediction_images=PredictionImages(display_path)image_list=prediction_images.getdigitlist()#model_path is the path of the model you want to usemodel_using=ModelUsing(model_path)result=model_using.otp_prediction(image_list)#result is a list of every digits value like [5, 6, 8, 6, 8, 5]use
model_using = ModelUsing(model_path)result = model_using.otp_prediction(image_list)
#result is a list of every digits value like [5, 6, 8, 6, 8, 5]use
    model_using = ModelUsing(model_path)

    result = model_using.otp_prediction(image_list)
    #result is a list of every digits value like [5, 6, 8, 6, 8, 5]resultis the result of the prediction made by the model.