Animal Monitoring Screen Code
Animal Monitoring Screen Code
Animal Monitoring Screen Code
Preprocessing
import cv2
if n==0:
tit="Preprocessing"
image = PIL.Image.open("static/dataset/"+fn)
#new_image = PIL.image.resize((300, 300))
image.save('static/trained/'+fn)
path='static/trained/'+fn
im = Image.open(path)
pfn="p"+fn
path3="static/trained/"+pfn
for idx, box in enumerate(split(im)):
im.crop(box).save(path3.format(idx))
fnn=fn
elif n==1:
tit="Grayscale"
pfn="p"+fn
path3="static/trained/"+pfn
image = Image.open(path3).convert('L')
image.save(path3)
fnn=pfn
elif n==2:
pfn="p"+fn
img = cv2.imread('static/trained/'+pfn)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh =
cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
segment = cv2.subtract(sure_bg,sure_fg)
fname="s"+fn
cv2.imwrite("static/trained/"+fname, segment)
fnn=fname
###Segmentation using RNN
def crfrnn_segmenter(model_def_file, model_file, gpu_device, inputs):
if gpu_device >= 0:
caffe.set_device(gpu_device)
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu()
num_images = len(inputs)
num_channels = inputs[0].shape[2]
assert num_channels == 3, "Unexpected channel count. A 3-channel RGB image is
exptected."
start_time = time.time()
out = net.forward_all(**{net.inputs[0]: caffe_in})
end_time = time.time()
return predictions[0].argmax(axis=0).astype(np.uint8)
image = PILImage.fromarray(np.uint8(input_image))
image = np.array(image)
palette = get_palette(256)
#PIL reads image in the form of RGB, while cv2 reads image in the form of BGR, mean_vec
= [R,G,B]
mean_vec = np.array([123.68, 116.779, 103.939], dtype=np.float32)
mean_vec = mean_vec.reshape(1, 1, 3)
# Pad as necessary
cur_h, cur_w, cur_c = im.shape
pad_h = _MAX_DIM - cur_h
pad_w = _MAX_DIM - cur_w
im = np.pad(im, pad_width=((0, pad_h), (0, pad_w), (0, 0)), mode='constant',
constant_values=0)
# Get predictions
segmentation = crfrnn_segmenter(_MODEL_DEF_FILE, _MODEL_FILE, gpu_device, [im])
segmentation = segmentation[0:cur_h, 0:cur_w]
output_im = PILImage.fromarray(segmentation)
output_im.putpalette(palette)
output_im.save(output_file)
###Feature extraction & Classification
def DCNN_process(self):
train_data_preprocess = ImageDataGenerator(
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_data_preprocess = (1./255)
train = train_data_preprocess.flow_from_directory(
'dataset/training',
target_size = (128,128),
batch_size = 32,
class_mode = 'binary')
test = train_data_preprocess.flow_from_directory(
'dataset/test',
target_size = (128,128),
batch_size = 32,
class_mode = 'binary')
## Initialize the Convolutional Neural Net
# Step 1 - Convolution
# Step 2 - Pooling
cnn.add(Conv2D(32, (3, 3), input_shape = (128, 128, 3), activation = 'relu'))
cnn.add(MaxPooling2D(pool_size = (2, 2)))
# Step 3 - Flattening
cnn.add(Flatten())
history = cnn.fit_generator(train,
steps_per_epoch = 250,
epochs = 25,
validation_data = test,
validation_steps = 2000)
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model Accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
test_image = image.load_img('\\dataset\\', target_size=(128,128))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = cnn.predict(test_image)
print(result)
if result[0][0] == 1:
print('feature extracted and classified')
Testing Phase
if n==0:
tit="Preprocessing"
image = PIL.Image.open("static/trained/"+fn)
#new_image = PIL.image.resize((300, 300))
image.save('static/trained/'+fn)
path='static/trained/'+fn
im = Image.open(path)
pfn="q"+fn
path3="static/trained/"+pfn
for idx, box in enumerate(split(im)):
im.crop(box).save(path3.format(idx))
fnn=fn
elif n==1:
tit="Grayscale"
pfn="q"+fn
path3="static/trained/"+pfn
image = Image.open(path3).convert('L')
image.save(path3)
fnn=pfn
elif n==2:
pfn="q"+fn
img = cv2.imread('static/trained/'+pfn)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh =
cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)
# Labels of Network.
frame = cv2.imread("static/dataset/"+fnn)
frame_resized = cv2.resize(frame,(300,300)) # resize frame for prediction
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2] #Confidence of prediction
if confidence > args.thr: # Filter prediction
class_id = int(detections[0, 0, i, 1]) # Class label
# Object location
xLeftBottom = int(detections[0, 0, i, 3] * cols)
yLeftBottom = int(detections[0, 0, i, 4] * rows)
xRightTop = int(detections[0, 0, i, 5] * cols)
yRightTop = int(detections[0, 0, i, 6] * rows)