Esempio n. 1
0
def detectFace(img_path,threshold):
    img = cv2.imread(img_path)
    caffe_img = (img.copy()-127.5)/128
    origin_h,origin_w,ch = caffe_img.shape
    scales = tools.calculateScales(img)
    out = []
    for scale in scales:
        hs = int(origin_h*scale)
        ws = int(origin_w*scale)
        scale_img = cv2.resize(caffe_img,(ws,hs))
        scale_img = np.swapaxes(scale_img, 0, 2)
        net_12.blobs['data'].reshape(1,3,ws,hs)
        net_12.blobs['data'].data[...]=scale_img
        caffe.set_device(0)
        caffe.set_mode_gpu()
        out_ = net_12.forward()
        out.append(out_)
    image_num = len(scales)
    rectangles = []
    for i in range(image_num):    
        cls_prob = out[i]['prob1'][0][1]
        roi      = out[i]['conv4-2'][0]
        out_h,out_w = cls_prob.shape
        out_side = max(out_h,out_w)
        rectangle = tools.detect_face_12net(cls_prob,roi,out_side,1/scales[i],origin_w,origin_h,threshold[0])
        rectangles.extend(rectangle)
    rectangles = tools.NMS(rectangles,0.7,'iou')

    if len(rectangles)==0:
        return rectangles
    net_24.blobs['data'].reshape(len(rectangles),3,24,24)
    crop_number = 0
    for rectangle in rectangles:
        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
        scale_img = cv2.resize(crop_img,(24,24))
        scale_img = np.swapaxes(scale_img, 0, 2)
        net_24.blobs['data'].data[crop_number] =scale_img 
        crop_number += 1
    out = net_24.forward()
    cls_prob = out['prob1']
    roi_prob = out['conv5-2']
    rectangles = tools.filter_face_24net(cls_prob,roi_prob,rectangles,origin_w,origin_h,threshold[1])
    
    if len(rectangles)==0:
        return rectangles
    net_48.blobs['data'].reshape(len(rectangles),3,48,48)
    crop_number = 0
    for rectangle in rectangles:
        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
        scale_img = cv2.resize(crop_img,(48,48))
        scale_img = np.swapaxes(scale_img, 0, 2)
        net_48.blobs['data'].data[crop_number] =scale_img 
        crop_number += 1
    out = net_48.forward()
    cls_prob = out['prob1']
    roi_prob = out['conv6-2']
    pts_prob = out['conv6-3']
    rectangles = tools.filter_face_48net(cls_prob,roi_prob,pts_prob,rectangles,origin_w,origin_h,threshold[2])

    return rectangles
Esempio n. 2
0
 def PNet_(self, caffe_img):
     origin_h, origin_w, ch = caffe_img.shape
     scales = tools.calculateScales(caffe_img, self.min_size)
     out = []
     for scale in scales:
         hs = int(origin_h * scale)
         ws = int(origin_w * scale)
         #print(hs,ws)
         if self.test:
             scale_img = cv2.resize(caffe_img, (ws, hs))
             scale_img = np.swapaxes(scale_img, 0, 2)
             self.net_12.blobs['data'].reshape(1, 3, ws, hs)
         else:
             scale_img = cv2.resize(caffe_img, (ws, hs))
             scale_img = np.transpose(scale_img, (2, 0, 1))
             self.net_12.blobs['data'].reshape(1, 3, hs, ws)
         scale_img = np.asarray(scale_img, dtype=np.float32)
         self.net_12.blobs['data'].data[...] = scale_img
         out_ = self.net_12.forward()
         out.append(out_)
     image_num = len(scales)
     rectangles = []
     for i in range(image_num):
         cls_prob = out[i]['prob1'][0][1]
         if self.test:
             roi = out[i]['conv4-2'][0]
             #roi      = out[i]['conv4_2'][0]
         else:
             roi = out[i]['conv4_2'][0]
         out_h, out_w = cls_prob.shape
         out_side = max(out_h, out_w)
         rectangle = tools.detect_face_12net(cls_prob, roi, out_side,
                                             1 / scales[i], origin_w,
                                             origin_h, self.threshold[0])
         rectangles.extend(rectangle)
     rectangles_box = tools.NMS(rectangles, 0.7, 'iou')
     return rectangles_box
