|
本帖最后由 jackwmj12 于 2016-2-4 22:39 编辑
代码基于树莓派B+硬件,python2.7平台下的opencv3.0例程改编。
好了,废话不多说,下面发布源码:
# -*- coding: utf-8 -*-
import cv2.cv as cv
import cv2
import numpy as np
from cv2 import VideoCapture
from video import create_capture
from common import clock,draw_str
#cv.NamedWindow("W1", cv.CV_WINDOW_AUTOSIZE)
cv2.namedWindow("W1")
#cv2.ResizeWindow("video", 600, 600)
capture = cv2.VideoCapture(0)
success,frame=capture.read()
color=(0,0,255)
classfier = cv2.CascadeClassifier("/home/opencv/data/haarcascades/haarcascade_eye.xml")
def repeat():
succes,frame = capture.read()
image_size =frame.shape[:2]
#print image_size
image = np.zeros(image_size, dtype=np.float16)
image = cv2.cvtColor(frame, cv2.cv.CV_BGR2GRAY)
hi,wi=image_size
minSize=(wi/8,hi/8)
image=cv2.equalizeHist(image)
#storage = cv2.CvMemStorage(0)
# cv2.rectangle(frame, (210, 0), (210, 480),(0,0,255))
cv2.line(frame, (210,0),(210,480), (0,255,255),1)
cv2.line(frame, (420,0),(420,480), (0,255,255),1)
cv2.line(frame, (0,160),(640,160), (0,255,255),1)
cv2.line(frame, (0,320),(640,320), (0,255,255),1)
# detect objects
# faces = cv.HaarDetectObjects(image, cascade, storage, 1.2,
# 2,cv.CV_HAAR_DO_CANNY_PRUNING,(100, 100))
faceRects = classfier.detectMultiScale(image, 1.2, 2,
cv2.CASCADE_SCALE_IMAGE, minSize)
for (x,y,w,h) in faceRects:
# print x,y
if x<210:
print "right"
elif x>310:
print "left"
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,128,0))
# cv.ShowImage("W1", greyscale)
# cv.ShowImage("W1", frame)
cv2.imshow("W1",frame)
while True:
repeat()
c = cv2.waitKey(10)
c=chr(c & 255)
if c in['q','Q',chr(27)]:
#cv2.VideoCapture(0).release()
cv2.destroyWindow("W1")
break
该例程可以识别眼睛,在眼睛部位画上绿色的小框框,并把人脸的转移方向输出在树莓派终端。
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|