#include<iostream>
#include"stdafx.h"
#include"serialport.h"
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc_c.h>
#include"opencv2/objdetect/objdetect.hpp"
#include<opencv.hpp>
using namespacecv;
IplImage *iplgray= NULL;
IplImage*iplbinary = NULL;
const char*pstrWindowsSrcTitle = "vincenzo";
Stringface_cascade_name = "haarcascade_frontalface_alt.xml";
Stringeyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifierface_cascade;
CascadeClassifiereyes_cascade;
RNG rng(12345);
CSerialPortmySerialPort;
unsigned int len =8;
unsigned charl[]={0xff,0x01,0x00,0x04,0x00,0x00,0x05};
unsigned charl1[]={0xff,0x01,0x00,0x04,0x10,0x00,0x15};
unsigned charl2[]={0xff,0x01,0x00,0x04,0x20,0x00,0x25};
unsigned charr[]={0xff,0x01,0x00,0x02,0x00,0x00,0x03};
unsigned charr1[]={0xff,0x01,0x00,0x02,0x10,0x00,0x13};
unsigned charr2[]={0xff,0x01,0x00,0x02,0x20,0x00,0x23};
unsigned charu[]={0xff,0x01,0x00,0x08,0x05,0x00,0x0e};
unsigned charu1[]={0xff,0x01,0x00,0x08,0x10,0x00,0x19};
unsigned charu2[]={0xff,0x01,0x00,0x08,0x20,0x00,0x29};
unsigned chard[]={0xff,0x01,0x00,0x010,0x05,0x00,0x16};
unsigned chard1[]={0xff,0x01,0x00,0x10,0x10,0x00,0x21};
unsigned chard2[]={0xff,0x01,0x00,0x10,0x20,0x00,0x31};
unsigned chars[]={0xff,0x01,0x00,0x00,0x00,0x00,0x01};
int flag=0;
void useSerial(intx, int y)
{
//unsigned charpdata[]={0xff,0x01,0x00,0x08,0x00,0xff,0x08};
std::cout << x << std::endl;
std::cout << ' ' <<std::endl;
std::cout <<y << std::endl;
std::cout << '\n' <<std::endl;
std::cout <<flag<< std::endl;
if(flag==0)
{
if (x<200&&x>120)
{
mySerialPort.WriteData(s,len);
flag++;
}
else {
if (x > 160)
{
if (x>260)
{
mySerialPort.WriteData(r2,len);
}
if(x>200&&x<=260)
{
mySerialPort.WriteData(r1,len);
}
if (x<=200)
{
mySerialPort.WriteData(r,len);
}
}
if (x < 160)
{
if (x<60)
{
mySerialPort.WriteData(l2,len);
}
if(x>=60&&x<120)
{
mySerialPort.WriteData(l1,len);
}
if (x>=120)
{
mySerialPort.WriteData(l,len);
}
}
}
}
else{/**************************此处是垂直方向****************/
if (y<140&&y>100)
{
mySerialPort.WriteData(s,len);
flag=0;
}
else {
if (y > 120)
{
if (y>200)
{
mySerialPort.WriteData(d2,len);
}
if(y>160&&y<=200)
{
mySerialPort.WriteData(d1,len);
}
if (y<=160)
{
mySerialPort.WriteData(d,len);
}
}
if (y< 120)
{
if (y<40)
{
mySerialPort.WriteData(u2,len);
}
if(y>=40&&y<80)
{
mySerialPort.WriteData(u1,len);
}
if (y>=80)
{
mySerialPort.WriteData(u,len);
}
}
}
}
}
voiddetectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor(frame, frame_gray,COLOR_BGR2GRAY);//颜色空间转换函数 RGB转换位GRAY
equalizeHist(frame_gray, frame_gray);//灰度图象直方图均衡化
//-- Detect faces
face_cascade.detectMultiScale(frame_gray,faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (size_t i = 0; i < faces.size();i++)
{
Point center(faces[ i].x +faces[ i].width / 2, faces[ i].y + faces[ i].height / 2);
useSerial(faces[ i].x +faces[ i].width / 2, faces[ i].y + faces[ i].height / 2);
ellipse(frame, center,Size(faces[ i].width / 2, faces[ i].height / 2), 0, 0, 360, Scalar(255, 0,255),2, 8, 0);
}
std::cout <<1 << std::endl;
//-- Show what you got
imshow(pstrWindowsSrcTitle, frame);
}
void ImageCanny(BYTE*m_pImageBuffer, int m_Width, int m_Height)
{
CvSize cvSize;
cvSize.width = m_Width;
cvSize.height = m_Height;
IplImage *iplImage =cvCreateImageHeader(cvSize,IPL_DEPTH_8U,3);
cvSetData(iplImage,m_pImageBuffer,m_Width*3);
Mat dst(iplImage, 0);
Mat dst1;
flip(dst, dst1, 0); // flip by bothaxises
detectAndDisplay(dst1);
}
void __stdcallStreamNotify(const BYTE* pDIBHead, const BYTE* pDIBits, LPVOID pParam)
{
const BITMAPINFO * pInfo =(BITMAPINFO*)pDIBHead;
int w = pInfo->bmiHeader.biWidth;
int h =pInfo->bmiHeader.biHeight;
BYTE* pData=new BYTE[w*h*3];
memcpy(pData,pDIBits,w*h*3);
ImageCanny(pData,w,h);
waitKey(100);
}
int main()
{
BOOL isConnected;
char szDevName[80][80];
int pDevNum;
HRESULT hr;
int iCardID = 0;
do
{
;
} while(!face_cascade.load(face_cascade_name));
do
{
;
} while(!eyes_cascade.load(eyes_cascade_name));
DSStream_Initialize();
DSStream_EnumVideoCaptureDev(szDevName,&pDevNum);
hr =DSStream_GetCardNumber(&pDevNum);
if(FAILED(hr) || pDevNum<=0)
{
std::cout<<0<<std::endl;
return FALSE;
}
DSStream_ConnectDevice(0, false);
DSStream_IsConnected(0,&isConnected);
std::cout<<isConnected<<std::endl;
BOOL bSignalLocked = FALSE;
HRESULT hr1 =DSStream_IsVideoSignalLocked(0, &bSignalLocked);//是否有信号输入
if( SUCCEEDED(hr1) &&bSignalLocked )
{
std::cout<<2<<std::endl;
}
VIDEOSTREAMINFO vsi;
HRESULT hr2= DSStream_GetVideoInfo(0,&vsi, PREVIEW);
if( SUCCEEDED(hr2))
{
vsi.subtype =VideoSubType_RGB24;
DSStream_DisconnectPin(iCardID,PREVIEW);
DSStream_SetVideoInfo(iCardID,vsi, PREVIEW);
DSStream_ConnectPin(iCardID,PREVIEW);
std::cout<<vsi.subtype<<std::endl;
}
VideoStandard zhishi;
long avail;
HRESULT hr3 =DSStream_GetVideoStandard(0, &zhishi, &avail);
if (SUCCEEDED(hr3))
{
std::cout<<zhishi<<std::endl;//256即表示PAL_I
}
const BYTE* pDIBHead;
const BYTE* pDIBits;
LPVOID pParam;
if (!mySerialPort.InitPort(2))//使用com2
{
std::cout << "initPortfail !" << std::endl;
}
HRESULT hr4 = DSStream_GetVideoStream(0,StreamNotify, NULL);
if( SUCCEEDED(hr4) )
{
std::cout<<5<<std::endl;
}
while(1) ;
}
|