[Jderobot-admin] jderobot-r865 - trunk/src/components/traffic-monitor-gui

redo en jderobot.org redo en jderobot.org
Dom Feb 24 20:04:41 CET 2013


Author: redo
Date: 2013-02-24 20:03:41 +0100 (Sun, 24 Feb 2013)
New Revision: 865

Added:
   trunk/src/components/traffic-monitor-gui/database.cpp
   trunk/src/components/traffic-monitor-gui/database.h
   trunk/src/components/traffic-monitor-gui/vehicle_model.cpp
   trunk/src/components/traffic-monitor-gui/vehicle_model.h
Modified:
   trunk/src/components/traffic-monitor-gui/CMakeLists.txt
   trunk/src/components/traffic-monitor-gui/viewgtk.cpp
   trunk/src/components/traffic-monitor-gui/viewgtk.h
Log:
-- Adding support for stats


Modified: trunk/src/components/traffic-monitor-gui/CMakeLists.txt
===================================================================
--- trunk/src/components/traffic-monitor-gui/CMakeLists.txt	2013-02-24 18:56:31 UTC (rev 864)
+++ trunk/src/components/traffic-monitor-gui/CMakeLists.txt	2013-02-24 19:03:41 UTC (rev 865)
@@ -13,6 +13,9 @@
 include_directories(${libglademm_INCLUDE_DIRS})
 link_directories(${libglademm_LIBRARY_DIRS})
 
+INCLUDE_DIRECTORIES(/usr/include/mysql++/)
+INCLUDE_DIRECTORIES(/usr/include/mysql/)
+
 SET( LIBS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../libs)
 SET( INTERFACES_CPP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../interfaces/cpp) # Directorio con las interfaces ICE en C++
 SET( LIBS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../libs) # Directorio donde se encuentran las librerias propias de jderobot
