Amazing-Python-Scripts
45 строк · 1.4 Кб
1import cv22import label_image3
4size = 45
6
7# We load the xml file
8classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')9
10webcam = cv2.VideoCapture(0) # Using default WebCam connected to the PC.11
12while True:13(rval, im) = webcam.read()14im = cv2.flip(im, 1, 0) # Flip to act as a mirror15
16# Resize the image to speed up detection17mini = cv2.resize(im, (int(im.shape[1]/size), int(im.shape[0]/size)))18
19# detect MultiScale / faces20faces = classifier.detectMultiScale(mini)21
22# Draw rectangles around each face23for f in faces:24(x, y, w, h) = [v * size for v in f] # Scale the shapesize backup25cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 4)26
27# Save just the rectangle faces in SubRecFaces28sub_face = im[y:y+h, x:x+w]29
30# Saving the current image from the webcam for testing.31FaceFileName = "test.jpg"32cv2.imwrite(FaceFileName, sub_face)33
34# Getting the Result from the label_image file, i.e., Classification Result.35text = label_image.main(FaceFileName)36text = text.title() # Title Case looks Stunning.37font = cv2.FONT_HERSHEY_TRIPLEX38cv2.putText(im, text, (x+w, y), font, 1, (0, 0, 255), 2)39
40# Show the image41cv2.imshow('Capture', im)42key = cv2.waitKey(10)43# if Esc key is press then break out of the loop44if key == 0: # The Esc key45break46