int main(int argc, char *argv[]) { init_signals(); if(init_terminal(&hContext,&hCard)) return -1; else { if(open_tunnel(&tunnel) !=-1) { int output_len; raw_packet packet; short sw = 0x9000 ; printf("SCARD host adapter is now ready to accept connections\n"); /* main loop */ while((output_len = utun_read(&tunnel, packet.raw, MTU)) >= 0) { LONG card_status = 0; int select_ret; BYTE *packet_ptr; if(output_len == 0) continue; select_ret = utun_select(&tunnel); /* send the last tun input to the card */ printf("send %d bytes\n",output_len); packet_ptr = packet.raw; do { unsigned char curr_len = output_len > 255 ? 255 : output_len; sw = send_apdu(hContext,hCard,packet_ptr,curr_len,&card_status,select_ret); if(SW1(sw) < 0x90) { printf("Error: %02x%02x\n",SW1(sw),SW2(sw)); break; } packet_ptr += curr_len; output_len -= curr_len; } while(output_len > 0); /* if there is something else to send, do it now */ if(select_ret>0) { memset(&packet,0,sizeof(raw_packet)); continue; } /* receive data from the card (and forward it to the tun) if needed */ while(SW2(sw)) { sw = recv_apdu(hContext,hCard,&tunnel,sw,&card_status); if(SW1(sw) < 0x90 ) { printf("Error: %02x%02x\n",SW1(sw),SW2(sw)); break; } } if(card_status) { check_terminal_status(hContext,hCard); } memset(&packet,0,sizeof(raw_packet)); } } } close_tunnel(&tunnel); release_terminal(&hContext,&hCard); return 0; }
void face_detect( void ) { CvCapture *capture = 0; IplImage *frame = 0; IplImage *frame_copy = 0; double height = 480; double width = 640; int c; CvRect last_rect = {0}; CvHaarClassifierCascade* cvHCC = (CvHaarClassifierCascade*)cvLoad(filename); CvMemStorage* cvMStr = cvCreateMemStorage(0); CvSeq* face; capture = cvCreateCameraCapture (0); cvSetCaptureProperty (capture, CV_CAP_PROP_FRAME_WIDTH, width); cvSetCaptureProperty (capture, CV_CAP_PROP_FRAME_HEIGHT, height); cvNamedWindow ("capture_face_detect", CV_WINDOW_AUTOSIZE); open_tonnel(); while (1) { CvRect near_rect = {0}; frame = cvQueryFrame (capture); frame_copy = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, frame->nChannels); if(frame->origin == IPL_ORIGIN_TL) { cvCopy(frame, frame_copy); } else { cvFlip(frame, frame_copy); } IplImage* gray = cvCreateImage(cvSize(frame_copy->width, frame_copy->height), IPL_DEPTH_8U, 1); IplImage* detect_frame = cvCreateImage(cvSize((frame_copy->width / SCALE), (frame_copy->height / SCALE)), IPL_DEPTH_8U, 1); cvCvtColor(frame_copy, gray, CV_BGR2GRAY); cvResize(gray, detect_frame, CV_INTER_LINEAR); cvEqualizeHist(detect_frame, detect_frame); face = cvHaarDetectObjects(detect_frame, cvHCC, cvMStr, 1.1, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(30, 30) ); CvScalar detect_color = CV_RGB(255, 0, 0); double d = 1000000000000000.0; for (int i = 0; i < face->total; i++) { CvRect* faceRect = (CvRect*)cvGetSeqElem(face, i); if(last_rect.x == 0 && last_rect.y == 0) { } else { double x = abs(last_rect.x - faceRect->x); double y = abs(last_rect.y - faceRect->y); double e = sqrt( x*x+y*y ); if( d > e) { last_rect.x = faceRect->x; last_rect.y = faceRect->y; last_rect.width = faceRect->width; last_rect.height = faceRect->height; printf("x\n"); } } // rect cvRectangle(frame_copy, cvPoint(faceRect->x * SCALE, faceRect->y * SCALE), cvPoint((faceRect->x + faceRect->width) * SCALE, (faceRect->y + faceRect->height) * SCALE), detect_color, 3, CV_AA); detect_color = CV_RGB(0, 0, 255); } // send to server { char str[1024]; sprintf_s(str, "[{ \"x\" : %f, \"y\" : %f}]", last_rect.x * SCALE, last_rect.y * SCALE); printf("%s", str); send_tunnel(str); } cvShowImage ("capture_face_detect", frame_copy); cvReleaseImage(&gray); cvReleaseImage(&detect_frame); // key event c = cvWaitKey (16); if (c == 'e') { break; } if( c == 's') { CvRect* faceRect = (CvRect*)cvGetSeqElem(face, 0); if(faceRect != NULL) { last_rect.x = faceRect->x; last_rect.y = faceRect->y; last_rect.width = faceRect->width; last_rect.height = faceRect->height; } } } close_tunnel(); /* free */ cvReleaseMemStorage(&cvMStr); cvReleaseCapture (&capture); cvDestroyWindow("capture_face_detect"); cvReleaseHaarClassifierCascade(&cvHCC); }