# 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
        trap_cascade = cv2.CascadeClassifier(path_cascade+'trap_3stagecascade.xml')
        traps = trap_cascade.detectMultiScale(gray,scaleFactor=model.scale,minNeighbors=model.neighbors,
            maxSize=(ma,ma),minSize=(mi,mi))
        for (x,y,w,h) in traps:
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,255),6)
            cv2.rectangle(img, (x+ w/2, y + h/2), (x + w/2+1, y + h/2 + 1), (255, 255, 255), 6)
            cv2.putText(img,'trap',(x,y),font,2,(255,255,255),2)

    _,filename,_ = desectpath(model.picture.url)
    tempfile = settings.BASE_DIR + '/media/mainapp/temp.jpg'
    cv2.imwrite(tempfile, img)

    content = urllib.urlretrieve(tempfile)
    model.resultfile.save(filename+'.jpg', File(open(content[0])), save=True)

    return

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)


# 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

        trap_cascade = cv2.CascadeClassifier(path_cascade+'trap_3stagecascade.xml')
        traps = trap_cascade.detectMultiScale(gray,scaleFactor=model.scale,minNeighbors=model.neighbors,
            maxSize=(ma,ma),minSize=(mi,mi))
        for (x,y,w,h) in traps:
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,255),6)
            cv2.rectangle(img, (x+ w/2, y + h/2), (x + w/2+1, y + h/2 + 1), (255, 255, 255), 6)
            cv2.putText(img,'trap',(x,y),font,2,(255,255,255),2)

    _,filename,_ = desectpath(model.picture.url)
    tempfile = settings.BASE_DIR + '/media/mainapp/temp.jpg'
    cv2.imwrite(tempfile, img)

    content = urllib.urlretrieve(tempfile)
    model.resultfile.save(filename+'.jpg', File(open(content[0])), save=True)

    return

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