I was wondering if anyone could help me out a bit with this problem I cannot solve. I am using Pafy to search Youtube from a text file that has a song name written in it and that gets a new song every few minutes. I am using Watchdog to watch for file modification and when i first run the script, watchdog catches the file modification and runs the pafy and opencv script, but it won't do the same when the following modification occurs.
#watchdog file change monitoring
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print ("Received modified event - %s." % event.src_path)
cv2.destroyAllWindows()
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='//PLAYING', recursive=False)
observer.start()
try:
while True:
#read PLAYING.txt
PLAYING = open('//PLAYING.txt').readline()
PLAYING = PLAYING[7:]
print (PLAYING)
#search youtube based on NowOnAir.txt
query_string = urllib.parse.urlencode({"search_query" : PLAYING})
html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
link = ('http://www.youtube.com/watch?v=' + search_results[0])
videoPafy = pafy.new(link)
best = videoPafy.getbestvideo()
videompv = best.url
#opencv youtube video output
video = cv2.VideoCapture(videompv)
while(video.isOpened()):
ret, frame = video.read()
resize = cv2.resize(frame, (1680, 1050))
gray = cv2.cvtColor(resize, cv2.COLOR_BGR2GRAY)
result = cv2.addWeighted(image, 0.2, resize, 0.8, 0)
cv2.namedWindow('frame', 0)
cv2.resizeWindow('frame', 1680, 1050)
cv2.imshow('frame', result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
So, I what I want to happen is, when file gets modified, I want openCV to close the window and open a new one with the new youtube query result.
Any suggestions would be quite welcome, thank You in advance.