def getVideo(window_name, camera_id, catch_pic_num, path_name): cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) model = Model() model.load_model(model_path) # 人脸识别分类器 classfier = \ cv2.CascadeClassifier("/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml") # 识别出人脸后要画的边框的颜色,RGB格式 color = (0, 255, 0) # open video capture = cv2.VideoCapture(camera_id) captureNum = 0 if not os.path.exists("%s/" % path_name): os.makedirs("%s/" % path_name) while capture.isOpened(): ok, img = capture.read() # 转为灰度,容易识别 grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32)) if len(faceRects) > 0: for rect in faceRects: x, y, w, h, = rect img_name = '%s/me_%d.jpg' % (path_name, captureNum) image = img[y - 10:y + h + 10, x - 10:x + w + 10] #cv2.imwrite(img_name, image) faceId = model.face_predict(image) print faceId captureNum += 1 if captureNum > catch_pic_num: break cv2.rectangle(img, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 2) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img, 'num:%d' % (captureNum), (x + 30, y + 30), font, 1, (255, 0, 255), 4) if captureNum > catch_pic_num: break cv2.imshow(window_name, img) key = cv2.waitKey(1) if key == 27: break capture.release() cv2.destroyAllWindows()
def testBadTypeFeatureKeys(self): with self.assertRaises(Exception) as context: model = Model(feature_keys_spec='comment_text', prediction_keys='prediction_key', model_names=None, project_name=None) self.assertIn('Spec should be a dictionary', str(context.exception))
def testModelIsCompatibleWithDataset(self): model = Model(feature_keys_spec={ 'comment_text': EncodingFeatureSpec.LIST_STRING }, prediction_keys='prediction_key', model_names=None, project_name=None) def input_fn(max_n_examples): return pd.DataFrame({ 'comment_text': ['This is one'] * max_n_examples, 'label_name': [0] * max_n_examples }) try: dataset = Dataset(input_fn) dataset.check_compatibility(model) except ValueError: self.fail('Dataset raised an exception unexpectedly!')
def testInputFnMissingFeatureKeys(self): model = Model(feature_keys_spec={ 'comment_text': EncodingFeatureSpec.LIST_STRING }, prediction_keys='prediction_key', model_names=None, project_name=None) def input_fn(max_n_examples): return pd.DataFrame({ 'other_feature': ['This is one'] * max_n_examples, 'label_name': [0] * max_n_examples }) with self.assertRaises(Exception) as context: dataset = Dataset(input_fn) dataset.check_compatibility(model) self.assertIn('input_fn must contain at least the feature keys', str(context.exception))
def setUp(self): def input_fn_test(max_n_examples): return pd.DataFrame( {'comment_text': [['This', 'is', 'one']] * max_n_examples}) gcs_path_test = os.path.join('gs://kaggle-model-experiments/', getpass.getuser(), 'unittest', 'dataset_test', TestEndPipeline.test_version) self.dataset = Dataset(input_fn_test, gcs_path_test) self.dataset.load_data(5) model_input_spec = { 'comment_text': EncodingFeatureSpec.LIST_STRING, } self.model = Model(feature_keys_spec=model_input_spec, prediction_keys='frac_neg/logistic', example_key='comment_key', model_names=[ 'tf_gru_attention:v_20180914_163804', 'tf_gru_attention:v_20180823_133625' ], project_name='wikidetox')