In [1]:
# Extract from youtube the video of gathering using pytube

In [2]:
import pytube

In [3]:
url_video = 'https://www.youtube.com/watch?v=he8_V0BvbWg'

In [4]:
yt = pytube.YouTube(url_video)
yt.set_filename('Gathering-gameplay')


Out[4]:
True

In [5]:
print(yt.get_videos())
print(yt.filename)


[<Video: MPEG-4 Visual (.3gp) - 144p - Simple>, <Video: MPEG-4 Visual (.3gp) - 240p - Simple>, <Video: H.264 (.mp4) - 360p - Baseline>, <Video: VP8 (.webm) - 360p - N/A>]
Gathering-gameplay

In [6]:
# To select a video by a specific resolution and filetype you can use the get
# method.
video = yt.get('mp4')
video.download('./')


---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-6-3429e447f59e> in <module>()
      2 # method.
      3 video = yt.get('mp4')
----> 4 video.download('./')

/home/roby/.local/lib/python3.5/site-packages/pytube/models.py in download(self, path, chunk_size, on_progress, on_finish, force_overwrite)
     75         # handled by the library.
     76         if os.path.isfile(path) and not force_overwrite:
---> 77             raise OSError("Conflicting filename:'{0}'".format(self.filename))
     78         # TODO: Split up the downloading and OS jazz into separate functions.
     79         response = urlopen(self.url)

OSError: Conflicting filename:'Gathering-gameplay'

In [7]:
video


Out[7]:
<Video: H.264 (.mp4) - 360p - Baseline>

In [8]:
import pylab
import imageio
filename = yt.filename+'.mp4'
vid = imageio.get_reader(filename,  'ffmpeg')
nums = range(0,5)
for num in nums:
    image = vid.get_data(num)
    fig = pylab.figure()
    fig.suptitle('image #{}'.format(num), fontsize=20)
    pylab.imshow(image)
pylab.show()



In [9]:
imageio.plugins.ffmpeg.download()


Imageio: 'ffmpeg.linux64' was not found on your computer; downloading it now.
Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/ffmpeg/ffmpeg.linux64 (27.2 MB)
Downloading: 8192/28549024 bytes (0.0229376/28549024 bytes (0.8802816/28549024 bytes (2.81458176/28549024 bytes (5.1%2097152/28549024 bytes (7.3%2736128/28549024 bytes (9.6%3375104/28549024 bytes (11.84022272/28549024 bytes (14.14669440/28549024 bytes (16.45308416/28549024 bytes (18.65947392/28549024 bytes (20.86586368/28549024 bytes (23.17225344/28549024 bytes (25.37864320/28549024 bytes (27.58519680/28549024 bytes (29.89158656/28549024 bytes (32.19814016/28549024 bytes (34.410452992/28549024 bytes (36.6%10846208/28549024 bytes (38.0%12042240/28549024 bytes (42.2%12681216/28549024 bytes (44.4%13320192/28549024 bytes (46.7%13975552/28549024 bytes (49.0%14614528/28549024 bytes (51.2%15261696/28549024 bytes (53.5%15892480/28549024 bytes (55.7%16539648/28549024 bytes (57.9%17170432/28549024 bytes (60.1%17563648/28549024 bytes (61.5%18415616/28549024 bytes (64.5%19234816/28549024 bytes (67.4%20029440/28549024 bytes (70.2%20660224/28549024 bytes (72.4%21307392/28549024 bytes (74.6%21946368/28549024 bytes (76.9%22593536/28549024 bytes (79.1%23232512/28549024 bytes (81.4%23887872/28549024 bytes (83.7%24526848/28549024 bytes (85.9%25165824/28549024 bytes (88.1%25804800/28549024 bytes (90.4%26443776/28549024 bytes (92.6%27082752/28549024 bytes (94.9%27721728/28549024 bytes (97.1%28360704/28549024 bytes (99.3%28549024/28549024 bytes (100.0%)
  Done
File saved as /home/roby/.imageio/ffmpeg/ffmpeg.linux64.

In [9]:
image = vid.get_data(0)

In [10]:
type(image)


Out[10]:
imageio.core.util.Image

In [11]:
import pylab
pylab.imshow(image)
pylab.show()



In [20]:
image.shape


Out[20]:
(208, 560, 3)

In [13]:
image[100,300,:]


Out[13]:
Image([ 61, 252,   0], dtype=uint8)

In [30]:
print(image.shape[0]/16,image.shape[1]/16)


13.0 35.0

In [131]:
import numpy as np
import pylab
a=np.zeros((5,5,3),dtype=np.uint8)
a[:,:,0]=0
a[:,:,1]=0
a[2,3,2]=255
a[3,3,2]=255
a[3,3,1]=255
a[4,4,2]=255
a[4,4,1]=255

In [132]:
a.shape


Out[132]:
(5, 5, 3)

In [133]:
pylab.imshow(a)
pylab.show()



In [134]:
ch_pl = 0 # channel of player
other_chs = [0,1,2]
other_chs = [x for x in other_chs if x != ch_pl]

In [135]:
tmp = np.argwhere(a[:,:,ch_pl] == 255)

In [136]:
tmp


Out[136]:
array([], shape=(0, 2), dtype=int64)

In [137]:
for [x,y] in tmp:
    if a[x,y,other_chs[0]]==0 and a[x,y,other_chs[1]]==0:
        print(x,y)

In [146]:
np.mgrid[0:0,0:-1].squeeze()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-146-7319cead11ea> in <module>()
----> 1 np.mgrid[0:0,0:-1].squeeze()

/home/roby/.local/lib/python3.5/site-packages/numpy/lib/index_tricks.py in __getitem__(self, key)
    174                         for _x, _t in zip(size, (typ,)*len(size))]
    175             else:
--> 176                 nn = _nx.indices(size, typ)
    177             for k in range(len(size)):
    178                 step = key[k].step

/home/roby/.local/lib/python3.5/site-packages/numpy/core/numeric.py in indices(dimensions, dtype)
   2092     if N == 0:
   2093         return array([], dtype=dtype)
-> 2094     res = empty((N,)+dimensions, dtype=dtype)
   2095     for i, dim in enumerate(dimensions):
   2096         tmp = arange(dim, dtype=dtype)

ValueError: negative dimensions are not allowed

In [45]:
dir_dict = {'up':1,'down':3,'left':2,'right':0} # exp of 4th roots of 1
a = dir_dict['right']
def rot_right(a):
    a=(a-1)%4
    return a
def rot_left(a):
    a=(a+1)%4
    return a

In [46]:
for k,v in dir_dict.items():
    if v == rot_right(a):
        print('rot right',k)
    if v == rot_left(a):
        print('rot left',k)


rot right down
rot left up

In [76]:
import numpy as np
import pylab
x=2
y=3
H=10
W=10
s=np.ones((3,W,H),dtype=np.uint8)*255
gr=np.mgrid[x:x+1,y:H].squeeze()
print(gr)
print('s',s.shape,'gr',gr.shape)
s[0,gr[0],gr[1]]=np.ones(gr.shape[1])*255
s[1,gr[0],gr[1]]=np.ones(gr.shape[1])*255
s[2,gr[0],gr[1]]=np.zeros(gr.shape[1])
print(s)
pylab.imshow(s.transpose((1,2,0)))
pylab.show()

xp=2;yp=5


[[2 2 2 2 2 2 2]
 [3 4 5 6 7 8 9]]
s (3, 10, 10) gr (2, 7)
[[[255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]]

 [[255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]]

 [[255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255   0   0   0   0   0   0   0]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]
  [255 255 255 255 255 255 255 255 255 255]]]

In [42]:
a=np.arange(10).reshape(2,5)
pp=np.arange(4).reshape(2,2)
print(a)
a[[[0,1,0],[2,3,0]]]


[[0 1 2 3 4]
 [5 6 7 8 9]]
Out[42]:
array([2, 8, 0])

In [14]:
ll=[1,2,-1]
for x in ll:
    if x >= 0:
        x += 1

In [15]:
ll


Out[15]:
[1, 2, -1]

In [18]:
W=3;H=3
import numpy as np
mid_x = int((W-1)/2)+1
mid_y = int((H-1)/2)+1
mid = np.array([mid_x,mid_y]) # coordinate central point
dx = np.array([1,0])
dy = np.array([0,1])
a = np.array([mid+2*dy,
            mid+dy-dx,mid+dy,mid+dy+dx,
            mid-2*dx,mid-dx,mid,mid+dx,mid+2*dx,
            mid-dy-dx,mid-dy,mid-dy+dx,
            mid-2*dy])

In [21]:
for x,y in a:
    print(x,y)
print(a)


2 4
1 3
2 3
3 3
0 2
1 2
2 2
3 2
4 2
1 1
2 1
3 1
2 0
[[2 4]
 [1 3]
 [2 3]
 [3 3]
 [0 2]
 [1 2]
 [2 2]
 [3 2]
 [4 2]
 [1 1]
 [2 1]
 [3 1]
 [2 0]]

In [4]:
import numpy as np
a=np.arange(10)
b=np.argwhere(a==11)
print(b.size)
a[b]


0
Out[4]:
array([], shape=(0, 1), dtype=int64)

In [6]:
import numpy as np
a=np.arange(2)
x=a[0]
y=a[1]

In [25]:
x.shape==()
x=x.reshape((1,1))
print(x.shape)


(1, 1)

In [26]:
for i in x:
    print(i)


[0]

In [19]:
a=np.array([[2]])

In [23]:
a=a.reshape((1,))

In [24]:
a.shape


Out[24]:
(1,)

In [32]:
ll=[1,2,3,3]
ll_pos=[[-1,2],[-2,2],[0,1],[2,3]]

pos_tb_res = []
new_pos = []
new_t_apples = []
for i,pos in zip(ll,ll_pos)
    if i>2:
        pos_tb_res.append(pos)
    else:
        new_pos.append(pos)
        new_t_apples.append(pos)

In [35]:
for x,y in pp:
    print(x,y)


0 1
2 3

In [2]:
import numpy as np

In [6]:
a=np.arange(8).reshape((2,2,2))
print(a[0,:,:])
print(a[1,:,:])


[[0 1]
 [2 3]]
[[4 5]
 [6 7]]

In [10]:
np.flip(a,[1,2])


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-058da5030842> in <module>()
----> 1 np.flip(a,[1,2])

/home/roby/.local/lib/python3.5/site-packages/numpy/lib/function_base.py in flip(m, axis)
    203     indexer = [slice(None)] * m.ndim
    204     try:
--> 205         indexer[axis] = slice(None, None, -1)
    206     except IndexError:
    207         raise ValueError("axis=%i is invalid for the %i-dimensional input array"

TypeError: list indices must be integers or slices, not list

In [7]:
a=np.array([0,1])
b=np.zeros(2)
all(a==b)


Out[7]:
False