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

    if model.fault == True:
        fault_cascade = cv2.CascadeClassifier(path_cascade+'fault_cascades.xml')
        faults = fault_cascade.detectMultiScale(gray,scaleFactor=model.scale,minNeighbors=model.neighbors,
            maxSize=(ma,ma),minSize=(mi,mi))
        for (x,y,w,h) in faults:
            cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),6)
            cv2.rectangle(img, (x+ w/2, y + h/2), (x + w/2+1, y + h/2 + 1), (0, 0, 255), 6)
            cv2.putText(img,'fault',(x,y),font,2,(0,0,255),2)

    if model.trap == True:
        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)


# 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

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

    if model.fault == True:
        fault_cascade = cv2.CascadeClassifier(path_cascade+'fault_cascades.xml')
        faults = fault_cascade.detectMultiScale(gray,scaleFactor=model.scale,minNeighbors=model.neighbors,
            maxSize=(ma,ma),minSize=(mi,mi))
        for (x,y,w,h) in faults:
            cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),6)
            cv2.rectangle(img, (x+ w/2, y + h/2), (x + w/2+1, y + h/2 + 1), (0, 0, 255), 6)
            cv2.putText(img,'fault',(x,y),font,2,(0,0,255),2)

    if model.trap == True:
        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)