<jupyter_start><jupyter_text><jupyter_code>from bs4 import BeautifulSoup
import requests
import numpy as np
import re
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO

# create list of names of images to scrape from the web
names=['ankles', 'hands']

# base url to search images in google
url1='https://www.google.com/search?tbm=isch&q='
# create empty dictionary to store images
# in format Name_Of_Item --> list of images corresponding to this name

images={}

# for each item
for name in names:
  
  # get information from web page as html text
  page = requests.get(url1+name)
  soup = BeautifulSoup(page.text, 'html.parser')
  # find all images on the web page
  img_tags = soup.find_all('img')

  urls = [img['src'] for img in img_tags]
  # empty list to store images
  f=[]
 
  # add each image to list
  for url in urls:

    if 'http' not in url:
      url = '{}{}'.format(url1, url)

    response = requests.get(url)
    img = Image.open(BytesIO(response.content))
    f.append(img)
  
  # add to dictionary list of images as values
  # and names of items as keys
  images[name]=f


# let's look at one of the scraped images
plt.imshow(np.array(images[names[0]][0]))

import cv2


def to_array(dic):

  # this function takes dictionary wich contains 
  # pictures of hands and ankles
  # and transforms each image from PIL Image to numpy array,
  # scales them and resizes to the same resolution
  # to put images to the model

  dict_toarray={}

  for key in dic:
      list_array=[]

    for img in dic[key]:
      img=np.array(img)/255.
      img = cv2.resize(img, dsize=(250, 250), interpolation=cv2.INTER_CUBIC)
      list_array.append(img)
    dict_toarray[key]=list_array
  return dict_toarray

# convert images to numpy arrays
images=to_array(images)
# let's look at one of resized images
plt.imshow(images[names[1]][16])
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score as acc

# create labels: half of images are ankles (0)
# and other one is hands (1)
Y=np.zeros(40)
Y[20:]=np.ones(20)

# create training examples by concatenating
# two lists of images of ankles and hands
# then reshape X into shape that would be suitable for the future model
X=np.concatenate(tuple(images.values())).reshape(40,250*250*3)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)
# create model instance
reg = LogisticRegression()

# fit training data
reg.fit(X_train,y_train)

# make preductions
y_pred = reg.predict(X_test)

# print out quality of the predictions made by the model
acc(y_test,y_pred)<jupyter_output>/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
{'fraction_non_alphanumeric': 0.09239686327991817, 'fraction_numerical': 0.017388339584043642, 'mean_word_length': 4.296028880866426, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '417292', 'license': 'MIT', 'n_tokens_mistral': 1005, 'n_tokens_neox': 952, 'n_words': 333, 'path': 'ML Classification of Images of Ankles And Hand.ipynb', 'repo_name': 'marianna13/Notebooks', 'size': '2933'}