From 224bebb81d2fcd9f83c51b0c048d1fafe6c59edf Mon Sep 17 00:00:00 2001 From: gdamms Date: Wed, 13 Apr 2022 09:12:04 +0200 Subject: [PATCH] feat: live --- .vscode/settings.json | 2 +- src/live.py | 40 ++++++++++++++++++++++++++++++++++++++++ src/notebook.ipynb | 10 ++++++++++ src/utils.py | 11 ++++++----- 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 src/live.py diff --git a/.vscode/settings.json b/.vscode/settings.json index 0cac076..5811aa0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,5 +11,5 @@ "source.organizeImports": true } }, - "jupyter.jupyterServerType": "remote" + "jupyter.jupyterServerType": "local" } \ No newline at end of file diff --git a/src/live.py b/src/live.py new file mode 100644 index 0000000..9e762f5 --- /dev/null +++ b/src/live.py @@ -0,0 +1,40 @@ +import subprocess + +import numpy as np +import PIL +import ppadb.client +import ppadb.command.serial +import ppadb.device +import tensorflow +from keras import models + +import utils + +RESIZED_SIZE = (100, 50, 3) +LABELS = ['octane', 'werewolf', 'breakout', 'aftershock'] + +# Load model +model = models.load_model('../models/model') + +utils.startup(need_focus=False) + +running = True + +while running: + utils.screenshot(filename='live', folder='../models') + + # Lecture de l'image + img = PIL.Image.open('../models/live') + + # print(f"img.size = {img.size}") + + # Redimensionnement de l'image + img = img.resize(RESIZED_SIZE[:-1], PIL.Image.ANTIALIAS) + + # print(f"img.size = {img.size}") + + X = np.asarray(img) + + Y = model.evaluate(X) + + print(f"Model detected : {LABELS[Y]}") diff --git a/src/notebook.ipynb b/src/notebook.ipynb index cde6e8e..fffbf52 100644 --- a/src/notebook.ipynb +++ b/src/notebook.ipynb @@ -549,6 +549,16 @@ "\n", "plot_training_analysis()\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Save the weights\n", + "model.save('../models/model')" + ] } ], "metadata": { diff --git a/src/utils.py b/src/utils.py index 901af44..db13b28 100644 --- a/src/utils.py +++ b/src/utils.py @@ -118,7 +118,7 @@ def start_scrpy() -> None: ) -def startup() -> None: +def startup(need_focus=True) -> None: subprocess.call(("sudo modprobe v4l2loopback").split()) @@ -127,7 +127,7 @@ def startup() -> None: users = detect_game(device, users) start_game(device, users) - while not (is_zen_mode(device) and is_focused(device) and is_v4l2_loaded()): + while need_focus and not (is_zen_mode(device) and is_focused(device) and is_v4l2_loaded()): time.sleep(DELAY_DETECT) time.sleep(DELAY_DETECT) @@ -135,11 +135,12 @@ def startup() -> None: start_scrpy() -def screenshot() -> uuid.UUID: - filename = uuid.uuid1() +def screenshot(filename=None, folder=DATA_FOLDER) -> uuid.UUID: + if filename == None: + filename = uuid.uuid1() subprocess.Popen( - f"ffmpeg -f video4linux2 -i /dev/video2 -frames:v 1 -filter:v crop=400:150:45:30 {DATA_FOLDER}/{filename}.jpg".split(), + f"ffmpeg -f video4linux2 -i /dev/video2 -frames:v 1 -filter:v crop=400:150:45:30 {folder}/{filename}.jpg".split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, )