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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| """ # File : mask_check.py # Time :2021/6/10 15:02 # Author :Meng # version :python 3.10 # Description: """ import cv2 import time
"""实现鼻子检测""" def nose_dection(img): img = cv2.GaussianBlur(img,(5,5),0) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) nose_cascade = cv2.CascadeClassifier("./haarcascades/haarcascade_mcs_nose.xml") nose_cascade.load("./haarcascades/haarcascade_mcs_nose.xml") '''此文件是opencv的haar鼻子特征分类器''' noses = nose_cascade.detectMultiScale(gray, 1.3, 5) for(x,y,w,h) in noses: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) flag = 0 if len(noses)>0: flag = 1 return img,flag
""""实现眼睛检测""" def eye_dection(img): img = cv2.GaussianBlur(img,(5,5),0) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) eyes_cascade = cv2.CascadeClassifier("./haarcascades/haarcascade_eye_tree_eyeglasses.xml") eyes_cascade.load("./haarcascades/haarcascade_eye_tree_eyeglasses.xml") '''此文件是opencv的haar眼睛特征分类器''' eyes = eyes_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in eyes: frame = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) print("x y w h is",(x,y,w,h)) return img,eyes
def empty(a): pass
def main(): image = cv2.imread("images/backgound.png") cv2.imshow('skin', image) cv2.createTrackbar("Hmin", "skin", 0, 90, empty) cv2.createTrackbar("Hmax", "skin", 25, 90, empty) capture = cv2.VideoCapture(0) while True: ref,img=capture.read() img_hsv = img image_nose,flag_nose = nose_dection(img) if flag_nose == 1: frame = cv2.putText(image_nose, "NO MASK", (10, 30), cv2.FONT_HERSHEY_COMPLEX, 0.9,(0, 0, 255), 1) cv2.imshow('img', image_nose) if flag_nose == 0: img_eye,eyes = eye_dection(img) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) H, S, V = cv2.split(hsv) Hmin= cv2.getTrackbarPos("Hmin", 'skin') Hmax= cv2.getTrackbarPos("Hmax", 'skin') if Hmin> Hmax: Hmax= Hmin thresh_h = cv2.inRange(H, Hmin, Hmax) if len(eyes) > 1: mask_x_begin = min(eyes[0][0],eyes[1][0]) mask_x_end = max(eyes[0][0],eyes[1][0]) + eyes[list([eyes[0][0], eyes[1][0]]).index(max(list([eyes[0][0], eyes[1][0]])))][2] mask_y_begin = max(eyes[0][1] + eyes[0][3],eyes[1][1] + eyes[1][3]) + 20 if mask_y_begin > img_eye.shape[1]: mask_y_begin = img_eye.shape[1] mask_y_end = max(eyes[0][1] + 3 * eyes[0][3],eyes[1][1] + 3 * eyes[1][3]) + 20 if mask_y_end > img_eye.shape[1]: mask_y_end = img_eye.shape[1] frame = cv2.rectangle(img_eye, (mask_x_begin, mask_y_begin), (mask_x_end, mask_y_end), (255, 0, 0), 2) total_mask_pixel = 0 total_face_pixel = 0 for i in range(mask_x_begin,mask_x_end): for j in range(mask_y_begin,mask_y_end): if 0<i<480 and 0<j<480: if thresh_h[i,j] == 0: total_mask_pixel += 1 else: total_face_pixel += 1 print("total_mask_pixel",total_mask_pixel) print("total_face_pixel", total_face_pixel) if total_mask_pixel > total_face_pixel: frame = cv2.putText(img_eye, "HAVE MASK", (mask_x_begin, mask_y_begin - 10),cv2.FONT_HERSHEY_COMPLEX, 0.9, (0, 0, 255), 1) if total_mask_pixel < total_face_pixel: frame = cv2.putText(img_eye, "NO MASK", (mask_x_begin, mask_y_begin - 10), cv2.FONT_HERSHEY_COMPLEX,0.9, (0, 0, 255), 1) cv2.imshow("skin", thresh_h) cv2.imshow("img", img_eye) c = cv2.waitKey(10) if c==27: break capture.release() cv2.destroyAllWindows()
if __name__ == '__main__': main()
|