-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfly_demo.py
113 lines (90 loc) · 3.53 KB
/
fly_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import cv2 as cv
import imutils
# from scipy import ndimage
from PIL import Image
import numpy as np
fig = plt.figure()
fig_two = plt.axes()
plt.axis('off')
img = Image.open('tonto.png')
img_array = np.array(img)
height, width = img_array.shape[:2]
print(f"Image dimensions: {width}x{height} pixels")
# ims is a list of lists, each row is a list of artists to draw in the
# current frame; here we are just animating one artist, the image, in
# each frame
# Add angle management class
class HeadingAngle:
def __init__(self, initial_angle=0):
self._angle = initial_angle
def get_angle(self):
return self._angle
def set_angle(self, new_angle):
self._angle = new_angle % 360 # Keep angle within 0-360 range
heading_indi(self._angle) # Update heading indicator
# Replace global angle with HeadingAngle instance
heading = HeadingAngle()
rotation_speed = 1.5 # Degrees to rotate per key press
# Create a separate figure and axes for the heading indicator
heading_fig = plt.figure()
heading_ax = heading_fig.add_subplot(111)
heading_ax.axis('off')
def heading_indi(deg):
heading_ax.clear() # Clear previous content
img = Image.open("hi2.png")
rotated_img = img.rotate(deg)
# Sample data for line
x = [171, 171]
y = [0, 50]
# Update the plot
heading_ax.plot(x, y, 'r', linewidth=2)
heading_ax.imshow(rotated_img)
heading_ax.axis('off')
plt.draw() # Update the figure
# Add keyboard event handler
def on_key(event):
if event.key == 'left':
heading.set_angle(heading.get_angle() - rotation_speed)
plt.figure(heading_fig.number) # Switch to heading figure
heading_indi(heading.get_angle()) # Force update
plt.figure(fig.number) # Switch back to main figure
elif event.key == 'right':
heading.set_angle(heading.get_angle() + rotation_speed)
plt.figure(heading_fig.number) # Switch to heading figure
heading_indi(heading.get_angle()) # Force update
plt.figure(fig.number) # Switch back to main figure
# Connect key event handler to both figures
fig.canvas.mpl_connect('key_press_event', on_key)
heading_fig.canvas.mpl_connect('key_press_event', on_key)
# Make sure both figures are visible
plt.figure(fig.number) # Switch back to main figure
im = plt.imshow(img)
def translate_image(image, x_shift, y_shift):
"""Translates the image by the specified amount in x and y directions."""
img_array = np.array(image)
M = np.float32([[1, 0, x_shift], [0, 1, y_shift]])
shifted = cv.warpAffine(img_array, M, (width, height))
return shifted
def update(frame):
# Calculate center offset based on rotation
angle_rad = np.radians(heading.get_angle())
y_offset = abs(np.sin(angle_rad) * width/4) # Adjust centering amount
# First rotate the image
img_pil = Image.fromarray(img_array)
rotated = img_pil.rotate(heading.get_angle(), expand=False, center=(width/2, height/2))
# Then translate the rotated image
shifted = translate_image(rotated, 0, -(frame - y_offset))
im.set_array(shifted)
return [im]
heading_indi(heading.get_angle())
# Add key binding before animation creation
fig.canvas.mpl_connect('key_press_event', on_key)
ani = animation.FuncAnimation(fig, update, frames=range(0, height, 2),
interval=50, blit=False)
fig_two.set_xlim(0, width/2)
fig_two.set_ylim(0,height/2)
plt.show()
# ani.save('fly_over_tonto.mp4',fps=10)