Esempio n. 3
0
def detectFace(img, threshold):

    caffe_img = (img.copy() - 127.5) / 127.5
    origin_h, origin_w, ch = caffe_img.shape
    scales = tools.calculateScales(img)
    out = []
    t0 = time.time()
    # del scales[:4]

    for scale in scales:
        hs = int(origin_h * scale)
        ws = int(origin_w * scale)
        scale_img = cv2.resize(caffe_img, (ws, hs))
        input = scale_img.reshape(1, *scale_img.shape)
        ouput = Pnet.predict(
            input
        )  # .transpose(0,2,1,3) should add, but seems after process is wrong then.
        out.append(ouput)
    image_num = len(scales)
    rectangles = []
    for i in range(image_num):
        cls_prob = out[i][0][
            0][:, :,
               1]  # i = #scale, first 0 select cls score, second 0 = batchnum, alway=0. 1 one hot repr
        roi = out[i][1][0]
        out_h, out_w = cls_prob.shape
        out_side = max(out_h, out_w)
        # print('calculating img scale #:', i)
        cls_prob = np.swapaxes(cls_prob, 0, 1)
        roi = np.swapaxes(roi, 0, 2)
        rectangle = tools.detect_face_12net(cls_prob, roi, out_side,
                                            1 / scales[i], origin_w, origin_h,
                                            threshold[0])
        rectangles.extend(rectangle)
    rectangles = tools.NMS(rectangles, 0.7, 'iou')

    t1 = time.time()
    print('time for 12 net is: ', t1 - t0)

    if len(rectangles) == 0:
        return rectangles

    crop_number = 0
    out = []
    predict_24_batch = []
    for rectangle in rectangles:
        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]),
                             int(rectangle[0]):int(rectangle[2])]
        scale_img = cv2.resize(crop_img, (24, 24))
        predict_24_batch.append(scale_img)
        crop_number += 1

    predict_24_batch = np.array(predict_24_batch)

    out = Rnet.predict(predict_24_batch)

    cls_prob = out[
        0]  # first 0 is to select cls, second batch number, always =0
    cls_prob = np.array(cls_prob)  # convert to numpy
    roi_prob = out[
        1]  # first 0 is to select roi, second batch number, always =0
    roi_prob = np.array(roi_prob)
    rectangles = tools.filter_face_24net(cls_prob, roi_prob, rectangles,
                                         origin_w, origin_h, threshold[1])
    t2 = time.time()
    print('time for 24 net is: ', t2 - t1)

    if len(rectangles) == 0:
        return rectangles

    crop_number = 0
    predict_batch = []
    for rectangle in rectangles:
        # print('calculating net 48 crop_number:', crop_number)
        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]),
                             int(rectangle[0]):int(rectangle[2])]
        scale_img = cv2.resize(crop_img, (48, 48))
        predict_batch.append(scale_img)
        crop_number += 1

    predict_batch = np.array(predict_batch)

    output = Onet.predict(predict_batch)
    cls_prob = output[0]
    roi_prob = output[1]
    pts_prob = output[2]  # index
    # rectangles = tools.filter_face_48net_newdef(cls_prob, roi_prob, pts_prob, rectangles, origin_w, origin_h,
    #                                             threshold[2])
    rectangles = tools.filter_face_48net(cls_prob, roi_prob, pts_prob,
                                         rectangles, origin_w, origin_h,
                                         threshold[2])
    t3 = time.time()
    print('time for 48 net is: ', t3 - t2)

    return rectangles
Esempio n. 4
0
def detectFace(img_path,threshold):
    img = cv2.imread(img_path)
    caffe_img = (img.copy()-127.5)/127.5
    origin_h,origin_w,ch = caffe_img.shape
    scales = tools.calculateScales(img)
    out = []
    for scale in scales:
        hs = int(origin_h*scale)
        ws = int(origin_w*scale)
        scale_img = cv2.resize(caffe_img,(ws,hs))
        scale_img = np.swapaxes(scale_img, 0, 2)
        caffe_img_2 = img.copy()
        scale_img_2 = cv2.resize(caffe_img_2,(ws,hs))
        scale_img_2 = np.swapaxes(scale_img_2, 0, 2)
        scale_img_2 = scale_img_2.astype(np.float32)
        scale_img_2[0,:,:]  = (scale_img_2[0,:,:] - 104.146) / 127.5
        scale_img_2[1,:,:]  = (scale_img_2[1,:,:] - 110.807) / 127.5
        scale_img_2[2,:,:]  = (scale_img_2[2,:,:] - 119.856) / 127.5
        net_12.blobs['data'].reshape(1,3,ws,hs)
        net_12.blobs['data'].data[...]=scale_img_2
        caffe.set_device(0)
        caffe.set_mode_gpu()
        out_ = net_12.forward()
        out.append(out_)
    image_num = len(scales)
    rectangles = []
    for i in range(image_num):    
        cls_prob = out[i]['prob1'][0][1]
