mirror of
https://github.com/arnaucube/objectImageIdentifierAI.git
synced 2026-02-07 11:46:55 +01:00
pipeline model chooser working, server predictor working
This commit is contained in:
4
other/cropObjects/.gitignore
vendored
Normal file
4
other/cropObjects/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
object
|
||||
noobject
|
||||
*.jpeg
|
||||
*.png
|
||||
37
other/cropObjects/README.md
Normal file
37
other/cropObjects/README.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# imagesToDataset
|
||||
Gets all the images from the directories 'object' and 'noobject', and puts in a dataset file.
|
||||
The dataset file is a dataset.data file that contains 2 columns:
|
||||
- images arrays of pixels
|
||||
- 0 or 1, depending if is from the 'noobject' or 'object' directory
|
||||
|
||||
|
||||
First, install the libraries.
|
||||
|
||||
### install scikit-learn
|
||||
http://scikit-learn.org/stable/install.html
|
||||
pip install -U scikit-learn
|
||||
|
||||
### install scikit-image
|
||||
http://scikit-image.org/download
|
||||
pip install -U scikit-image
|
||||
|
||||
### install numpy
|
||||
https://www.scipy.org/install.html
|
||||
python -m pip install --upgrade pip
|
||||
pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
|
||||
|
||||
### install Pillow
|
||||
http://pillow.readthedocs.io/en/3.0.x/installation.html
|
||||
(sudo) pip install Pillow
|
||||
|
||||
### install matplotlib
|
||||
https://matplotlib.org/users/installing.html
|
||||
python -mpip install -U pip
|
||||
python -mpip install -U matplotlib
|
||||
|
||||
may need to install python-tk:
|
||||
sudo apt-get install python-tk
|
||||
|
||||
|
||||
## to run
|
||||
python readDataset.py
|
||||
57
other/cropObjects/detectObject.py
Normal file
57
other/cropObjects/detectObject.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
import os
|
||||
from skimage import io
|
||||
|
||||
from skimage import color
|
||||
from skimage import filters
|
||||
|
||||
|
||||
|
||||
def imgFileToData(path):
|
||||
image = Image.open(path)
|
||||
image_data = np.asarray(image)
|
||||
return image_data
|
||||
|
||||
def imgFileToData2(path):
|
||||
img = io.imread(path)
|
||||
return img
|
||||
|
||||
def detectObj(image_data):
|
||||
#image_data_blue = image_data[:,:,2]
|
||||
image_data_blue = color.rgb2grey(image_data)
|
||||
#image_data_blue = threshold(image_data)
|
||||
|
||||
median_blue = np.median(image_data_blue)
|
||||
print median_blue
|
||||
median_blue = median_blue - median_blue/1.5
|
||||
print median_blue
|
||||
print image_data_blue
|
||||
|
||||
non_empty_columns = np.where(image_data_blue.min(axis=0)<median_blue)[0]
|
||||
non_empty_rows = np.where(image_data_blue.min(axis=1)<median_blue)[0]
|
||||
|
||||
boundingBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))
|
||||
print boundingBox
|
||||
return boundingBox
|
||||
|
||||
def threshold(img):
|
||||
#img = color.rgb2grey(img)
|
||||
#img = img[:,:,2]
|
||||
img = color.rgb2grey(img)
|
||||
thresh = filters.threshold_mean(img)
|
||||
binary = img > thresh
|
||||
return binary
|
||||
|
||||
def prova(img):
|
||||
#return color.rgb2grey(img)
|
||||
return img
|
||||
|
||||
def crop(image_data, box):
|
||||
return image_data[box[0]:box[1], box[2]:box[3]]
|
||||
|
||||
def saveDataToImageFile(data, filename):
|
||||
image = Image.fromarray(data)
|
||||
image.save(filename)
|
||||
BIN
other/cropObjects/detectObject.pyc
Normal file
BIN
other/cropObjects/detectObject.pyc
Normal file
Binary file not shown.
24
other/cropObjects/detectObjects.py
Normal file
24
other/cropObjects/detectObjects.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import cv2
|
||||
#reading the image
|
||||
#image = cv2.imread("demo.jpeg")
|
||||
|
||||
def detectObjects(image):
|
||||
edged = cv2.Canny(image, 10, 250)
|
||||
cv2.imshow("Edges", edged)
|
||||
cv2.waitKey(0)
|
||||
|
||||
#applying closing function
|
||||
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
|
||||
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
|
||||
cv2.imshow("Closed", closed)
|
||||
cv2.waitKey(0)
|
||||
|
||||
#finding_contours
|
||||
(_, cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
for c in cnts:
|
||||
peri = cv2.arcLength(c, True)
|
||||
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
|
||||
cv2.drawContours(image, [approx], -1, (0, 255, 0), 2)
|
||||
cv2.imshow("Output", image)
|
||||
cv2.waitKey(0)
|
||||
BIN
other/cropObjects/detectObjects.pyc
Normal file
BIN
other/cropObjects/detectObjects.pyc
Normal file
Binary file not shown.
42
other/cropObjects/main.py
Normal file
42
other/cropObjects/main.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from os import walk
|
||||
import detectObject as do
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
|
||||
#image_data = do.imgFileToData("imgs/34.png")
|
||||
image_data = do.imgFileToData2("object/25.png")
|
||||
|
||||
|
||||
boundingBox = do.detectObj(image_data)
|
||||
image_data = do.prova(image_data)
|
||||
r = do.crop(image_data, boundingBox)
|
||||
|
||||
|
||||
import detectObjects as dos
|
||||
r_copy = r
|
||||
dos.detectObjects(r_copy)
|
||||
#do.saveDataToImageFile(image_data, "out.png")
|
||||
|
||||
#r = do.prova(image_data)
|
||||
|
||||
fig = plt.figure()
|
||||
ax = fig.add_subplot(121)
|
||||
ax.set_title("Original")
|
||||
ax.imshow(image_data)
|
||||
|
||||
ax1 = fig.add_subplot(122)
|
||||
ax1.set_title("Result")
|
||||
ax1.imshow(r)
|
||||
|
||||
plt.show()
|
||||
|
||||
'''
|
||||
f = []
|
||||
for (dirpath, dirnames, filenames) in walk("imgs"):
|
||||
for filename in filenames:
|
||||
print filename
|
||||
image_data = do.imgFileToData("imgs/" + filename)
|
||||
boundingBox = do.detectObj(image_data)
|
||||
print boundingBox
|
||||
'''
|
||||
2
other/imagesToDataset/.gitignore
vendored
Normal file
2
other/imagesToDataset/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
object
|
||||
noobject
|
||||
37
other/imagesToDataset/README.md
Normal file
37
other/imagesToDataset/README.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# imagesToDataset
|
||||
Gets all the images from the directories 'object' and 'noobject', and puts in a dataset file.
|
||||
The dataset file is a dataset.data file that contains 2 columns:
|
||||
- images arrays of pixels
|
||||
- 0 or 1, depending if is from the 'noobject' or 'object' directory
|
||||
|
||||
|
||||
First, install the libraries.
|
||||
|
||||
### install scikit-learn
|
||||
http://scikit-learn.org/stable/install.html
|
||||
pip install -U scikit-learn
|
||||
|
||||
### install scikit-image
|
||||
http://scikit-image.org/download
|
||||
pip install -U scikit-image
|
||||
|
||||
### install numpy
|
||||
https://www.scipy.org/install.html
|
||||
python -m pip install --upgrade pip
|
||||
pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
|
||||
|
||||
### install Pillow
|
||||
http://pillow.readthedocs.io/en/3.0.x/installation.html
|
||||
(sudo) pip install Pillow
|
||||
|
||||
### install matplotlib
|
||||
https://matplotlib.org/users/installing.html
|
||||
python -mpip install -U pip
|
||||
python -mpip install -U matplotlib
|
||||
|
||||
may need to install python-tk:
|
||||
sudo apt-get install python-tk
|
||||
|
||||
|
||||
## to run
|
||||
python readDataset.py
|
||||
BIN
other/imagesToDataset/dataset.npy
Normal file
BIN
other/imagesToDataset/dataset.npy
Normal file
Binary file not shown.
54
other/imagesToDataset/main.py
Normal file
54
other/imagesToDataset/main.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from os import walk
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from PIL import Image, ImageOps
|
||||
import pandas as pd
|
||||
|
||||
#pixels, pixels of the output resizing images
|
||||
size = 100, 100
|
||||
def imgFileToData(path):
|
||||
image = Image.open(path)
|
||||
#resize the image
|
||||
thumb = ImageOps.fit(image, size, Image.ANTIALIAS)
|
||||
image_data = np.asarray(thumb)
|
||||
#.flatten()
|
||||
|
||||
#check if the image had been resized to 100x100. 3pixels * 100width + 100 height = 30000
|
||||
if len(image_data)!=100:
|
||||
print("possible future ERROR!")
|
||||
print("len: " + str(len(image_data)))
|
||||
print("please, delete: " + path)
|
||||
return np.array(list(image_data))
|
||||
|
||||
def getDirectoryFiles(path, imgClass):
|
||||
images = []
|
||||
for (dirpath, dirnames, filenames) in walk(path):
|
||||
for filename in filenames:
|
||||
#print(filename)
|
||||
image_data = imgFileToData(path + "/" + filename)
|
||||
images.append([image_data, imgClass])
|
||||
print(path + "/" + filename)
|
||||
return images
|
||||
|
||||
|
||||
objects = getDirectoryFiles("object", 1)
|
||||
noobjects = getDirectoryFiles("noobject", 0)
|
||||
|
||||
dataset = np.concatenate((objects, noobjects), axis=0)
|
||||
#print(dataset[0])
|
||||
|
||||
np.save('dataset.npy', dataset)
|
||||
'''
|
||||
print(dataset)
|
||||
np.savetxt('dataset.csv', dataset, delimiter=",", fmt='%d')
|
||||
|
||||
pd.set_option('display.max_colwidth', -1)
|
||||
df = pd.DataFrame(dataset)
|
||||
print(df.head())
|
||||
print("aaa")
|
||||
print(df[0][0])
|
||||
print("aaa")
|
||||
pd.set_option('display.max_colwidth', -1)
|
||||
pd.set_option('display.max_columns', None)
|
||||
df.to_csv("dataset.csv", encoding='utf-8', index=False, header=False)
|
||||
'''
|
||||
16
other/imagesToDataset/openDataset.py
Normal file
16
other/imagesToDataset/openDataset.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from random import randint
|
||||
|
||||
|
||||
|
||||
dataset = np.load('dataset.npy')
|
||||
|
||||
n = randint(0, len(dataset))
|
||||
|
||||
plt.plot(111)
|
||||
plt.axis('off')
|
||||
plt.imshow(dataset[n][0])
|
||||
plt.title('class: ' + str(dataset[n][1]))
|
||||
|
||||
plt.show()
|
||||
1
other/serverPredictorOLD/.gitignore
vendored
Normal file
1
other/serverPredictorOLD/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
currentimage.png
|
||||
0
other/serverPredictorOLD/loadNN.py
Normal file
0
other/serverPredictorOLD/loadNN.py
Normal file
63
other/serverPredictorOLD/main.py
Normal file
63
other/serverPredictorOLD/main.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from flask import Flask
|
||||
from flask_restful import Resource, Api, request
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import cv2
|
||||
import io
|
||||
from PIL import Image, ImageOps
|
||||
|
||||
import pickle
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB
|
||||
api = Api(app)
|
||||
|
||||
size = 100, 100
|
||||
|
||||
|
||||
#load Neural Network, generated with nnTrain
|
||||
nn = pickle.load(open('nn.pkl', 'rb'))
|
||||
|
||||
class Predict(Resource):
|
||||
def get(self):
|
||||
message = {'message': 'getted route1'}
|
||||
return message
|
||||
def post(self):
|
||||
filer = request.files['file']
|
||||
#open the uploaded image, and transform to the numpy array
|
||||
filer.save("currentimage.png")
|
||||
image = Image.open("currentimage.png")
|
||||
thumb = ImageOps.fit(image, size, Image.ANTIALIAS)
|
||||
image_data = np.asarray(thumb).flatten()
|
||||
imagetopredict = np.array([image_data])
|
||||
|
||||
#predict the class of the image with the neural network
|
||||
prediction = nn.predict(imagetopredict)
|
||||
print "prediction"
|
||||
print prediction[0][0]
|
||||
if prediction[0][0]==0:
|
||||
result = "noobject"
|
||||
else:
|
||||
result = "object"
|
||||
message = {'class': result}
|
||||
return message
|
||||
|
||||
|
||||
class Route2(Resource):
|
||||
def get(self):
|
||||
return {'message': 'getted route2'}
|
||||
|
||||
|
||||
class Route3(Resource):
|
||||
def get(self):
|
||||
return {'message': 'getted route3'}
|
||||
|
||||
|
||||
api.add_resource(Predict, '/predict')
|
||||
api.add_resource(Route2, '/route2')
|
||||
api.add_resource(Route3, '/route3')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(port='3045')
|
||||
265
other/serverPredictorOLD/nn.pkl
Normal file
265
other/serverPredictorOLD/nn.pkl
Normal file
File diff suppressed because one or more lines are too long
10
other/serverPredictorOLD/test.sh
Normal file
10
other/serverPredictorOLD/test.sh
Normal file
@@ -0,0 +1,10 @@
|
||||
echo "sending img1 to server"
|
||||
echo "server response:"
|
||||
curl -F file=@./test1.png http://127.0.0.1:3045/predict
|
||||
echo ""
|
||||
|
||||
|
||||
echo "sending img2 to server"
|
||||
echo "server response:"
|
||||
curl -F file=@./test2.png http://127.0.0.1:3045/predict
|
||||
echo ""
|
||||
BIN
other/serverPredictorOLD/test1.png
Normal file
BIN
other/serverPredictorOLD/test1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 71 KiB |
BIN
other/serverPredictorOLD/test2.png
Normal file
BIN
other/serverPredictorOLD/test2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 152 KiB |
Reference in New Issue
Block a user