{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 🔵 [Projet DL : Lecture automatique des plaques d'immatriculation 🚗🕵️‍](https://github.com/sereromain/projet_detection_plaques_immatriculation.git)\n> Détection / lecture des plaques d'immatriculation dans des environnements non-contraints\n\n<img src=\"https://www.aaeon.com/_media/bnr_190628_3ywgpa_m.jpg\">\n\nCe Kaggle notebook contient le code de notre projet de Deep-Learning sur la détection et lecture de plaques d'immatriculation.\nIl fait appel à du code importé depuis le dépot GitHub suivant : https://github.com/sereromain/projet_detection_plaques_immatriculation.git\n\n### Ce notebook contient 2 parties principales : \n> `Entrainement` du réseau de détection des plaques \n\n> `Test` de la détection sur des images ou des vidéos\n\nAfin de mener à bien ce projet, nous nous sommes inspiré des travaux de [Sérgio Montazzolli Silva et Claudio Rosito Jung](https://openaccess.thecvf.com/content_ECCV_2018/papers/Sergio_Silva_License_Plate_Detection_ECCV_2018_paper.pdf) sur la thématique de la détection et lecture des plaques d'immatriculation.\n\n**Note :** L'entrainement du réseau peut prendre plusieurs heures il est donc conseillé de n'effectuer que l'inférence si votre temps est une ressource précieuse","metadata":{}},{"cell_type":"markdown","source":"# 🛠 Import du code GitHub","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working\n!rm -r /kaggle/working/alpr-unconstrained\n!git clone https://github.com/sereromain/projet_detection_plaques_immatriculation.git /kaggle/working/alpr-unconstrained","metadata":{"execution":{"iopub.status.busy":"2022-01-25T09:17:51.179855Z","iopub.execute_input":"2022-01-25T09:17:51.180218Z","iopub.status.idle":"2022-01-25T09:17:54.418084Z","shell.execute_reply.started":"2022-01-25T09:17:51.180183Z","shell.execute_reply":"2022-01-25T09:17:54.417129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📚 Séquençage de la vidéo d'entrée en images JPG","metadata":{}},{"cell_type":"code","source":"!rm -r /kaggle/working/video_jpgs/\n!rm -r /kaggle/working/video_output/\n!mkdir /kaggle/working/video_jpgs/\n!mkdir /kaggle/working/video_output/\n!ffmpeg -i /kaggle/input/video-of-traffic/traffic_video_input.mp4 -vf fps=30 -qscale:v 2 /kaggle/working/video_jpgs/out%d.jpg","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📁 Importation des poids pour les réseaux YOLO","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working/alpr-unconstrained/\n!bash get-networks.sh","metadata":{"execution":{"iopub.status.busy":"2022-01-25T09:17:56.877913Z","iopub.execute_input":"2022-01-25T09:17:56.878369Z","iopub.status.idle":"2022-01-25T09:18:19.98424Z","shell.execute_reply.started":"2022-01-25T09:17:56.878325Z","shell.execute_reply":"2022-01-25T09:18:19.983392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/yolovocweights/yolo-voc.weights /kaggle/working/alpr-unconstrained/data/vehicle-detector/yolo-voc.weights\n!cp /kaggle/input/yolovocweights/ocr-net.weights /kaggle/working/alpr-unconstrained/data/ocr/ocr-net.weights\n\n!cp /kaggle/input/20000-iterations-plate-detection/my-trained-model-15000-iterations-lr001-b128.h5 /kaggle/working/alpr-unconstrained/data/my-trained-model-15000-iterations-lr001-b128.h5\n!cp /kaggle/input/20000-iterations-plate-detection/my-trained-model-15000-iterations-lr001-b128.json /kaggle/working/alpr-unconstrained/data/my-trained-model-15000-iterations-lr001-b128.json","metadata":{"execution":{"iopub.status.busy":"2022-01-25T09:18:19.987983Z","iopub.execute_input":"2022-01-25T09:18:19.98827Z","iopub.status.idle":"2022-01-25T09:18:22.784768Z","shell.execute_reply.started":"2022-01-25T09:18:19.988238Z","shell.execute_reply":"2022-01-25T09:18:22.783815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!chmod a+x /kaggle/working/alpr-unconstrained/darknet/darknet","metadata":{"execution":{"iopub.status.busy":"2022-01-25T09:18:22.787552Z","iopub.execute_input":"2022-01-25T09:18:22.787968Z","iopub.status.idle":"2022-01-25T09:18:23.440766Z","shell.execute_reply.started":"2022-01-25T09:18:22.787916Z","shell.execute_reply":"2022-01-25T09:18:23.439786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!cp /kaggle/working/alpr-unconstrained/models/my-trained-model/my-trained-model-test-show_final.json /kaggle/working/my-trained-model-test-show_final.json\n#!cp /kaggle/working/video_output/results.csv /kaggle/working/results.csv","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🚅 `Entrainement` du réseau de détection des plaques d'immatriculation ","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working/alpr-unconstrained\n#!cp /kaggle/input/20000-iterations-plate-detection/my-trained-model_20000_iterations.h5 /kaggle/working/alpr-unconstrained/models/my-trained-model_20000_iterations.h5\n#!cp /kaggle/input/20000-iterations-plate-detection/my-trained-model_20000_iterations.json /kaggle/working/alpr-unconstrained/models/my-trained-model_20000_iterations.json\n#!mkdir models\n!python create-model.py eccv models/eccv-model-scracth\n!python train-detector.py --model /kaggle/working/alpr-unconstrained/models/eccv-model-scracth --name my-trained-model-test-show --train-dir /kaggle/input/images-annotations-cars --output-dir models/my-trained-model/ -op Adam -lr .001 -its 15000 -bs 128","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📈 Visualisation de l'entrainement 🌈","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\nloss_history = np.load(\"/kaggle/working/alpr-unconstrained/loss_history.npy\",allow_pickle=True)\nmean_loss_history = np.load(\"/kaggle/working/alpr-unconstrained/mean_loss_history.npy\",allow_pickle=True)\n\nmean_loss_history = [None]*499 + mean_loss_history.tolist()[1000:] + [None]*500\n\nwindow_size = 100\n  \ni = 0\n# Initialize an empty list to store moving averages\nmoving_averages = []\n  \n# Loop through the array to consider\n# every window of size 3\nwhile i < len(loss_history) - window_size + 1:\n    \n    # Store elements from i to i+window_size\n    # in list to get the current window\n    window = loss_history[i : i + window_size]\n  \n    # Calculate the average of current window\n    window_average = round(sum(window) / window_size, 2)\n      \n    # Store the average of current\n    # window in moving average list\n    moving_averages.append(window_average)\n      \n    # Shift window to right by one position\n    i += 1\n\nplt.plot(loss_history.tolist()[100:])\nplt.plot(moving_averages[100:])\nplt.legend([\"Loss\",\"Loss moyenne mobile (\"+str(window_size)+\")\"])\nplt.savefig(\"/kaggle/working/alpr-unconstrained/loss_graph.png\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!cp /kaggle/working/alpr-unconstrained/loss_graph.png /kaggle/working/loss_graph.png\n#!ls -l /kaggle/working/alpr-unconstrained/models/my-trained-model/\n#!cp /kaggle/working/alpr-unconstrained/models/my-trained-model/my-trained-model-test-show_final.h5 /kaggle/working/my-trained-model-test-show_final.h5\n#!cp /kaggle/working/alpr-unconstrained/models/my-trained-model/my-trained-model-test-show_final.json /kaggle/working/my-trained-model-test-show_final.json\n#import tensorflow as tf\n#json = open('/kaggle/working/alpr-unconstrained/models/my-trained-model_20000_iterations.json')\n#mdl_j = json.read()\n#model = tf.keras.models.model_from_json(mdl_j)\n#!ls -l /kaggle/working/alpr-unconstrained/models/my-trained-model/","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔍 `Test` (ou Inférence)","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working/alpr-unconstrained/\ninput  = \"/kaggle/working/alpr-unconstrained/samples/test/\"#\"/kaggle/working/video_jpgs\"\noutput = \"/kaggle/working/\"#\"/kaggle/working/video_output\"\nmodel  = \"/kaggle/working/alpr-unconstrained/data/my-trained-model-15000-iterations-lr001-b128.h5\" #\"/kaggle/working/alpr-unconstrained/models/my-trained-model/my-trained-model-test-show_final.h5\"\ncsv    = \"/kaggle/working/results.csv\" #\"/kaggle/working/video_output/results.csv\"\n!bash run.sh -i $input -o $output -c $csv -l $model","metadata":{"execution":{"iopub.status.busy":"2022-01-25T09:27:30.575268Z","iopub.execute_input":"2022-01-25T09:27:30.575656Z","iopub.status.idle":"2022-01-25T09:27:51.501014Z","shell.execute_reply.started":"2022-01-25T09:27:30.575607Z","shell.execute_reply":"2022-01-25T09:27:51.500132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2022-01-25T09:23:20.362478Z","iopub.execute_input":"2022-01-25T09:23:20.362861Z","iopub.status.idle":"2022-01-25T09:23:21.030423Z","shell.execute_reply.started":"2022-01-25T09:23:20.362824Z","shell.execute_reply":"2022-01-25T09:23:21.029459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ✂️ Transformation des JPG en MP4","metadata":{}},{"cell_type":"code","source":"!ffmpeg -r 30 -i /kaggle/working/video_output/out%d_output.jpg -vf scale=1920x1080 /kaggle/working/output_network_30fps_traffic.mp4","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}