#        print(cls_prob.shape)
        roi      = out[i]['conv4-2'][0]
        out_h,out_w = cls_prob.shape
        out_side = max(out_h,out_w)
        #print("mean0:%f" % out[i]['prob1'][0][0].mean()) 
        #print("mean0:%f" % out[i]['prob1'][0][1].mean()) 
        #print("shape:",out[i]['prob1'][0][0].shape)
        #print(len(np.where(out[i]['prob1'][0][0] > threshold[0])[0]))
        #print(len(np.where(out[i]['prob1'][0][0] < threshold[0])[0]))
        ##continue
        #print(cls_prob)
        rectangle = tools.detect_face_12net(cls_prob,roi,out_side,1/scales[i],origin_w,origin_h,threshold[0])
        rectangles.extend(rectangle)
    print("-------------")
    print(len(rectangles))
    rectangles = tools.NMS(rectangles,0.7,'iou')
    print(len(rectangles))

#    if len(rectangles)==0:
#        return rectangles
#    net_24.blobs['data'].reshape(len(rectangles),3,24,24)
#    crop_number = 0
#    for rectangle in rectangles:
#        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
#        scale_img = cv2.resize(crop_img,(24,24))
#        scale_img = np.swapaxes(scale_img, 0, 2)
#        net_24.blobs['data'].data[crop_number] =scale_img 
#        crop_number += 1
#    out = net_24.forward()
#    cls_prob = out['prob1']
#    #roi_prob = out['fc5-2']
#    roi_prob = out['conv5-2']
#    print(len(rectangles))
#    print(cls_prob.shape)
#    rectangles = tools.filter_face_24net(cls_prob,roi_prob,rectangles,origin_w,origin_h,threshold[1])
#    print(len(rectangles))
#
#    
#    if len(rectangles)==0:
#        return rectangles
#    net_48.blobs['data'].reshape(len(rectangles),3,48,48)
#    crop_number = 0
#    for rectangle in rectangles:
#        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
#        scale_img = cv2.resize(crop_img,(48,48))
#        scale_img = np.swapaxes(scale_img, 0, 2)
#        net_48.blobs['data'].data[crop_number] =scale_img 
#        crop_number += 1
#    out = net_48.forward()
#    cls_prob = out['prob1']
#    roi_prob = out['conv6-2']
#    pts_prob = out['conv6-3']
#    #roi_prob = out['fc6-2']
#    #pts_prob = out['fc6-3']
#    #roi_prob = roi_prob - roi_prob
#    #print(roi_prob)
#    print(cls_prob.shape)
#    rectangles = tools.filter_face_48net(cls_prob,roi_prob,pts_prob,rectangles,origin_w,origin_h,threshold[2])
#    rectangles = tools.NMS(rectangles,0.7,'iou')
#    print(len(rectangles))
#
#####################################################
##  tmp code:
#    if len(rectangles)==0:
#        return rectangles
#    net_48.blobs['data'].reshape(len(rectangles),3,48,48)
#    crop_number = 0
#    bbox = "531.0 215.0 574.0 277.0 363.0 219.0 421.0 296.0 251.0 262.0 320.0 354.0 116.0 252.0 196.0 351.0 707.0 248.0 786.0 338.0 870.0 250.0 960.0 376.0 280.0 124.0 308.0 155.0"
#    boxes = np.array(bbox.split(), dtype=np.float32).reshape(-1, 4)
#    for rectangle in rectangles:
#        print("%d %f" % (crop_number,np.max(IoU(rectangle,boxes))))
#        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
#        cv2.imwrite('./crop_file2/%d.jpg' % crop_number,crop_img*127.5 + 127.5)
#        scale_img = cv2.resize(crop_img,(48,48))
#        cv2.imwrite('./crop_file/%d.jpg' % crop_number,scale_img*127.5 + 127.5)
#        scale_img = np.swapaxes(scale_img, 0, 2)
#        net_48.blobs['data'].data[crop_number] =scale_img 
#        crop_number += 1
#    out = net_48.forward()
#    cls_prob = out['prob1']
#    #roi_prob = out['conv6-2']
#    #pts_prob = out['conv6-3']
#    roi_prob = out['fc6-2']
#    pts_prob = out['fc6-3']
#    roi_prob = roi_prob - roi_prob
#    print(roi_prob)
#    print(cls_prob.shape)
#    print(len(rectangles))
#    rectangles = tools.filter_face_48net(cls_prob,roi_prob,pts_prob,rectangles,origin_w,origin_h,threshold[2])
#    rectangles = tools.NMS(rectangles,0.7,'iou')
#    print(len(rectangles))
    return rectangles
