Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Purpose : launch a video by an Arduino connected by serial and activated by ultrasound sensor
- Author : Patrick Dubois
- Date : 2022-03-22
- Copyright : Public Domain
- Usage : COMx speed parity bits stopbit video_path_filename
- ex. : COM12 9600 N 8 1 "D:\Data\Videos\GOPR0366.MP4"
- : serial baud must the same for Arduino serial port
- : to complete
- ToDo : fullscreen - close VLC - pass video filename in Arduino Serial
- """
- import sys
- import time
- import cv2 as cv # opencv-python https://opencv.org/
- import keyboard # keyboard https://pypi.org/project/keyboard/
- import numpy as np
- import serial as portcomm # pyserial https://pyserial.readthedocs.io/en/latest/pyserial.html
- import serial.tools.list_ports
- import vlc # python-vlc https://wiki.videolan.org/Python_bindings/ https://www.olivieraubert.net/vlc/python-ctypes/
- from PIL import Image # https://pypi.org/project/Pillow/
- from ffpyplayer.player import MediaPlayer # https://pypi.org/project/ffpyplayer/
- def ffplayer_play(my_movie): # have a look at cvplayer https://github.com/addyett/cvplayer/
- player = MediaPlayer(my_movie)
- window_name = "Movie"
- cv.namedWindow(window_name)
- val = ''
- while val != 'eof':
- frame, val = player.get_frame()
- if val != 'eof' and frame is not None:
- image, pts = frame
- x, y = image.get_size()
- data = image.to_bytearray()[0]
- image = Image.frombytes("RGB", (x, y), bytes(data))
- image = cv.cvtColor(np.array(image), cv.COLOR_RGB2BGR)
- cv.imshow('Movie', image)
- cv.setWindowProperty(window_name, cv.WND_PROP_TOPMOST, cv.WINDOW_NORMAL)
- del image
- # Press ESC on keyboard to stop video. windows must have the focusq
- if cv.waitKey(20) == 27:
- break
- cv.destroyWindow(window_name)
- def opencv_play(my_movie):
- window_name = "Movie"
- video = cv.VideoCapture(my_movie)
- cv.namedWindow(window_name)
- fps = video.get(cv.CAP_PROP_FPS) # frames per second, doesn't work for webcam
- if fps > 144 or fps is None or fps == 0:
- fps = 25
- ms_waitkey = int(1000 / fps)
- nb_frame_grabbed = 0
- while video.isOpened():
- ret, frame = video.read()
- if ret:
- nb_frame_grabbed = 0
- cv.imshow(window_name, frame)
- cv.setWindowProperty(window_name, cv.WND_PROP_TOPMOST, cv.WINDOW_NORMAL)
- # cv.setWindowProperty(window_name, cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN)
- # Press ESC on keyboard to stop video. windows must have the focus
- if cv.waitKey(ms_waitkey) == 27:
- break
- else: # elif frame is None:
- # break
- nb_frame_grabbed += 1
- if nb_frame_grabbed > 2:
- break
- video.release()
- cv.destroyWindow(window_name)
- # cv.destroyAllWindows()
- def vlc_play(my_movie):
- vlc_instance = vlc.Instance()
- media = vlc_instance.media_new(my_movie)
- player = vlc_instance.media_player_new()
- player.set_media(media)
- # player.set_fullscreen(b_fullscreen=True)
- player.play()
- while player.is_playing():
- time.sleep(1)
- # player.close() # no way. must be in a windows to get the handle and manage it
- if __name__ == '__main__':
- args = sys.argv[1:]
- ports = serial.tools.list_ports.comports()
- commports_liste = [comport.device for comport in ports]
- if len(args) != 6:
- # print(len(args), " ", args)
- print("Arguments de communication incorrects")
- print("ex: COM3 9600 N 8 1 video_path_filename")
- exit(1)
- else:
- serial_port = args[0]
- serial_baud = int(args[1])
- serial_parity = args[2]
- serial_bits = int(args[3])
- serial_stop = int(args[4])
- movie = args[5]
- try:
- com_portx = portcomm.Serial(port=serial_port, baudrate=serial_baud, parity=serial_parity,
- stopbits=serial_stop, bytesize=serial_bits)
- bFlag = True
- while bFlag:
- if com_portx.in_waiting:
- sline_byte = com_portx.readline()
- sline_utf8 = sline_byte.decode('UTF-8')
- if sline_utf8[:12] == 'LAUNCH_VIDEO':
- # print("video launched")
- ffplayer_play(movie)
- if keyboard.is_pressed("f"): # F is also working play movie by ffplayer / opencv
- ffplayer_play(movie)
- if keyboard.is_pressed("o"): # O is also working play movie by opencv
- opencv_play(movie)
- elif keyboard.is_pressed("q"): # Q is also working
- print("q or Q")
- bFlag = False
- elif keyboard.is_pressed("v"): # V is also working play movie by VLC
- vlc_play(movie)
- time.sleep(1)
- com_portx.close()
- except portcomm.PortNotOpenError:
- print("Cannot open serial port")
- except portcomm.SerialTimeoutException:
- print("Timeout on serial port")
- except portcomm.SerialException:
- print("Other problem on serial port, as non-existing port or bad settings")
- print("Available ports are " + " ".join(commports_liste))
- finally:
- print("\r\nExit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement