I want to control something in a tkinter window by the movement of the detected face from a openCV window. For tkinter window, here is my settings:
def run(width=300, height=300):
root = Tk()
def redrawAllWrapper(canvas, data):
canvas.delete(ALL)
redrawAll(canvas, data)
canvas.update()
def mousePressedWrapper(event, canvas, data):
mousePressed(event, data)
redrawAllWrapper(canvas, data)
def mouseMovedWrapper(event, canvas, data):
mouseMoved(event, data)
redrawAllWrapper(canvas, data)
def keyPressedWrapper(event, canvas, data):
keyPressed(event, data)
redrawAllWrapper(canvas, data)
def timerFiredWrapper(canvas, data):
timerFired(data)
redrawAllWrapper(canvas, data)
# pause, then call timerFired again
canvas.after(data.timerDelay, timerFiredWrapper, canvas, data)
# Set up data and call init
class Struct(object): pass
data = Struct()
data.width = width
data.height = height
data.timerDelay = 50 # milliseconds
init(data)
# create the root and the canvas
# root = Tk()
canvas = Canvas(root, width=data.width, height=data.height)
canvas.pack()
# set up events
root.bind("<B1-Motion>", lambda event: mouseMovedWrapper(event, canvas, data))
timerFiredWrapper(canvas, data)
root.bind("<Button-1>", lambda event:
mousePressedWrapper(event, canvas, data))
root.bind("<Key>", lambda event:
keyPressedWrapper(event, canvas, data))
# and launch the app
root.mainloop() # blocks until window is closed
print("bye!")
run(1000, 750)
About the openCV part is like this:
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeCascade = cv2.CascadeClassifier('haarcascade_eye.xml')
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
# cv2.imread('face2.jpg',1)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# img = cv2.imread('face2.jpg',1)
# img = cv2.resize(img, (x,y), fx=0.1, fy=0.1)
# cv2.imshow('img', img)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eyeCascade.detectMultiScale(roi_gray, 1.1, 2, minSize=(70, 70), flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
for (x1, y1, w1, h1) in eyes:
cv2.rectangle(roi_color, (x1, y1), (x1+w1, y1+h1), (0, 0, 255), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
So how can I achieve like that? Can anybody help me?