def detect_like(img):

    draw = img.copy()

    threshold = [0.19, 0.4, 0.4]
    caffe_img = (img.copy() - 127.5) / 128
    origin_h, origin_w, ch = caffe_img.shape

    out = []
    for scale in scales:
        hs = int(origin_h * scale)
        ws = int(origin_w * scale)
        scale_img = cv2.resize(caffe_img, (ws, hs))
        scale_img = np.swapaxes(scale_img, 0, 2)
        net_12.blobs['data'].reshape(1, 3, ws, hs)
        net_12.blobs['data'].data[...] = scale_img
        caffe.set_device(0)
        caffe.set_mode_gpu()
        out_ = net_12.forward()
        out.append(out_)
    image_num = len(scales)
    rectangles = []
    for i in range(image_num):
        cls_prob = out[i]['prob1'][0][1]
        roi = out[i]['conv4-2'][0]
        out_h, out_w = cls_prob.shape
        out_side = max(out_h, out_w)
        rectangle = tools.detect_face_12net(cls_prob, out_side, 1 / scales[i],
                                            origin_w, origin_h, threshold[0])
        rectangles.extend(rectangle)
    rectangles = tools.NMS(rectangles, 0.7, 'iou')

    #print("like")
    #print(rectangles)

    if len(rectangles) == 0:
        return rectangles, draw
    '''
    net_24.blobs['data'].reshape(len(rectangles),3,24,24)
    crop_number = 0
    for rectangle in rectangles:

        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
        scale_img = cv2.resize(crop_img,(24,24))
        scale_img = np.swapaxes(scale_img, 0, 2)
        net_24.blobs['data'].data[crop_number] =scale_img 
        crop_number += 1
    out = net_24.forward()
    cls_prob = out['prob1']
    roi_prob = out['conv5-2']
    rectangles = tools.filter_face_24net(cls_prob,roi_prob,rectangles,origin_w,origin_h,threshold[1])

    #print("lik1")
    #print(rectangles)

    
    if len(rectangles)==0:
        return rectangles, draw
    net_48.blobs['data'].reshape(len(rectangles),3,48,48)
    crop_number = 0
    for rectangle in rectangles:

        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
        scale_img = cv2.resize(crop_img,(48,48))
        scale_img = np.swapaxes(scale_img, 0, 2)
        net_48.blobs['data'].data[crop_number] =scale_img 
        crop_number += 1
    out = net_48.forward()
    cls_prob = out['prob1']
    roi_prob = out['conv6-2']
    pts_prob = out['conv6-3']
    rectangles = tools.filter_face_48net(cls_prob,roi_prob,pts_prob,rectangles,origin_w,origin_h,threshold[2])
    '''
    #print("like2")
    #print(rectangles)

    for rectangle in rectangles:
        #    cv2.putText(img,str(rectangle[4]),(int(rectangle[0]),int(rectangle[1])),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0))
        cv2.rectangle(draw, (int(rectangle[0]), int(rectangle[1])),
                      (int(rectangle[2]), int(rectangle[3])), (0, 0, 255), 1)

    cv2.imshow('draw', draw)
    cv2.waitKey(1)

    return rectangles, draw
