From e2c7880ca29a1104f64ecff3d06473d2e6de2f2d Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sat, 6 Apr 2019 19:11:40 +0200 Subject: [PATCH 1/4] Print. --- server/nightr/app.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server/nightr/app.py b/server/nightr/app.py index f1f534e..07842cd 100644 --- a/server/nightr/app.py +++ b/server/nightr/app.py @@ -34,9 +34,11 @@ strategies = { @app.route("/", methods=["GET", "POST"]) def probabilities(): + if request.method == 'GET': + logger.warning("GET request: using default context parameters") phone_data = request.get_json(force=True) - context = Context(**phone_data) logger.debug("phone_data:\n%s", json.dumps(phone_data, indent=2)) + context = Context(**phone_data) logger.debug("Context: %s", context) predictions: List[dict] = [] From 55977d9dc4debc869d2a10eb6946f305fd64881f Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sat, 6 Apr 2019 19:22:48 +0200 Subject: [PATCH 2/4] [data] --- server/nightr/app.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/server/nightr/app.py b/server/nightr/app.py index 07842cd..7ba6a99 100644 --- a/server/nightr/app.py +++ b/server/nightr/app.py @@ -34,11 +34,14 @@ strategies = { @app.route("/", methods=["GET", "POST"]) def probabilities(): - if request.method == 'GET': + if request.method == "GET": logger.warning("GET request: using default context parameters") - phone_data = request.get_json(force=True) - logger.debug("phone_data:\n%s", json.dumps(phone_data, indent=2)) - context = Context(**phone_data) + context = Context() + else: + phone_data = request.get_json(force=True) + logger.debug("phone_data:\n%s", json.dumps(phone_data, indent=2)) + context = Context(**phone_data["data"]) + logger.debug("Context: %s", context) predictions: List[dict] = [] From d65ea171d8231c14ac62e27cdedf5fca781e4c13 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sat, 6 Apr 2019 19:45:38 +0200 Subject: [PATCH 3/4] Image. --- server/nightr/app.py | 5 +++-- server/nightr/strategies/miloStrats.py | 9 +++------ server/nightr/util.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/server/nightr/app.py b/server/nightr/app.py index 7ba6a99..3eecf5e 100644 --- a/server/nightr/app.py +++ b/server/nightr/app.py @@ -42,15 +42,16 @@ def probabilities(): logger.debug("phone_data:\n%s", json.dumps(phone_data, indent=2)) context = Context(**phone_data["data"]) - logger.debug("Context: %s", context) + #logger.debug("Context: %s", context) predictions: List[dict] = [] for name, strategy in strategies.items(): try: + logger.debug("Executing %s..", name) start = timeit.default_timer() prediction = strategy(context) stop = timeit.default_timer() - logger.debug("Execution time for %s: %ss", name, stop - start) + logger.debug("Execution time for %s: %ss", name, round(stop - start, 3 )) except Exception as e: logger.warning("Strategy '%s' failed:", name) logger.exception(e) diff --git a/server/nightr/strategies/miloStrats.py b/server/nightr/strategies/miloStrats.py index 593c981..3fc4c43 100644 --- a/server/nightr/strategies/miloStrats.py +++ b/server/nightr/strategies/miloStrats.py @@ -1,8 +1,6 @@ from datetime import datetime -from pathlib import Path -import requests -import cv2 +import requests from pytz import timezone from ..util import Context, Prediction @@ -12,12 +10,11 @@ def camImgStrat(context : Context) -> Prediction: """ The contents of the camera image """ - img = cv2.imread(str(Path(__file__).parent.joinpath("night.jpg")), 0) + img = context.image average = img.mean(axis=0).mean(axis=0) p = Prediction() p.weight = 0.7 - - if average < 100: + if average < 100: p.probability = 1.0 p.reasons.append('Image was dark') else: diff --git a/server/nightr/util.py b/server/nightr/util.py index 5b2bc3c..ce7d143 100644 --- a/server/nightr/util.py +++ b/server/nightr/util.py @@ -1,16 +1,30 @@ +import base64 from dataclasses import dataclass, field +from pathlib import Path from typing import List, Dict +import cv2 +import numpy as np + @dataclass class Context: battery: float = 1.0 position: Dict[str, float] = field(default_factory=lambda: {'latitude': 53.0, 'longitude': 9.0}) + image: np.ndarray = None # App settings in_australia: bool = False flat_earth: bool = False + def __post_init__(self): + if self.image is None: # no image given + self.image = cv2.imread(str(Path(__file__).parent.joinpath("gray.png"))) + else: + img_original = base64.b64decode(self.image) + img_as_np = np.frombuffer(img_original, dtype=np.uint8) + self.image = cv2.imdecode(img_as_np, flags=1) + @dataclass class Prediction: From 253c3762c58d5a95ade836e0cbe647eca52ce85a Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sat, 6 Apr 2019 19:47:02 +0200 Subject: [PATCH 4/4] fuck jeg er god --- server/nightr/gray.png | Bin 0 -> 155 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 server/nightr/gray.png diff --git a/server/nightr/gray.png b/server/nightr/gray.png new file mode 100644 index 0000000000000000000000000000000000000000..0a0f09ff00737f2010f4fe8335fd83761c7bd297 GIT binary patch literal 155 zcmeAS@N?(olHy`uVBq!ia0vp^AT}2V6Od#Ih{XE z)7O>#F$S|xv6<249-QVi6yBi3gww484B*6z5(HleBwYw vqMj~}ArhC9b22g#4)D&9_|P;_i