You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.4 KiB
Python

3 months ago
# importing libraries
import cv2
import numpy as np
import os
import random
import simpleaudio as sa
import time
import board
import busio
import adafruit_mpr121
3 months ago
MEDIA_FOLDER = "media"
WINDOW_NAME = "Image Viewer"
WINDOW_WIDTH = 300
WINDOW_HEIGHT = 500
3 months ago
i2c = busio.I2C(board.SCL, board.SDA)
mpr121 = adafruit_mpr121.MPR121(i2c)
3 months ago
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)
3 months ago
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)
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)
3 months ago
window_background[
y_offset : y_offset + new_height, x_offset : x_offset + new_width
] = resized_frame
return window_background
# Show an image on the canvas
def showImage(file):
# Read the image
current_image = cv2.imread(file)
3 months ago
if current_image is None:
print(f"Error loading image: {current_image}")
3 months ago
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.waitKey(1)
while True:
for i in range(12):
if mpr121[i].value:
print("Pin {} touched!".format(i))
load_media(i)
break
3 months ago
def play_audio(file):
clear_frame()
sound = sa.WaveObject.from_wave_file(file)
3 months ago
play_obj = sound.play()
# TODO: check for sensor data change
while play_obj.is_playing():
# Add a small delay to prevent high CPU usage
time.sleep(0.1)
load_media()
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)
3 months ago
return file
# create a function with an optional parameter
def load_media(region=None, override=None):
global current_file
print("runningn load_media")
3 months ago
if region:
print("i should now load a media from region ")
print(region)
else:
print("region is not defined, just do something!")
folder = os.listdir(MEDIA_FOLDER)[region]
print(folder)
3 months ago
if(override):
file = override
else:
file = get_random_file(MEDIA_FOLDER + "/" + folder)
3 months ago
current_file = file
file_path = MEDIA_FOLDER + "/" + folder + "/" + file
print(file_path)
3 months ago
# 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)
3 months ago
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)
3 months ago
print("ive loaded a video" + file)
# 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)
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"):
3 months ago
print("destroy all windows")
cv2.destroyAllWindows()
break
else:
print("ois the video finished?")
load_media()
break
elif file.endswith(".wav") or file.endswith(".mp3"):
print("ive loaded an audio file: " + file)
play_audio(file_path)
3 months ago
else:
print("Unsupported file format: " + file)
load_media(region)
print("start script ")
load_media(2)