# importing libraries import cv2 import numpy as np import os import random import vlc import time import board import busio import adafruit_mpr121 MEDIA_FOLDER = "media" WINDOW_NAME = "Image Viewer" WINDOW_WIDTH = 475 WINDOW_HEIGHT = 370 i2c = busio.I2C(board.SCL, board.SDA) mpr121 = adafruit_mpr121.MPR121(i2c) mediaFolder = os.listdir(MEDIA_FOLDER) current_file = False def clear_frame(): # Create a black frame black_frame = np.zeros((WINDOW_HEIGHT, WINDOW_WIDTH, 3), dtype=np.uint8) cv2.imshow(WINDOW_NAME, black_frame) cv2.namedWindow(WINDOW_NAME,cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) cv2.waitKey(1) # Add a small delay to ensure the frame is displayed 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: 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 # 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 def showImage(file): # Read the image current_image = cv2.imread(file) if current_image is None: print(f"Error loading image: {current_image}") return a = cv2.cvtColor(current_image, cv2.COLOR_BGR2GRAY) 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) cv2.waitKey(1) while True: for i in range(12): if mpr121[i].value: print("Pin {} touched!".format(i)) load_media(i) break def play_audio(file): clear_frame() p = vlc.MediaPlayer(file) p.play() time.sleep(.1) while p.is_playing(): # Add a small delay to prevent high CPU usage time.sleep(0.1) for i in range(12): if mpr121[i].value: print("Pin {} touched!".format(i)) load_media(i) load_media(0) def get_random_file(f): print("getting random file from ") print(f) folder = os.listdir(f) random.shuffle(folder) file = folder[0] if os.path.isdir(file): print("oh no u gave me a folder, load the sub instead") nF = os.listdir(f + "/" + file) return get_random_file(nF) return file # create a function with an optional parameter def load_media(region=None, override=None): global current_file print("runningn load_media") if region: print("i should now load a media from region ") print(region) else: print("region is not defined, just do something!") region=0 foldercount = len(os.listdir(MEDIA_FOLDER)) - 1 print(foldercount) if(foldercount < region): region = foldercount - region folder = os.listdir(MEDIA_FOLDER)[region] if(override): file = override else: file = get_random_file(MEDIA_FOLDER + "/" + folder) current_file = file file_path = MEDIA_FOLDER + "/" + folder + "/" + file print(file_path) # Check if the file is an image or a video if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"): # Read and resize the image print("ive loaded an image: " + file) showImage(file_path) elif file.endswith(".mov") or file.endswith(".mp4") or file.endswith(".avi"): # Create a VideoCapture object and read from input file cap = cv2.VideoCapture(file_path) print("ive loaded a video" + file) # show the input video file while cap.isOpened(): ret, frame = cap.read() # if q is pressed, the app is closed if ret == True: 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: print("Pin {} touched!".format(i)) load_media(i) break if cv2.waitKey(3) & 0xFF == ord("q"): print("destroy all windows") cv2.destroyAllWindows() break else: print("is the video finished?") load_media(0) break elif file.endswith(".wav") or file.endswith(".mp3"): print("ive loaded an audio file: " + file) play_audio(file_path) else: print("Unsupported file format: " + file) load_media(region) print("start script ") load_media(2)