Skip to content

Commit

Permalink
add example in iris demo to load video from video file instead of webcam
Browse files Browse the repository at this point in the history
  • Loading branch information
Rassibassi committed Jul 19, 2021
1 parent 35eea65 commit f2c3556
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 97 deletions.
2 changes: 1 addition & 1 deletion face_detection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mediapipe as mp

from webcam import WebcamSource
from videosource import WebcamSource

mp_drawing = mp.solutions.drawing_utils
mp_face_detection = mp.solutions.face_detection
Expand Down
2 changes: 1 addition & 1 deletion face_mesh.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mediapipe as mp

from webcam import WebcamSource
from videosource import WebcamSource

mp_drawing = mp.solutions.drawing_utils
mp_face_mesh = mp.solutions.face_mesh
Expand Down
2 changes: 1 addition & 1 deletion hands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mediapipe as mp

from webcam import WebcamSource
from videosource import WebcamSource

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
Expand Down
2 changes: 1 addition & 1 deletion head_posture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import mediapipe as mp
import numpy as np

from webcam import WebcamSource
from videosource import WebcamSource

from custom.face_geometry import ( # isort:skip
PCF,
Expand Down
2 changes: 1 addition & 1 deletion holistic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mediapipe as mp

from webcam import WebcamSource
from videosource import WebcamSource

mp_drawing = mp.solutions.drawing_utils
mp_holistic = mp.solutions.holistic
Expand Down
67 changes: 28 additions & 39 deletions iris.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
import argparse

import cv2
import mediapipe as mp
import numpy as np

from webcam import WebcamSource

from custom.iris_lm_depth import ( # isort:skip
calculate_iris_depth,
detect_iris,
from_landmarks_to_depth,
)

from custom.core import ( # isort:skip
detections_to_rect,
landmarks_to_detections,
slice_from_roi,
transform_rect,
)
from custom.iris_lm_depth import from_landmarks_to_depth
from videosource import FileSource, WebcamSource

mp_face_mesh = mp.solutions.face_mesh

points_idx = [33, 133, 362, 263, 61, 291, 199]
points_idx = list(set(points_idx))
points_idx.sort()

frame_height, frame_width, channels = (720, 1280, 3)
image_size = (frame_width, frame_height)

left_eye_landmarks_id = np.array([33, 133])
right_eye_landmarks_id = np.array([362, 263])

frame_height, frame_width, channels = (720, 1280, 3)
frame_size = np.array((frame_width, frame_height))

# pseudo camera internals
focal_length = frame_width
center = (frame_width / 2, frame_height / 2)
camera_matrix = np.array(
[[focal_length, 0, center[0]], [0, focal_length, center[1]], [0, 0, 1]],
dtype="double",
)

dist_coeff = np.zeros((4, 1))

YELLOW = (0, 255, 255)
Expand All @@ -50,21 +26,28 @@
LARGE_CIRCLE_SIZE = 2


def main():
source = WebcamSource(width=frame_width, height=frame_height)
def main(inp):
if inp is None:
frame_height, frame_width = (720, 1280)
source = WebcamSource(width=frame_width, height=frame_height)
else:
source = FileSource(inp)
frame_width, frame_height = (int(i) for i in source.get_image_size())

image_size = (frame_width, frame_height)

# pseudo camera internals
focal_length = frame_width

landmarks = None
smooth_left_depth = -1
smooth_right_depth = -1
smooth_factor = 0.1
face_mesh = mp_face_mesh.FaceMesh(

with mp_face_mesh.FaceMesh(
static_image_mode=False,
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
)

with mp_face_mesh.FaceMesh(
min_detection_confidence=0.5, min_tracking_confidence=0.5
) as face_mesh:

for idx, (frame, frame_rgb) in enumerate(source):
Expand Down Expand Up @@ -129,7 +112,7 @@ def main():

# draw subset of facemesh
for ii in points_idx:
pos = (frame_size * landmarks[:2, ii]).astype(np.int32)
pos = (np.array(image_size) * landmarks[:2, ii]).astype(np.int32)
frame = cv2.circle(frame, tuple(pos), LARGE_CIRCLE_SIZE, GREEN, -1)

# draw eye contours
Expand All @@ -140,7 +123,7 @@ def main():
]
)
for landmark in eye_landmarks:
pos = (frame_size * landmark[:2]).astype(np.int32)
pos = (np.array(image_size) * landmark[:2]).astype(np.int32)
frame = cv2.circle(frame, tuple(pos), SMALL_CIRCLE_SIZE, RED, -1)