@@ -28,7 +31,7 @@
 ADD_CUSTOM_COMMAND(OUTPUT ${GENERATED_FILES} COMMAND slice2cpp  -I${CMAKE_CURRENT_SOURCE_DIR}/../../../src/interfaces/slice/ --output-dir ${GENERATED_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/traffic_monitor.ice)
 
 
-SET( SOURCE_FILES main.cpp viewgtk.cpp trafficmonitor_config.cpp ${GENERATED_FILES})
+SET( SOURCE_FILES main.cpp database.cpp viewgtk.cpp vehicle_model.cpp trafficmonitor_config.cpp ${GENERATED_FILES})
 
 SET( CMAKE_CXX_FLAGS " -O2 -std=gnu++0x")
 
@@ -45,6 +48,7 @@
 add_executable (trafficmonitor-gui ${SOURCE_FILES})
 
 TARGET_LINK_LIBRARIES( trafficmonitor-gui
+  -lmysqlpp
   ${gtkmm_LIBRARIES}
   ${cairomm_LIBRARIES}
   ${libglademm_LIBRARIES}

Added: trunk/src/components/traffic-monitor-gui/database.cpp
===================================================================
--- trunk/src/components/traffic-monitor-gui/database.cpp	                        (rev 0)
+++ trunk/src/components/traffic-monitor-gui/database.cpp	2013-02-24 19:03:41 UTC (rev 865)
@@ -0,0 +1,115 @@
+#include <iostream>
+#include "database.h"
+#include <stdlib.h>
+
+using namespace std;
+using namespace mysqlpp;
+
+namespace trafficmonitor{
+
+DataBase database;
+
+/**
+ *
+ */
+DataBase::DataBase()
+{
+   
+   m_conn = new Connection(false);
+}
+
+/**
+ *
+ */
+bool DataBase::connect()
+{
+   try
+   {
+      if(m_conn->connect("trafficmonitor_db", "localhost", "trafficmonitor", "trafficmonitor123"))
+      {
+         cout << "Database connected successfully" << endl;
+      }
+      else
+      {
+         cerr << "Error: Can't connect to the database" << endl;
+      }
+   }
+   catch (const std::exception& e)
+   {
+      cerr << "Error while connecting to the database. Details: " << e.what() << endl;
+      exit(1);
+   }
+}
+
+/**
+ *
+ */
+bool DataBase::disconnect()
+{
+
+}
+
+/**
+ *
+ */
+// bool DataBase::store (const Vehicle& vehicle)
+// {
+//    std::stringstream sql_insert;
+   
+//    Query query = m_conn->query();
+//    query << "INSERT INTO traffic_stats VALUES("
+//          << vehicle.get_id()
+//          << ",'"
+//          << VehicleModel::get_model_desc(vehicle.get_matched_class())
+//          << "',"
+//          << vehicle.get_speed()
+//          << ");";
+
+//    const std::string& ins_query = sql_insert.str();
+   
+//    try
+//    {
+//       query.execute();
+//    }
+//    catch (const std::exception &e)
+//    {
+      
+//    }
+// }
+
+/**
+ *
+ */
+mysqlpp::StoreQueryResult DataBase::getLastVehicles(int limit_query)
+{
+   /* Now SELECT */
+   Query query = m_conn->query();
+   query << "select * from traffic_stats order by timestamp desc limit " << limit_query;
+   StoreQueryResult ares = query.store();   
+   return ares;
+}
+
+/**
+ *
+ */
+int DataBase::getCount(std::string category)
+{
+   /* Now SELECT */
+   Query query = m_conn->query();
+   query << "select category, count(*) from traffic_stats where category = \"" << category << "\"";
+   StoreQueryResult result = query.store();
+
+   // This query returns a table in the following format, so we just pick the count(*) column value
+   //
+   // +----------+----------+
+   // | category | count(*) |
+   // +----------+----------+
+   // | Van      |        7 |
+   // +----------+----------+
+   //
+
+   return stoi(result[0]["count(*)"].c_str());
+}
+
+
+} // trafficmonitor namespace


Property changes on: trunk/src/components/traffic-monitor-gui/database.cpp
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/src/components/traffic-monitor-gui/database.h
===================================================================
--- trunk/src/components/traffic-monitor-gui/database.h	                        (rev 0)
+++ trunk/src/components/traffic-monitor-gui/database.h	2013-02-24 19:03:41 UTC (rev 865)
@@ -0,0 +1,47 @@
+#ifndef DATABASE_H
+#define DATABASE_H
+
+#include <mysql++.h>
+
+namespace trafficmonitor{
+
+class DataBase
+{
+public:
+   
+   /**
+    *
+    */
+   DataBase();
+   virtual ~DataBase(){};
+
+   /**
+    *
+    */
+   bool connect();
+
+   /**
+    *
+    */
+   bool disconnect();
+
+   /**
+    *
+    */
+   int getCount(std::string category);
+
+   /**
+    *
+    */ 
+   mysqlpp::StoreQueryResult getLastVehicles(int limit_query);
+
+private:
+
+   mysqlpp::Connection *m_conn; 
+};
+
+extern DataBase database;
+
+}
+
+#endif


Property changes on: trunk/src/components/traffic-monitor-gui/database.h
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/src/components/traffic-monitor-gui/vehicle_model.cpp
===================================================================
--- trunk/src/components/traffic-monitor-gui/vehicle_model.cpp	                        (rev 0)
+++ trunk/src/components/traffic-monitor-gui/vehicle_model.cpp	2013-02-24 19:03:41 UTC (rev 865)
@@ -0,0 +1,129 @@
+#include <stdio.h>
+#include <string.h>
+#include "vehicle_model.h"
+
+const char* VehicleModel::models_names[MAX_MODELS] = {
+   "Invalid",
+   "Motocycle",
+   "Car",
+   "SUV",
+   "Van",
+   "Truck",
+};
+
+const char* VehicleModel::models_description[MAX_MODELS] = {
+   "INV",
+   "Motorcycle",
+   "Car",
+   "SUV",
+   "Van",
+   "Truck/Bus",
+};
+
+/**
+ *  The units are in meteres
+ *
+ *   z / 
+ *    /  
+ *   |  / 
+ * x | / 
+ *   |/____ 
+ *      y 
+ *
+ */
+T3dmodel VehicleModel::models_dimensions[MAX_MODELS] = {
+  
+   /** invalid vehicle*/
+   {0,0,0},
+   
+   /** MOTO CYCLE*/
+   {1.2, 0.5, 2},
+   
+   /** CAR **/
+   {1.3, 1.6, 4},
+
+   /** SUV **/
+   {1.8, 1.7, 4.5},
+   
+   /** VAN **/
+   {2, 2, 7},
+   
+   /** TRUCK **/
+   {3.5, 2.5, 13.5},
+};
+
+/**
+ *
+ */
+const char* VehicleModel::get_model_name(tvehicle_category category){
+   
+   if (VALID_CATEGORY(category))
+      return models_names[category];
+   else
+      return models_names[INVALID_VEHICLE_CLASS];
+}
+
+/**
+ *
+ */
+float VehicleModel::get_category_dimension_x(tvehicle_category category){
+   
+   if (VALID_CATEGORY(category))
+      return models_dimensions[category].x;
+   else
+      return -1;
+}
+
+/**
+ *
+ */
+float VehicleModel::get_category_dimension_y(tvehicle_category category){
+   
+   if (VALID_CATEGORY(category))
+      return models_dimensions[category].y;
+   else
+      return -1;
+}
+
+/**
+ *
+ */
+float VehicleModel::get_category_dimension_z(tvehicle_category category){
+   
+   if (VALID_CATEGORY(category))
+      return models_dimensions[category].z;
+   else
+      return -1;
+}
+
+/**
+ *
+ */
+const char* VehicleModel::get_model_desc(tvehicle_category category){
+   
+   if (VALID_CATEGORY(category))
+      return models_description[category];
+   else
+   {
+      return models_description[INVALID_VEHICLE_CLASS];
+   }
+}
+
+/**
+ *
+ */
+tvehicle_category VehicleModel::get_model_id(const std::string model_name){
+
+   tvehicle_category i;
+  
+   for (i=MOTORCYCLE; i<MAX_MODELS; i++)
+   {
+      if (strcmp(model_name.c_str(), models_names[i]) == 0)
+      {
+         printf(" %s has been selected\n",models_names[i]);
+         return i;
+      }
+   }
+  
+   return INVALID_VEHICLE_CLASS;
+}


Property changes on: trunk/src/components/traffic-monitor-gui/vehicle_model.cpp
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/src/components/traffic-monitor-gui/vehicle_model.h
===================================================================
--- trunk/src/components/traffic-monitor-gui/vehicle_model.h	                        (rev 0)
+++ trunk/src/components/traffic-monitor-gui/vehicle_model.h	2013-02-24 19:03:41 UTC (rev 865)
@@ -0,0 +1,106 @@
+/*
+ *  Copyright (C) 1997-2008 JDE Developers Team
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see http://www.gnu.org/licenses/. 
+ *
+ *  Authors : Redouane Kachach <redo.robot at gmail.com>
+ *
+ */
+#ifndef _VEHICLE_MODEL_
+#define _VEHICLE_MODEL_
+
+#include <string>
+
+/**
+ *
+ */
+typedef enum models_ids 
+{
+   INVALID_VEHICLE_CLASS,
+   MOTORCYCLE,
+   CAR,
+   SUV,
+   VAN,
+   TRUCK,
+   MAX_MODELS
+} tvehicle_category;
+
+/**
+ *
+ */
+typedef struct 
+{
+   float x;
+   float y;
+   float z;
+} T3dmodel;
+
+#define VALID_CATEGORY(c) (c>INVALID_VEHICLE_CLASS && c<MAX_MODELS)
+
+/**
+ *
+ */
+inline tvehicle_category& operator++(tvehicle_category& m) 
+{ 
+   return (m=tvehicle_category(m+1)); 
+} 
+
+/**
+ *
+ */
+inline tvehicle_category operator++(tvehicle_category& m,int) 
+{ 
+   tvehicle_category mret = m; 
+   ++m; 
+   return mret; 
+} 
+
+/**
+ *
+ */
+class VehicleModel{
+
+public:
+   
+   /**
+    *
+    */
+   static const char* get_model_name(tvehicle_category category);
+
+   /**
+    *
+    */
+   static const char* get_model_desc(tvehicle_category category);
+
+   /**
+    * Returns the category dimension (x,y or z) if the category is valid
+    * and -1 if the passed category is not valid.
+    */
+   static float get_category_dimension_x(tvehicle_category category);
+   static float get_category_dimension_y(tvehicle_category category);
+   static float get_category_dimension_z(tvehicle_category category);
+   
+   /**
+    *
+    */
+   static tvehicle_category get_model_id(const std::string model_name);
+
+private:
+   
+   static const char* models_names[MAX_MODELS];
+   static const char* models_description[MAX_MODELS];
+   static T3dmodel models_dimensions[MAX_MODELS];
+};
+
+#endif


Property changes on: trunk/src/components/traffic-monitor-gui/vehicle_model.h
___________________________________________________________________
Added: svn:executable
   + *

Modified: trunk/src/components/traffic-monitor-gui/viewgtk.cpp
===================================================================
--- trunk/src/components/traffic-monitor-gui/viewgtk.cpp	2013-02-24 18:56:31 UTC (rev 864)
+++ trunk/src/components/traffic-monitor-gui/viewgtk.cpp	2013-02-24 19:03:41 UTC (rev 865)
@@ -13,6 +13,8 @@
 #include <unistd.h>
 
 #include "viewgtk.h"
+#include "database.h"
+#include "vehicle_model.h"
 
 #define DISTANCE_2D(a,b) sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) )
 
@@ -27,6 +29,13 @@
    refXml(Gnome::Glade::Xml::create(gladepath)), //FIXME: check for existence
    m_currentFrame(current_frame),   
 
+   INIT_WIDGET(vehicles_stats, refXml),
+   INIT_WIDGET(moto_view , refXml),
+   INIT_WIDGET(car_view , refXml),
+   INIT_WIDGET(suv_view , refXml),
+   INIT_WIDGET(van_view , refXml),
+   INIT_WIDGET(truck_view , refXml),
+   INIT_WIDGET(total_view, refXml),
    INIT_WIDGET(mainwindow, refXml),
    INIT_WIDGET(input_image_window, refXml),
    INIT_WIDGET(play, refXml),
@@ -47,6 +56,8 @@
    INIT_WIDGET(show_klt_points, refXml),
    INIT_WIDGET(drawingarea_input_image, refXml)
 {
+
+   database.connect();
    
    /* initialize random seed: */
    srand ( time(NULL) );
@@ -215,6 +226,7 @@
       Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
       display(m_currentFrame);
       display_tracking_zone(cr);
+      update_stats();
       // display_detection_zone(cr);
    }
 
@@ -283,7 +295,81 @@
       drawingarea_input_image->set_size_request(img_rgb8.width, img_rgb8.height);
    }
 }
