def detectFace(img_path, threshold): img = cv2.imread(img_path) caffe_img = img.copy() - 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() caffe.set_mode_cpu() 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) return rectangles
def detectFace(img_path,threshold): img = cv2.imread(img_path) caffe_img = img.copy()-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]['cls_score'][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) return rectangles
def detectFace(img_path, threshold): img = cv2.imread(img_path) caffe_img = img.copy() - 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) 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[rectangle[1]:rectangle[3], rectangle[0]: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]) return rectangles
def detectFace(img_path, threshold): fs = cv2.FileStorage(img_path, cv2.FileStorage_READ) img = fs.getNode('depth').mat() fs.release() if img is None: print("Fail to read XML file: " + depth_path) return None origin_h, origin_w = img.shape scales = tools.calculateScales(img) out = [] for scale in scales: hs = int(origin_h * scale) ws = int(origin_w * scale) net_12.blobs['data'].reshape(1, 1, hs, ws) net_12.blobs['data'].data[...] = preprocess(img, (ws, hs)) 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: ", cls_prob.shape roi = out[i]['conv4-2'][0] out_h, out_w = cls_prob.shape # print "out_h: ", out_h # print "out_w: ", out_w 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) rectangles.extend(rectangle) rectangles = tools.NMS(rectangles, 0.7, 'iou') if len(rectangles) == 0: print "rect drop to 0 at 12net" return rectangles rectangles_new = [] for rectangle in rectangles: rectangles_new.append(rectangular2square(rectangle, img)) return rectangles_new
def detectFace(img_path, threshold): # img = cv2.imread(img_path) fs = cv2.FileStorage(img_path, cv2.FileStorage_READ) img = fs.getNode('depth').mat() fs.release() origin_h, origin_w= img.shape scales = tools.calculateScales(img) # scales = [0.1, 0.07] out = [] for scale in scales: hs = int(origin_h * scale) ws = int(origin_w * scale) net_12.blobs['data'].reshape(1, 1, hs, ws) net_12.blobs['data'].data[...] = preprocess(img, (ws, hs)) 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: ", cls_prob.shape roi = out[i]['conv4-2'][0] out_h, out_w = cls_prob.shape print "out_h: ", out_h print "out_w: ", out_w 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: print "rect drop to 0 at 12net" return rectangles doc12 = open('/home/xingduan/YupengHan/inference/saving_docs/12/12doc.txt', 'w') for temp_rectangle in rectangles: doc12.write('%d %d %d %d %f\n' %(temp_rectangle[0], temp_rectangle[1], temp_rectangle[2], temp_rectangle[3], temp_rectangle[4])) # Here might be a problme net_24.blobs['data'].reshape(len(rectangles), 1, 24, 24) crop_number = 0 for rectangle in rectangles: rectangle = rectangular2square(rectangle, img) crop_img = img[int(rectangle[1]):int( rectangle[3]), int(rectangle[0]):int(rectangle[2])] net_24.blobs['data'].data[crop_number] = preprocess(crop_img, (24, 24)) crop_number += 1 # Here might be a problme out = net_24.forward() cls_prob = out['prob1'] roi_prob = out['ip_roi'] rectangles = tools.filter_face_24net( cls_prob, roi_prob, rectangles, origin_w, origin_h, threshold[1]) doc24 = open('/home/xingduan/YupengHan/inference/saving_docs/24/24doc.txt', 'w') for temp_rectangle in rectangles: doc24.write('%d %d %d %d %f\n' %(temp_rectangle[0], temp_rectangle[1], temp_rectangle[2], temp_rectangle [3], temp_rectangle[4])) if len(rectangles) == 0: print "rect drop to 0 at 24net" return rectangles net_48.blobs['data'].reshape(len(rectangles), 1, 48, 48) crop_number = 0 for rectangle in rectangles: rectangle = rectangular2square(rectangle, img) crop_img = img[int(rectangle[1]):int( rectangle[3]), int(rectangle[0]):int(rectangle[2])] net_48.blobs['data'].data[crop_number] = preprocess(crop_img, (48, 48)) crop_number += 1 out = net_48.forward() cls_prob = out['prob1'] roi_prob = out['ip_roi'] rectangles = tools.filter_face_48net( cls_prob, roi_prob, rectangles, origin_w, origin_h, threshold[2]) return rectangles
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
def detectFace(img, threshold): caffe_img = (img.copy() - 127.5) / 127.5 origin_h, origin_w, ch = caffe_img.shape scales = 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)) input = scale_img.reshape(1, *scale_img.shape) ouput = Pnet.predict(input) out.append(ouput) image_num = len(scales) rectangles = [] for i in range(image_num): cls_prob = out[i][0][0][:, :, 1] roi = out[i][1][0] out_h, out_w = cls_prob.shape out_side = max(out_h, out_w) cls_prob = np.swapaxes(cls_prob, 0, 1) roi = np.swapaxes(roi, 0, 2) rectangle = detect_face_12net(cls_prob, roi, out_side, 1 / scales[i], origin_w, origin_h, threshold[0]) rectangles.extend(rectangle) rectangles = NMS(rectangles, 0.7, 'iou') 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 = filter_face_24net(cls_prob, roi_prob, rectangles, origin_w, origin_h, threshold[1]) if len(rectangles) == 0: return rectangles crop_number = 0 predict_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, (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] rectangles = filter_face_48net(cls_prob, roi_prob, pts_prob, rectangles, origin_w, origin_h, threshold[2]) return rectangles