# draw iris landmarks
Expand All @@ -151,7 +134,7 @@ def main():
]
)
for landmark in iris_landmarks:
pos = (frame_size * landmark[:2]).astype(np.int32)
pos = (np.array(image_size) * landmark[:2]).astype(np.int32)
frame = cv2.circle(frame, tuple(pos), SMALL_CIRCLE_SIZE, YELLOW, -1)

# write depth values into frame
Expand All @@ -173,4 +156,10 @@ def main():


if __name__ == "__main__":
main()
parser = argparse.ArgumentParser(description="Choose video file otherwise webcam is used.")
parser.add_argument(
"-i", metavar="path-to-file", type=str, help="Path to video file"
)

args = parser.parse_args()
main(args.i)
2 changes: 1 addition & 1 deletion objectron.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mediapipe as mp

from webcam import WebcamSource
from videosource import WebcamSource

mp_drawing = mp.solutions.drawing_utils
mp_objectron = mp.solutions.objectron
Expand Down
2 changes: 1 addition & 1 deletion pose.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mediapipe as mp
import numpy as np

from webcam import WebcamSource
from videosource import WebcamSource

mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
Expand Down
3 changes: 1 addition & 2 deletions selfie_segmentation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import cv2
import mediapipe as mp
import numpy as np

from webcam import WebcamSource
from videosource import WebcamSource

mp_drawing = mp.solutions.drawing_utils
mp_selfie_segmentation = mp.solutions.selfie_segmentation
Expand Down
80 changes: 80 additions & 0 deletions videosource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import cv2
import numpy as np
from PIL import Image


class VideoSource:
def __init__(self, display=False, dtype=np.uint8):
self._name = "VideoSource"
self._capture = None
self._display = display
self._dtype = dtype

def get_fps(self):
return self._capture.get(cv2.CAP_PROP_FPS)

def get_frame_count(self):
return int(self._capture.get(cv2.CAP_PROP_FRAME_COUNT))

def get_image_size(self):
width = self._capture.get(cv2.CAP_PROP_FRAME_WIDTH)
height = self._capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
return width, height

def release(self):
self._capture.release()
cv2.destroyAllWindows()

def __iter__(self):
self._capture.isOpened()
return self

def __next__(self):
ret, frame = self._capture.read()

if self._display:
cv2.imshow(f"{self._name} - FPS: {self.get_fps()}", frame)

if not ret:
raise StopIteration

if cv2.waitKey(1) & 0xFF == ord("q"):
raise StopIteration

cv2_im_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_rgb = np.asarray(Image.fromarray(cv2_im_rgb), dtype=self._dtype)

return frame, frame_rgb

def __del__(self):
self.release()

def show(self, frame):
cv2.imshow(f"{self._name} - FPS: {self.get_fps()}", frame)


class WebcamSource(VideoSource):
def __init__(
self,
camera_id=0,
width=1280,
height=720,
fps=30,
autofocus=0,
absolute_focus=75,
display=False,
):
super().__init__(display)
self._capture = cv2.VideoCapture(camera_id)
self._capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self._capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
self._capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
self._capture.set(cv2.CAP_PROP_FPS, fps)
self._capture.set(cv2.CAP_PROP_AUTOFOCUS, autofocus)
self._capture.set(cv2.CAP_PROP_FOCUS, absolute_focus / 255)


class FileSource(VideoSource):
def __init__(self, file_path, display=False):
super().__init__(display)
self._capture = cv2.VideoCapture(str(file_path))
49 changes: 0 additions & 49 deletions webcam.py

This file was deleted.

0 comments on commit f2c3556

Please sign in to comment.