【HandVu学习】HandVu使用Socket和线程进行状态读取和绘制

简介: 作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ /** * HandVu - a library for computer vision-based hand gesture * recognition.

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

/**
* HandVu - a library for computer vision-based hand gesture
* recognition.
* Copyright (C) 2004 Mathias Kolsch, matz@cs.ucsb.edu
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, 
* Boston, MA  02111-1307, USA.
*
* $Id: hv_OpenCV.cpp,v 1.15 2006/01/03 21:44:15 matz Exp $
**/

/*********************************
2009.3.15 gnuhpc@gmail.com
Show How to get the hand state using the socket in a thread.
Copyright (C) GPL
***********************************/

#ifdef WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/time.h>
#include  <errno.h>
#include <pthread.h>
#include  <sys/socket.h>
#include  <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>

#include <cv.h>
#include <highgui.h>
#include <ctype.h>

#include "HandVu.h"

#define BILLION 1000000000L
#define BUFFSIZE 256

IplImage *capture_image = 0;
IplImage *display_image = 0;
IplImage *routing_image = 0;

bool async_processing = false;
int num_async_bufs = 30;
IplImage *m_async_image = 0;
int m_async_bufID = -1;
bool sync_display = true;

CvPoint origin;
int select_object = 0;
int sel_area_left=0, sel_area_top=0, sel_area_right=0, sel_area_bottom=0;
bool correct_distortion = false;

hvState state;

int c;

void* thread(void *arg)
{
    int socketfd;
    int len;
    struct sockaddr_in address;
    int result;
    char buffer[256];
    int received = 0;

    if(!(socketfd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)))
    {
        perror("Error when creating socket!/n");
        pthread_exit(NULL);
    }

    memset(&address,0,sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr= inet_addr("127.0.0.1");
    address.sin_port = htons(1394);
    if( connect(socketfd,(struct sockaddr *)&address,sizeof(address))<0)
    {
        perror("Error when connecting socket!/n");
        pthread_exit(NULL);
    }

    while(1)
    {
        int bytes=0;
        if( (bytes = recv(socketfd,buffer,BUFFSIZE-1,0))<1 )
        {
            perror("Error When Reading the socket!/n");
            pthread_exit(NULL);
        }

        buffer[bytes]='/0';

        printf("%s/n",buffer);

        if( c==27 )
        {
            break;
            pthread_exit(NULL);
        }
    }
    
}

static void myhandler(int s)
{
    cvZero(routing_image);
}

void OnMouse( int event, int x, int y, int /*flags*/, void* /*params*/ )
{
  if( !capture_image )
    return;

  if( capture_image->origin )
    y = capture_image->height - y;

  if( select_object )
  {
    sel_area_left = MIN(x,origin.x);
    sel_area_top = MIN(y,origin.y);
    sel_area_right = sel_area_left + CV_IABS(x - origin.x);
    sel_area_bottom = sel_area_top + CV_IABS(y - origin.y);

    sel_area_left = MAX( sel_area_left, 0 );
    sel_area_top = MAX( sel_area_top, 0 );
    sel_area_right = MIN( sel_area_right, capture_image->width );
    sel_area_bottom = MIN( sel_area_bottom, capture_image->height );

    if( sel_area_right-sel_area_left > 0 && sel_area_bottom-sel_area_top> 0 )
      hvSetDetectionArea(sel_area_left, sel_area_top,
                         sel_area_right, sel_area_bottom);
  }

  switch( event )
  {
  case CV_EVENT_LBUTTONDOWN:
    origin = cvPoint(x,y);
    sel_area_left = sel_area_right = x;
    sel_area_top = sel_area_bottom = y;
    select_object = 1;
    break;
  case CV_EVENT_LBUTTONUP:
    select_object = 0;
    break;
  }
}


void showFrame(IplImage* img, hvAction action, hvState state)
{
  int underline;
  CvFont     font; 
  CvSize     textsize;
  char        text[256];

  cvInitFont(&font,CV_FONT_HERSHEY_PLAIN,1.0,1.0,0.0,1,8);

  if (action==HV_DROP_FRAME) {
    // HandVu recommends dropping the frame entirely
    // printf("HandVuFilter: dropping frame/n");
    return;
  } else if (action==HV_SKIP_FRAME) {
    // HandVu recommends displaying the frame, but not doing any further
    // processing on it - keep going
    // printf("HandVuFilter: supposed to skip frame/n");
  } else if (action==HV_PROCESS_FRAME) {
    // full processing was done and is recommended for following steps;
    // keep going
    //printf("HandVuFilter: processed frame/n");
  } else {
    assert(0); // unknown action
  }
  
  sprintf(text,"x=%f,y=%f",100.0*state.center_xpos,100.0*state.center_ypos);
  cvGetTextSize( text, &font, &textsize, &underline );
  cvPutText(img,text,cvPoint(img->width/2,img->height-10),
          &font,CV_RGB(0,255,0));
  cvShowImage( "HandVu", img );
  cvCircle(routing_image,cvPoint(300.0*state.center_xpos,300.0*state.center_ypos),1,CV_RGB(255,0,0),-1,CV_AA,0);
  cvShowImage("Routing",routing_image);
}

int main( int argc, char** argv )
{
  int t=123;//For testing
  int retv;
  int flag1=1;

  pthread_t id1;

  CvCapture* capture = 0;

  struct itimerval value;
  struct sigaction act;
  act.sa_handler = myhandler;
  act.sa_flags = 0;
  sigemptyset(&act.sa_mask);
  sigaction(SIGPROF,&act,NULL);

  value.it_interval.tv_sec=1;
  value.it_interval.tv_usec=0;
  value.it_value = value.it_interval;

  if((setitimer(ITIMER_PROF,&value,NULL)==-1))
  {
      perror("Set timer Error!/n");
    return -1;
  }

  if (argc<2) {
    printf("you need to specify a conductor file as first argument/n");
    printf("for example: ../config/default.conductor/n");
    return -1;
  }

  string conductor_fname(argv[1]);//声明配置参数的对象
  printf("will load conductor from file:/n%s/n", conductor_fname.c_str());//屏显提示

  /*是否设定特定的摄像头,并初始化摄像头  */
  if( argc == 2 || argc == 3) {
    int num = 0;
    if (argc==3) {
      num = atoi(argv[2]);
    }
    capture = cvCaptureFromCAM( num );
    if (!capture) {
      capture = cvCaptureFromAVI( argv[2] ); 
    }
  }

  if( !capture )
  {
    fprintf(stderr,"Could not initialize capturing through OpenCV./n");
    return -1;
  }

  /* 屏显提示 */
  printf( "Hot keys: /n"
    "/tESC - quit the program/n"
    "/tr - restart the tracking/n"
    "/t0-3 - set the overlay (verbosity) level/n"
    "use the mouse to select the initial detection area/n" );

  routing_image = cvCreateImage(cvSize(300,300),IPL_DEPTH_8U,3 );

  //设定采集图像大小
  int p = 0; // according to docs, these calls don't work in OpenCV beta 4 yet
  p = cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 640);
  p = cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 480);

  //获取一帧
  capture_image = cvQueryFrame( capture );
  if ( !capture_image ) {
    fprintf(stderr,"Could not retrieve image through OpenCV./n");
    return -1;
  }

  /* allocate all the buffers */
  CvSize size = cvGetSize(capture_image);
  hvInitialize(size.width, size.height);//初始化要分析的图像大小
  hvLoadConductor(conductor_fname);//装载参数
  hvStartRecognition();//开始识别
  hvSetOverlayLevel(2);//设置识别的覆盖区级别

  /* 设置鼠标事件的回调参数 */
  cvSetMouseCallback( "HandVu", OnMouse );
  /* 设置窗口 */
  int success1 = cvNamedWindow( "HandVu", 1 );
  int success2 = cvNamedWindow( "Routing", 1 );
  cvResizeWindow("Routing",300,300);
  cvMoveWindow("Routing",1000,0);
  fprintf(stderr, "initialized highgui/n");
  if ((success1!=1)&&(success2!=1)) {
    printf("can't open window - did you compile OpenCV with highgui support?");
    return -1;
  }

  hvStartGestureServer(1394,10);

  retv = pthread_create(&id1,NULL,thread,NULL);
  if( retv )
  {
      perror("Error when creating a thread!/n");
      return -1;
  }

  retv = pthread_detach(id1);
  if( retv )
  {
      perror("Error when deteching a thread!/n");
      return -1;
  }
 
  for (;;) {
      
      // synchronous processing in HandVu
      
      // ------- main library call ---------
      hvAction action = HV_INVALID_ACTION;
      action = hvProcessFrame(capture_image);

      // -------
       
      hvGetState(0, state);
      showFrame(capture_image, action,state);
      
    
    c = cvWaitKey(10);
    if( c == 27 || c == 'q' )
      break;
    switch( c )
      {
      case 'r':
        hvStopRecognition();
        hvStartRecognition();
        break;
      case '0':
        hvSetOverlayLevel(0);
        break;
      case '1':
        hvSetOverlayLevel(1);
        break;
      case '2':
        hvSetOverlayLevel(2);
        break;
      case '3':
        hvSetOverlayLevel(3);
        break;
      case 'u':
        if (hvCanCorrectDistortion()) {
          correct_distortion = !correct_distortion;
          hvCorrectDistortion(correct_distortion);
        }
        break;
      default:
        ;
      }
    
    // capture next image
    capture_image = cvQueryFrame( capture );
    if ( !capture_image ) {
      fprintf(stderr,"Could not retrieve image through OpenCV./n");
      break;
    }
  }
  cvReleaseCapture( &capture );
  cvDestroyWindow("HandVu");
  cvDestroyWindow("Routing");
  return 0;
}

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/


               作者:gnuhpc
               出处:http://www.cnblogs.com/gnuhpc/
               除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


