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
| img_path = 'data/test_img2.png' test_img = cv2.imread(img_path) test_img_org = cv2.imread(img_path, 0)
faces = faceCascade.detectMultiScale(test_img_org, scaleFactor=1.15, minNeighbors=10, minSize=(5,5)) if len(faces) >= 1: for (x, y, w, h) in faces: face = test_img_org[y:y + h, x:x + w] face = cv2.resize(face, dsize=img_size) face = cv2.equalizeHist(face)
test_img_pca = pca.transform(face.reshape((1, -1))) svm_pred = svm_model.predict(test_img_pca)[0] svm_max_P = svm_model.predict_proba(test_img_pca).max(axis=1)[0] if svm_max_P < 0.8: name_pred='NULL' test_img = cv2.rectangle(test_img, (x, y), (x+w, y+h), (25, 57, 191), int(np.ceil(test_img_org.shape[0]/70))) test_img = cv2.putText(test_img, name_pred, (x-5, y-5), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=test_img_org.shape[0]/ 400, color=(25, 57, 191), thickness=int(np.floor(test_img_org.shape[0]/200))) else: name_pred = name_list[svm_pred]
test_img = cv2.rectangle(test_img, (x, y), (x+w, y+h), (255,224, 93), int(np.ceil(test_img_org.shape[0]/70))) test_img = cv2.putText(test_img, name_pred, (x-5, y-5), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=test_img_org.shape[0]/ 400, color=(255,224, 93), thickness=int(np.floor(test_img_org.shape[0]/200))) else: print('未检测到人脸!')
|