Exemplo n.º 1
0
// This function takes tif/box pair of files and runs recognition on the image,
// while making sure that the word bounds that tesseract identified roughly
// match to those specified by the input box file. For each word (ngram in a
// single bounding box from the input box file) it outputs the ocred result,
// the correct label, rating and certainty.
void Tesseract::recog_training_segmented(const STRING &fname,
                                         PAGE_RES *page_res,
                                         volatile ETEXT_DESC *monitor,
                                         FILE *output_file) {
  STRING box_fname = fname;
  const char *lastdot = strrchr(box_fname.string(), '.');
  if (lastdot != NULL) box_fname[lastdot - box_fname.string()] = '\0';
  box_fname += ".box";
  // read_next_box() will close box_file
  FILE *box_file = open_file(box_fname.string(), "r");

  PAGE_RES_IT page_res_it;
  page_res_it.page_res = page_res;
  page_res_it.restart_page();
  char label[kBoxReadBufSize];

  // Process all the words on this page.
  TBOX tbox;  // tesseract-identified box
  TBOX bbox;  // box from the box file
  bool keep_going;
  int line_number = 0;
  do {
    keep_going = read_t(&page_res_it, &tbox);
    keep_going &= read_b(applybox_page, &line_number, box_file, label, &bbox);
    // Align bottom left points of the TBOXes.
    while (keep_going &&
           !NearlyEqual<int>(tbox.bottom(), bbox.bottom(), kMaxBoxEdgeDiff)) {
      keep_going = (bbox.bottom() < tbox.bottom()) ?
          read_t(&page_res_it, &tbox) :
            read_b(applybox_page, &line_number, box_file, label, &bbox);
    }
    while (keep_going &&
           !NearlyEqual<int>(tbox.left(), bbox.left(), kMaxBoxEdgeDiff)) {
      keep_going = (bbox.left() > tbox.left()) ? read_t(&page_res_it, &tbox) :
        read_b(applybox_page, &line_number, box_file, label, &bbox);
    }
    // OCR the word if top right points of the TBOXes are similar.
    if (keep_going &&
        NearlyEqual<int>(tbox.right(), bbox.right(), kMaxBoxEdgeDiff) &&
        NearlyEqual<int>(tbox.top(), bbox.top(), kMaxBoxEdgeDiff)) {
        ambigs_classify_and_output(page_res_it.prev_word(),
                                   page_res_it.prev_row(),
                                   page_res_it.prev_block(),
                                   label, output_file);
    }
  } while (keep_going);
}
Exemplo n.º 2
0
ssize_t read_msg_t(client_t* client, msg_t** msg, double timeout)
{
    if (qtun->use_udp)
    {
        *msg = pool_room_realloc(&qtun->pool, RECV_ROOM_IDX, qtun->recv_buffer_len);
        return read_t(client, *msg, qtun->recv_buffer_len, timeout);
    }
    else
    {
        ssize_t rc;
        size_t len;

        *msg = pool_room_realloc(&qtun->pool, RECV_ROOM_IDX, sizeof(msg_t));
        if (*msg == NULL) return -2;
        rc = read_t(client, *msg, sizeof(**msg), timeout);
        if (rc <= 0)
        {
            pool_room_free(&qtun->pool, RECV_ROOM_IDX);
            *msg = NULL;
            return rc;
        }
        len = msg_data_length(*msg);
        *msg = pool_room_realloc(&qtun->pool, RECV_ROOM_IDX, sizeof(msg_t) + len);
        if (*msg == NULL) return -2;
        rc = read_t(client, (*msg)->data, len, timeout);
        if (rc <= 0 && len)
        {
            pool_room_free(&qtun->pool, RECV_ROOM_IDX);
            *msg = NULL;
            return rc;
        }

        if (checksum(*msg, sizeof(msg_t) + len))
        {
            SYSLOG(LOG_ERR, "Invalid msg");
            pool_room_free(&qtun->pool, RECV_ROOM_IDX);
            *msg = NULL;
            return -2;
        }

        SYSLOG(LOG_INFO, "read msg length: %lu", (unsigned long)len);
        return rc + sizeof(msg_t);
    }
}
Exemplo n.º 3
0
uint64_t read_leb128_rest(struct reader *r, bool is_signed) {
    uint64_t result = 0;
    int shift;
    uint8_t c = 0x80;
    for(shift = 0; c & 0x80; shift += 7) {
        c = read_t(uint8_t, r);
        result |= (c & 0x7f) << shift;
    }
    if(is_signed && shift > 0 && (result & (1 << (shift - 1))))
        result |= -(1 << shift);
    return result;
}
Exemplo n.º 4
0
int connect_server(char* ip, unsigned short port)
{
    int fd, rc;
    struct sockaddr_in addr = {0};
    unsigned char buffer[1024] = {0};
    ssize_t readen;

    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd == -1)
    {
        perror("socket");
        return -1;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    if (inet_aton(ip, &addr.sin_addr) == 0)
    {
        fprintf(stderr, "Convert ip address error!\n");
        close(fd);
        return -1;
    }

    rc = connect(fd, (struct sockaddr*)&addr, sizeof(addr));
    if (rc == -1)
    {
        perror("connect");
        close(fd);
        return -1;
    }

    readen = read_t(fd, buffer, sizeof(buffer), 5);
    if (readen > 0 && strcmp(SERVER_AUTH_MSG, buffer) == 0)
    {
        pid_t pid = getpid();
        strcpy(buffer, CLIENT_AUTH_MSG);
        client_id = pid;
        client_id = htonl(client_id);
        memcpy(&buffer[sizeof(CLIENT_AUTH_MSG) - sizeof(client_id) - 1], &client_id, sizeof(client_id));
        write_n(fd, buffer, sizeof(CLIENT_AUTH_MSG) - 1);
    }
    else
    {
        fprintf(stderr, "is not allowed server\n");
        close(fd);
        return -1;
    }

    return fd;
}
Exemplo n.º 5
0
static void accept_and_check(int bindfd)
{
    int fd = accept(bindfd, NULL, NULL);
    char buffer[sizeof(CLIENT_AUTH_MSG) - 1];
    ssize_t readen;
    if (fd == -1) return;

    write_n(fd, SERVER_AUTH_MSG, sizeof(SERVER_AUTH_MSG) - 1);
    readen = read_t(fd, buffer, sizeof(buffer), 5);
    if (readen <= 0)
    {
        struct sockaddr_in addr;
        socklen_t len = sizeof(addr);
        char* str;

        if (getpeername(fd, (struct sockaddr*)&addr, &len) == -1)
        {
            perror("getpeername");
            close(fd);
            return;
        }
        str = inet_ntoa(addr.sin_addr);
        fprintf(stderr, "authcheck failed: %s\n", str);
        close(fd);
        return;
    }

    if (strncmp(buffer, CLIENT_AUTH_MSG, sizeof(CLIENT_AUTH_MSG) - 5) == 0)
    {
        memcpy(&client_id, &buffer[sizeof(CLIENT_AUTH_MSG) - sizeof(client_id) - 2], sizeof(client_id));
        client_id = ntohl(client_id);
        connfd = fd;
    }
    else
    {
        struct sockaddr_in addr;
        socklen_t len = sizeof(addr);
        char* str;

        if (getpeername(fd, (struct sockaddr*)&addr, &len) == -1)
        {
            perror("getpeername");
            close(fd);
            return;
        }
        str = inet_ntoa(addr.sin_addr);
        fprintf(stderr, "authcheck failed: %s\n", str);
        close(fd);
    }
}
Exemplo n.º 6
0
// This function takes tif/box pair of files and runs recognition on the image,
// while making sure that the word bounds that tesseract identified roughly
// match to those specified by the input box file. For each word (ngram in a
// single bounding box from the input box file) it outputs the ocred result,
// the correct label, rating and certainty.
void Tesseract::recog_training_segmented(const STRING &fname,
                                         PAGE_RES *page_res,
                                         volatile ETEXT_DESC *monitor,
                                         FILE *output_file) {
  STRING box_fname = fname;
  const char *lastdot = strrchr(box_fname.string(), '.');
  if (lastdot != NULL) box_fname[lastdot - box_fname.string()] = '\0';
  box_fname += ".box";
  // ReadNextBox() will close box_file
  FILE *box_file = open_file(box_fname.string(), "r");

  PAGE_RES_IT page_res_it;
  page_res_it.page_res = page_res;
  page_res_it.restart_page();
  STRING label;

  // Process all the words on this page.
  TBOX tbox;  // tesseract-identified box
  TBOX bbox;  // box from the box file
  bool keep_going;
  int line_number = 0;
  int examined_words = 0;
  do {
    keep_going = read_t(&page_res_it, &tbox);
    keep_going &= ReadNextBox(applybox_page, &line_number, box_file, &label,
                              &bbox);
    // Align bottom left points of the TBOXes.
    while (keep_going &&
           !NearlyEqual<int>(tbox.bottom(), bbox.bottom(), kMaxBoxEdgeDiff)) {
      if (bbox.bottom() < tbox.bottom()) {
        page_res_it.forward();
        keep_going = read_t(&page_res_it, &tbox);
      } else {
        keep_going = ReadNextBox(applybox_page, &line_number, box_file, &label,
                                 &bbox);
      }
    }
    while (keep_going &&
           !NearlyEqual<int>(tbox.left(), bbox.left(), kMaxBoxEdgeDiff)) {
      if (bbox.left() > tbox.left()) {
        page_res_it.forward();
        keep_going = read_t(&page_res_it, &tbox);
      } else {
        keep_going = ReadNextBox(applybox_page, &line_number, box_file, &label,
                                 &bbox);
      }
    }
    // OCR the word if top right points of the TBOXes are similar.
    if (keep_going &&
        NearlyEqual<int>(tbox.right(), bbox.right(), kMaxBoxEdgeDiff) &&
        NearlyEqual<int>(tbox.top(), bbox.top(), kMaxBoxEdgeDiff)) {
        ambigs_classify_and_output(label.string(), &page_res_it, output_file);
        examined_words++;
    }
    page_res_it.forward();
  } while (keep_going);

  // Set up scripts on all of the words that did not get sent to
  // ambigs_classify_and_output.  They all should have, but if all the
  // werd_res's don't get uch_sets, tesseract will crash when you try
  // to iterate over them. :-(
  int total_words = 0;
  for (page_res_it.restart_page(); page_res_it.block() != NULL;
       page_res_it.forward()) {
    if (page_res_it.word()) {
      if (page_res_it.word()->uch_set == NULL)
        page_res_it.word()->SetupFake(unicharset);
      total_words++;
    }
  }
  if (examined_words < 0.85 * total_words) {
    tprintf("TODO(antonova): clean up recog_training_segmented; "
            " It examined only a small fraction of the ambigs image.\n");
  }
  tprintf("recog_training_segmented: examined %d / %d words.\n",
          examined_words, total_words);
}
Exemplo n.º 7
0
 void CAPMT::send(const int sid )
 {
#ifdef PMT_FILE
   unlink("/tmp/pmt.tmp");
#endif
   int fd=-1;
   int length;
   int k,ii;
   int pmt_pid=0;
//   FILE *fout;
   unsigned char buffer[4096];
   char *demux_dev;
   asprintf(&demux_dev, "/dev/dvb/adapter%d/demux%d",adapter,demux);
   esyslog("DEMUX: %s",demux_dev);
   if ((fd = open(demux_dev, O_RDWR)) < 0)
   {
      esyslog("DVPAPI: Error opening demux device");
   }
   else
   {
    if (set_filter(fd, 0) < 0)
    {
      esyslog("DVPAPI: Error in set filter pat");
    }
     if ((length = read_t(fd, buffer)) < 0)
     {
        esyslog("DVPAPI: Error in read read_t (pat)");
     }
     else
     {
       pmt_pid=get_pmt_pid(buffer, sid);
       if (pmt_pid==0)
       {
         esyslog("DVPAPI: Error pmt_pid not found");
       }
       else
       {
        if (set_filter_pmt(fd, pmt_pid) < 0)
        {
         esyslog("DVPAPI: Error in set pmt filter");
        }
        for (k=0; k<64; k++)
       {
         if ((length = read_t(fd, buffer)) < 0)
         {
           esyslog("DVPAPI: Error in read pmt\n");
         }
         if (sid==((buffer[4]<<8)+buffer[5]))
         {
           break;
         }
        }
        
        length=((buffer[2]&0xf)<<8) + buffer[3]+3;
        close(fd);  // just in case ;)
#ifdef PMT_FILE
              FILE *fout=fopen("/tmp/pmt.tmp","wt");
              for (k=0;k<length;k++)
              {
                putc(buffer[k+1],fout);
              }
             fclose (fout);
#else
        char* caPMT=(char*)malloc(1024);
   // http://cvs.tuxbox.org/lists/tuxbox-cvs-0208/msg00434.html
        isyslog("DVBAPI: :: CAMPT channelSid =0x%x(%d) ",sid,sid);
        memcpy(caPMT, "\x9F\x80\x32\x82\xFF\xFB\x03\xFF\xFF\x00\x00\x13\x00", 12);
        int toWrite=(length-12-4-1)+13+2;
        caPMT[4]=(toWrite)>>8;
        caPMT[5]=(toWrite)&0xff;
        // [6]=03
        caPMT[7] = buffer[4]; // program no
        caPMT[8] = buffer[5]; // progno
        //
        caPMT[11]=buffer[12]+1;    
        caPMT[12]=(char)demux; // demux id
        caPMT[13]=(char)adapter; // adapter id

        memcpy(caPMT+13+2,buffer+13,length-12-4-1);        

        if(camdSocket==0)
        {
        	camdSocket=socket(AF_LOCAL,SOCK_STREAM,0);
        	sockaddr_un serv_addr_un;
        	memset(&serv_addr_un,0,sizeof(serv_addr_un));
        	serv_addr_un.sun_family=AF_LOCAL;
        	snprintf(serv_addr_un.sun_path,sizeof(serv_addr_un.sun_path),"/tmp/camd.socket");
        	if(connect(camdSocket,(const sockaddr*)&serv_addr_un,sizeof(serv_addr_un))!=0)
        	{
               esyslog("DVPAPI: Canot connecto to /tmp/camd.socket, Do you have OSCam running?");
        	   camdSocket=0;
        	}
        }
        if(camdSocket!=0)
        {
          int wrote=write(camdSocket,caPMT,toWrite);
          isyslog("DVBAPI: :: CAMPT length=%d toWrite=%d wrote=%d",length,toWrite,wrote);
          if(wrote!=toWrite)
          {
              esyslog("DVPAPI: CAMPT:send failed");
        	  close(camdSocket);
        	  camdSocket=0;
          }
        }
        free(caPMT);
#endif
         }
      }
   }
   free(demux_dev);
 }