Die Anleitung gibt es hier: http://www.dasskript.com/wiki/augmented ... und_opengl
die Funktion" detect_faces" in meinem code ist eigentlich nur eine Kopie der o.g. Anleitung - ich habe sie lediglich für Tkinter umgeändert.
Code: Alles auswählen
import Tkinter as tk
import cv
import opencv
from opencv import highgui
from PIL import ImageTk, ImageDraw
TITLE = "Face Detection"
UPDATE_TIME = 20
CAMERA_INDEX = 0
CASCADE = "haarcascade_frontalface_alt.xml"
MIN_SIZE = (50, 50)
IMAGE_SCALE = 2
HAAR_SCALE = 1.2
MIN_NEIGHBORS = 2
HAAR_FLAGS = 0
class Cam(tk.Label):
def __init__(self, root):
tk.Label.__init__(self, root)
self.root = root
self.camera = highgui.cvCreateCameraCapture(CAMERA_INDEX)
self.cascade = cv.Load(CASCADE)
self.tk_image = None
def run(self):
self.tk_image = ImageTk.PhotoImage(self.detect_faces())
self.config(image = self.tk_image)
self.root.after(UPDATE_TIME, self.run)
def detect_faces(self):
frame = highgui.cvQueryFrame(self.camera)
current_image = opencv.adaptors.Ipl2PIL(frame)
frame_copy = cv.CreateImage((frame.width, frame.height),
cv.IPL_DEPTH_8U, frame.nChannels)
gray_image = cv.CreateImage((frame_copy.width, frame_copy.height), 8,
1)
small_img = cv.CreateImage((cv.Round(frame_copy.width / IMAGE_SCALE),
cv.Round(frame_copy.height / IMAGE_SCALE)), 8, 1)
cv.CvtColor(frame_copy, gray_image, cv.CV_BGR2GRAY)
cv.Resize(gray_image, small_img, cv.CV_INTER_LINEAR)
cv.EqualizeHist(small_img, small_img)
faces = cv.HaarDetectObjects(small_img, self.cascade,
cv.CreateMemStorage(0), HAAR_SCALE, MIN_NEIGHBORS, HAAR_FLAGS,
MIN_SIZE)
if faces:
for ((x, y, w, h), n) in faces:
pt1 = (int(x * IMAGE_SCALE), int(y * IMAGE_SCALE))
pt2 = (int((x + w) * IMAGE_SCALE), int((y + h) * IMAGE_SCALE))
draw = ImageDraw.Draw(current_image)
draw.rectangle((pt1, pt2))
return current_image
if __name__ == '__main__':
root = tk.Tk()
cam = Cam(root)
cam.pack()
cam.run()
root.title(TITLE)
root.mainloop()