Skip to content
Snippets Groups Projects
Commit 1b66907b authored by s183897's avatar s183897 :ice_skate:
Browse files

Added prediction only versions of main.

parent ab8ad738
No related branches found
No related tags found
No related merge requests found
255 30 81
197 197 252
220 127 40
177 138 100
28 144 123
132 172 118
31 222 160
236 203 189
51 216 193
20 153 186
161 118 11
107 211 203
9 163 178
248 16 164
52 179 158
243 146 223
33 219 225
24 211 16
204 59 197
55 245 107
129 200 151
130 157 40
# -*- coding: utf-8 -*-
# Nearest Centroid Text Color Chooser: Main
import pygame
import os
import numpy as np
import scipy
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D
from random import randint
from centroid import centroid
from matrixToFile import matrixToFile
# init
pygame.init()
# resolution
width = 800
height = 600
# make the pygame window
display = pygame.display.set_mode((width, height))
# set framerate
clock = pygame.time.Clock()
clock.tick(60)
# keyboard pictures and predictor
leftPic = pygame.image.load('left.png')
rightPic = pygame.image.load('right.png')
upPic = pygame.image.load('up.png')
predPic = pygame.image.load('predictor.png')
iconPic = pygame.image.load('icon.png')
# set window icon
pygame.display.set_icon(iconPic)
# title of app
pygame.display.set_caption('Nearest Centroid Text Color Chooser 1.2')
# set fonts
smallfont = pygame.font.SysFont('comicsansms', 25)
mediumfont = pygame.font.SysFont('comicsansms', 50)
largefont = pygame.font.SysFont('comicsansms', 80)
# matrixes of color, index 0 should be deleted
points = np.reshape(np.array([randint(0,255), randint(0,255), randint(0,255)]), (-1, 3))
white = np.reshape(np.array([randint(0,255), randint(0,255), randint(0,255)]), (-1, 3))
black = np.reshape(np.array([randint(0,255), randint(0,255), randint(0,255)]), (-1, 3))
# ////////////////////////////////////////////////////////////
# write on screen
# ////////////////////////////////////////////////////////////
def text(msg,color,font,x,y):
screen_text = font.render(msg, True, color)
display.blit(screen_text, [x, y])
# program loop
while True:
# random color
color = np.array([randint(0,255), randint(0,255), randint(0,255)])
# color background
display.fill((123, 123, 123))
# draw color boxes
pygame.draw.rect(display, color, [width / 3 - 200 / 2, 300, 200, 200])
pygame.draw.rect(display, color, [width / 1.5 - 200 / 2, 300, 200, 200])
pygame.draw.rect(display, (37,37,37), [width / 3 - 220 / 2 - 4, 75, 220, 40])
pygame.draw.rect(display, (37,37,37), [width / 1.5 - 245 / 2 - 4, 75, 245, 40])
# blit pics of arrows
display.blit(leftPic, (width / 3 - 63 / 2, 512))
display.blit(rightPic, (width / 1.5 - 63 / 2, 512))
display.blit(upPic, ((width / 3 - 63 / 2), 125))
# text
text('Text', (255,255,255), largefont, width / 3 - 200 / 2 + 20, 330)
text('Text', (0,0,0), largefont, width / 1.5 - 200 / 2 + 20, 330)
text('Import Training Data', (255,255,255), smallfont, width / 3 - 216 / 2, 75)
text('*Prediction Only*', (255,255,255), smallfont, width / 1.5 - 241 / 2, 75)
text('RGB Color Value: ',(255,255,255), smallfont, 5, 200)
text(str(color), (255,255,255), smallfont, 5, 230)
text('press tab to show data', (255,255,255), smallfont, width - 275, 10)
# make prediction, display prediction above color box
whitecent = centroid(white)
blackcent = centroid(black)
y = np.reshape(color, (-1, 3))
if len(points) > 1:
if scipy.spatial.distance.euclidean(y, whitecent) < scipy.spatial.distance.euclidean(y, blackcent):
display.blit(predPic, (width / 3 - 80 / 2, 200))
elif scipy.spatial.distance.euclidean(y, whitecent) > scipy.spatial.distance.euclidean(y, blackcent):
display.blit(predPic, (width / 1.5 - 80 / 2, 200))
else:
display.blit(predPic, (width / 1.5 - 80 / 2, 200))
display.blit(predPic, (width / 3 - 80 / 2, 200))
# update display
pygame.display.update()
loop = True
while loop:
# check for input to exit
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# check if white or black is best color and append to corresponding list
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
loop = False
if event.key == pygame.K_RIGHT:
loop = False
# check if button presses are down or up, if they are, they save or import data
if event.key == pygame.K_UP:
try:
points = np.loadtxt("points.txt")
white = np.loadtxt("white.txt")
black = np.loadtxt("black.txt")
text("Data Successfully Imported", (255,255,255), smallfont, 10, 10)
pygame.display.update()
except:
text("Data Failed to Be Imported", (255,255,255), mediumfont, 10, 10)
pygame.display.update()
if event.key == pygame.K_TAB:
fig = pyplot.figure()
ax = Axes3D(fig)
ax.set_facecolor((1.0, 0.47, 0.42))
for n in range(1,len(white)):
ax.scatter(white[n,0],white[n,1],white[n,2], color = "white")
for n in range(1,len(black)):
ax.scatter(black[n,0],black[n,1],black[n,2], color = "black")
ax.scatter(color[0], color[1], color[2], color = "blue")
# display centroids
ax.scatter(whitecent[:, 0], whitecent[:, 1], whitecent[:, 2], marker='*', c='white', s=500)
ax.scatter(blackcent[:, 0], blackcent[:, 1], blackcent[:, 2], marker='*', c='black', s=500)
# axis labels
ax.set_xlabel('R', fontsize=15)
ax.set_ylabel('G', fontsize=15)
ax.set_zlabel('B', fontsize=15)
pyplot.show()
\ No newline at end of file
# -*- coding: utf-8 -*-
# Nearest Neighbor Text Color Chooser: Main (Prediction Only)
import pygame
import os
import time
import numpy as np
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D
from random import randint
from matrixToFile import matrixToFile
from nearestneighbor import nn
# init
pygame.init()
# resolution
width = 800
height = 600
# make the pygame window
display = pygame.display.set_mode((width, height))
# set framerate
clock = pygame.time.Clock()
clock.tick(60)
# keyboard pictures and predictor
leftPic = pygame.image.load('left.png')
rightPic = pygame.image.load('right.png')
upPic = pygame.image.load('up.png')
predPic = pygame.image.load('predictor.png')
iconPic = pygame.image.load('icon.png')
# set window icon
pygame.display.set_icon(iconPic)
# title of app
pygame.display.set_caption('Nearest Neighbor Text Color Chooser 1.2.1')
# set fonts
smallfont = pygame.font.SysFont('comicsansms', 25)
mediumfont = pygame.font.SysFont('comicsansms', 50)
largefont = pygame.font.SysFont('comicsansms', 80)
# matrixes of color, index 0 should be deleted
points = np.array([[1000000,1000000,1000000]])
white = np.array([[1000000,1000000,1000000]])
black = np.array([[1000000,1000000,1000000]])
# ////////////////////////////////////////////////////////////
# write on screen
# ////////////////////////////////////////////////////////////
def text(msg,color,font,x,y):
screen_text = font.render(msg, True, color)
display.blit(screen_text, [x, y])
# program loop
while True:
# random color
color = np.array([randint(0,255), randint(0,255), randint(0,255)])
# color background
display.fill((123, 123, 123))
# draw color boxes
pygame.draw.rect(display, color, [width / 3 - 200 / 2, 300, 200, 200])
pygame.draw.rect(display, color, [width / 1.5 - 200 / 2, 300, 200, 200])
pygame.draw.rect(display, (37,37,37), [width / 3 - 220 / 2 - 4, 75, 220, 40])
pygame.draw.rect(display, (37,37,37), [width / 1.5 - 245 / 2 - 4, 75, 245, 40])
# blit pics of arrows
display.blit(leftPic, (width / 3 - 63 / 2, 512))
display.blit(rightPic, (width / 1.5 - 63 / 2, 512))
display.blit(upPic, ((width / 3 - 63 / 2), 125))
# text
text('Text', (255,255,255), largefont, width / 3 - 200 / 2 + 20, 330)
text('Text', (0,0,0), largefont, width / 1.5 - 200 / 2 + 20, 330)
text('Import Training Data', (255,255,255), smallfont, width / 3 - 216 / 2, 75)
text('*Prediction Only*', (255,255,255), smallfont, width / 1.5 - 241 / 2, 75)
text('RGB Color Value: ',(255,255,255), smallfont, 5, 200)
text(str(color), (255,255,255), smallfont, 5, 230)
text('press tab to show data', (255,255,255), smallfont, width - 275, 10)
# make prediction, display prediction above color box
y = np.reshape(color, (-1, 3))
prediction = nn(points, y)
if len(points) > 1:
for n in white:
if np.array_equal(n, prediction):
display.blit(predPic, (width / 3 - 80 / 2, 200))
for n in black:
if np.array_equal(n, prediction):
display.blit(predPic, (width / 1.5 - 80 / 2, 200))
# update display
pygame.display.update()
loop = True
while loop:
# check for input to exit
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# check if white or black is best color and append to corresponding list
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
loop = False
if event.key == pygame.K_RIGHT:
loop = False
# check if button presses are down or up, if they are, they save or import data
if event.key == pygame.K_UP:
try:
points = np.loadtxt("points.txt")
white = np.loadtxt("white.txt")
black = np.loadtxt("black.txt")
text("Data Successfully Imported", (255,255,255), smallfont, 10, 10)
pygame.display.update()
except:
text("Data Failed to Be Imported", (255,255,255), mediumfont, 10, 10)
pygame.display.update()
if event.key == pygame.K_TAB:
fig = pyplot.figure()
ax = Axes3D(fig)
ax.set_facecolor((1.0, 0.47, 0.42))
for n in range(1,len(white)):
ax.scatter(white[n,0],white[n,1],white[n,2], color = "white")
for n in range(1,len(black)):
ax.scatter(black[n,0],black[n,1],black[n,2], color = "black")
ax.scatter(color[0], color[1], color[2], color = "blue")
# axis labels
ax.set_xlabel('R', fontsize=15)
ax.set_ylabel('G', fontsize=15)
ax.set_zlabel('B', fontsize=15)
pyplot.show()
\ No newline at end of file
143 217 243
197 197 252
220 127 40
46 99 170
177 138 100
30 23 39
28 144 123
108 120 82
132 172 118
100 96 75
31 222 160
95 124 81
236 203 189
51 216 193
182 17 82
20 153 186
18 80 67
161 118 11
107 211 203
9 163 178
248 16 164
52 179 158
58 70 93
243 146 223
33 219 225
24 211 16
204 59 197
55 245 107
129 200 151
118 164 114
120 12 25
140 174 1
130 157 40
186 195 62
71 76 55
46 99 170
30 23 39
108 120 82
100 96 75
95 124 81
182 17 82
18 80 67
58 70 93
118 164 114
120 12 25
140 174 1
186 195 62
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment