# Path: website2.0/mainapp/utils.py
# def desectpath(path):
#     basepath = ('/').join(path.split('/')[:-1])
#     filename = path.split('/')[-1]
#     ext = filename.split('.')[-1]
#     fname = ('.').join(filename.split('.')[:-1])
#     return basepath, fname, ext
# 
# def url_to_img(url):
#     req = urllib.urlopen(url)
#     arr = asarray(bytearray(req.read()), dtype=uint8)
#     return imdecode(arr,1)


import cv2
import urllib
from django.conf import settings
from django.core.files import File
from .utils import desectpath, url_to_img
def detectfeatures(path,cascades,scale_fact=1.2,nbrs=3,minsize=00,maxsize=100):

	print "starting detection with reading image..."
	
	img = cv2.imread(settings.BASE_DIR+path)
	gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

	height, width, channels = img.shape
	
	if width > 800:
		newheight = int(800*height/width)
		img = cv2.resize(img,(800, newheight), interpolation = cv2.INTER_AREA)

	mi = minsize * min(height,width) / 100
	ma = max(maxsize * min(height, width) / 100, mi)

	path_cascade  = settings.BASE_DIR+'/mainapp/static/mainapp/assets/haarcascades/'
	font = cv2.FONT_HERSHEY_DUPLEX
	
	if cascades['face'] == True:
		face_cascade = cv2.CascadeClassifier(path_cascade+'haarcascade_frontalface_default.xml')
		faces = face_cascade.detectMultiScale(gray,scaleFactor=scale_fact,minNeighbors=nbrs,maxSize=(ma,ma),minSize=(mi,mi))
		for (x,y,w,h) in faces:
			cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),6)
			cv2.rectangle(img, (x+ w/2, y + h/2), (x + w/2+1, y + h/2 + 1), (255, 0, 0), 6)
			cv2.putText(img,'face',(x,y),font,2,(255,0,0),2)

	if cascades['bottle'] == True:
		bottle_cascade = cv2.CascadeClassifier(path_cascade+'bottle_7stagecascade.xml')
		bottles = bottle_cascade.detectMultiScale(gray,scaleFactor=scale_fact,minNeighbors=nbrs,maxSize=(ma,ma),minSize=(mi,mi))

# Path: website2.0/mainapp/utils.py
# def desectpath(path):
#     basepath = ('/').join(path.split('/')[:-1])
#     filename = path.split('/')[-1]
#     ext = filename.split('.')[-1]
#     fname = ('.').join(filename.split('.')[:-1])
#     return basepath, fname, ext
# 
# def url_to_img(url):
#     req = urllib.urlopen(url)
#     arr = asarray(bytearray(req.read()), dtype=uint8)
#     return imdecode(arr,1)


# Path: website2.0/mainapp/detect.py
import cv2
import urllib
from django.conf import settings
from django.core.files import File
from .utils import desectpath, url_to_img

def detectfeatures(path,cascades,scale_fact=1.2,nbrs=3,minsize=00,maxsize=100):

	print "starting detection with reading image..."
	
	img = cv2.imread(settings.BASE_DIR+path)
	gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

	height, width, channels = img.shape
	
	if width > 800:
		newheight = int(800*height/width)
		img = cv2.resize(img,(800, newheight), interpolation = cv2.INTER_AREA)

	mi = minsize * min(height,width) / 100
	ma = max(maxsize * min(height, width) / 100, mi)

	path_cascade  = settings.BASE_DIR+'/mainapp/static/mainapp/assets/haarcascades/'
	font = cv2.FONT_HERSHEY_DUPLEX
	
	if cascades['face'] == True:
		face_cascade = cv2.CascadeClassifier(path_cascade+'haarcascade_frontalface_default.xml')
		faces = face_cascade.detectMultiScale(gray,scaleFactor=scale_fact,minNeighbors=nbrs,maxSize=(ma,ma),minSize=(mi,mi))
		for (x,y,w,h) in faces:
			cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),6)
			cv2.rectangle(img, (x+ w/2, y + h/2), (x + w/2+1, y + h/2 + 1), (255, 0, 0), 6)
			cv2.putText(img,'face',(x,y),font,2,(255,0,0),2)

	if cascades['bottle'] == True:
		bottle_cascade = cv2.CascadeClassifier(path_cascade+'bottle_7stagecascade.xml')
		bottles = bottle_cascade.detectMultiScale(gray,scaleFactor=scale_fact,minNeighbors=nbrs,maxSize=(ma,ma),minSize=(mi,mi))

		for (x,y,w,h) in bottles: