mirror of
https://github.com/arnaucube/objectImageIdentifierAI.git
synced 2026-02-07 11:46:55 +01:00
training neural network ok, needs too much ram. Started implementation of the server
This commit is contained in:
4
cropObjects/.gitignore
vendored
Normal file
4
cropObjects/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
object
|
||||
noobject
|
||||
*.jpeg
|
||||
*.png
|
||||
37
cropObjects/README.md
Normal file
37
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
cropObjects/detectObject.py
Normal file
57
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
cropObjects/detectObject.pyc
Normal file
BIN
cropObjects/detectObject.pyc
Normal file
Binary file not shown.
24
cropObjects/detectObjects.py
Normal file
24
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
cropObjects/detectObjects.pyc
Normal file
BIN
cropObjects/detectObjects.pyc
Normal file
Binary file not shown.
42
cropObjects/main.py
Normal file
42
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
|
||||
'''
|
||||
Reference in New Issue
Block a user