include correct regions

main
vitrinekast 5 months ago
parent c0e7473732
commit ff7710ecb5

@ -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)
# Resize the frame
resized_frame = cv2.resize(frame, (new_width, new_height))
# Calculate offset to center the resized frame in the window # Ensure the image does not exceed the max dimensions
x_offset = max(0, (WINDOW_WIDTH - new_width) // 2) if scale * h > w_h:
y_offset = max(0, (WINDOW_HEIGHT - new_height) // 2) scale = w_h / h
elif scale * w > w_w:
scale = w_w / w
# Create a black background of the window size print(scale)
window_background = np.zeros((WINDOW_HEIGHT, WINDOW_WIDTH, 3), dtype=np.uint8) new_w = int(w * scale)
new_h = int(h * scale)
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)
print(new_height)
print(x_offset)
print(x_offset)
print(new_width)
window_background[ # Get the dimensions of the resized frame
y_offset : y_offset + new_height, x_offset : x_offset + new_width h, w = resized.shape[:2]
] = resized_frame # Calculate the top-left corner to place the resized frame on the black background
x_offset = (w_w - w) // 2
y_offset = (w_h - h) // 2
return window_background # Place the resized frame on the black background
background[y_offset:y_offset + h, x_offset:x_offset + w] = resized
return 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)
print(folder) if(foldercount < region):
region = foldercount - region
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)

Loading…
Cancel
Save