#include <stdio.h>
#include "FaceDetectDef.h"
#include "FaceDetectMain.h"
#include <time.h>
#include "stdlib.h"#include "BvsDriverState.h"#define MAXFACESINPHOTO 50/* ==== RGB ==== */
#define CHID_R     0
#define CHID_G     1
#define CHID_B     2#define val_pixel_image( img, x, y, ch, x_num, y_num, ch_num )  \
( img[( (y) * (x_num) + (x) ) * (ch_num) + ch] )/* rgb from rgb */
#define val_r_rgb( img, x, y, x_num, y_num )         ( val_pixel_image( img, x, y, CHID_R, x_num, y_num, 3 ) )
#define val_g_rgb( img, x, y, x_num, y_num )         ( val_pixel_image( img, x, y, CHID_G, x_num, y_num, 3 ) )
#define val_b_rgb( img, x, y, x_num, y_num )         ( val_pixel_image( img, x, y, CHID_B, x_num, y_num, 3 ) )/* ycbcr from rgb */
#define val_y_rgb( img, x, y, x_num, y_num )        \
( (  77 * val_r_rgb( img, x, y, x_num, y_num )  \
+ 151 * val_g_rgb( img, x, y, x_num, y_num )  \
+  28 * val_b_rgb( img, x, y, x_num, y_num ) ) >> 8 )/* ---- Clip ---- */
#define limit( x, a, b )  ( ( ( x ) < ( a ) ) ? ( a ) : ( ( ( x ) > ( b ) ) ? ( b ) : ( x ) ) )
#define clip( x )         limit( x, 0, 255 )#define BYTES_OF_PERLINE(width, bitcount) ((((width * bitcount) + 31) & ~31) >> 3)
#define BYTE unsigned char
BYTE * readBVImage(char *szFileName, int *nWidth,  int *nHeight){
BYTE *pY = NULL;
int size = 0;
FILE *fp = NULL;

fp = fopen(szFileName, "rb");
//assert(fp!=NULL);
if (fp==NULL){
*nWidth = 0;
*nHeight = 0;
return NULL;
} //now read all;
fread(nWidth, sizeof(int), 1, fp);
fread(nHeight, sizeof(int), 1, fp);
size = (*nWidth) * (*nHeight) * 1;
pY = (BYTE *) malloc(size*sizeof(BYTE));
fread(pY, sizeof(BYTE), size, fp);
fclose(fp);
return pY;
}//void ConvertBGR24toY( const unsigned char* in_pImage, const int in_Width, const int in_Height, unsigned char* io_pYImage )
//{
// int x, y;
// /* Parameter check */
// if( in_pImage == NULL ||
// io_pYImage == NULL ) {
//
// }
//
// for ( y = 0 ; y < in_Height ; y++ ) {
// for ( x = 0 ; x < in_Width ; x++ ) {
// io_pYImage[in_Width * y + x]  = ( unsigned char ) clip( val_y_rgb( in_pImage, x, y, in_Width, in_Height ) );
// }
// }
//
//}
int main(void) {
FaceRecognitionHandle m_pFaceInstance=NULL;
BYTE * pY = NULL;
FACEINFO io_pFace[MAXFACESINPHOTO]; 
char szFileName[1024];
char szYFileName[1024];
char szMessage[1024];
int nWidth;
int nHeight;
int nFaces=MAXFACESINPHOTO;
FILE * fp = NULL;
long t = 0;
int ret = 0;
int nF = 0;

CBvsDriverState stateFrame; //处理一帧的状态 printf("start now.\n"); fp = fopen("c:\\DetectResult.txt", "a");
//
//Initialize(&m_pFaceInstance); stateFrame.BvsInitialize();
stateFrame.BvsSetSensitiveLevel(1); for (int i=20; i<958; i++){
sprintf(szFileName, "..\\test_images\\Frame%05d.y", i);
pY = readBVImage(szFileName, &nWidth, &nHeight); if (pY == NULL){
sprintf(szMessage, "read image file error %s \n", szFileName);
fputs(szMessage, fp);
continue;
} //将图像数据转换成灰度图像数据
//BYTE * pY = new BYTE[nWidth * nHeight];
//ConvertBGR24toY(pRaw8, nWidth, nHeight, pY); //sprintf(szYFileName, "..\\test_images\\Frame%05d.y", i);
//
//FILE * fp1 = fopen(szYFileName, "wb");
//if (fp1)
//{
// fwrite(&nWidth, sizeof(int), 1, fp1);
// fwrite(&nHeight, sizeof(int), 1, fp1);
// fwrite(pY, sizeof(BYTE), nWidth*nHeight, fp1);
// fclose(fp1);
//} sprintf(szMessage, "%ld: before BvsProcessOneFrame();\n", clock());
fputs(szMessage, fp);
//stateFrame.BvsSetFrameInfo(pRaw8, nWidth, nHeight, 3, 0);
stateFrame.BvsSetFrameInfo(pY, nWidth, nHeight, 1, 0);
int nResult = stateFrame.BvsProcessOneFrame();
sprintf(szMessage, "%ld: after BvsProcessOneFrame();\n", clock());
fputs(szMessage, fp); switch (nResult)
{
case -2:
fputs("Result:-2,Error\n", fp);
break;
case -1:
fputs("Result:-1,Error\n", fp);
break;
case 0:
fputs("Result:0,Normal\n", fp);
break;
case 1:
fputs("Result:1,Leave\n", fp);
break;
case 2:
fputs("Result:2,Start learning\n", fp);
break;
case 3:
fputs("Result:3, Learning\n", fp);
break;
case 4:
fputs("Result:4,End learning, Start to detect driver's state\n", fp);
break;
case 5:
fputs("Result:5,Warning - Fatigue\n", fp);
break;
case 6:
fputs("Result:6,Warning - Pose\n", fp);
break;
case 7:
fputs("Result:7, Becareful\n", fp);
break;
}
free(pY);
pY = NULL; //delete pY;
} fclose(fp); stateFrame.BvsUnInitialize(); return 0;
}