Skip to content

Commit

Permalink
Fixed some issues with new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
seba3c committed Apr 17, 2017
1 parent 8f64c82 commit 5e42c63
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 17 deletions.
25 changes: 25 additions & 0 deletions images/migrations/0002_auto_20170417_2336.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
20 changes: 16 additions & 4 deletions images/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()
20 changes: 20 additions & 0 deletions notifications/migrations/0007_auto_20170417_2235.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
9 changes: 8 additions & 1 deletion notifications/telegram/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 21 additions & 4 deletions notifications/telegram/scamera_bot_impl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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)
Expand All @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions notifications/telegram/telegrambotrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ 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()
except ImportError:
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)
15 changes: 12 additions & 3 deletions notifications/telegram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
imutils==0.3.10
# opencv-python==3.2.0.7 alternative to install opencv from source code
4 changes: 2 additions & 2 deletions start_people_detector_test
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5e42c63

Please sign in to comment.