-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8654e6d
Showing
12 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Image recognition using the Hopfield Network | ||
|
||
_Image recognition of The Simpsons using the Hopfield network. An implementation with Python._ | ||
|
||
## Built with 🛠️ | ||
|
||
* [OpenCV-python](https://github.com/opencv/opencv-python) | ||
* [Numpy](https://numpy.org/) | ||
* [Neurolab](https://pypi.org/project/neurolab/) | ||
* [Matplotlib](https://matplotlib.org/) | ||
|
||
## Screenshot 📖 | ||
|
||
![Screenshot](image.PNG) | ||
|
||
--- | ||
⌨️ with ❤️ by [hrypasato](https://github.com/hrypasato) 😊 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Example of use Hopfield Recurrent network | ||
========================================= | ||
Task: Recognition of Simpsons | ||
""" | ||
import cv2, glob | ||
import numpy as np | ||
import neurolab as nl | ||
import matplotlib.pyplot as plt | ||
|
||
IMAGE_SIZE = (48, 48, 3) #size 48x48 pixels | ||
TEST_PATH = './data/test/*.png' | ||
TRAIN_PATH = './data/train/*.png' | ||
|
||
def img2array(name): | ||
img = cv2.imread(name) #read binary image | ||
img = img.flatten() #Return a copy of the array collapsed into one dimension. | ||
img[img == 255] = 1 | ||
return img | ||
|
||
def array2img(array): | ||
array[array == -1] = 0 | ||
array *= 255 # assing white spaces | ||
img = np.reshape(array,IMAGE_SIZE) #transform one dimension array to multidimension array | ||
return img | ||
|
||
def array2float(array): | ||
tmp = np.asfarray(array) | ||
tmp[tmp == 0] = -1 | ||
return tmp | ||
|
||
def show_images(images): | ||
fig = plt.figure() | ||
|
||
ax = fig.add_subplot(1, 2, 1) | ||
imgplot = plt.imshow(images[0]) | ||
ax.set_title('Test') | ||
|
||
ax = fig.add_subplot(1, 2, 2) | ||
imgplot = plt.imshow(images[1]) | ||
ax.set_title('Result') | ||
|
||
plt.show() | ||
|
||
|
||
|
||
target = [] | ||
for file in glob.glob(TRAIN_PATH): | ||
array = img2array(file) | ||
target.append(array) | ||
|
||
target = array2float(target) | ||
|
||
net = nl.net.newhop(target) # Create and train network | ||
|
||
|
||
img_test = img2array('./data/test/lisa1.png') | ||
test = array2float(img_test) | ||
|
||
out = net.sim([test]) #test network | ||
|
||
out_image = array2img(out[0]) #output network image | ||
img_test = array2img(test) #test image | ||
show_images([img_test, out_image]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#extract data from image and save into excel | ||
#Getting Pixel Values | ||
from skimage import io | ||
import pandas as pd | ||
import glob | ||
|
||
TEST_PATH = '../data/test/*.png' | ||
TRAIN_PATH = '../data/train/*.png' | ||
TEST_NAME = 'test.xlsx' | ||
TRAIN_NAME = 'train.xlsx' | ||
|
||
def get_data(path,name): | ||
df = pd.DataFrame() | ||
c = 0 | ||
for file in glob.glob(path): | ||
img = io.imread(file) | ||
df.insert(c,str(c),img.flatten()) | ||
c += 1 | ||
|
||
df.to_excel(name, index=False) | ||
|
||
get_data(TEST_PATH,TEST_NAME) | ||
get_data(TRAIN_PATH,TRAIN_NAME) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#convert images to gray and scale to 48x48 pixels | ||
from skimage import io, color, data | ||
import glob, cv2 | ||
|
||
def new_name(name): | ||
return name.replace('.png','g.png') | ||
|
||
for file in glob.glob('../data/*.png'): | ||
img = cv2.imread(file) | ||
dsize = (48,48) #new size | ||
|
||
gray_img = color.rgb2gray(img) #conver to grayscale | ||
gray_img = cv2.resize(gray_img,dsize, interpolation = cv2.INTER_AREA) | ||
gray_img *= 255 | ||
cv2.imwrite(new_name(file),gray_img)#save image |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
''' | ||
Objetive: remove the background and focus the object | ||
Threshold types | ||
0 - Binary | ||
1 - Binary Inverted | ||
2 - Truncated | ||
3 - Threshold to zero | ||
4 - Threshold to zero inverted | ||
''' | ||
import cv2, glob | ||
|
||
BINARY = 0 | ||
THRESHOLD_VALUE = 141 | ||
TEST_PATH = '../data/test/*.png' | ||
TRAIN_PATH = '../data/train/*.png' | ||
|
||
def new_name(name): | ||
return name.replace('g.png','.png') | ||
|
||
def threshold_image(path): | ||
for file in glob.glob(path): | ||
image = cv2.imread(file) | ||
#apply threshold | ||
_, img1 = cv2.threshold(image,THRESHOLD_VALUE, 255, BINARY) | ||
cv2.imwrite(new_name(file), img1) #save image | ||
|
||
threshold_image(TEST_PATH) | ||
threshold_image(TRAIN_PATH) |