Commit 4f32b77c authored by Ahmed Hendawy's avatar Ahmed Hendawy
Browse files

Upload the source code

parent 1abdbe79
import pafy
import numpy as np
import cv2
import time
from yonoarc_utils.header import set_timestamp
from std_msgs.msg import Header
from yonoarc_utils.image import to_ndarray,from_ndarray
class YouTubePlayer:
def __init__(self):
self.image = None
self.cap = None
self.play = None
self.frame_id = 0
def on_start(self):
''' [Optional] Called at the beginning when the block starts. '''
self.url = self.get_property('URL')
self.framerate = self.get_property('FrameRate')
vPafy = pafy.new(self.url)
self.play = vPafy.getbest(preftype="mp4")
def run (self):
''' [Optional] Called after on_start in a separate thread.
Notes
-----
This method can be useful for background tasks.
'''
#start the video
self.play.download("/video.mp4")
self.cap = cv2.VideoCapture("/video.mp4")
while(self.cap.isOpened()== False):
self.cap = cv2.VideoCapture("/video.mp4")
# Check if camera opened successfully
'''
To send an alert to the user of the block
(causes the block’s alerts icon to flicker and the alert message to be provided in the block’s Alerts tab),
use the following method given that alert_message can be any arbitrary string and
level is a string whose value is either INFO, WARN, or ERROR.
'''
self.alert("Error opening video stream or file",ERROR)
while(True):
start = time.time()
if self.cap.isOpened() == True:
ret, frame = self.cap.read()
if ret == True:
header = Header()
set_timestamp(header,time.time())
image = from_ndarray(frame, header)
self.publish('output_image',image)
else:
self.cap = cv2.VideoCapture("/video.mp4")
time_to_sleep = 1/self.framerate - (time.time() - start)
if time_to_sleep > 0:
time.sleep(time_to_sleep)
def on_properties_changed(self, affected_properties):
''' [Optional] Called when one or more block properties are changed in live mode.
Parameters
----------
affected_properties : dict
A dictionary of the keys of the affected properties and their new values.
'''
self.url = self.get_property('URL')
self.framerate = self.get_property('FrameRate')
vPafy = pafy.new(self.url)
self.play = vPafy.getbest(preftype="mp4")
#start the video
self.play.download("/video.mp4")
self.cap = cv2.VideoCapture("/video.mp4")
while(self.cap.isOpened()== False):
self.cap = cv2.VideoCapture("/video.mp4")
# Check if camera opened successfully
'''
To send an alert to the user of the block
(causes the block’s alerts icon to flicker and the alert message to be provided in the block’s Alerts tab),
use the following method given that alert_message can be any arbitrary string and
level is a string whose value is either INFO, WARN, or ERROR.
'''
self.alert("Error opening video stream or file",ERROR)
def on_stop(self):
# When everything done, release the video capture object
self.cap.release()
# Closes all the frames
cv2.destroyAllWindows()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment