-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmt_old.py
More file actions
64 lines (53 loc) · 1.66 KB
/
mt_old.py
File metadata and controls
64 lines (53 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import cv
def GrabAndShow(windowName, captureDevice):
frame = cv.QueryFrame(captureDevice)
if frame is None:
break
cv.Flip(frame, None, 1) # Mirror the display.
frame = DetectFaces(frame)
cv.ShowImage(windowName,frame)
def CreateWindow(windowName):
cv.NamedWindow(windowName, flags=cv.CV_WINDOW_AUTOSIZE)
cv.MoveWindow(windowName,0,0)
def CloseWindow(windowName):
cv.DestroyWindow(windowName)
def InitCapture():
numCameras = cv.cvcamGetCamerasCount()
if numCameras < 1:
print "No cameras found."
return None
else if cv.cvcamGetCamerasCount() > 1:
print "Multiple cameras found, choosing latest."
return cv.CaptureFromCAM(numCameras-1)
return cv.CaptureFromCAM(-1) # Else choose the only one.
def ReleaseCapture(captureDevice):
del captureDevice
def DetectFaces(image):
imageSize = cv.GetSize(image)
# Convert to grayscale
grayscale = cv.CreateImage(imageSize, 8, 1)
cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
# Equalise histogram
cv.EqualizeHist(grayscale, grayscale)
# Detect objects
cascade = cv.Load('/opt/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml')
faces = cv.HaarDetectObjects(grayscale,cascade,cv.CreateMemStorage(0), 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (50,50))
for ((x,y,w,h),n) in faces:
cv.Rectangle( image,
( int(x), int(y) ),
( int(x + w), int(y + h) ),
cv.CV_RGB(0, 255, 0), 3, 8, 0
)
return image
if __name__ == "__main__":
print "Press Esc to exit..."
windowName = "Test"
CreateWindow(windowName)
cvCap = InitCapture()
while(1):
GrabAndShow(windowName, cvCap)
key = cv.WaitKey(10)
if key == 27: #Esc to quit.
break
CloseWindow(windowName)
ReleaseCapture(cvCap)