Opencv-python Study NOTE 2: Achieve eye follow (also called Face follow)

Source: Internet
Author: User

reprint Please specify: @ Xiao Wu Yi Http://www.cnblogs.com/xiaowuyi qq Group: 64770604

If the robot's face rotates with the face in front of you, you will not find this interaction interesting. Years ago, learning a bit of OPENCV, through the OPENCV can be simple to achieve the face of the follow. Plus a few servos to control the head rotation, the robot can interact. Oh

Here do not do video demonstration, just put out the code, interested friends can experiment with their own. The basic principle is to recognize the face first, and then judge the position of the human face. I divided the window with the method of drawing lines into 9 small lattice, representing 9 directions, when the face into a small lattice, the robot's head in which direction to turn.

The following code is successful on the Raspberry Pi B + Branch, the system Raspbian,python code.

#-*-coding:utf-8-*-# # Reprint Please specify: @ Xiao Wu Yi Http://www.cnblogs.com/xiaowuyi qq Group: 64770604import CV2.CV asCV Import Cv2 fromcv2 Import videocapture #cv. Namedwindow ("W1", CV. Cv_window_autosize) CV. Namedwindow ("W1", CV. Cv_window_normal) CV. Resizewindow ("W1", -, -) #找到设备对象 capture= CV. Capturefromcam (0) #检测人脸函数 def repeat (): #每次从摄像头获取一张图片 frame=CV. Queryframe (capture) image_size=CV. GetSize (frame) #获取图片的大小 #print image_size greyscale= CV. CreateImage (Image_size,8,1) #建立一个相同大小的灰度图像 CV. Cvtcolor (frame, greyscale, CV. Cv_bgr2gray) #将获取的彩色图像, converted to grayscale image storage= CV. Creatememstorage (0#创建一个内存空间, face detection is to be utilized, the specific function is unclear CV. Equalizehist (greyscale, greyscale) #将灰度图像直方图均衡化, seemingly can reduce the amount of gray image information, speed up the detection speed #画图像分割线 CV. Line (frame, ( About,0),( About,480), (0,255,255),1) CV. Line (frame, (420,0),(420,480), (0,255,255),1) CV. Line (frame, (0, the),(640, the), (0,255,255),1) CV. Line (frame, (0, the),(640, the), (0,255,255),1) # Detect objects Cascade= CV. Load ('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml') #加载Intel公司的训练库 #检测图片中的人脸 and returns an object that contains the face information faces faces= CV. Haardetectobjects (greyscale, cascade, storage,1.2,2, CV. Cv_haar_do_canny_pruning, ( -, -)) #获得人脸所在位置的数据 for(x,y,w,h), ninchfaces: # print x, yifx< About: Print" Right"elif x>310: Print" Left"CV. Rectangle (frame, (x, y),+W,Y+H), (0, -,0),2) #在相应位置标识一个矩形 Border Properties (0,0,255) Red 20 width cv. ShowImage ("W1", greyscale) #显示互有边框的图片 CV. ShowImage ("W1", frame) #循环检测每一帧的图片 Esc key to exit the program whiletrue:repeat () C= CV. Waitkey (Ten)      ifc = = -: #cv2. Videocapture (0). Release () Cv2.destroywindow ("W1")           Break  

Opencv-python Study NOTE 2: Achieve eye follow (also called Face follow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.