From 5e42c632f03d9bbbb25f71301b6acb8d08e29b1a Mon Sep 17 00:00:00 2001 From: seba3c Date: Mon, 17 Apr 2017 20:41:34 -0300 Subject: [PATCH] Fixed some issues with new functionality --- images/migrations/0002_auto_20170417_2336.py | 25 +++++++++++++++++++ images/models.py | 20 ++++++++++++--- .../migrations/0007_auto_20170417_2235.py | 20 +++++++++++++++ notifications/telegram/models.py | 9 ++++++- notifications/telegram/scamera_bot_impl2.py | 25 ++++++++++++++++--- notifications/telegram/telegrambotrunner.py | 5 ++-- notifications/telegram/utils.py | 15 ++++++++--- requirements.txt | 3 ++- start_people_detector_test | 4 +-- 9 files changed, 109 insertions(+), 17 deletions(-) create mode 100644 images/migrations/0002_auto_20170417_2336.py create mode 100644 notifications/migrations/0007_auto_20170417_2235.py diff --git a/images/migrations/0002_auto_20170417_2336.py b/images/migrations/0002_auto_20170417_2336.py new file mode 100644 index 0000000..0bd4b26 --- /dev/null +++ b/images/migrations/0002_auto_20170417_2336.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.2 on 2017-04-17 23:36 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('images', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='peopledetectortest', + name='negative_samples_dir', + field=models.CharField(blank=True, max_length=150, null=True), + ), + migrations.AlterField( + model_name='peopledetectortest', + name='positive_samples_dir', + field=models.CharField(blank=True, max_length=150, null=True), + ), + ] diff --git a/images/models.py b/images/models.py index a17d306..d75f1bd 100644 --- a/images/models.py +++ b/images/models.py @@ -40,8 +40,8 @@ class PeopleDetectorTest(models.Model): null=True, blank=False) time_took = models.FloatField(default=0.0, null=False, blank=True) - positive_samples_dir = models.CharField(max_length=150, null=False, blank=False) - negative_samples_dir = models.CharField(max_length=150, null=False, blank=False) + positive_samples_dir = models.CharField(max_length=150, null=True, blank=True) + negative_samples_dir = models.CharField(max_length=150, null=True, blank=True) save_enhaced_images = models.BooleanField(default=False, null=False, blank=False) positive_samples_count = models.PositiveIntegerField("Positive Samples", @@ -59,9 +59,8 @@ class PeopleDetectorTest(models.Model): @classmethod def get_test(cls, test_name): - (obj, created) = cls.objects.get_or_create(title=test_name) + (obj, created) = cls.objects.get_or_create(title=test_name, state='running') if created: - obj.state = 'running' obj.running_timestamp = timezone.now() obj.save() return obj @@ -93,6 +92,9 @@ def run(self): self.time_took += result.time self.inc_NS() + self.finish() + + def finish(self): self.state = 'finished' self.save() @@ -258,3 +260,13 @@ def register_NS_FP(self): self.inc_NS() self.inc_FP() self.save() + + def register_PS_FN(self): + self.inc_PS() + self.inc_FN() + self.save() + + def register_NS_TN(self): + self.inc_NS() + self.inc_TN() + self.save() diff --git a/notifications/migrations/0007_auto_20170417_2235.py b/notifications/migrations/0007_auto_20170417_2235.py new file mode 100644 index 0000000..2221da1 --- /dev/null +++ b/notifications/migrations/0007_auto_20170417_2235.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.2 on 2017-04-17 22:35 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('notifications', '0006_auto_20170415_2211'), + ] + + operations = [ + migrations.AlterField( + model_name='telegrambot', + name='module_name', + field=models.CharField(choices=[('scamera_bot_impl1', 'Impl. v1.0'), ('scamera_bot_impl2', 'Impl. v2.0')], default='scamera_bot_impl1', help_text='Handler module (telegram bot API implementation)', max_length=25, verbose_name='Bot implementation'), + ), + ] diff --git a/notifications/telegram/models.py b/notifications/telegram/models.py index d95732e..122e133 100644 --- a/notifications/telegram/models.py +++ b/notifications/telegram/models.py @@ -15,11 +15,18 @@ class TelegramBot(models.Model): + TELEGRAM_BOT_IMPL_CHOICES = (('scamera_bot_impl1', 'Impl. v1.0'), + ('scamera_bot_impl2', 'Impl. v2.0')) + name = models.CharField(max_length=25, null=False, primary_key=True) # allows more than one telegram bot implementation with the same token token = models.CharField(max_length=100, null=False, unique=True) - module_name = models.CharField(max_length=25, null=True) + module_name = models.CharField("Bot implementation", + help_text="Handler module (telegram bot API implementation)", + max_length=25, null=False, + default='scamera_bot_impl1', + choices=TELEGRAM_BOT_IMPL_CHOICES) debug = models.BooleanField("Debug enabled", help_text="Enable online evaluation of each image received", diff --git a/notifications/telegram/scamera_bot_impl2.py b/notifications/telegram/scamera_bot_impl2.py index 36358e8..800ae20 100644 --- a/notifications/telegram/scamera_bot_impl2.py +++ b/notifications/telegram/scamera_bot_impl2.py @@ -7,7 +7,11 @@ from notifications.telegram.scamera_bot_base import (SCameraBotTelegramHandlers, UnregisterdNotificationUserProfile) -from notifications.telegram.utils import (POS_SAMPLE_TRUE_POS, NEG_SAMPLE_FALSE_POS) +from notifications.telegram.utils import (POS_SAMPLE_TRUE_POS, + NEG_SAMPLE_FALSE_POS, + POS_SAMPLE_FALSE_NEG, + NEG_SAMPLE_TRUE_NEG, + DISCARD_SAMPLE) logger = logging.getLogger(__name__) @@ -31,14 +35,25 @@ def handle_callback(self, bot, update): self._check_user_registered(update) query = update.callback_query if query.data in [POS_SAMPLE_TRUE_POS, - NEG_SAMPLE_FALSE_POS]: + NEG_SAMPLE_FALSE_POS, + POS_SAMPLE_FALSE_NEG, + NEG_SAMPLE_TRUE_NEG, + DISCARD_SAMPLE]: test = PeopleDetectorTest.get_test(self.telegrambot.name) if query.data == POS_SAMPLE_TRUE_POS: test.register_PS_TP() - text = "Image registered as 'Positive Sample - True Positive'" + text = "Image sample registered as 'Positive Sample - True Positive'" elif query.data == NEG_SAMPLE_FALSE_POS: test.register_NS_FP() - text = "Image registered as 'Negative Sample - False Positive'" + text = "Image sample registered as 'Negative Sample - False Positive'" + elif query.data == POS_SAMPLE_FALSE_NEG: + test.register_PS_FN() + text = "Image sample registered as 'Positive Sample - False Negative'" + elif query.data == NEG_SAMPLE_TRUE_NEG: + test.register_NS_TN() + text = "Image sample registered as 'Negative Sample - True Negative'" + else: + text = "Image sample 'discarded'" else: text = "Invalid callback data!" logger.error("Invalid callback data '%s'!", query) @@ -65,6 +80,8 @@ def test_stats(self, bot, update): msg += "Total negative samples: %d\n" % test.negative_samples_count msg += "True positives (TP): %d\n" % test.TP msg += "False positives (FP): %d\n" % test.FP + msg += "True negatives (TN): %d\n" % test.TN + msg += "False negatives (FN): %d\n" % test.FN self._send_message(bot, update, msg) def _build_updater(self): diff --git a/notifications/telegram/telegrambotrunner.py b/notifications/telegram/telegrambotrunner.py index 15160da..3bbfa44 100644 --- a/notifications/telegram/telegrambotrunner.py +++ b/notifications/telegram/telegrambotrunner.py @@ -20,6 +20,7 @@ def run_telelgram_bot(botname): telegram_bot_module = importlib.import_module(telegram_bot_module_name) updater = telegram_bot_module.get_telegram_updater(telegram_bot) logger.debug("Running telegram bot...") + logger.debug("Module implementation: '%s'", telegram_bot_module_name) updater.start_polling() logger.debug("Telegram bot %s running...", telegram_bot) updater.idle() @@ -27,5 +28,5 @@ def run_telelgram_bot(botname): logger.error("Telegram bot %s not implemented!", telegram_bot) except TelegramBot.DoesNotExist: logger.error("Telegram bot %s not registered!", botname) - except: - logger.exception("Error trying to run telegram bot %s!") + except Exception: + logger.exception("Error trying to run telegram bot %s!", botname) diff --git a/notifications/telegram/utils.py b/notifications/telegram/utils.py index fa7d43b..ea43146 100644 --- a/notifications/telegram/utils.py +++ b/notifications/telegram/utils.py @@ -2,13 +2,22 @@ POS_SAMPLE_TRUE_POS = 'ps-tp' NEG_SAMPLE_FALSE_POS = 'ns-fp' +POS_SAMPLE_FALSE_NEG = 'ps-fn' +NEG_SAMPLE_TRUE_NEG = 'ns-tn' +DISCARD_SAMPLE = 'discard' def send_live_test_keyboard(tbot, chat_id): - keyboard = [[telegram.InlineKeyboardButton("pos-sample/true-pos", - callback_data=POS_SAMPLE_TRUE_POS)], - [telegram.InlineKeyboardButton("neg-sample/false-pos", + keyboard = [[telegram.InlineKeyboardButton(POS_SAMPLE_TRUE_POS.upper(), + callback_data=POS_SAMPLE_TRUE_POS), + telegram.InlineKeyboardButton(NEG_SAMPLE_FALSE_POS.upper(), callback_data=NEG_SAMPLE_FALSE_POS)], + [telegram.InlineKeyboardButton(POS_SAMPLE_FALSE_NEG.upper(), + callback_data=POS_SAMPLE_FALSE_NEG), + telegram.InlineKeyboardButton(NEG_SAMPLE_TRUE_NEG.upper(), + callback_data=NEG_SAMPLE_TRUE_NEG)], + [telegram.InlineKeyboardButton(DISCARD_SAMPLE.upper(), + callback_data=DISCARD_SAMPLE)], ] reply_markup = telegram.InlineKeyboardMarkup(keyboard) tbot.sendMessage(chat_id=chat_id, diff --git a/requirements.txt b/requirements.txt index 80d4074..2092a9c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,5 @@ numpy==1.12.0 pygame==1.9.2 PyOpenGL==3.1.0 django-dbsettings==0.10.0 -imutils==0.3.10 \ No newline at end of file +imutils==0.3.10 +# opencv-python==3.2.0.7 alternative to install opencv from source code \ No newline at end of file diff --git a/start_people_detector_test b/start_people_detector_test index 69bceba..e9c673c 100755 --- a/start_people_detector_test +++ b/start_people_detector_test @@ -2,7 +2,7 @@ source config_enviroment -NEG_SAMPLES_DIR=/home/seba/dev/work/scamera-dataset/large/negatives -POS_SAMPLES_DIR=/home/seba/dev/work/scamera-dataset/large/positives +NEG_SAMPLES_DIR=/home/seba/dev/work/scamera-dataset/small/negatives +POS_SAMPLES_DIR=/home/seba/dev/work/scamera-dataset/small/positives exec python manage.py run_people_detector_test $NEG_SAMPLES_DIR $POS_SAMPLES_DIR