Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import youtube_dl
- import subprocess
- from PIL import Image
- import pytesseract
- import cv2
- import numpy as np
- VIDEO_URL = 'https://www.youtube.com/watch?v=hHW1oY26kxQ'
- def get_media_url(ytb_url):
- ydl = youtube_dl.YoutubeDL()
- video = ydl.extract_info(ytb_url, download=False)
- return video['formats'][-1]['url']
- def get_white_mask(img, lower=np.array([150, 150, 150]), upper=np.array([255, 255, 255])):
- return cv2.inRange(img, lower, upper)
- def save_text_screen(media_url):
- subprocess.call(f'ffmpeg -i "{media_url}" -vframes 1 -y tmp_screen.png', shell=True)
- image = Image.open('tmp_screen.png')
- w, h = image.size
- image.crop((0, 0, w, h-1000)).save('tmp_text_screen.png')
- media_url = get_media_url(VIDEO_URL)
- save_text_screen(media_url)
- cv2.imwrite('tmp_mask_text_screen.png', get_white_mask(cv2.imread('tmp_text_screen.png')))
- result = pytesseract.image_to_string(Image.open('tmp_mask_text_screen.png'), lang='eng')
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement