{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"},{"sourceId":7095532,"sourceType":"datasetVersion","datasetId":4089322}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom fastai.vision.all import *\n\nclass DriverBehaviorPredictor:\n    def __init__(self, model_path):\n        self.learn = load_learner(model_path)\n\n        # Define labels\n        self.labels = {\n            'c0': 'safe driving',\n            'c1': 'texting - right',\n            'c2': 'talking on the phone - right',\n            'c3': 'texting - left',\n            'c4': 'talking on the phone - left',\n            'c5': 'operating the radio',\n            'c6': 'drinking',\n            'c7': 'reaching behind',\n            'c8': 'hair and makeup',\n            'c9': 'talking to passenger'\n        }\n    # returns a label and probability that the driver is distracted\n    # label will be used in andriod \n    # save both output to firebase and send the prob to matlab as input\n    def predict_behavior(self, img_path):\n        # Load and preprocess the single image you want to predict\n        img = PILImage.create(img_path)\n\n        # Make the prediction\n        pred, _, prob_tensor = self.learn.predict(img)\n\n        # Convert tensor to a Python list\n        prob_list = prob_tensor.numpy().tolist()\n        prob_is_distracted = sum(prob_list) - prob_list[0]\n        # Return prediction and probability list\n        return self.labels[pred], prob_is_distracted\n\n# Example usage:\nmodel_path = '/kaggle/input/ml-model/cnn_model.pth'\ndriver_predictor = DriverBehaviorPredictor(model_path)\n\nimg_path = '/kaggle/input/state-farm-distracted-driver-detection/imgs/test/img_10.jpg'\nprediction, probability = driver_predictor.predict_behavior(img_path)\nprint(\"Prediction:\", prediction)\nprint(\"Probability that driver is distracted:\", probability)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-04T07:11:28.351018Z","iopub.execute_input":"2023-12-04T07:11:28.351401Z","iopub.status.idle":"2023-12-04T07:11:33.343625Z","shell.execute_reply.started":"2023-12-04T07:11:28.351354Z","shell.execute_reply":"2023-12-04T07:11:33.342216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}