"""
OpenCV 3.1.1による顔追跡
"""
import cv2
class DisplayInfomation:
pass
def traceFace(cap, cascade, di):
_, im = cap.read()
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
di.height = round(gray.shape[1]/2)
di.width = round(gray.shape[0]/2)
gray = cv2.resize(gray,(di.height,di.width))
face = cascade.detectMultiScale(gray, 1.1, 3)
TODO
for x,y,w,h in face:
cx = round(x+w/2)
cy = round(y+h/2)
di.x = x
di.y = y
di.w = w
di.h = h
di.cx = cx
di.cy = cy
if innerDisplay(di):
infoStr = "(x,y)="+"("+str(di.cx)+","+str(di.cy)+")"
cv2.rectangle(gray, (x, y), (x+w, y+h), 255, 3)
else:
infoStr = "Outer Area"
cv2.putText(gray,infoStr,(10,20),1,1,(220),2)
cv2.imshow("Face detect",gray)
def innerDisplay(di):
print(di.x+di.w)
if(di.x > 0
and di.y > 0
and di.w < di.width
and di.h < di.height):
return True
return False
def main():
di = DisplayInfomation()
cap = cv2.VideoCapture(0)
cascade = cv2.CascadeClassifier("./haarcascade_frontalface_alt.xml")
while(1):
traceFace(cap, cascade,di)
if cv2.waitKey(10) > 0:
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()