|
|
@ -12,8 +12,8 @@ import adafruit_mpr121
|
|
|
|
MEDIA_FOLDER = "media"
|
|
|
|
MEDIA_FOLDER = "media"
|
|
|
|
WINDOW_NAME = "Image Viewer"
|
|
|
|
WINDOW_NAME = "Image Viewer"
|
|
|
|
|
|
|
|
|
|
|
|
WINDOW_WIDTH = 300
|
|
|
|
WINDOW_WIDTH = 475
|
|
|
|
WINDOW_HEIGHT = 500
|
|
|
|
WINDOW_HEIGHT = 370
|
|
|
|
|
|
|
|
|
|
|
|
i2c = busio.I2C(board.SCL, board.SDA)
|
|
|
|
i2c = busio.I2C(board.SCL, board.SDA)
|
|
|
|
mpr121 = adafruit_mpr121.MPR121(i2c)
|
|
|
|
mpr121 = adafruit_mpr121.MPR121(i2c)
|
|
|
@ -31,44 +31,37 @@ def clear_frame():
|
|
|
|
cv2.waitKey(1) # Add a small delay to ensure the frame is displayed
|
|
|
|
cv2.waitKey(1) # Add a small delay to ensure the frame is displayed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def resize_and_center_frame(frame):
|
|
|
|
def get_frame_with_fill(frame, w_w, w_h):
|
|
|
|
# Get original frame dimensions
|
|
|
|
# Resize the frame to fit within 400x400 while maintaining aspect ratio
|
|
|
|
height, width = frame.shape[:2]
|
|
|
|
h, w = frame.shape[:2]
|
|
|
|
|
|
|
|
|
|
|
|
# Calculate aspect ratio
|
|
|
|
if w > h:
|
|
|
|
aspect_ratio = width / height
|
|
|
|
scale = w_w / w
|
|
|
|
|
|
|
|
|
|
|
|
# Determine resizing dimensions while maintaining aspect ratio
|
|
|
|
|
|
|
|
if width > height:
|
|
|
|
|
|
|
|
new_width = WINDOW_WIDTH
|
|
|
|
|
|
|
|
new_height = int(new_width / aspect_ratio)
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
new_height = WINDOW_HEIGHT
|
|
|
|
scale = w_h / h
|
|
|
|
new_width = int(new_height * aspect_ratio)
|
|
|
|
|
|
|
|
|
|
|
|
# Ensure the image does not exceed the max dimensions
|
|
|
|
# Resize the frame
|
|
|
|
if scale * h > w_h:
|
|
|
|
resized_frame = cv2.resize(frame, (new_width, new_height))
|
|
|
|
scale = w_h / h
|
|
|
|
|
|
|
|
elif scale * w > w_w:
|
|
|
|
# Calculate offset to center the resized frame in the window
|
|
|
|
scale = w_w / w
|
|
|
|
x_offset = max(0, (WINDOW_WIDTH - new_width) // 2)
|
|
|
|
|
|
|
|
y_offset = max(0, (WINDOW_HEIGHT - new_height) // 2)
|
|
|
|
print(scale)
|
|
|
|
|
|
|
|
new_w = int(w * scale)
|
|
|
|
# Create a black background of the window size
|
|
|
|
new_h = int(h * scale)
|
|
|
|
window_background = np.zeros((WINDOW_HEIGHT, WINDOW_WIDTH, 3), dtype=np.uint8)
|
|
|
|
resized = cv2.resize(frame, (new_w, new_h), interpolation=cv2.INTER_AREA)
|
|
|
|
|
|
|
|
|
|
|
|
# Overlay the resized frame onto the background at the calculated offset
|
|
|
|
background = np.zeros((w_h, w_w, 3), dtype=np.uint8)
|
|
|
|
print(y_offset)
|
|
|
|
|
|
|
|
print(y_offset)
|
|
|
|
# Get the dimensions of the resized frame
|
|
|
|
print(new_height)
|
|
|
|
h, w = resized.shape[:2]
|
|
|
|
print(x_offset)
|
|
|
|
# Calculate the top-left corner to place the resized frame on the black background
|
|
|
|
print(x_offset)
|
|
|
|
x_offset = (w_w - w) // 2
|
|
|
|
print(new_width)
|
|
|
|
y_offset = (w_h - h) // 2
|
|
|
|
|
|
|
|
|
|
|
|
window_background[
|
|
|
|
# Place the resized frame on the black background
|
|
|
|
y_offset : y_offset + new_height, x_offset : x_offset + new_width
|
|
|
|
background[y_offset:y_offset + h, x_offset:x_offset + w] = resized
|
|
|
|
] = resized_frame
|
|
|
|
return background
|
|
|
|
|
|
|
|
|
|
|
|
return window_background
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Show an image on the canvas
|
|
|
|
# Show an image on the canvas
|
|
|
@ -81,8 +74,11 @@ def showImage(file):
|
|
|
|
return
|
|
|
|
return
|
|
|
|
a = cv2.cvtColor(current_image, cv2.COLOR_BGR2GRAY)
|
|
|
|
a = cv2.cvtColor(current_image, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
|
|
|
|
|
# resized_frame = resize_and_center_frame(current_image)
|
|
|
|
cv2.namedWindow(WINDOW_NAME,cv2.WND_PROP_FULLSCREEN)
|
|
|
|
cv2.imshow(WINDOW_NAME, current_image)
|
|
|
|
cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
|
|
|
|
|
|
|
|
cv2.imshow(WINDOW_NAME, get_frame_with_fill(current_image, WINDOW_WIDTH, WINDOW_HEIGHT))
|
|
|
|
|
|
|
|
# cv2.namedWindow("Name", CV_WINDOW_NORMAL)
|
|
|
|
|
|
|
|
# cv2.setWindowProperty("Name", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN)
|
|
|
|
# cv2.namedWindow(WINDOW_NAME,cv2.WND_PROP_FULLSCREEN)
|
|
|
|
# cv2.namedWindow(WINDOW_NAME,cv2.WND_PROP_FULLSCREEN)
|
|
|
|
# cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
|
|
|
|
# cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
|
|
|
|
|
|
|
|
|
|
|
@ -136,10 +132,15 @@ def load_media(region=None, override=None):
|
|
|
|
print(region)
|
|
|
|
print(region)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
print("region is not defined, just do something!")
|
|
|
|
print("region is not defined, just do something!")
|
|
|
|
|
|
|
|
region=0
|
|
|
|
|
|
|
|
|
|
|
|
folder = os.listdir(MEDIA_FOLDER)[region]
|
|
|
|
foldercount = len(os.listdir(MEDIA_FOLDER))
|
|
|
|
|
|
|
|
print(foldercount)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(foldercount < region):
|
|
|
|
|
|
|
|
region = foldercount - region
|
|
|
|
|
|
|
|
|
|
|
|
print(folder)
|
|
|
|
folder = os.listdir(MEDIA_FOLDER)[region]
|
|
|
|
|
|
|
|
|
|
|
|
if(override):
|
|
|
|
if(override):
|
|
|
|
file = override
|
|
|
|
file = override
|
|
|
@ -162,12 +163,13 @@ def load_media(region=None, override=None):
|
|
|
|
# show the input video file
|
|
|
|
# show the input video file
|
|
|
|
while cap.isOpened():
|
|
|
|
while cap.isOpened():
|
|
|
|
ret, frame = cap.read()
|
|
|
|
ret, frame = cap.read()
|
|
|
|
# resized = cv2.resize(frame, (800, 480))
|
|
|
|
|
|
|
|
resized_frame = resize_and_center_frame(frame)
|
|
|
|
|
|
|
|
# if q is pressed, the app is closed
|
|
|
|
# if q is pressed, the app is closed
|
|
|
|
|
|
|
|
|
|
|
|
if ret == True:
|
|
|
|
if ret == True:
|
|
|
|
cv2.imshow(WINDOW_NAME, resized_frame)
|
|
|
|
cv2.namedWindow(WINDOW_NAME,cv2.WND_PROP_FULLSCREEN)
|
|
|
|
|
|
|
|
cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
|
|
|
|
|
|
|
|
cv2.imshow(WINDOW_NAME, get_frame_with_fill(frame, WINDOW_WIDTH, WINDOW_HEIGHT))
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(12):
|
|
|
|
for i in range(12):
|
|
|
|
if mpr121[i].value:
|
|
|
|
if mpr121[i].value:
|
|
|
@ -180,7 +182,7 @@ def load_media(region=None, override=None):
|
|
|
|
break
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
print("ois the video finished?")
|
|
|
|
print("ois the video finished?")
|
|
|
|
load_media()
|
|
|
|
load_media(0)
|
|
|
|
break
|
|
|
|
break
|
|
|
|
elif file.endswith(".wav") or file.endswith(".mp3"):
|
|
|
|
elif file.endswith(".wav") or file.endswith(".mp3"):
|
|
|
|
print("ive loaded an audio file: " + file)
|
|
|
|
print("ive loaded an audio file: " + file)
|
|
|
|