how play mp4 video in google colab - python

I have a video.mp4 in content/video.mp4
if I wanted to play the video in google colab without downloading , ¿what code I should use to open a kind of video player in my jupyter notebook?

Here's the code
from IPython.display import HTML
from base64 import b64encode
mp4 = open('video.mp4','rb').read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
HTML("""
<video width=400 controls>
<source src="%s" type="video/mp4">
</video>
""" % data_url)
You can test it in a colab notebook here.
Update (Jun 2020)
To support a big vdo file, I write a library to upload to Google Drive and set it public. Then use the returned URL to display the video.
!pip install -U kora
from kora.drive import upload_public
url = upload_public('video.mp4')
# then display it
from IPython.display import HTML
HTML(f"""<video src={url} width=500 controls/>""")

Currently, we need to compress the video file to play it in google colaboratory, if the format is not supported.
from IPython.display import HTML
from base64 import b64encode
import os
# Input video path
save_path = "/content/videos/result.mp4"
# Compressed video path
compressed_path = "/content/videos/result_compressed.mp4"
os.system(f"ffmpeg -i {save_path} -vcodec libx264 {compressed_path}")
# Show video
mp4 = open(compressed_path,'rb').read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
HTML("""
<video width=400 controls>
<source src="%s" type="video/mp4">
</video>
""" % data_url)
Reference: https://towardsdatascience.com/yolov3-pytorch-on-google-colab-c4a79eeecdea

Just input the mp4 video path to that function and you're good to go.
from IPython.display import HTML
from base64 import b64encode
def show_video(video_path, video_width = 600):
video_file = open(video_path, "r+b").read()
video_url = f"data:video/mp4;base64,{b64encode(video_file).decode()}"
return HTML(f"""<video width={video_width} controls><source src="{video_url}"></video>""")
show_video(video_path)

To support a big vdo file ,getting link url for big videos
!pip install httplib2==0.15.0
!pip install google-api-python-client==1.7.11
#don't forget to restart the environment
from IPython.display import HTML
import IPython
from google.colab import output
!pip install -U kora
from kora.drive import upload_public
video_path='/content/drive/MyDrive/BigFileName.mkv'
video_url = upload_public(video_path) #for google disk to https://
if (video_url.startswith('https://drive.google.com/')):
video_url+='&confirm=t' # to bypass the window Google Drive - Virus scan warning
print('video_url',video_url)

This is all you need to define
import html
from IPython.display import display, Javascript, Image
from google.colab.output import eval_js
def preProcessVideo():
js = Javascript('''
const video = document.createElement('video');
const labelElement = document.createElement('span');
const videoUrl = 'https://rr2---sn-npoldn7z.c.drive.google.com/videoplayback?expire=1641882417&ei=8ercYbCiIuCKmvUPz5WB6Ac&ip=1.55.250.186&cp=QVRJU0lfUVRPSFhPOmpHU0F4ZW1JUnNobkNZVzY0MHlMYm44NDdNek45Nm5sSVQyTWota2J4MlE&id=8955091d9a3609fd&itag=18&source=webdrive&requiressl=yes&mh=yD&mm=32&mn=sn-npoldn7z&ms=su&mv=u&mvi=2&pl=27&ttl=transient&susc=dr&driveid=1S9PGt2CHDfuJSB1nIWebi4KVNRI7jEbf&app=explorer&mime=video/mp4&vprv=1&prv=1&dur=22.825&lmt=1641801389629564&mt=1641867503&txp=0011224&sparams=expire,ei,ip,cp,id,itag,source,requiressl,ttl,susc,driveid,app,mime,vprv,prv,dur,lmt&sig=AOq0QJ8wRgIhAJ8QuQoDRVLULTONbECJ9GyCqACa9Ci7i-4yK6vqgFdxAiEAoC-AMccHI239SCSOukNJEkXmqzKBIPqmb41I25Sjljs=&lsparams=mh,mm,mn,ms,mv,mvi,pl&lsig=AG3C_xAwRgIhAI650mDvui7WOdCTc-zfXSR_jXGCX0_marfJav3vEZDvAiEAz5-kvizrRBxJxmIZpO9LxDxkPQpcMTheY5Sq7pBMPQc=&cpn=BsF1Vhd4TGv91-3f&c=WEB_EMBEDDED_PLAYER&cver=1.20220109.00.00'
async function playVideo() {
const div = document.createElement('div');
video.style.width = 320;
video.style.height = 320;
document.body.appendChild(div);
div.appendChild(labelElement);
div.appendChild(video);
var source = document.createElement('source');
source.setAttribute('src', videoUrl);
source.setAttribute('type', 'video/mp4');
video.appendChild(source);
video.play();
// Resize the output to fit the video element.
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
}
''')
display(js)
eval_js('playVideo()'.format())
Then call it preProcessVideo()

