text: # -*- coding: utf-8 -*-
  """lab_project.ipynb
  
  Automatically generated by Colaboratory.
  
  Original file is located at
      https://colab.research.google.com/drive/1UY-KJHWTQUhQ0v6aGiH48P69Cap7RkIc
  """
  
  import numpy as np
  import matplotlib.pyplot as plt
  from PIL import Image 
  import pandas as pd
  import cv2
  from sklearn.cluster import KMeans
  import os
  import pandas as pd
  
  x=[]
  y=[]
  com = []
  
  def drawImage(img):
    plt.imshow(img)
    plt.show()
  
  def func(img):
    all_pixels = img.reshape((-1,1)) 
    km = KMeans(n_clusters=2)
    km.fit(all_pixels)
    centers = km.cluster_centers_
    com.append(centers)
    labels =km.labels_
    labels = labels.reshape((100,100))
    newImage = np.zeros((100,100))
    xint=0
    yint =0
    count=0
    for i in range(100):
      for j in range(100):
        if centers[0]<centers[1]:
          if(labels[i][j]==1):
            newImage[i][j]=1
            xint+=i
            yint+=j
            count+=1
          else:
            newImage[i][j]=0
        else:
          if(labels[i][j]==0):
            newImage[i][j]=1
            xint+=i
            yint+=j
            count+=1
          else:
            newImage[i][j]=0
    print(xint,yint)
    x.append(xint/count)
    y.append(yint/count)
    plt.scatter(xint/count,yint/count,c='blue')
    drawImage(newImage)
  
  dirname = './DataSet/'
  timeaccord= []
  for fname in os.listdir(dirname):
    timeaccord.append(fname)
  timeaccord.sort()
  final = []
  for fname in timeaccord[:24]:
      img = Image.open(os.path.join(dirname, fname))
      img = img.resize((100,100))
      drawImage(img)
      imarray = np.array(img)
      func(imarray)
      final.append(imarray)
  
  cmx =[]
  for i in range(24):
    if com[i][0]>com[i][1]:
      cmx.append(com[i][0])
    else:
      cmx.append(com[i][1])
  
  cmx = np.array(cmx)
  cmx_predict = []
  cmx_predict2 =[]
  for i in range(4):
    cmx_predict.append(cmx[i])
  for i in range(4):
    cmx_predict2.append(cmx[i])
  for i in range(3,cmx.shape[0]):
    dx = cmx[i-1]-cmx[i-2] 
    xt = cmx[i-1]+dx
    xat = cmx[i]
    dx2 = xat - xt
    cmx_predict.append(xat+dx2)
  
  for i in range(3,cmx.shape[0]):
    xt = (cmx[i-1]+cmx[i-2])/2
    xat = cmx[i]
    dx = xat-xt
    xt1 = ((xat+xt)/2)+dx
    cmx_predict2.append(xt1)
  plt.plot(cmx,'blue')
  plt.title("actual pixels value of cloud cluster center ")
  plt.xlabel("Image number")
  plt.ylabel("Pixel value")
  plt.show()
  plt.plot(cmx_predict,'red')
  plt.title("Predicted pixels value of cloud cluster center by algo1 ")
  plt.xlabel("Image number")
  plt.ylabel("Pixel value")
  plt.show()
  plt.plot(cmx_predict2,'black')
  plt.title("Predicted pixels value of cloud cluster center by algo2 ")
  plt.xlabel("Image number")
  plt.ylabel("Pixel value")
  plt.show()
  
  
  plt.plot(cmx,'blue')
  plt.plot(cmx_predict,'red')
  plt.plot(cmx_predict2,'black')
  plt.legend(['original','algo1',"algo2 plot"])
  plt.title("Comparision")
  plt.show()
  
  x=np.array(x)
  y=np.array(y)
  x_predict = []
  y_predict = []
  
  for i in range(4):
    x_predict.append(x[i])
    y_predict.append(y[i])
  
  for i in range(3,x.shape[0]):
    dx = x[i-1]-x[i-2]
    xt = x[i-1]+dx
    xat = x[i]
    dx2 = xat - xt
    x_predict.append(xat+dx2)
    dy = y[i-1]-y[i-2]
    yt = y[i-1]+dy
    yat = y[i]
    dy2 = yat - yt
    y_predict.append(yat+dy2)
  
  plt.plot(x, 'r')
  plt.plot(x_predict, 'b')
  plt.title("X coordinate actual to predicted by algo 1 comparision")
  plt.legend(['actual','algo1 predicted'])
  plt.xlabel('Image number')
  plt.ylabel('x coordinate')
  plt.show()
  plt.plot(y,'r')
  plt.plot(y_predict, 'b')
  plt.title("Y coordinate actual to predicted by algo 1 comparision")
  plt.legend(['actual','algo1 predicted'])
  plt.xlabel('Image number')
  plt.ylabel('Y coordinate')
  plt.show()
  plt.scatter(x,y)
  plt.plot(x,y,'r')
  plt.title("Actual center of mass value")
  plt.xlabel("x coordinates")
  plt.ylabel("y coordinates")
  plt.show()
  plt.scatter(x_predict,y_predict)
  plt.plot(x_predict,y_predict,'b')
  plt.title("Prediction by algo1 center of mass value")
  plt.xlabel("x coordinates")
  plt.ylabel("y coordinates")
  plt.show()
  
  plt.scatter(x,y,c="red")
  plt.plot(x,y,'blue')
  plt.title("The predicted coordinates of center of mass by algo1 ")
  plt.xlabel("x coordinate")
  plt.ylabel("y coordinate")
  plt.scatter(x_predict[-1],y_predict[-1],c="black")
  print(x_predict[-1],y_predict[-1])
  plt.show()
  
  x_predict2 = []
  y_predict2 = []
  
  for i in range(4):
    x_predict2.append(x[i])
    y_predict2.append(y[i])
  
  for i in range(3,x.shape[0]):
    xt = (x[i-1]+x[i-2])/2
    yt = (y[i-1]+y[i-2])/2
    xat = x[i]
    yat = y[i]
    dx = xat-xt
    dy = yat-yt
    xt1 = ((xat+xt)/2)+dx
    yt1 = ((yat+yt)/2)+dy
    x_predict2.append(xt1)
    y_predict2.append(yt1)
  
  plt.plot(x, 'r')
  plt.plot(x_predict2, 'b')
  plt.title("X coordinate actual to predicted by algo 2 comparision")
  plt.legend(['actual','algo1 predicted'])
  plt.xlabel('Image number')
  plt.ylabel('x coordinate')
  plt.show()
  plt.plot(y,'r')
  plt.plot(y_predict2, 'b')
  plt.title("Y coordinate actual to predicted by algo 2 comparision")
  plt.legend(['actual','algo2 predicted'])
  plt.xlabel('Image number')
  plt.ylabel('Y coordinate')
  plt.show()
  plt.scatter(x,y)
  plt.plot(x,y,'r')
  plt.title("Actual center of mass value")
  plt.xlabel("x coordinates")
  plt.ylabel("y coordinates")
  plt.show()
  plt.scatter(x_predict2,y_predict2)
  plt.plot(x_predict2,y_predict2,'b')
  plt.title("Prediction by algo2 center of mass value")
  plt.xlabel("x coordinates")
  plt.ylabel("y coordinates")
  plt.show()
  
  print("Prediction of movement of cluster center\n")
  print("Cloud cluster center moves to:",x_predict2[-1],y_predict2[-1])
  
  plt.scatter(x,y,c="red")
  plt.plot(x,y,'blue')
  plt.scatter(x_predict2[-1],y_predict2[-1],c="black")
  plt.title("The predicted coordinates of center of mass by algo2 ")
  plt.xlabel("x coordinate")
  plt.ylabel("y coordinate")
  plt.show()
  
  plt.plot(x,y,'blue')
  plt.plot(x_predict,y_predict,'red')
  plt.plot(x_predict2,y_predict2,'black')
  plt.legend(['original','algo1',"algo2 plot"])
  plt.xlabel("x coordinate")
  plt.ylabel("y coordinate")
  plt.title("Comparision")
  plt.show()
  
  data = [[]]
  for i in range(x.shape[0]):
    dummy = []
    dummy.append(i)
    dummy.append((x[i],y[i]))
    dummy.append((x_predict[i],y_predict[i]))
    dummy.append((x[i]-x_predict[i],y[i]-y_predict[i]))
    data.append(dummy)
  
  data1 = [[]]
  for i in range(x.shape[0]):
    dummy = []
    dummy.append(i)
    dummy.append((x[i],y[i]))
    dummy.append((x_predict2[i],y_predict2[i]))
    dummy.append((x[i]-x_predict2[i],y[i]-y_predict2[i]))
    data1.append(dummy)
  
  df = pd.DataFrame(data, columns = ["Image Number","Actual COM","Predicted COM", "Difference"]) 
  df1 = pd.DataFrame(data1, columns = ["Image Number","Actual COM","Predicted COM", "Difference"])
  
  df
  
  df1
  
  s =0
  s1=0
  sy=0
  sy1=0
  for i in range(x.shape[0]):
    s+=(x[i]-x_predict[i])**2
    s1+=(x[i]-x_predict2[i])**2
    sy+=(y[i]-y_predict[i])**2
    sy1+=(y[i]-y_predict2[i])**2
  print("Mean Square value for algo 1 for x and y coordinates are",s**0.5,sy**0.5)
  print("Mean Square value for algo 1 for x and y coordinates are",s1**0.5,sy1**0.5)
  