-    
 
+/**
+ *
+ */
+void ViewGtk::update_stats()
+{
+   std::stringstream stats_text;
+   mysqlpp::StoreQueryResult result = database.getLastVehicles(20);
 
+   for (size_t i = 0; i < result.num_rows(); i++)
+   {
+      stats_text << result[i]["vehicle_id"]
+                 << " :: "
+                 << std::fixed
+                 << std::setprecision(2)
+                 << result[i]["speed"]
+                 << "  km/h";
+            
+      /** When classifying we show the vehicle class also*/
+      if (cfg->classify)
+         stats_text << " -- " << result[i]["category"] << endl;
+      else
+         stats_text << endl;
+
+      vehicles_stats->get_buffer()->set_text(stats_text.str());
+      vehicles_stats->show();
+   }
+
+   update_vehicles_counts();
+}
+
+/**
+ *
+ */
+void  ViewGtk::update_vehicles_counts()
+{
+   std::stringstream ss;
+   int motorcycles_count = database.getCount(VehicleModel::get_model_desc(MOTORCYCLE));
+   int trucks_count      = database.getCount(VehicleModel::get_model_desc(TRUCK));
+   int cars_count        = database.getCount(VehicleModel::get_model_desc(CAR));
+   int suvs_count        = database.getCount(VehicleModel::get_model_desc(SUV));
+   int vans_count        = database.getCount(VehicleModel::get_model_desc(VAN));
+
+   ss << motorcycles_count;
+   moto_view->get_buffer()->set_text(ss.str());
+   ss.str("");
+
+   ss << cars_count;
+   car_view->get_buffer()->set_text(ss.str());
+   ss.str("");
+
+   ss << suvs_count;
+   suv_view->get_buffer()->set_text(ss.str());
+   ss.str("");
+      
+   ss << vans_count;
+   van_view->get_buffer()->set_text(ss.str());
+   ss.str("");
+
+   ss << trucks_count;
+   truck_view->get_buffer()->set_text(ss.str());
+   ss.str("");
+      
+   int sum = motorcycles_count + cars_count + suvs_count + vans_count + trucks_count;
+      
+   ss << sum;
+   total_view->get_buffer()->set_text(ss.str());
+   ss.str("");
+
+   moto_view->show();
+   car_view->show();
+   suv_view->show();
+   van_view->show();
+   truck_view->show();
+   total_view->show();
+}
+
 } //namespace

