from __future__ import print_function
from imutils.object_detection import non_max_suppression
import numpy as np
from PIL import Image
import imutils
import cv2
import os
from convertor import Convertor
import gc
import datetime

class MultiPeopleDetector:

    def __init__(self):
        self.hog = cv2.HOGDescriptor()
        self.hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

    def detect(self, image):

        '''
        open_cv_image = np.array(image)
        open_cv_image = open_cv_image[:, :, ::-1].copy()
        '''


        #image = Convertor.toCv2Image(imageByte)
        #image = Convertor.toCv2Image(imageByte)
        start = datetime.datetime.now()
        (rects, weights) = self.hog.detectMultiScale(image, winStride=(4,4),padding=(8,8), scale=1.05)



        rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
        pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)

        #for (xA, yA, xB, yB) in pick:
        #    cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)

        print("[INFO]: {} original boxes, {} after suppression".format(
    		len(rects), len(pick)))

        print("[INFO] detection took: {}s".format(
	       (datetime.datetime.now() - start).total_seconds()))

        collected = gc.collect()
        #converted = cv2.imencode('.jpg', image)[1].tostring()
        #img_str = cv2.imencode('.jpg', image)[1].tostring()
        return image
