From ff7710ecb50878c8da210a4e7541a205c7f6a1eb Mon Sep 17 00:00:00 2001 From: vitrinekast Date: Wed, 26 Jun 2024 16:50:30 +0100 Subject: [PATCH] include correct regions --- input.mp4 | 0 script.py | 94 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 48 insertions(+), 46 deletions(-) delete mode 100644 input.mp4 diff --git a/input.mp4 b/input.mp4 deleted file mode 100644 index e69de29..0000000 diff --git a/script.py b/script.py index ceb7dbc..392e899 100644 --- a/script.py +++ b/script.py @@ -12,8 +12,8 @@ import adafruit_mpr121 MEDIA_FOLDER = "media" WINDOW_NAME = "Image Viewer" -WINDOW_WIDTH = 300 -WINDOW_HEIGHT = 500 +WINDOW_WIDTH = 475 +WINDOW_HEIGHT = 370 i2c = busio.I2C(board.SCL, board.SDA) 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 -def resize_and_center_frame(frame): - # Get original frame dimensions - height, width = frame.shape[:2] - - # Calculate aspect ratio - aspect_ratio = width / height - - # Determine resizing dimensions while maintaining aspect ratio - if width > height: - new_width = WINDOW_WIDTH - new_height = int(new_width / aspect_ratio) +def get_frame_with_fill(frame, w_w, w_h): + # Resize the frame to fit within 400x400 while maintaining aspect ratio + h, w = frame.shape[:2] + + if w > h: + scale = w_w / w else: - new_height = WINDOW_HEIGHT - 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 - x_offset = max(0, (WINDOW_WIDTH - new_width) // 2) - y_offset = max(0, (WINDOW_HEIGHT - new_height) // 2) - - # Create a black background of the window size - window_background = np.zeros((WINDOW_HEIGHT, WINDOW_WIDTH, 3), dtype=np.uint8) - - # Overlay the resized frame onto the background at the calculated offset - print(y_offset) - print(y_offset) - print(new_height) - print(x_offset) - print(x_offset) - print(new_width) + scale = w_h / h + + # Ensure the image does not exceed the max dimensions + if scale * h > w_h: + scale = w_h / h + elif scale * w > w_w: + scale = w_w / w + + print(scale) + new_w = int(w * scale) + new_h = int(h * scale) + resized = cv2.resize(frame, (new_w, new_h), interpolation=cv2.INTER_AREA) + + background = np.zeros((w_h, w_w, 3), dtype=np.uint8) + + # Get the dimensions of the resized frame + h, w = resized.shape[:2] + # 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 - window_background[ - y_offset : y_offset + new_height, x_offset : x_offset + new_width - ] = resized_frame - - 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 @@ -81,8 +74,11 @@ def showImage(file): return a = cv2.cvtColor(current_image, cv2.COLOR_BGR2GRAY) - # resized_frame = resize_and_center_frame(current_image) - cv2.imshow(WINDOW_NAME, current_image) + 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(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.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) @@ -136,10 +132,15 @@ def load_media(region=None, override=None): print(region) else: 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): file = override @@ -162,12 +163,13 @@ def load_media(region=None, override=None): # show the input video file while cap.isOpened(): 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 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): if mpr121[i].value: @@ -180,7 +182,7 @@ def load_media(region=None, override=None): break else: print("ois the video finished?") - load_media() + load_media(0) break elif file.endswith(".wav") or file.endswith(".mp3"): print("ive loaded an audio file: " + file)