Modified: trunk/src/components/traffic-monitor-gui/viewgtk.h
===================================================================
--- trunk/src/components/traffic-monitor-gui/viewgtk.h	2013-02-24 18:56:31 UTC (rev 864)
+++ trunk/src/components/traffic-monitor-gui/viewgtk.h	2013-02-24 19:03:41 UTC (rev 865)
@@ -35,7 +35,8 @@
    bool onDrawingAreaMainExposeEvent(GdkEventExpose* event);
    bool onDrawingAreaButtonPressEvent(GdkEventButton* event);
    void findNearestPoint(int y, int x);
-
+   void update_stats();
+   void  update_vehicles_counts();
    
    void update_play_cfg()
       {
@@ -77,6 +78,16 @@
    Widget<Gtk::CheckButton> show_bounding_box;
    Widget<Gtk::CheckButton> show_klt_points;
 
+   //Text views
+   Widget<Gtk::TextView> vehicles_stats;
+   Widget<Gtk::TextView> moto_view;     
+   Widget<Gtk::TextView> car_view;
+   Widget<Gtk::TextView> suv_view;
+   Widget<Gtk::TextView> van_view;
+   Widget<Gtk::TextView> truck_view;
+   Widget<Gtk::TextView> total_view;
+
+   
    TrafficMonitorAlgorithmConfig* cfg;
    colorspaces::Image& m_currentFrame;
 };



More information about the Jderobot-admin mailing list