packages = ["numpy", "opencv-python"]

Driver Drowsiness Detection

blog-icon github-icon

- Click on any of the videos to run the simulation

card1
card2
card3

Simulation

left_eye

right_eye

import js import cv2 import numpy as np import base64 def get_object(param): print(type(param)) print(param) def bytes_to_data_url(img_bytes): return base64.b64encode(img_bytes).decode("ascii") def get_image(dataurl, mesh_points): image_b64 = dataurl.split(",")[1] binary = base64.b64decode(image_b64) image = np.asarray(bytearray(binary), dtype="uint8") image = cv2.imdecode(image, cv2.IMREAD_UNCHANGED) img_h, img_w, _ = image.shape xTL, yTL = [mesh_points[27].x, mesh_points[27].y] # Top xLL, yLL = [mesh_points[130].x, mesh_points[130].y] # Left xBL, yBL = [mesh_points[23].x, mesh_points[23].y] # Bottom xRL, yRL = [mesh_points[244].x, mesh_points[244].y] # Right xTR, yTR = [mesh_points[257].x, mesh_points[257].y] # Top xLR, yLR = [mesh_points[464].x, mesh_points[464].y] # Left xBR, yBR = [mesh_points[253].x, mesh_points[253].y] # Bottom xRR, yRR = [mesh_points[359].x, mesh_points[359].y] # Right xTL = int(xTL * img_w) xLL = int(xLL * img_w) xBL = int(xBL * img_w) xRL = int(xRL * img_w) xTR = int(xTR * img_w) xLR = int(xLR * img_w) xBR = int(xBR * img_w) xRR = int(xRR * img_w) yTL = int(yTL * img_h) yLL = int(yLL * img_h) yBL = int(yBL * img_h) yRL = int(yRL * img_h) yTR = int(yTR * img_h) yLR = int(yLR * img_h) yBR = int(yBR * img_h) yRR = int(yRR * img_h) rightFrame = image[yTR - 2:yBR + 2, xLR - 2:xRR + 2] leftFrame = image[yTL - 2:yBL + 2, xLL - 2:xRL + 2] _, buffer = cv2.imencode(".jpg", rightFrame) data_url_R = bytes_to_data_url(buffer) link_R = f"data:image/jpg;base64,{data_url_R}" _, buffer = cv2.imencode(".jpg", leftFrame) data_url_L = bytes_to_data_url(buffer) link_L = f"data:image/jpg;base64,{data_url_L}" js.update_canvas(link_L, link_R)