first commit
commit
367f15e37c
@ -0,0 +1,6 @@
|
||||
bin
|
||||
lib
|
||||
lib64
|
||||
media
|
||||
pyvenv.cfg
|
||||
|
@ -0,0 +1,161 @@
|
||||
# importing libraries
|
||||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
import random
|
||||
import simpleaudio as sa
|
||||
import threading
|
||||
import time
|
||||
|
||||
MEDIA_FOLDER = "media"
|
||||
WINDOW_NAME = "Image Viewer"
|
||||
|
||||
WINDOW_WIDTH = 800
|
||||
WINDOW_HEIGHT = 480
|
||||
|
||||
|
||||
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.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
|
||||
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(MEDIA_FOLDER + "/" + file)
|
||||
|
||||
if current_image is None:
|
||||
print(f"Error loading image: {image_path}")
|
||||
return
|
||||
a = cv2.cvtColor(current_image, cv2.COLOR_BGR2GRAY)
|
||||
resized_frame = resize_and_center_frame(current_image)
|
||||
cv2.imshow(WINDOW_NAME, resized_frame)
|
||||
|
||||
k = cv2.waitKey(0)
|
||||
if k < 58 and k > 48:
|
||||
print("number is pressed")
|
||||
print(k - 48)
|
||||
load_media(k - 48)
|
||||
elif k == ord("q"):
|
||||
print("destroy all windows")
|
||||
cv2.destroyAllWindows()
|
||||
else:
|
||||
print("key pressed: " + str(k))
|
||||
|
||||
|
||||
def play_audio(file):
|
||||
clear_frame()
|
||||
sound = sa.WaveObject.from_wave_file("media/" + file)
|
||||
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(previous=None):
|
||||
random.shuffle(mediaFolder)
|
||||
file = mediaFolder[0]
|
||||
if file == previous:
|
||||
return get_random_file(previous)
|
||||
return file
|
||||
|
||||
|
||||
# create a function with an optional parameter
|
||||
def load_media(region=None, override=None):
|
||||
global current_file
|
||||
|
||||
if region:
|
||||
print("i should now load a media from region ")
|
||||
print(region)
|
||||
else:
|
||||
print("region is not defined, just do something!")
|
||||
|
||||
if(override):
|
||||
file = override
|
||||
else:
|
||||
file = get_random_file(current_file)
|
||||
|
||||
current_file = file
|
||||
|
||||
# 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)
|
||||
elif file.endswith(".mov") or file.endswith(".mp4") or file.endswith(".avi"):
|
||||
# Create a VideoCapture object and read from input file
|
||||
cap = cv2.VideoCapture("media/" + file)
|
||||
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)
|
||||
if cv2.waitKey(3) & 0xFF == ord("1"):
|
||||
print("key 2 pressed")
|
||||
load_media()
|
||||
break
|
||||
elif cv2.waitKey(3) & 0xFF == ord("q"):
|
||||
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)
|
||||
|
||||
else:
|
||||
print("Unsupported file format: " + file)
|
||||
load_media(region)
|
||||
|
||||
|
||||
print("start script ")
|
||||
load_media()
|
Loading…
Reference in New Issue