How to load an image in Keras from url ?

How to load an image in Keras for machine learning from a url for image classification


How to load an image in Keras for machine learning from a url ?

For loading image in Keras we used to write code like this

img = keras.utils.load_img(path="OIP.jpg",
color_mode = "grayscale",target_size=(28,28,1))

How about using a image URL instead of the above code?

Need to perform couple of things.

  1. Get response from the image url.
  2. Get the image as byte using ByteIO
  3. Covert the image as Grayscale for prediction and apply appropriate resize.
  4. Finally covert the image into a NumPy array.

Let’s wrap all these in a function.

from tensorflow import keras
from io import BytesIO
from PIL import Image
import requests

def loadImage(url):
    response = requests.get(url)
    img_bytes = BytesIO(response.content)
    img = Image.open(img_bytes)
    img = img.convert('L')
 
    img = img.resize((28, 28), Image.NEAREST)
    img = keras.utils.img_to_array(img)

    return img

before using the image for prediction, we need to reshape the image.

url = 'https://edwin-de-jong.github.io/blog/mnist-sequence-data/fig/5.png'
img = loadImage(url)
test_img = img.reshape((1, 784))
img_class = model.predict(test_img)

ML/AI Posts, you may interested.

Python : list to tuple

Convert Python List to tuple snippet


Convert Python List to tuple snippet

import sqlite3
con = sqlite3.connect('mydb.sqlite')
con.execute("CREATE TABLE IF NOT EXISTS users(id INTEGER ,name TEXT, ecode TEXT);")
con.execute("CREATE TABLE IF NOT EXISTS employees(id INTEGER PRIMARY KEY,ename TEXT, eid INTEGER);")
empnames = [
('jhon', 1),
('martin', 2),
('dev', 3)
]
companies = [
('Foo', 12),
('Bar', 7),
('Moo', 99),
]
comp='SAMSUNG'
comp_id=7006
# argument must be touple
# if one parameter place additional coma
con.execute("INSERT INTO users (id) VALUES (?)", (comp_id,))
con.execute("INSERT INTO users (name,id) VALUES (?,?)", (comp,comp_id))
con.executemany("INSERT or ignore INTO employees (ename,id) VALUES (?,?)", empnames)
con.commit()
# I think this will help you

Python SQlite – insertion with tuples and List

Python SQlite – insertion with tuples and List


Python SQlite – insertion with tuples and List

# This code will show how you can convert a list into tuple list
print('Touple meet List')
l1=['Python','C','C++','C#','GO','Dart','Kotlin'] # the list
# List elements to touples
x=[(x,) for x in l1]
print(x)
Out Put
Touple meet List
[('Python',), ('C',), ('C++',), ('C#',), ('GO',), ('Dart',), ('Kotlin',)]

How to extract keys from json object in Python

How to extract keys from a JavaScript / JSON object in Python


How to extract keys from complex json Object in Python? We can loop through it, but there is a better way.

JSON Object and Keys in Python

Usually we use json module to parse json objects in Python.

import json
...
data = json.loads(source)

The data variable hold the parsed json object and it can be a list of dictionary values. So how do we extract those keys ? Just like JS, Python also provides the keys method which will extract a list of keys.

countries = list(data.keys())

This will extract all country keys and convert it into a Python list.

Following json post may help you learn more

How to fetch API data in Python

How to make an API call and fetch json data in Python


API allows programmers to share the data in an organization or network. Modern apps may use multiple API’s too. So do we access API data in Python apps ?

Requirements

  • URLLIB3
  • JSON

In Most case data is shared as JSON , so we need to use json package to read the data, and urllib3 library for access and make request .

Function for fetch data

It is wise to create function for fetching data from API , so that you can reuse the function to read data from another API.

Here is the Python function to read data from an API end point .

</p>
from pip._vendor import urllib3
import json

def apicall(url):
    try:
        http = urllib3.PoolManager()
        weburl = http.request('GET', url)
        data = weburl.data.decode('utf-8')
        return data
    except urllib3.exceptions:
        print('Error')
    return 
<p>

Using the function

</p>
        urldata = 'https://api.mysite.com/somjsom.json'
        data = apicall(urldata)
        j_datax = json.loads(data) 
<p>

The apicall function will return the data in json format

Following Python post deserve a good read

How to run selenium webdriver with headless mode

How to run selenium webdriver in headless mode


Selenium is one of the powerful package available in Python , used for web automation , no doubt about that. In the last we learned how to use a web driver with webdriver manager package. In this we will learn how to use a headless mode of action in selenium webdriver.

Headless mode

What is headless mode ? In head less mode ,browser you automate work in the background and the work can be completed silently. So how to run selenium web driver in headless mode .

  • You need to create browser option and add –headless.
  • Add the option to webdriver

Chrome option

Create a chrome option as follows and add attch to the web driver.

</p>
from selenium.webdriver.chrome.options import Options
chrome_opt = Options()
chrome_opt.add_argument('--headless')
browser = webdriver.Chrome(executable_path=ChromeDriverManager().install(),chrome_options=chrome_opt)

<p>

That’s all you need to know

Fix Chrome Driver Error or Selenium webdriver error

How to solve the chromewebdriver / selenium webdriver error using python package wedriver_manager


When I started with Selenium in Python my biggest problem was the webdriver issue which I later found that there is an automatic download option in python using some special package.

The webdriver sometimes not compatible with the browser you are using. You have to manually find them from the official websites. This is messy part of selenium, I think.

We can get ride off this problem using webdriver_manager

first you need to install the Python package using pip

</p>
pip install webdriver_manager
<p>

Now you can use it in your project

</p>
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

<p>

The package will automatically check for compatible version and install it and no more worries about web driver and scrap the web with ease and peace.

How to pass python list to JavaScript in Flask app

