Skip to content

Commit

Permalink
overlay webcam frame in the preview scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ker0olos committed Jun 9, 2024
1 parent 49b9ea1 commit 9f9e3ef
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
7 changes: 5 additions & 2 deletions preview_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

from src.svg.model import Model
from src.tracking.tracking import Tracking
from src.utils import overlay_webcam

if __name__ == "__main__":
with open("models/normal.svg", "r") as model_file:
with open("models/face.svg", "r") as model_file:
tracking = Tracking()
model = Model(model_file.read())
tracking.set_model(model)
Expand All @@ -26,8 +27,10 @@
image_bytes = resvg_python.svg_to_png(model.tostring())
image_decoded = cv2.imdecode(np.array(bytearray(image_bytes)), cv2.IMREAD_COLOR)

final_image = overlay_webcam(frame, image_decoded)

while True:
cv2.imshow("rein", image_decoded)
cv2.imshow("rein", final_image)

if cv2.waitKey(1) & 0xFF == ord("q"):
cv2.destroyAllWindows()
Expand Down
12 changes: 7 additions & 5 deletions preview_webcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

from src.svg.model import Model
from src.tracking.tracking import Tracking
from src.utils import overlay_webcam

if __name__ == "__main__":
tracking = Tracking()

vid = cv2.VideoCapture(0)

with open("models/face.svg", "r") as model_file:
tracking = Tracking()
vid = cv2.VideoCapture(0)
model = Model(model_file.read())
tracking.set_model(model)

while True:
ret, frame = vid.read()

Expand All @@ -25,7 +25,9 @@
np.array(bytearray(image_bytes)), cv2.IMREAD_COLOR
)

cv2.imshow("rein", image_decoded)
final_image = overlay_webcam(frame, image_decoded)

cv2.imshow("rein", final_image)

if cv2.waitKey(1) & 0xFF == ord("q"):
vid.release()
Expand Down
13 changes: 1 addition & 12 deletions src/tracking/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ def _origin(bbox, px, py):
class Tracking:
def __init__(self):
options = FaceLandmarkerOptions(
base_options=BaseOptions(
model_asset_path="./face_landmarker.task", delegate="CPU"
),
base_options=BaseOptions(model_asset_path="./face_landmarker.task"),
output_face_blendshapes=True,
output_facial_transformation_matrixes=True,
num_faces=1,
running_mode=VisionRunningMode.LIVE_STREAM,
result_callback=self.__process_callback__,
Expand Down Expand Up @@ -157,16 +154,8 @@ def _get(indices):
)

def __get_mouth_size__(self):
# x0, x1 = self._getn_face([61, 291])
# y0, y1 = self._getn_face([0, 17])

# w = x1[0] - x0[0]
# h = y1[1] - y0[1]
# print((w, h))
w = 1.0 - self.__face_blendshape["mouthPucker"]
h = self.__face_blendshape["jawOpen"] / 2

print((w, h))
return self.__filter__("mouth-0", w), self.__filter__("mouth-1", h)

def __get_iris_diff__(self):
Expand Down
33 changes: 33 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import cv2
import numpy as np


def resize_image(image: np.ndarray, width=240):
if image.shape[1] == width:
return image

aspect_ratio = float(image.shape[0]) / float(image.shape[1])

height = round(width * aspect_ratio)

resized_image = cv2.resize(image, (width, height))
# brightened_image = cv2.convertScaleAbs(
# resized_image, alpha=1, beta=brightness
# )
# return brightened_image
return resized_image


def overlay_webcam(webcam_image, output_image, x=5, y=5):
webcam_image = resize_image(webcam_image)
# if img.ndim == 3 and img.shape[2] == 4:
# img = img[..., :3]
start_x = x
start_y = y

output_image[
start_y : start_y + webcam_image.shape[0],
start_x : start_x + webcam_image.shape[1],
] = webcam_image

return output_image

0 comments on commit 9f9e3ef

Please sign in to comment.