Added typing to all funtions.
Added a pixel mirror function and trying to print both eyes now.
This commit is contained in:
parent
c7d9d9a537
commit
3bf5083b99
1 changed files with 22 additions and 20 deletions
|
|
@ -48,7 +48,16 @@ class Point2D:
|
|||
return (self.x, self.y) == (other.x, other.y)
|
||||
|
||||
|
||||
def generate_point_array_from_image(image):
|
||||
def mirror_points(points: list[Point2D]) -> list[Point2D]:
|
||||
mirrored_points = []
|
||||
for point in points:
|
||||
mirrored_x = 128 - point.x # Calculate the mirrored x-coordinate
|
||||
mirrored_point = Point2D(mirrored_x, point.y)
|
||||
mirrored_points.append(mirrored_point)
|
||||
return mirrored_points
|
||||
|
||||
|
||||
def generate_point_array_from_image(image: Image) -> list[Point2D]:
|
||||
image = image.convert("RGB") # Convert image to RGB color mode
|
||||
|
||||
width, height = image.size
|
||||
|
|
@ -65,7 +74,7 @@ def generate_point_array_from_image(image):
|
|||
return point_array
|
||||
|
||||
|
||||
def generate_image_from_point_array(points, width, height):
|
||||
def generate_image_from_point_array(points: list[Point2D], width: int, height: int) -> Image:
|
||||
# Create a new blank image
|
||||
image = Image.new("RGB", (width, height), "black")
|
||||
|
||||
|
|
@ -80,7 +89,7 @@ def generate_image_from_point_array(points, width, height):
|
|||
return image
|
||||
|
||||
|
||||
def pair_points(points1, points2):
|
||||
def pair_points(points1: list[Point2D], points2: list[Point2D]) -> list[tuple[Point2D, Point2D]]:
|
||||
# Determine the size of the point arrays
|
||||
size1 = len(points1)
|
||||
size2 = len(points2)
|
||||
|
|
@ -122,8 +131,8 @@ def pair_points(points1, points2):
|
|||
return pairs
|
||||
|
||||
|
||||
def interpolate_point_pairs(pairs, percentage):
|
||||
interpolated_points = []
|
||||
def interpolate_point_pairs(pairs: list[tuple[Point2D, Point2D]], percentage: float) -> list[Point2D]:
|
||||
interpolated_points:list[Point2D] = []
|
||||
for pair in pairs:
|
||||
point1, point2 = pair
|
||||
interpolated_point = point1.interpolate(point2, percentage)
|
||||
|
|
@ -137,11 +146,7 @@ Image2 = Image.open("faces/eyeLeftClosed.png")
|
|||
pixelArray1 = generate_point_array_from_image(Image1)
|
||||
pixelArray2 = generate_point_array_from_image(Image2)
|
||||
|
||||
pairs = pair_points(pixelArray1, pixelArray2)
|
||||
|
||||
|
||||
|
||||
|
||||
LeftEyeBlinkPairs = pair_points(pixelArray1, pixelArray2)
|
||||
|
||||
|
||||
DesiredBlinkState = 10
|
||||
|
|
@ -149,24 +154,21 @@ currentBlinkState = 0
|
|||
|
||||
blinkFrameCanvases = []
|
||||
|
||||
offscreen_interpolated_canvasA = matrix.CreateFrameCanvas()
|
||||
offscreen_interpolated_canvasA.SetImage(generate_image_from_point_array(interpolate_point_pairs(pairs, 0), 128, 32), unsafe=False)
|
||||
blinkFrameCanvases.append(offscreen_interpolated_canvasA)
|
||||
|
||||
for alpha in range(1,10):
|
||||
for alpha in range(0,11):
|
||||
offscreen_interpolated_canvas = matrix.CreateFrameCanvas()
|
||||
interpolated_image = generate_image_from_point_array(interpolate_point_pairs(pairs, alpha/10), 128, 32)
|
||||
leftEye = interpolate_point_pairs(LeftEyeBlinkPairs, alpha/10)
|
||||
RightEye = mirror_points(leftEye)
|
||||
face = leftEye + RightEye
|
||||
interpolated_image = generate_image_from_point_array(face, 128, 32)
|
||||
offscreen_interpolated_canvas.SetImage(interpolated_image, unsafe=False)
|
||||
blinkFrameCanvases.append(offscreen_interpolated_canvas)
|
||||
|
||||
offscreen_interpolated_canvasB = matrix.CreateFrameCanvas()
|
||||
offscreen_interpolated_canvasB.SetImage(generate_image_from_point_array(interpolate_point_pairs(pairs, 1), 128, 32), unsafe=False)
|
||||
blinkFrameCanvases.append(offscreen_interpolated_canvasB)
|
||||
|
||||
|
||||
|
||||
def update_screen():
|
||||
global DesiredBlinkState, currentBlinkState, blinkFrameCanvases, matrix, offscreen_interpolated_canvasA
|
||||
global DesiredBlinkState, currentBlinkState, blinkFrameCanvases, matrix
|
||||
|
||||
# open eye again after blink
|
||||
if currentBlinkState == 10:
|
||||
|
|
@ -217,6 +219,6 @@ client.loop_start()
|
|||
|
||||
|
||||
while True:
|
||||
time.sleep(0.05)
|
||||
time.sleep(0.01)
|
||||
update_screen()
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue