Partie technique Pharmalog

De Learning Lab Environnements Connectés
Sauter à la navigation Sauter à la recherche

Matériel utilisé

Lors de ce workshop nous avons utilisé une carte électronique Raspberry Pi 2-Model B. Il était nécessaire de détecter un QR-code. Pour cela nous avons décidé d'utiliser une Raspberry Pi Camera Board et donc une détection via module vidéo.


Code source

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <zbar.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h> 
#include <string.h>
using namespace cv;
using namespace std;
using namespace zbar;
static int callback(void *data, int argc, char **argv, char **azColName)
   {
   int i;
   fprintf(stderr, "%s", (const char*)data);
   for(i=0; i<argc; i++){
      printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
   }
   printf("\n");
   return 0;
}

int main(int argc, char *argv[])
{
	String res;
	char *res2;
    VideoCapture cap(0); // open the video camera no. 0
    cap.set(CV_CAP_PROP_FRAME_WIDTH,800);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT,640);
   if (!cap.isOpened()) // if not success, exit program
   {
      cout << "Cannot open the video cam" << endl;
      return -1;
   }

   ImageScanner scanner;
   scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

   double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
   double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
   bool flag;
   
   cout << "Frame size : " << dWidth << " x " << dHeight << endl;

   // namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
 flag = true;
   while (flag==true) 
   {
      Mat frame;
      bool bSuccess = cap.read(frame); // read a new frame from video

      if (!bSuccess) //if not success, break loop
      {
         cout << "Cannot read a frame from video stream" << endl;
         break;
      }

      Mat grey;
      cvtColor(frame, grey, CV_BGR2GRAY);

      int width = frame.cols;
      int height = frame.rows;
      uchar *raw = (uchar *)grey.data;
      // wrap image data
      Image image(width, height, "Y800", raw, width * height);

      // scan the image for barcodes
      int n = scanner.scan(image);

      // extract results
      for(Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol)
      {
         vector<Point> vp;
         res=symbol->get_data();
         // do something useful with results
        // cout << "decoded " << symbol->get_type_name() << " symbol \"" << res << "\"" << endl;

	int n = symbol->get_location_size();
        for(int i=0;i<n;i++)
        {
           vp.push_back(Point(symbol->get_location_x(i), symbol->get_location_y(i)));
        }

        RotatedRect r = minAreaRect(vp);
        Point2f pts[4];
        r.points(pts);

	res2=&res[0];

        for(int i=0;i<4;i++)
		{
          line(frame, pts[i], pts[(i+1)%4], Scalar(255,0,0), 3);
        }

    cout<<"Angle: "<<r.angle<<endl;
	flag = false;// get the correct data from the Qr code
     }

     
   }
    


   sqlite3 *db;
   char *zErrMsg = 0;
   int rc;
   char *sql;
   char *abo="SELECT nom, stock FROM temps WHERE id=";
   const char* data = "Callback function called";
   char *res3;
   strcpy(res3, abo);
   strcat(res3, res2);

   /* Open database */
   rc = sqlite3_open("mydatabase.db", &db);
   if( rc )
   {
      fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
      exit(0);
   }
       else
	   {
          fprintf(stderr, "Opened database successfully\n");
       }
   /* Create SQL statement */
   sql =res3;

   /* Execute SQL statement */
   rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
   if( rc != SQLITE_OK )
   {
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   }
   else
     {
        fprintf(stdout, "Operation done successfully\n");
     }
   sqlite3_close(db);

   return 0;
}

Différents étape du code

Cahier des charges

id nom notice notice avancée stock
1 dafalgan
Initialisation
  • Plug & Play
  • Autonomie
  • Dimension
Détection
  • Détection mouvement/présence = démarrage du système.
  • Tapis = Impact
  • Confirmation mouvement
  • Question/réponse => vocal
Mise en veille
  • Sortie de la salle de bain