Amazing-Python-Scripts
35 строк · 1.3 Кб
1import cv22cap = cv2.VideoCapture(0)3face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")4
5while True:6# capture camera frame, and ret store true and false7r, frame = cap.read()8gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)9
10if r == False:11continue12faces = face_cascade.detectMultiScale(gray_frame, 1.3, 5)13
14# """ The first argument is the image, the second is the15# scalefactor (how much the image size will be reduced at each image scale),16# and the third is the minNeighbors (how many neighbors each rectangle should have)"""17
18for (x, y, w, h) in faces:19cv2.rectangle(frame, (x, y), (x+w, y+h),20(255, 125, 0), 2) # color and width21cv2.putText(frame, "DETECTED", (x, y-10),22cv2.FONT_HERSHEY_SIMPLEX, 1, (100, 125, 255), 1, cv2.LINE_AA)23cv2.imshow("video frame", frame)24key_pressed = cv2.waitKey(1) & 0xFF25
26# """cv2.waitKey() returns a 32 Bit integer value (might be dependent on the platform).27# The key input is in ASCII which is an 8 Bit integer value. So you only care28# about these 8 bits and want all other bits to be 0. This you can achieve with:0xFF"""29
30# ord converts characters in unicode31if key_pressed == ord('n'):32break33
34cap.release()35cv2.destroyAllWindows()36