arnaucube
arnaucube/objectImageIdentifierAIPublic Self-hosted repository
objectImageIdentifierAI/cropObjects/detectObject.py
cropObjects/detectObject.pyRaw
 1import numpy as np
 2from PIL import Image
 3import matplotlib.pyplot as plt
 4
 5import os
 6from skimage import io
 7
 8from skimage import color
 9from skimage import filters
10
11
12
13def imgFileToData(path):
14    image = Image.open(path)
15    image_data = np.asarray(image)
16    return image_data
17
18def imgFileToData2(path):
19    img = io.imread(path)
20    return img
21
22def detectObj(image_data):
23    #image_data_blue = image_data[:,:,2]
24    image_data_blue = color.rgb2grey(image_data)
25    #image_data_blue = threshold(image_data)
26
27    median_blue = np.median(image_data_blue)
28    print median_blue
29    median_blue = median_blue - median_blue/1.5
30    print median_blue
31    print image_data_blue
32
33    non_empty_columns = np.where(image_data_blue.min(axis=0)<median_blue)[0]
34    non_empty_rows = np.where(image_data_blue.min(axis=1)<median_blue)[0]
35
36    boundingBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))
37    print boundingBox
38    return boundingBox
39
40def threshold(img):
41    #img = color.rgb2grey(img)
42    #img = img[:,:,2]
43    img = color.rgb2grey(img)
44    thresh = filters.threshold_mean(img)
45    binary = img > thresh
46    return binary
47
48def prova(img):
49    #return color.rgb2grey(img)
50    return img
51
52def crop(image_data, box):
53    return image_data[box[0]:box[1], box[2]:box[3]]
54
55def saveDataToImageFile(data, filename):
56    image = Image.fromarray(data)
57    image.save(filename)