How to pass a python list to JavaScript with ninja template in Flask web app


You can’t use Python list directly into JavaScript, JavaScript lislt/array is not compatible with Python or wise versa. So what to do.

Solution

I have a list of names, which I want pass to a JavaScript in a Flask ninja template. Ninja template allow use to safely convert the python list to Json string then we can pass it to the JavaScript function / just JavaScript

</p>
<script type="text/javascript">

        dn = {{ distabr|tojson }}
</script>
<p>

You can’t use same route variable in multiple places in same template, it may not serve the purpose

In JavaScript Functions

Suppose you have separate JavaScript function files for some functionality like drawing analytic plots and pie diagram, or some donut chart. So how do we pass value to those functions ?

You can create a JavaScript as above, in your current template, then in function create a script variable to use it as follows.

</p>
function init_echarts() {
    var distNames=dn;
}
<p>

That’s all you need to know to embed some morris diagram to your flask app.

You should look at Morris JS , it is free and easy to use JavaScript charts library.

Pass variables to other templates in Flask

How to pass variable to other template using { % set % } in Python Flask web app


Flask is one of my Python frame work in which I build simple web application with ease and peace. Today we learn how to pass a variables to other templates in a Flask application.

Suppose your web app have a base template and a top nav bar template which included using

</p>
{% include "site_template/top_navigation.html" %}
<p>

and you want to pass a title or something else from a Index page to navigation bar where you want show up some text.

Using {% set %} you can create global variables.

The solution

We can use a set block in top of the index page as follows ,to pass variables to other sub templates

</p>
{% set mytitle=title %}
<p>

In the route it will look like

</p>
return render_template('index.html',  title=title)
<p>

and in the nav bar page we can use as mytitle as variable using

</p>
<div id="site_title"><h2  >{{ mytitle}} </h2></div>
<p>

If you had doubt and suggestion please leave a comment

RapidAPI example using request in Python

Create RapidAPI powered app with Python and request


This tutorial is similar to RapidAPI example found in the official blog, with one exception, this example is users request method instead of unirest package which is not available on RapidAPI blog.

RapidAPI

RapidAPI is a website which provides useful APIs (Application Programing Interface) like Yahoo Finance, Email Verification, Geolocation which can be used to add features to your websites and apps using variety of programming languages like Python, Java, .Net etc.

Yahoo Finance example

For creating application to show stock chart using YahooFinanceAPI and Python, we are go through the following

  • Create a Rapid API Account
  • Subscribe to Yahoo Finance to get the endpoint (Which is a URL for getting information)
  • Create methods to connect API and Process data
  • Draw Plot

Install requirements

Install required packages using pip. I suggest using Python virtual environment.

</p>
pip install seaborn pandas 
<p>

Complete Source code

</p>
import json
from datetime import datetime
import matplotlib.pyplot as plt
import pandas as pd
import requests
import seaborn as sns
from matplotlib import rcParams
from past.builtins import raw_input

RAPIDAPI_KEY = "<YOUR_RAPIDAPI_KEY>"
RAPIDAPI_HOST = "<YOUR_RAPIDAPI_ENDPOINT>"

symbol_string = ""
inputdata = {}


def fetchStockData(symbol):
    url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/market/get-charts"

    querystring = {"comparisons": "%5EGDAXI%2C%5EFCHI", "region": "US", "lang": "en", "symbol": symbol,
                   "interval": "5m", "range": "1d"}

    headers = {
        'x-rapidapi-host': "apidojo-yahoo-finance-v1.p.rapidapi.com",
        'x-rapidapi-key': "275d968795msheb0c2d90f2d7a32p1141eajsnef8c254e756a"
    }
    response = requests.request("GET", url, headers=headers, params=querystring)
    if (response.status_code == 200):

        return json.loads(response.text)
    else:
        return None


def parseTimestamp(inputdata):
    timestamplist = []
    timestamplist.extend(inputdata["chart"]["result"][0]["timestamp"])
    timestamplist.extend(inputdata["chart"]["result"][0]["timestamp"])
    calendertime = []
    for ts in timestamplist:
        dt = datetime.fromtimestamp(ts)
        calendertime.append(dt.strftime("%m/%d/%Y"))
    return calendertime


def parseValues(inputdata):
    valueList = []
    valueList.extend(inputdata["chart"]["result"][0]["indicators"]["quote"][0]["open"])
    valueList.extend(inputdata["chart"]["result"][0]["indicators"]["quote"][0]["close"])
    return valueList


def attachEvents(inputdata):
    eventlist = []
    for i in range(0, len(inputdata["chart"]["result"][0]["timestamp"])):
        eventlist.append("open")
    for i in range(0, len(inputdata["chart"]["result"][0]["timestamp"])):
        eventlist.append("close")
    return eventlist


if __name__ == "__main__":
    try:
        while len(symbol_string) <= 2:
            symbol_string = raw_input("Enter the stock symbol: ")
        retdata = fetchStockData(symbol_string)

        if (None != inputdata):
            inputdata["Timestamp"] = parseTimestamp(retdata)
            inputdata["Values"] = parseValues(retdata)
            inputdata["Events"] = attachEvents(retdata)
            df = pd.DataFrame(inputdata)
            sns.set(style="darkgrid")
            rcParams['figure.figsize'] = 13, 5
            rcParams['figure.subplot.bottom'] = 0.2

            ax = sns.lineplot(x="Timestamp", y="Values", hue="Events", dashes=False, markers=True,
                              data=df, sort=False)
            ax.set_title('Symbol: ' + symbol_string)

            plt.xticks(
                rotation=45,
                horizontalalignment='right',
                fontweight='light',
                fontsize='xx-small'
            )
            plt.show()
    except Exception as e:
        print("Error")
        print(e)

<p>

Output