Example #1
0
int main(void)
{
  /* receive image */
  struct{
    int w, h;
  } temp;
  int sd;
  struct sockaddr_in name, cli_name;
//  int sock_opt_val = 1;


  if((sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    printf("socket() error!\n");
    exit(-1);
  }

/*
  if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *) &sock_opt_val,
    sizeof(sock_opt_val)) < 0) {
    fprintf(stderr, "Failed to set SO_REUSEADDR on INET socket!\n");
    exit(-1);
  }
*/
  int port = 54000;
  bzero(&name, sizeof(name));
  name.sin_family = AF_INET;
  name.sin_port = htons(port);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  if(bind(sd, (struct sockaddr *)&name, sizeof(name)) < 0){
    printf("bind() fails!\n");
    exit(-1);
  }

  int slen = sizeof(cli_name);
  printf("listening on port %d\n", port);
  if(recvfrom(sd, &temp, sizeof(temp), 0, (struct sockaddr *) &cli_name, &slen) < 0){
    printf("receive h/w fails!\n");
    exit(-1);
  }
  printf("size received: %d, %d\n", temp.w, temp.h);
  int width = temp.w, height = temp.h;

  //int length = sizeof(char) * width * height * 3;
  int length = WIDTH * HEIGHT * 3;
  //char *buf = (char *)malloc(length), *ptr = buf;
  unsigned char *ptr = buf;

  int ttt = length;
  while(ttt > 0){
    if(recvfrom(sd, ptr, ttt < PACK_SIZE ? ttt : PACK_SIZE, 0,
                (struct sockaddr *) &cli_name, &slen) < 0){
      printf("receive pixels fails!\n");
      exit(-1);
    }
    ttt -= PACK_SIZE;
    ptr += PACK_SIZE;
  }
  printf("data received\n");

  /* image processing */
  Image img;
  I_init(&img, width, height, TYPE_3BYTE_BGR);

  int i, j, index;
/*
  unsigned char a1, a2, a3;
  for(i = 0; i < height; i++){
    for(j = 0; j < width; j++){
      index = (i * width + j) * 3;
      a1 = buf[index];
      a2 = buf[index + 1];
      a3 = buf[index + 2];
      img.data[index] = a1;
      img.data[index + 1] = a2;
      img.data[index + 2] = a3;
    }
  }
printf("after loop\n");
*/

  CEDetector detect;
  C_init(&detect);
  detect.lowThreshold = 4.0;
  detect.highThreshold = 7.5;
  detect.sourceImage = &img;
  i = 0;
  struct sched_param param;
  for (;;) {
    C_process(&detect);
    if (++i % 30 == 0) {
      printf ("30 frame mark\n");
    }
    time ();
    if (i == 240) {
      param.affinity = 1;
      sched_setparam (-1, &param);
    }
  }

  /* convert from BGRA to RGB */
  for(i = 0; i < height; i++){
    for(j = 0; j < width; j++){
      index = i * width + j;
      buf[index * 3] = detect.edgesImage->data[index * 4 + 2];
      buf[index * 3 + 1] = detect.edgesImage->data[index * 4 + 1];
      buf[index * 3 + 2] = detect.edgesImage->data[index * 4];
    }
  }

#if 0
  /* send result */
  ttt = length;
  ptr = buf;
  while(ttt > 0){
    if(sendto(sd, ptr, ttt < PACK_SIZE ? ttt : PACK_SIZE, 0, (struct sockaddr *)&cli_name, sizeof(cli_name)) < 0){
      printf("sendto() fails!\n");
      exit(-1);
    }
    ttt -= PACK_SIZE;
    ptr += PACK_SIZE;
  }
#endif

  /* clean up */
  C_deinit(&detect);
  I_deinit(&img);
  //free(buf);
  close(sd);
  return 0;
}
Example #2
0
static PyObject *init(PyObject *self, PyObject *args) {
        I_init();
        Py_RETURN_NONE;
}