分享到:

目录
相关文章
|
6月前
|
Java
Java Socket编程与多线程:提升客户端-服务器通信的并发性能
【6月更文挑战第21天】Java网络编程中,Socket结合多线程提升并发性能,服务器对每个客户端连接启动新线程处理,如示例所示,实现每个客户端的独立操作。多线程利用多核处理器能力,避免串行等待,提升响应速度。防止死锁需减少共享资源,统一锁定顺序,使用超时和重试策略。使用synchronized、ReentrantLock等维持数据一致性。多线程带来性能提升的同时,也伴随复杂性和挑战。
107 0
|
3月前
|
监控 Java 调度
【Java学习】多线程&JUC万字超详解
本文详细介绍了多线程的概念和三种实现方式,还有一些常见的成员方法,CPU的调动方式,多线程的生命周期,还有线程安全问题,锁和死锁的概念,以及等待唤醒机制,阻塞队列,多线程的六种状态,线程池等
182 6
【Java学习】多线程&JUC万字超详解
|
6月前
|
NoSQL Redis
Redis系列学习文章分享---第五篇(Redis实战篇--优惠券秒杀,全局唯一id 添加优惠券 实现秒杀下单 库存超卖问题分析 乐观锁解决超卖 实现一人一单功能 集群下的线程并发安全问题)
Redis系列学习文章分享---第五篇(Redis实战篇--优惠券秒杀,全局唯一id 添加优惠券 实现秒杀下单 库存超卖问题分析 乐观锁解决超卖 实现一人一单功能 集群下的线程并发安全问题)
133 0
|
6月前
|
调度 Python
Python多线程学习优质方法分享
Python多线程学习优质方法分享
27 0
|
6月前
|
网络协议
逆向学习网络篇:通过Socket建立连接并传输数据
逆向学习网络篇:通过Socket建立连接并传输数据
61 0
|
6月前
|
安全 API C++
逆向学习Windows篇:C++中多线程的使用和回调函数的实现
逆向学习Windows篇:C++中多线程的使用和回调函数的实现
206 0
|
7月前
|
Java 调度
【JAVA学习之路 | 提高篇】进程与线程(Thread)
【JAVA学习之路 | 提高篇】进程与线程(Thread)
|
6月前
|
Java
Java线程学习经典例子-读写者演示
Java线程学习经典例子-读写者演示
25 0
|
7月前
|
Java 调度
【JAVA学习之路 | 提高篇】线程的通信
【JAVA学习之路 | 提高篇】线程的通信
|
7月前
|
存储 Java
【JAVA学习之路 | 提高篇】线程安全问题及解决
【JAVA学习之路 | 提高篇】线程安全问题及解决

相关实验场景

更多