Last active
September 2, 2018 17:08
-
-
Save nihal-singh/2a1b540fc505e9491d212d82071f190b to your computer and use it in GitHub Desktop.
Image recognition using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def createExamples(): | |
createFile = open('exFile.txt','a') | |
numberEx = range(0,10) #0.1,1.1,2.1 | |
numberVer = range(1,10) #0.1,0.2,0.3 | |
for eachNum in numberEx: | |
for eachVer in numberVer: | |
imgFilePath = 'images/numbers/'+str(eachNum)+'.'+str(eachVer)+'.png' #create image path | |
i = Image.open(imgFilePath) | |
iar = np.array(i) | |
iar = str(iar.tolist()) | |
lineToWrite = str(eachNum) +'::'+iar+'\n' #generating liine to write in file | |
createFile.write(lineToWrite) # writing to file | |
#createExamples() //function call | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def whatNumberIsThis(imgFilePath): | |
matchedAr = [] | |
loadExmp = open('exFile.txt','r').read() | |
loadExmp = loadExmp.split('\n') | |
exImage = Image.open(imgFilePath) | |
eIAr = np.array(exImage) | |
eIAr = threshold(eIAr) | |
eiArL = eIAr.tolist() | |
imgToFind = str(eiArL) | |
for eachExmp in loadExmp: | |
if len(eachExmp) > 3 : | |
eachExmp = eachExmp.split('::') | |
numExmp = eachExmp[0] | |
numAr = eachExmp[1] | |
eachPix = numAr.split('],') | |
eachPix_imgToFind = imgToFind.split('],') | |
x=0; | |
while(x < len(eachPix)): | |
if eachPix[x] == eachPix_imgToFind[x]: | |
matchedAr.append(int(numExmp)) | |
x += 1 | |
c=Counter(matchedAr) | |
print(c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from PIL import Image | |
#(pil is pillow module) | |
i = Image.open('images/dot.png') | |
iar = np.asarray(i) | |
print(iar) | |
''' | |
install python and pip (as pip contains all the required modules like numpy,matplotlib and pillow or pil) | |
* start python using terminal and import numpy matplotlib and PIL | |
* import Images from PIL(run 'from PIL import Image') | |
So each pixel is measured in RBGA, so an example row is [255, 255, 255, 255], what does that mean? | |
This means we're looking at a 256-color image, since programming starts with a 0 rather than a 1. | |
This color means 255 red, 255 green, 255 blue, and then 255 Alpha. | |
this will print an array of RGB-A(red,green,blue and alpha )pixels on the screen. | |
Alpha is a measure of how opaque an image is. The higher the number, the more solid the color is, | |
the lower the number, the more transparent it is. | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import time | |
from PIL import Image | |
def threshold(imageArray): | |
balAr = [] | |
newAr = imageArray | |
from statistics import mean | |
for eachRow in imageArray: | |
for eachPix in eachRow: | |
avg = mean(eachPix[:3]) | |
balAr.append(avg) | |
balance = mean(balAr) | |
for eachRow in newAr: | |
for eachPix in eachRow: | |
if mean(eachPix[:3]) > balance : | |
eachPix[0] = 255 | |
eachPix[1] = 255 | |
eachPix[2] = 255 | |
eachPix[3] = 255 | |
else: | |
eachPix[0] = 0 | |
eachPix[1] = 0 | |
eachPix[2] = 0 | |
eachPix[3] = 255 | |
return newAr | |
i1 = Image.open('images/dot.png') | |
i1_ar = np.array(i1) | |
i1_ar = threshold(i1_ar) | |
i2 = Image.open('images/numbers/0.1.png') | |
i2_ar = np.array(i2) | |
i2_ar = threshold(i2_ar) | |
i3 = Image.open('images/numbers/y0.5.png') | |
i3_ar = np.array(i3) | |
i3_ar = threshold(i3_ar) | |
i4 = Image.open('images/dotndot.png') | |
i4_ar = np.array(i4) | |
i4_ar = threshold(i4_ar) | |
fig = plt.figure() | |
ax1 = plt.subplot2grid((8,6),(0,0),rowspan = 4,colspan = 3) | |
ax2 = plt.subplot2grid((8,6),(4,0),rowspan = 4,colspan = 3) | |
ax3 = plt.subplot2grid((8,6),(0,3),rowspan = 4,colspan = 3) | |
ax4 = plt.subplot2grid((8,6),(4,3),rowspan = 4,colspan = 3) | |
ax1.imshow(i1_ar) | |
ax2.imshow(i2_ar) | |
ax3.imshow(i3_ar) | |
ax4.imshow(i4_ar) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment