Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. import youtube_dl
  2. import subprocess
  3. from PIL import Image
  4. import pytesseract
  5. import cv2
  6. import numpy as np
  7.  
  8.  
  9. VIDEO_URL = 'https://www.youtube.com/watch?v=hHW1oY26kxQ'
  10.  
  11.  
  12. def get_media_url(ytb_url):
  13.     ydl = youtube_dl.YoutubeDL()
  14.     video = ydl.extract_info(ytb_url, download=False)
  15.     return video['formats'][-1]['url']
  16.  
  17.  
  18. def get_white_mask(img, lower=np.array([150, 150, 150]), upper=np.array([255, 255, 255])):
  19.     return cv2.inRange(img, lower, upper)
  20.  
  21.  
  22. def save_text_screen(media_url):
  23.     subprocess.call(f'ffmpeg -i "{media_url}" -vframes 1 -y tmp_screen.png', shell=True)
  24.  
  25.     image = Image.open('tmp_screen.png')
  26.     w, h = image.size
  27.     image.crop((0, 0, w, h-1000)).save('tmp_text_screen.png')
  28.  
  29.  
  30. media_url = get_media_url(VIDEO_URL)
  31. save_text_screen(media_url)
  32.  
  33. cv2.imwrite('tmp_mask_text_screen.png', get_white_mask(cv2.imread('tmp_text_screen.png')))
  34.  
  35. result = pytesseract.image_to_string(Image.open('tmp_mask_text_screen.png'), lang='eng')
  36. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement