created week 2

This commit is contained in:
H.T. Kruitbosch 2018-09-14 17:33:07 +02:00
commit 41b1f16804
11 changed files with 1256 additions and 0 deletions

View File

@ -0,0 +1,317 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This exercise shows you how to work with variables and expressions, Let's start, press CTRL+ENTER to run the cell.\n",
"\n",
"Everytimes there is a `None`, you need to change that to the correct expression.\n",
"\n",
"PS: this is exercise `0`, because computers start counting at `0` instead of `1`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = 13\n",
"print('x: ', x)\n",
"print('type(x): ', type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Excercise:** Apply integer division by `2` on `x` using `//`, replace `None` with the correct expression."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = None\n",
"print('x: ', x)\n",
"print('type(x): ', type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that `x` is still an integer\n",
"\n",
"**Excercise:** Apply floating point division by `2` on `x` using `/`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('x: ', x)\n",
"print('type(x): ', type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that `x` is a floating point number now, even though `6` is divisible by `2`.\n",
"\n",
"**Excercise:** Create a new variable `y`, with value `9`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('x: ', x, ', y: ', y)\n",
"print('type(x): ', type(x), ', type(y): ', type(y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that x is still a float, and y an integer.\n",
"\n",
"**Exercise:** Assign the sum of `x` and `y` to `y`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('x: ', x, ', y: ', y)\n",
"print('type(x): ', type(x), ', type(y): ', type(y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that `x` did not change, and that y becames a float.\n",
"\n",
"**Exercise:** Square `y` using `**`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('x: ', x, ', y: ', y)\n",
"print('type(x): ', type(x), ', type(y): ', type(y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's work with strings.\n",
"\n",
"**Exercise:** Assign the text value `'https://www.rug.nl/'` to the variable `url`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('url: ', url, ', type(url): ', type(url))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Use `+` and `*` to add `10` asterisks (`'*'`) to the url"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('url: ', url, ', type(url): ', type(url))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Use `url.find( ... )` to locate the first asterisk, assign that position to `asterisk_at`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('url: ', url, ', type(url): ', type(url))\n",
"print('asterisk_at: ', asterisk_at, ', type(asterisk_at): ', type(asterisk_at))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Use `[: ... ]` to extract the first `asterisk_at` characters of `url`, removing the asterisks"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"url = url[: None]\n",
"print('url: ', url, ', type(url): ', type(url))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Not everything can be solved by multiplication, addition and such operations, even in mathematics.\n",
"\n",
"Even though this is not entirely true, let us assume it is for practical purposes.\n",
"\n",
"Some functionality comes as a *function*. One example of such a function is `print`. You use this function with brackets. \n",
"\n",
"**Exercise:** get the type of `print` using `type( ... )`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"type(None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The syntax for *calling* functions is similar to that of mathematical functions, like sine and cosine. Python also has those."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from math import sin, cos, sqrt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** calculate the sine and cosine of 0, and the square root of 16."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"cosine_at_0 = None\n",
"sine_at_0 = None\n",
"sqrt_of_16 = None\n",
"\n",
"print('cosine at 0: ', cosine_at_0)\n",
"print('sine at 0: ', sine_at_0)\n",
"print('square root of 16: ', sqrt_of_16)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You've learned about\n",
"\n",
" * Expressions: `x * (5 + y)`\n",
" * Assigments: `x = 5`\n",
" * Strings: `'Mark ate a burger yesterday'`\n",
" * Functions: `print('hello')`, `sin( ... )`\n",
" * Types: `type( ... )`"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

280
Week-2/01. Stealing.ipynb Normal file
View File

@ -0,0 +1,280 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Obviously, stealing is not OK, also not for programmers. Nevertheless, many software is freely available, typically even opensource. Moreover, Python provides a lot out of the box. Next to software shipped with Python, there is many more on [https://pypi.python.org](pypi.python.org). PyPI stands for Python Package Index. You can install PyPI software with `pip3`, a command shipped with Python.\n",
"\n",
"This notebook shows you what happens if you install something, in particular where the software is stored on your hard disk. You will install `folium`, a software used to create (land) maps."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Run the cell below. Notice that *folium is not installed* and hence there is an \n",
"```\n",
"ImportError\n",
"```."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import folium"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Import the module `sys` using the instruction `import sys`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# YOUR CODE HERE, ~1 line\n",
"None\n",
"# END OF YOUR CODE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`sys` is a module that has an attribute `path`. `path` contains a list of folders where python looks whenever you import something. This is called the python path, sometimes reffered to as the `PYTHONPATH` variable, because this variable can be overridden via the operating system.\n",
"\n",
"**Note:** There is also a module `os.path`, to work with files on your hard disk. From now on, the <u>hard disk</u> also means the <u>file system</u>. Do not confulse `os.path` (file system operations) and `sys.path` (where python looks for software).\n",
"\n",
"**Exercise:** Look at the content of `sys.path`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# YOUR CODE HERE, ~1 line\n",
"None\n",
"# END OF YOUR CODE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This list include text in between apostrophes (`'`). This is Python's way to display text elements (a.k.a. <u>strings</u>).\n",
"\n",
"**Apple computer**\n",
"> You will also notice foward slashes (`/`). They separate folders in your computer, and are called <u>path separators</u>.\n",
"\n",
"**Windows computer**\n",
"> You will also notice slashes (`\\`). They separate folders in your computer, and are called <u>path separators</u>. \n",
">\n",
"> You will also notice that slahes are doubled (`\\\\`). Putting a `\\` before a character is called <u>escaping</u>. We will talk more about it in a future lecture.\n",
"\n",
"---\n",
"\n",
"---\n",
"\n",
"Among the folders you just saw, there is one with your packages. To you help you find it, execute one of the following cells:\n",
"\n",
"**On Windows**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"for folder in sys.path:\n",
" if '\\\\intropython\\\\lib\\\\site-packages' in folder:\n",
" print('==> ', folder)\n",
" else:\n",
" print(folder)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**On Apple**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"for folder in sys.path:\n",
" if '/intropython/lib/python3.6/site-packages' in folder:\n",
" print('==> ', folder)\n",
" else:\n",
" print(folder)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The folder with `==>` is the one you are looking for. Open it using Windows Explorer (on Windows) of Files (on Apple).\n",
"\n",
"You should see many folders with installed Python software, **MAKE SURE NOT TO DELETE OR EDIT ANYTHING**."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that you cannot find `folium` in this folder. Let's install it!\n",
"\n",
"You should always install or uninstall software using `pip3 install <package-name>` or `pip3 uninstall <package-name>`.\n",
"\n",
"Open Anaconda Console via the start menu, activate the Python environment you created last week via `conda activate introproc` and then install `folium` using `pip3 install folium`.\n",
"\n",
"**Exercise:** Run the activate and install commands in Anaconda Console, then refresh the aformentioned folder you should see folium."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's show a map of Leeuwarden with a marker where we are (approximately)! No worries about understanding the code below, make the following notes though:\n",
"\n",
" * with `pip3 install` and `import` we can use existing software,\n",
" * folium allows us to create a map, with only 3 or 4 lines of code,\n",
" * this map is stored in a variable `m`, via the first line with `m = ...`\n",
" * this line creates a `folium.Map`, tells it which background-map to use (`'stamentoner'`), the centre location in latitude and longitude, and the zoom level.\n",
" * then a marker is added to the map\n",
" * the last expression of the cell is its output, in this case that is the map `m`. If you delete this line, the map will be created, but not show.\n",
"\n",
"**Exercise:** delete the last line with `m` and verify that the map will be gone. Put it back again."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import folium"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"m = folium.Map(tiles='stamentoner', location=[53.1991713, 5.7941344], zoom_start=16)\n",
"\n",
"folium.Marker(location=[53.1991713, 5.7941344]).add_to(m)\n",
"\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The map `m` also has attributes, accessable via the period point (`.`). That's because `m` is a special type of variable: an `object`. Access the attribute `location` of `m`. You can start by typing `m.l` and then press the key *TAB*. Jupyter will show several options for you to choose from, choose `location`.\n",
"\n",
"**Exercise:** Get the location of `m`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# YOUR CODE HERE, ~1 line\n",
"None\n",
"# END OF YOUR CODE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The map also has an attribute `get_bounds` that is a function. Functions of objects are called methods. You also call them using brackets `()`.\n",
"\n",
"**Exercise:** use `get_bounds()` to get the top-left and bottom-right corners of the map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# YOUR CODE HERE, ~1 line\n",
"None\n",
"# END OF YOUR CODE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You have learned about\n",
"\n",
" * The Python path\n",
" * Installing software via pip\n",
" * Importing code of others\n",
" * Accessing attributes or methods with the dot `some_object.some_attribute`"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,136 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this exercise, we'll do calculations on many numbers at once. Python provides the numpy library for that."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# !pip install numpy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"%matplotlib notebook\n",
"from matplotlib import pyplot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Use `np.arange(..., ...)` to create a vector from `0` up to and including `5`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = None\n",
"\n",
"print('x: ', x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Extract `1` from all the numbers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = None\n",
"print('y: ', y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Take the square root of all these numbers, either using `** 0.5` or `np.sqrt( ... )`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = None\n",
"print('y: ', y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that the square root of `-1` is not defined, and hence the result is *not a number* (nan).\n",
"\n",
"The cell below will make a plot of `x` and `y`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pyplot.plot(x, y)\n",
"pyplot.xlabel('x')\n",
"pyplot.ylabel('y')\n",
"None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You have learned to use `numpy` for so called vector operations, these are mathematical operations on many numbers as well. This field is called *linear algebra*, and is a usefull field on its own, in particular often used in artificial intelligence."
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,427 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this exercise you will download the audio of a youtube video, convert it to the WAV format, and then use the Google Cloud API to transcribe its content.\n",
"\n",
"The lines below import some libraries that make this quite simple. Summerized they are:\n",
"\n",
" * **pafy**, a library to download youtube video and audio.\n",
" * **pydub**, a library to convert audio, for example from mp3 to wav.\n",
" * **google api**, contains a lot of stuff, in particular audio transcription using the speech API. This is done on a Google server, you send it audio and get a transcription back. This way Google can improve their machine learning algorithms and serve this to you.\n",
"\n",
"\n",
"This exercise uses reasonably complicated syntax and things you have not learned properly. Do not worry too much about that, try to understand what is happening. We only ask you to insert small and simple pieces of code.\n",
"\n",
"Select the 'cell' below and press CTRL+ENTER or SHIFT+ENTER to run the code inside it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# !pip install pafy pydub youtube-dl google-cloud google-cloud-speech google-api-python-client\n",
"\n",
"# import sys\n",
"# !conda install --yes --prefix {sys.prefix} -c conda-forge ffmpeg "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os\n",
"os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'credentials.json'\n",
"os.environ['PATH'] += ';' + os.path.join(os.path.abspath(os.curdir), 'bin')\n",
"\n",
"import io\n",
"import pafy\n",
"from pydub import AudioSegment\n",
"\n",
"from google.cloud import speech\n",
"from google.cloud.speech import enums\n",
"from google.cloud.speech import types\n",
"\n",
"from googleapiclient import discovery"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First use `pafy` to get some information of a video.\n",
"\n",
"**Exercise:** What are the types of url, video and video.length? You can use function `type( ... )` to find out."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# YOUR CODE HERE, ~ 3 lines\n",
"None\n",
"None\n",
"None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run the cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"url = 'https://www.youtube.com/watch?v=yY-P3D63Z18'\n",
"\n",
"video = pafy.new(url)\n",
"\n",
"print(\"Url:\", url)\n",
"print(\"Title:\", video.title)\n",
"print(\"Author:\", video.author)\n",
"print(\"Description:\", video.description)\n",
"print(\"Length:\", video.length)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's actually download the audio of the video and save it. The first line gets the best audio format from the existing ones (YouTube provides multiple formats and encodings of video and audio).\n",
"\n",
"The second line downloads the file to `audio.webm`.\n",
"\n",
"Run the cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"audio = video.getbestaudio()\n",
"\n",
"# remove the file if it already exists\n",
"if os.path.exists(audio.filename):\n",
" os.unlink(audio.filename)\n",
"\n",
"filename = audio.download(filepath=audio.filename)\n",
"print(\"Filename:\", filename)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Google's api works easier with a WAV file than with a WEBM file (even though webm is their own format). Moreover, the audio cannot be more than 60 seconds long. Longer audio need the so called `streaming api`, which is a bit harder to use.\n",
"\n",
"Let's keep it simple. The recipe below is, line by line:\n",
"\n",
" * open the WEBM file\n",
" * save it as WAV\n",
" * show the audio\n",
" \n",
"Run the cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sound = AudioSegment.from_file(audio.filename)\n",
"\n",
"sound.export(\"audio.wav\", format=\"wav\", bitrate=\"128k\")\n",
"\n",
"sound"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The variable `sound` contains an `AudioSegment`, an object with `frame_count()` method and a `frame_rate` attribute. \n",
"\n",
"As you may know, sound is a wave. For computers to store such waves, an audio file is composed of many frames. Each frame specifies the amplitude of the wave at a specific time. `frame_count()` calculates how many frames there are in total, and `frame_rate` states how many frames should go in one second.\n",
"\n",
"**Excercise:** Divide `frame_count()` by `frame_rate` to find out how many seconds are exactly in the audio file. Use both `/` and `//` for the devision, what is the difference?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(\"type(sound): \", type(sound))\n",
"\n",
"# YOUR CODE HERE, ~ 2 lines, use both / and //\n",
"audio_length_s = None # Use /\n",
"audio_length_s_ = None # Use //\n",
"# END OF YOUR CODE\n",
"\n",
"print(\"audio_length_s: \", audio_length_s)\n",
"print(\"audio_length_s_: \", audio_length_s_)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have a WAV file, we use the tools google provide to load these files in a data type Google likes, and we also specify a configuration which states that we want English transcriptions. This improves the transcriptions quality, as now the system know that **Je t'adore** is less likely to occur as **Shut the door**.\n",
"\n",
"The first two lines open the audio file and place its data in memory.\n",
"\n",
"The third line converts the data to a format in which Google can handle.\n",
"\n",
"The last line creates a configuration for the transcription task (in which the language is also specifyied).\n",
"\n",
"Run the cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"with open('audio.wav', 'rb') as audio_file:\n",
" content = audio_file.read()\n",
"\n",
"audio = types.RecognitionAudio(content=content)\n",
"config = types.RecognitionConfig(language_code='en-US')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's start transcoding. The first line creates a client, which is sort of a telephone that does the communication with Google.\n",
"\n",
"The second line does the hard work, or at least asks Google to do so.\n",
"\n",
"Run the cell (**it will result in an error**).\n",
"\n",
"Read the message, however cryptic it may seem. What does this error mean?\n",
"\n",
" * A) You need to pay for this Google service,\n",
" * B) You need to make an appointment (rendezvous) with a service agent from Google\n",
" * C) The audio was too long, and this API only accepts smaller files\n",
" * D) Google speech does not support audio files in stereo\n",
" * E) The transcription service was permanently terminated in 2016, Google now only offers web search and e-mail"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": false
},
"outputs": [],
"source": [
"client = speech.SpeechClient()\n",
"\n",
"response = client.recognize(config, audio)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An important part of programming is failing. No code was ever right the first time and most code isn't even right in the final version. `github.com` is a place to distribute software, but also register software bugs and manage solutions. Browse some of the issues there and you'll get the point (or not browse the site, and trust me on this).\n",
"\n",
"This issue is with the file length, it's too large as Google only allows 60 seconds. So let's extract **the first 30 seconds** and try again.\n",
"\n",
"To extract a time slice, the syntax is `[ <start_in_miliseconds> : <end_in_miliseconds> ]`. If you leave the start out, it starts from the beginning. If you leave the end out, it ends at the end. \n",
"\n",
"**Exercise:** Correct the error. For that, choose the correct line out of the 5 commented lines. \n",
"\n",
"All text after a hashtag (`#`) is commented, that is, <u>ignored by the computer</u>. This way you can disable code or add comments. You can uncomment a pieace of code by deleting the hashtag (`#`).\n",
"\n",
"*TIP*: You can test each option by uncommenting one at a time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sound = AudioSegment.from_file(filename)\n",
"\n",
"### Select one of these 5 options by removing the # and the space:\n",
"# sound = sound[:]\n",
"# sound = sound[1000:]\n",
"# sound = sound.get_sample_slice(0, 5*44000)\n",
"# sound = sound.split_to_mono()[0]\n",
"# sound = sound[103]\n",
"\n",
"\n",
"sound.export(\"audio.wav\", format=\"wav\", bitrate=\"128k\")\n",
"\n",
"sound"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"with open('audio.wav', 'rb') as audio_file:\n",
" content = audio_file.read()\n",
"\n",
"audio = types.RecognitionAudio(content=content)\n",
"config = types.RecognitionConfig(language_code='en-US')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"client = speech.SpeechClient()\n",
"\n",
"response = client.recognize(config, audio)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"response"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`response` is an object, autocomplete won't work on it, though. However, the output suggests that there is a `results` attribute.\n",
"\n",
"**Exercise:** Get the `results` attribute of `response`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# YOUR CODE, ~1 line\n",
"None\n",
"# END OF YOR CODE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To fully unravel this nested object, we need syntax not yet properly discusses in this course, namely `[0]`. It indicates that we want the first (zero-th) result and the first (zero-th) alternative. This time, the API only gives on result and one alternative.\n",
"\n",
"**Note:** The Google Speech Recognition API returns more results if we gave it more audio clips to translate at once, and more alternatives if it is not too sure about what was said, and came up with more options. Confidence is a number between `0` and `1` to indicate how ... confident Google is about its answer.\n",
"\n",
"This is how we unravel `response` to the transcript text."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"response.results[0].alternatives[0].transcript"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You have seen how to use existing software, to do a reasoanbly complicated task. Possibly you have never programmed before, yet using stuff from others allowed you to translate audio into text, without knowing how this was done. The artificial intelligence behind this translation was black-boxed, and is a field on its own.\n",
"\n",
"**Take home**\n",
"\n",
" * Using existing software, for example on PyPI or github, can help you get something done;\n",
" * You need to figure out how to tie together different software into something that gets your job done.\n",
" * Using software from others has benefits:\n",
" - if you write it yourself, you will do things wrong and have bugs messing up your system from time to time. Others may have more time for their specific problem and solve those with updates\n",
" - it enforces you to write modular code, since you are not allowed to change the library. This often results in better maintainable code.\n",
" - friends that will work on your code can read the documentation of the libraries you used, and get an idea of what is going on. Imagine all the work of explaining or documenting it yourself!\n",
" - you can, if you really need to, change code of libraries, but it is something you should try to avoid.\n",
" <br>\n",
" **NEVER (NEVER!!) DO THIS VIA THE `site-packages` (OR `dist-packages`) DIRECTORIES OF THE PYTHON PATH.\n",
" <br>\n",
" Those files are deleted and replaced each update**\n",
" * a lot is already out there!\n",
" * not everything, though :("
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

84
Week-2/04. IOU.ipynb Normal file
View File

@ -0,0 +1,84 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# IOU\n",
"\n",
"![IOU](IOU.png)\n",
"\n",
"Mark, Anna and Dennis had a great weekend in Hamburg and decided to split the cost equally. Consider that Mark spent 25 EUR, Anna 45 EUR and Dennis 17 EUR. Compute how much each owes the other two\n",
"\n",
"Use the cell below to calculate how much each owes the other two, and how much each should get from the other two.\n",
"\n",
"Make sure the code is easily adaptable to cases where Mark, Anna and Dennis have spend different ammounts.\n",
"\n",
"Hints:\n",
"\n",
" * Calculate how many each should pay, regarless of how much they already paid, you could call this variable `average`.\n",
" * To find out how much someone still owes, you can use `average - paid`, which might be negative. On the other hand, `paid - average` expresses how much someone should get.\n",
" * `max( ..., ... )` returns the maximum value out of two (or more), think how `max(0, ...)` can be used to clip negative values to `0`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mark = 25\n",
"anna = 45\n",
"dennis = 17\n",
"\n",
"# YOUR CODE\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"# END OF YOUR CODE\n",
"\n",
"print('name owes gets')\n",
"print('Mark {: 6.2f} EUR {: 6.2f} EUR'.format(mark_owes, mark_gets))\n",
"print('Anna {: 6.2f} EUR {: 6.2f} EUR'.format(anna_owes, anna_gets))\n",
"print('Dennis {: 6.2f} EUR {: 6.2f} EUR'.format(dennis_owes, dennis_gets))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Expected output**\n",
" 1. `name owes gets`\n",
" 1. `Mark 0.00 EUR 3.67 EUR`\n",
" 1. `Anna 12.67 EUR 0.00 EUR`\n",
" 1. `Anna 0.00 EUR 16.33 EUR`"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

BIN
Week-2/IOU.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

BIN
Week-2/bin/ffmpeg Normal file

Binary file not shown.

BIN
Week-2/bin/ffmpeg.exe Normal file

Binary file not shown.

BIN
Week-2/bin/ffplay.exe Normal file

Binary file not shown.

BIN
Week-2/bin/ffprobe.exe Normal file

Binary file not shown.

12
Week-2/credentials.json Normal file
View File

@ -0,0 +1,12 @@
{
"type": "service_account",
"project_id": "campus-fryslan-course",
"private_key_id": "e2998d5ccf2ce4f9a58f8eb2c3c1eb8edec7182e",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDObFd9hfJxIW00\n/hYn89LBwtH/THi8N4W8eWggvrcqGnIKNmOqLK9SXZy+9OgPO5LHKsf8iUXLIUWi\nlCD9XzZ0t6A+4kmQOm8T+t3zJ9FN+a3V5QsRYtk4MP4bAG0yByF1/Ts2vXZt/4oz\nDd54qs5dOdHpNN+VPz+gBCJEVkMloc66R+SqB4j1IGJOI+dXSlnwdwawg42uACeI\nBZDDtbvEQjztYDvech3IPtT+babVWPmSaQJ5z6UFTvDN85nfB+omMAmLDBep99Xx\nEcqfoZZuHmTrSNvu6AEGfqt8PGXkW7gKS8JstqegkSx8t9nNmCh6WZw+whtJmE83\nryGA3Z3hAgMBAAECggEAHOTabEYTFRXWOPw18rq07g1Mor08CzCEZkjiM+8uKVUR\nUG+jjL77fX6AAdS7JOK0XW+WFbnLTK8oFaVBZuXsc/L+Gb9IB8m+IaBUcARLTmxc\nElwgq/rXp/9nmjT5k+6eluru3m8vxB7oY9QVp3NVNoRw5wFRKI11ZPsfJsyiUoJu\n1qIfmlEAtqLjWP/RPfxNhO8DF2AA24mLuvnJ7z5SCQNFcGqngov/8IMa4j676pgN\niEyVSUdr9zkA3nbh6D2NQclPRg2O83XvL6uoAd/BBcfppHUKVu9Yd3QLERE33VTF\nT3+9Ihz1+0iH0UU0aq3X7Q6qiqmFqch7LP2uNecQ7wKBgQDo11yu1BoE3oCHiU70\ndxQWx9oUpjysC2yuQz2ZEBnZHS/em9PULDyqgCMKHjWfArXcd1mT62Jo2FPYu2qL\nkPmuCZ+W0HZvTQb7oh4s6/QUWwhr46EfZqxJb2q1Z1RJ+uiJ6P2d16DtGMcBK4Wb\n3zFqwYaDGLlvUoZ4ovNwHcoKqwKBgQDi9FHNRZQhdolAMrV9zLtj2C+NKVH3vGDY\nCdeVcSgQ1Bqvl0UpcfQxAOPXyWp+sfk3MDzx5umNMrdrFMLIs+bC3MvNtTB7jwWu\nTAadiJjKWfExtinJST5CZwz4WluhOEEHP7U9Wn4cMj36bjt5RxEfAba278EG2RSE\ntEWA3op5owKBgGMglM/FctJR28xYE6oWLBJKwTh7UWc5cep6q/XHGgoJ2ABgvP0e\nXDGGXeSH2fI5WC1lDljgJERYnNGvcHu8m9+RL0UouNNHqspkSY58Yaxs2Rf9j/Hr\nZzvYWEiklgjs7iUQcTDKxTNLkiWYXshua/50J40WcJPJQYCIdzVrKUIDAoGBAK0V\ngXFYeBFRMnlocuufAOazkAhLE/0oiH8aenS5WMIE0NlLN27VPlNFB36hWHuJRbQm\nMMSXw1Rw8ltS+P5R3VKNS0z1uyTwPXfg+UaZT1U7ZOvxrkqZdkb0Cns5hhodKdLa\nbmF9KyJb2tKr0OW8Ij2QdAVuMbsEQVa3mS4gCmGbAoGASqz9amadgLKlIBI3ydtg\n0D4aEZNs30jIvYuzR5UlGfMB7sKyCcJ9TRQFV/SrEhIvwfh5CIG0b7cQfg/knCjc\nJdsBKoOliK/A2J9Uvchpbm/OIb2lnzjC6pbRDln0KsG1pWTeDAuISpSyTwPuHXAb\nRwIA22A0YPQDYPV+4WyzJ+A=\n-----END PRIVATE KEY-----\n",
"client_email": "campus-fryslan-students@campus-fryslan-course.iam.gserviceaccount.com",
"client_id": "109518862650320538533",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/campus-fryslan-students%40campus-fryslan-course.iam.gserviceaccount.com"
}