Esempio n. 6
0
    #----laod images---
    img = cv2.imread(image_name)
    #imgPad = tools.PadImg(img)
    imgPad = img.copy()
    cv2.imwrite("imgPad.jpg", imgPad)

    img180 = cv2.flip(imgPad, 0)
    img90 = cv2.transpose(imgPad)
    imgNeg90 = cv2.flip(img90, 0)

    cv2.imwrite("img180.jpg", img180)
    cv2.imwrite("img90.jpg", img90)
    cv2.imwrite("imgNeg90.jpg", imgNeg90)

    rectangles = tools.detect_face_pnet(img, imgPad, pnet, 0.5, device_id=6)
    rectangles = tools.NMS(rectangles, 0.5, 'iou')
    rectangles = rectangles.numpy()

    count = 0
    imgpad_copy = imgPad.copy()
    for a_rectangle in rectangles:
        tmp_image = imgPad.copy()
        x1, y1, x2, y2, cls_score, rotate_score = a_rectangle
        print("cls_score:\t", cls_score)
        print("rotate_score:\t", rotate_score)
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
        cv2.rectangle(tmp_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
        cv2.rectangle(imgpad_copy, (x1, y1), (x2, y2), (0, 255, 0), 2)
        cv2.putText(tmp_image, str(cls_score), (x1, y1 + 5),
                    cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1)
        cv2.putText(tmp_image, str(rotate_score), (x1, y1 + 30),
Esempio n. 7
0
def detectFace(img, threshold):

    caffe_img = (img.copy() - 127.5) / 127.5
    origin_h, origin_w, ch = caffe_img.shape
    scales = tools.calculateScales(img)

    print("scales: ", scales)
    out = []
    t0 = time.time()
    # del scales[:4]

    for scale in scales:
        hs = int(origin_h * scale)
        ws = int(origin_w * scale)
        scale_img = cv2.resize(caffe_img, (ws, hs))
        input = scale_img.reshape(1, *scale_img.shape)
        #print("DEBUG 00: input.shape: ",input.shape)
        ouput = Pnet.predict(
            input
        )  # .transpose(0,2,1,3) should add, but seems after process is wrong then.
        #------------------------------
        # --------- rmaria -----------
        # For every scale, the we have 2 predictions and 4 bbox regressor coordinates

        classifier = Pnet.predict(
            input
        )[0]  # shape (1,outshape1, outshape2,2)     where outshape1 = the output of the image after applying KaoPnet forward pass
        bbox_regressor = Pnet.predict(input)[
            1]  # shape (1, outshape1, outshape2,4)
        #print("DEBUG 0")
        classifier = np.array(classifier)
        bbox_regressor = np.array(bbox_regressor)
        #print("classifier.shape: ",classifier.shape)
        #print("bbox_regressor.shape: ",bbox_regressor.shape)
        #print("------------------------")
        #print("classifier = :",classifier)
        #print("------------------------")
        # ---------------------------

        out.append(ouput)

    image_num = len(scales)

    rectangles = []
    for i in range(image_num):
        cls_prob = out[i][0][
            0][:, :,
               1]  # i = #scale, first 0 select cls score, second 0 = batchnum, alway=0. 1 one hot repr

        #print("cls_prob.shape: ",cls_prob.shape)
        roi = out[i][1][0]
        out_h, out_w = cls_prob.shape

        out_side = max(out_h, out_w)
        # print('calculating img scale #:', i)
        cls_prob = np.swapaxes(cls_prob, 0, 1)
        roi = np.swapaxes(roi, 0, 2)
        rectangle = tools.detect_face_12net(cls_prob, roi, out_side,
                                            1 / scales[i], origin_w, origin_h,
                                            threshold[0])
        rectangles.extend(rectangle)
    rectangles = tools.NMS(rectangles, 0.7, 'iou')

    t1 = time.time()
    print('time for 12 net is: ', t1 - t0)

    if len(rectangles) == 0:
        return rectangles

    crop_number = 0
    out = []
    predict_24_batch = []
    for rectangle in rectangles:
        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]),
                             int(rectangle[0]):int(rectangle[2])]
        scale_img = cv2.resize(crop_img, (24, 24))
        predict_24_batch.append(scale_img)
        crop_number += 1

    predict_24_batch = np.array(predict_24_batch)

    out = Rnet.predict(predict_24_batch)

    cls_prob = out[
        0]  # first 0 is to select cls, second batch number, always =0
    cls_prob = np.array(cls_prob)  # convert to numpy
    roi_prob = out[
        1]  # first 0 is to select roi, second batch number, always =0
    roi_prob = np.array(roi_prob)
    rectangles = tools.filter_face_24net(cls_prob, roi_prob, rectangles,
                                         origin_w, origin_h, threshold[1])
    t2 = time.time()
    print('time for 24 net is: ', t2 - t1)

    if len(rectangles) == 0:
        return rectangles

    crop_number = 0
    predict_batch = []
    for rectangle in rectangles:
        # print('calculating net 48 crop_number:', crop_number)
        crop_img = caffe_img[int(rectangle[1]):int(rectangle[3]),
                             int(rectangle[0]):int(rectangle[2])]
        scale_img = cv2.resize(crop_img, (48, 48))
        predict_batch.append(scale_img)
        crop_number += 1

    predict_batch = np.array(predict_batch)

    if (use_custom_model):
        output = custom_Onet_original(weights_biases_original_model,
                                      predict_batch)
    else:
        output = Onet.predict(predict_batch)

    cls_prob = output[0]
    roi_prob = output[1]
    pts_prob = output[2]  # index
    # rectangles = tools.filter_face_48net_newdef(cls_prob, roi_prob, pts_prob, rectangles, origin_w, origin_h,
    #                                             threshold[2])
    rectangles = tools.filter_face_48net(cls_prob, roi_prob, pts_prob,
                                         rectangles, origin_w, origin_h,
                                         threshold[2])
    t3 = time.time()
    print('time for 48 net is: ', t3 - t2)

    return rectangles