Related

Getting a thumbnail image from google books api into a python variable

I can do this OK both in js and php but not in python. I'm trying to pull a thumbnail image from google books api into a python variable.
The text objects are fine eg
newTitle = (parsed_json['items'][0]['volumeInfo']['title'])
isbn10 = (parsed_json['items'][0]['volumeInfo']['industryIdentifiers'][1]['identifier'])
isbn13 = (parsed_json['items'][0]['volumeInfo']['industryIdentifiers'][0]['identifier'])
The image is supplied in the api as follows. (if you put the http// url into a browser you see the image):
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?id=XUnNDwAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?id=XUnNDwAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
I have tried the simple:
myImage = (parsed_json['items'][0]['volumeInfo']['imageLinks'][thumbnail])
which doesn't work.
I have installed pillow to provide image management:
from PIL import Image
img = Image.open("parsed_json['items'][0]['volumeInfo']['imageLinks'][thumbnail]") or
img = Image.open(parsed_json['items'][0]['volumeInfo']['imageLinks'][thumbnail])
which doesn't work. I have tried more complex arrangements:
from PIL import Image
import requests
from io import BytesIO
response = requests.get(parsed_json['items'][0]['volumeInfo']['imageLinks'][thumbnail])
img = Image.open(BytesIO(response.content))
but nothing seems to work. I have tried many other variations on these attempts. I have also, unsuccessfully tried to load the text that points to the thumbnail to try another route. I am confident that the "['items'][0]['volumeInfo']['imageLinks'][thumbnail]" is correct though my only way of testing whether the variable is properly loaded is to save it or if the line of code isn't working.
I didn't have problems downloading and opening the image.
I have use the following code
import requests
from PIL import Image
image_url = "https://books.google.com/books/content?id=XUnNDwAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api"
r = requests.get(image_url)
with open("demo_image",'wb') as f:
f.write(r.content)
img = Image.open("demo_image")

Is there a way to scan QR codes in colab?

I have a colab notebook and have been fruitlessly trying to find a way to scan a QR code that would be held up to my webcam. I have code to capture an image, if live QR code detection is a problem (I yoinked it from another notebook which is why its kind of weird):
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
def take_photo(filename='photo.jpg', quality=0.8):
js = Javascript('''
async function takePhoto(quality) {
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.appendChild(capture);
const video = document.createElement('video');
video.style.display = 'block';
const stream = await navigator.mediaDevices.getUserMedia({video: true});
document.body.appendChild(div);
div.appendChild(video);
video.srcObject = stream;
await video.play();
// Resize the output to fit the video element.
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
// Wait for Capture to be clicked.
await new Promise((resolve) => capture.onclick = resolve);
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
stream.getVideoTracks()[0].stop();
div.remove();
return canvas.toDataURL('image/jpeg', quality);
}
''')
display(js)
data = eval_js('takePhoto({})'.format(quality))
binary = b64decode(data.split(',')[1])
with open(filename, 'wb') as f:
f.write(binary)
return filename
from IPython.display import Image
try:
filename = take_photo()
print('Saved to {}'.format(filename))
# Show the image which was just taken.
display(Image(filename))
except Exception as err:
# Errors will be thrown if the user does not have a webcam or if they do not
# grant the page permission to access it.
print(str(err))
I have tried things with pyzbar (followed tutorials) and many other ways, but none of them seem to work for me.
The ultimate goal is to take the data from the qr codes, and append it into lists that I would convert into a .csv (all data comes in the qr code like "name,email,phone#"). It would be great to have it work with live camera, so it just automatically does this every time it sees a QR code. Is there a way to do this?
Thanks for all your help!
hi you can use this pyzbar
from pyzbar.pyzbar import decode
from PIL import Image
decode(Image.open('pyzbar/tests/code128.png'))
Write to install:
pip install pyzbar[scripts] or !pip install pyzbar[scripts]
!apt install libzbar0
Later import some these, but the principal is pyzbar:
import shutil
import os
import random
import re
import cv2
import numpy as np
import pytesseract
from pytesseract import Output
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
import glob
import pyzbar.pyzbar
from PIL import Image
And check this:
from pyzbar.pyzbar import decode
from PIL import Image

Unable to read image data when converting from PDF to Image

I am trying to convert the PDF to Image to proceed further with the Tesseract. It works when I convert using cmd:
magick convert a.pdf b.png
But doesn't work when I try to do the same using Python:
from wand.image import Image
with Image (filename='a.pdf') as img:
img.save(filename = 'sample.png')`
The error I get is:
unable to read image data D:/Users/UserName/AppData/Local/Temp/magick-4908Cq41DDA5FxlX1 # error/pnm.c/ReadPNMImage/1346
I have also installed ghostscipt but the error is still there.
EDIT:
I took the code provided in the reply below and modified it to read all the pages. The original issue is still there and the code below uses pdf2image:
from pdf2image import convert_from_path
import os
pdf_dir = "D:/Users/UserName/Desktop/scraping"
for pdf_file in os.listdir(pdf_dir):
if pdf_file.endswith(".pdf"):
pages = convert_from_path(pdf_file, 300)
pdf_name = pdf_file[:-4]
for page in pages:
page.save("%s-page%d.jpg" % (pdf_name, pages.index(page)), "JPEG")
Instead of using wand.image, you can use pdf2image. Install it like this:
pip install pdf2image
Here is a code that loops through every page in the PDF, finally converting them to JPEG:
import os
import tempfile
from pdf2image import convert_from_path
filename = 'target.pdf'
with tempfile.TemporaryDirectory() as path:
images_from_path = convert_from_path(filename, output_folder=path, last_page=1, first_page =0)
base_filename = os.path.splitext(os.path.basename(filename))[0] + '.jpg'
save_dir = 'dir'
for page in images_from_path:
page.save(os.path.join(save_dir, base_filename), 'JPEG')

Python: the substitution of "%time"

As we know, we can use "%time ..." in the notebook of Jupyter. However, we could not use this line in Spyder. I have several lines used for reading video, doing image process and then writing a new video.
from moviepy.editor import VideoFileClip
from IPython.display import HTML
output = 'test_images/white.mp4' # output video
clip1 = VideoFileClip("test_images/solid.mp4") # video is readed by many clips
clip = clip1.fl_image(process_image) # process_image is a function for processing the image clips from a video
%time clip.write_videofile(output, audio=False)
HTML("""
<video width="960" height="540" controls>
<source src="{0}">
</video>
""".format(output))
Hence, how can I do if I use spyder (anaconda 3.0)? Any substitution for '%time'?

Using pytube library to download youtube links from csv

I'm trying to use the pytube library to download a bunch of links I have on a .csv file.
EDIT:
WORKING CODE:
import sys
reload(sys)
sys.setdefaultencoding('Cp1252')
import os.path
from pytube import YouTube
from pprint import pprint
import csv
with open('onedialectic.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
try:
yt = YouTube(row[1])
path = os.path.join('/videos/',row[0])
path2 = os.path.join(path + '.mp4')
print(path2)
if not os.path.exists(path2) :
print(row[0] + '\n')
pprint(yt.get_videos())
yt.set_filename(row[0])
video = yt.get('mp4', '360p')
video.download('/videos')
except Exception as e:
print("Passing on exception %s", e)
continue
To install it you need to use
pip install pytube
and then in your code run
from pytube import YouTube
I haven't seen any code examples of using this with csv though, are you sure it's supported?
You can download via command line directly using e.g.
$ pytube -e mp4 -r 720p -f Dancing Scene from Pulp Fiction http://www.youtube.com/watch?v=Ik-RsDGPI5Y
-e, -f and -r are optional, (extension, filename and resolution)
However for you I would suggest maybe the best thing is to put them all in a playlist and then use Jordan Mear's excellent Python Youtube Playlist Downloader
On a footnote, usually all [external] libraries need to be imported. You can read more about importing here, in the python online tutorials
You could maybe do something like this:
import csv
from pytube import YouTube
vidcsvreader = csv.reader(open("videos.csv"), delimiter=",")
header1 = vidcsvreader.next() #header
for id, url in vidcsvreader:
yt = url #assign url to var
#set resolution and filetype
video = yt.get('mp4', '720p')
# set a destination directory for download
video.download('/tmp/')
break

Categories

Resources