def test_no_faces(self, monkeypatch): monkeypatch.setattr('pychubby.detect.face_rectangle', lambda *args, **kwargs: ([], [])) img = np.random.random((10, 11)) with pytest.raises(ValueError): LandmarkFace.estimate(img)
def test_multiple_faces(self, monkeypatch): img = np.random.random((10, 11)) monkeypatch.setattr('pychubby.detect.face_rectangle', lambda *args, **kwargs: (2 * [None], 2 * [None])) monkeypatch.setattr('pychubby.detect.landmarks_68', lambda *args: (np.random.random((68, 2)), None)) with pytest.raises(ValueError): LandmarkFace.estimate(img, allow_multiple=False) lfs = LandmarkFace.estimate(img, allow_multiple=True) assert isinstance(lfs, LandmarkFaces) assert len(lfs) == 2 # only feed invalid, empty entry for LandmarkFaces constructor monkeypatch.setattr('pychubby.detect.landmarks_68', lambda *args: (np.zeros((68, 2)), None)) with pytest.raises(ValueError): LandmarkFace.estimate(img, allow_multiple=True)
def test_overall(self, monkeypatch): img = np.random.random((10, 11)) monkeypatch.setattr('pychubby.detect.face_rectangle', lambda *args, **kwargs: ([None], [None])) monkeypatch.setattr('pychubby.detect.landmarks_68', lambda *args: (np.random.random((68, 2)), None)) lf = LandmarkFace.estimate(img) assert isinstance(lf, LandmarkFace) assert lf.points.shape == (68, 2) assert lf.img.shape == (10, 11)
def smile(img): img_path = img img = cv2.imread(img_path) img8 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) lf = LandmarkFace.estimate(img8) from pychubby.actions import Smile a = Smile(scale=0.2) new_lf, df = a.perform(lf) ani = create_animation(df, img) plt.imsave('output_image.gif', ani) plt.show()
def f(*args, **kwargs): """Perform an action.""" inp_img = kwargs.pop("inp_img") out_img = kwargs.pop("out_img") img = plt.imread(str(inp_img)) lf = LandmarkFace.estimate(img) cls = getattr(pychubby.actions, self.name) a = pychubby.actions.Multiple(cls(**kwargs)) new_lf, df = a.perform(lf) if out_img is not None: plt.imsave(str(out_img), new_lf[0].img) else: new_lf.plot(show_landmarks=False, show_numbers=False)
def perform(*args, **kwargs): """Perform an action.""" action = kwargs.get("action", None) logger.info("action: {} START".format(action)) # Get uploaded file information: file = connexion.request.files["inp_img"] if action not in cli.ALL_ACTIONS: return problem( title="Wrong Action", detail="The action: {} does not exist.".format(action), status=400, ) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join("/tmp/", filename)) logger.info("inp_file: {}".format(file.filename)) img = plt.imread("/tmp/{}".format(filename)) logger.info("LandmarkFace.estimate") lf = LandmarkFace.estimate(img) logger.info("pychubby action: {}".format(action)) cls = getattr(pychubby.actions, action) a = pychubby.actions.Multiple(cls()) logger .info("new_lf: a.perform") new_lf, df = a.perform(lf) out_img = "/tmp/chubbyfied_{}".format(file.filename) logger.info("Saving output image: {}".format(out_img)) plt.imsave(str(out_img), new_lf[0].img) logger.info("action: {} END".format(action)) return send_file(out_img), 200 else: return problem( title="File not allowed", detail="The Input File: '{}' is not allowed.".format(file.filename), status=400, )
def photo(): img = request.args.get('photob62') imgdata = base64.b64decode(img) img = cv2.imread(io.BytesIO(base64.b64decode(imgdata))) img = cv2.imread(img) img8 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) lf = LandmarkFace.estimate(img8) from pychubby.actions import Smile, OpenEyes, Multiple, RaiseEyebrow, StretchNostrils, AbsoluteMove smile = Smile(scale=0.2) new_lf, df = smile.perform(lf) # lf defined above # new_lf.plot(show_landmarks=False) plt.imsave('output_image.png', new_lf.img) import base64 encoded = base64.b64encode(open("output_image.png", "rb").read()) return encoded
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # show the face number cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # loop over the (x, y)-coordinates for the facial landmarks # and draw them on the image for (x, y) in shape: cv2.circle(image, (x, y), 2, (0, 0, 255, 255), -1) cv2.circle(blank_image, (x, y), 2, (0, 0, 255, 0), -1) cv2.imshow('Video', blank_image) cv2.imshow('video1', image) # img = frame print(frame.shape) try: lf = LandmarkFace.estimate(img) a_per_face = Pipeline([Smile()]) a_all = Multiple(a_per_face) new_lf, _ = a_all.perform(lf) new_img = new_lf #new_lf.plot(figsize=(5, 5), show_numbers=False) except: pass # data = np.fromstring(new_lf, dtype=np.uint8, sep='') # cv2.imshow('video', data) cam.send(blank_image) cam.sleep_until_next_frame() if cv2.waitKey(1) & 0xFF == ord('q'):