def demo_checkill(): from detect.dlib_face import cv_get_frontal_face_shape_detector, draw_shapes, cv_get_frontal_face_detector predictor_path = "./detect/models/shape_predictor_68_face_landmarks.dat" predictor = cv_get_frontal_face_shape_detector(predictor_path) # img_path = gb.glob(r"Y:/maxiaofang/data/Uface_register_face/*g") img_path = "D:/data/tt/reg/" personNum = 0 lightcount =1 for path in img_path: # print path img = cv2.imread(path) img = cv2.imread(r"./image/0.93_267.jpg") h,w = img.shape[:2] maskw = np.zeros([h, w], dtype = np.uint8) # for some reason some too big picture may could not find face,so the shape will devided shapes, rects = predictor(img) for shape in shapes: if True: lightcount+=1 if lightcount%100==0: print "lightcount = ",lightcount whole =[] for pt in shape[:17]: whole.append(pt) for i in range(22, 27): whole.append(shape[22 + 26 - i]) for i in range(17, 21): whole.append(shape[17 + 21 - i]) wholeface = np.array([[whole]], dtype=np.int32) cv2.fillPoly(maskw, wholeface, 255) wholemask = cv2.bitwise_and(img, img, mask=maskw) wholemask_hsv = cv2.cvtColor(wholemask, cv2.COLOR_BGR2HSV) h,s,v = cv2.split(wholemask_hsv) facearea = np.sum(maskw)/255. mean_v = np.sum(v) * 1.0 / facearea/255. mean_b = np.sum(wholemask[:,:,0]) * 1.0 / facearea mean_g = np.sum(wholemask[:,:,1]) * 1.0 / facearea mean_r = np.sum(wholemask[:,:,2]) * 1.0 / facearea print mean_b,mean_g,mean_r # print "wholemask_v= ",mean_v,mean_v*255 lightcount+=1 # cv2.imshow("wholemask",wholemask) # cv2.imshow("wholemask_hsv",wholemask_hsv) info = "%.f_%.f_%.f.jpg"%(mean_b,mean_g,mean_r) min_light = min(mean_b,mean_g,mean_r) # if min_light<50: # newPath = os.path.join(save_dir,"dark") # elif min_light>200: # newPath = os.path.join(save_dir,"light") # else: # continue # if (not os.path.exists(newPath)): # os.mkdir(newPath) # cv2.imwrite(os.path.join(newPath,info),img) cv2.imshow("img", img) cv2.waitKey(0) personNum +=1 # cv2.waitKey(0) print "lightcount",lightcount
def firstDemo(): from detect.dlib_face import cv_get_frontal_face_shape_detector, draw_shapes, cv_get_frontal_face_detector predictor_path = "./detect/models/shape_predictor_68_face_landmarks.dat" predictor = cv_get_frontal_face_shape_detector(predictor_path) detector = cv_get_frontal_face_detector() img = cv2.imread(r"./image/test-12.jpg") h,w = img.shape[:2] mask = np.ones([h, w,3], dtype = np.uint8) # for some reason some too big picture may could not find face,so the shape will devided shapes, rects = predictor(img) for shape in shapes: keypoint_68= [] x1,y1 = np.min(shape, axis=0) x2,y2 = np.max(shape, axis=0) mean_pixels = (127.5) # _mean_pixels = np.array(mean_pixels).reshape((1, 2)) img_var = img[y1:y2,x1:x2].copy() img_var = cv2.cvtColor(img_var, cv2.COLOR_BGR2HSV) img_varH, img_varS, img_varV = cv2.split(img_var) chip = img[y1:y2,x1:x2] img = img.astype("float32") mask_t = mask.astype("float32") mask_t[y1:y2, x1:x2] = 0.5 img_t = np.multiply(img,mask_t) img_t = img_t.astype("uint8") cv2.imshow("img",img) cv2.imshow("mask",mask_t) cv2.imshow("img_t",img_t) cv2.waitKey(0)
def demo_keypoint68_txt(): from detect.dlib_face import cv_get_frontal_face_shape_detector, draw_shapes, cv_get_frontal_face_detector predictor_path = "./detect/models/shape_predictor_68_face_landmarks.dat" predictor = cv_get_frontal_face_shape_detector(predictor_path) img_path = gb.glob(r"D:\data\face\uface_for_register\images\*g") # img_path = gb.glob(r"./image\*g") histLength = len(img_path) yinyangScore = np.zeros(histLength) personNum = 0 with open("yinyang.txt","w") as fin: for path in img_path: img = cv2.imread(path) h, w = img.shape[:2] mask = np.zeros([h, w], dtype=np.uint8) mask2 = np.zeros([h, w], dtype=np.uint8) # for some reason some too big picture may could not find face,so the shape will devided shapes, rects = predictor(img) for shape in shapes: left = [] for pt in shape[:9]: left.append(pt) left.append(shape[28]) left.append(shape[27]) for i in range(17, 21): left.append(shape[17 + 21 - i]) leftface = np.array([[left]], dtype=np.int32) cv2.fillPoly(mask, leftface, 255) leftmask = cv2.bitwise_and(img, img, mask=mask) right = [] for pt in shape[8:16]: right.append(pt) right.append(shape[26]) right.append(shape[24]) right.append(shape[22]) for pt in shape[27:30]: right.append(pt) right = np.array([[right]], dtype=np.int32) cv2.fillPoly(mask2, right, 255) rightmask = cv2.bitwise_and(img, img, mask=mask2) leftmask_hsv = cv2.cvtColor(leftmask, cv2.COLOR_BGR2HSV) rightmask_hsv = cv2.cvtColor(rightmask, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(leftmask_hsv) h2, s2, v2 = cv2.split(rightmask_hsv) leftarea = np.sum(mask) / 255. rightarea = np.sum(mask2) / 255. leftvrate = np.sum(v) * 1.0 / leftarea rightRate = np.sum(v2) * 1.0 / rightarea # print leftarea, rightarea, "leftRate==", leftvrate, "rightRate==", rightRate minusrate = abs(leftvrate - rightRate) rate = abs(leftvrate / (leftvrate + rightRate) - 0.5) if personNum%100==0: print personNum yinyangScore[personNum] = rate personNum += 1 fin.write("%s_%.3f_%.3f\n"%(path,rate,minusrate)) drawHist(yinyangScore) cv2.waitKey(0)
def demo(): from detect.dlib_face import cv_get_frontal_face_shape_detector, draw_shapes, cv_get_frontal_face_detector predictor_path = "./detect/models/shape_predictor_68_face_landmarks.dat" predictor = cv_get_frontal_face_shape_detector(predictor_path) detector = cv_get_frontal_face_detector() # img = cv2.imread("../image/3.jpg") # img_path = gb.glob(r"Y:\maxiaofang\data\Uface_register_face\*g") # img_path = gb.glob(r"Y:\maxiaofang\data\10000\tight\suc\*g") img_path = gb.glob(r"./reg/0.2-0.8/*g") histLength = len(img_path) for path in img_path: print path img = cv2.imread(path) img = cv2.imread(r"./image/test-12.jpg") h, w = img.shape[:2] mask = np.ones([h, w, 3], dtype=np.uint8) # for some reason some too big picture may could not find face,so the shape will devided shapes, rects = predictor(img) for shape in shapes: keypoint_68 = [] x1, y1 = np.min(shape, axis=0) x2, y2 = np.max(shape, axis=0) mean_pixels = (127.5) # _mean_pixels = np.array(mean_pixels).reshape((1, 2)) img_var = img[y1:y2, x1:x2].copy() img_var = cv2.cvtColor(img_var, cv2.COLOR_BGR2HSV) img_varH, img_varS, img_varV = cv2.split(img_var) chip = img[y1:y2, x1:x2] # mask_t = np.multiply(mask,1) # mask_tmp = # cv2.imshow("chip",chip) img = img.astype("float32") mask_t = mask.astype("float32") mask_t[y1:y2, x1:x2] = 0.5 img_t = np.multiply(img, mask_t) img_t = img_t.astype("uint8") # hsv = cv2.cvtColor(chip, cv2.COLOR_BGR2HSV) # convert it to hsv # h, s, v = cv2.split(hsv) # v += 25 # final_hsv = cv2.merge((h, s, v)) # chip_r = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR) # cv2.imwrite("image_processed.jpg", img) # cv2.imshow("chip_r",chip_r) cv2.imshow("img", img) cv2.imshow("mask", mask_t) cv2.imshow("img_t", img_t) cv2.waitKey(0)
def demo_keypoint68(): from detect.dlib_face import cv_get_frontal_face_shape_detector, draw_shapes, cv_get_frontal_face_detector predictor_path = "./detect/models/shape_predictor_68_face_landmarks.dat" predictor = cv_get_frontal_face_shape_detector(predictor_path) detector = cv_get_frontal_face_detector() # img = cv2.imread("../image/3.jpg") # img_path = gb.glob(r"Y:\maxiaofang\data\Uface_register_face\*g") img_path = gb.glob(r"Y:\maxiaofang\data\10000\tight\suc\*g") img_path = gb.glob(r"./reg/0.2-0.8/light/*g") # img_path = gb.glob(r"./image/*g") # img_path = gb.glob(r"D:\face_lib\CelebA\Img\img_align_celeba_png.7z\img_align_celeba_png/*g") histLength = len(img_path) yinyangScore = np.zeros(histLength) personNum = 0 lightcount =1 for path in img_path: # print path img = cv2.imread(path) # img = cv2.imread(r"Y:\maxiaofang\data\all\light\05B1C54C697240228A6C62111FF6AB0D.jpg") # img = cv2.imread(r"./image/0.93_267.jpg") h,w = img.shape[:2] mask = np.zeros([h, w], dtype = np.uint8) mask2 = np.zeros([h, w], dtype = np.uint8) maskw = np.zeros([h, w], dtype = np.uint8) # for some reason some too big picture may could not find face,so the shape will devided shapes, rects = predictor(img) for shape in shapes: keypoint_68= [] x1,y1 = np.min(shape, axis=0) x2,y2 = np.max(shape, axis=0) mean_pixels = (127.5) # # _mean_pixels = np.array(mean_pixels).reshape((1, 2)) # img_var = img[y1:y2,x1:x2].copy() # img_var = cv2.cvtColor(img_var, cv2.COLOR_BGR2HSV) # img_varH, img_varS, img_varV = cv2.split(img_var) # data = img_varV.astype('float32') # _data = (data - np.array(mean_pixels)) * 1 / 127.5 # cv2.imshow("var",img[y1:y2,x1:x2]) # print "np.var = ",np.var(_data) keypointFive = estimate_5points(shape) if True: whole =[] for pt in shape[:17]: whole.append(pt) for i in range(22, 27): whole.append(shape[22 + 26 - i]) for i in range(17, 21): whole.append(shape[17 + 21 - i]) wholeface = np.array([[whole]], dtype=np.int32) cv2.fillPoly(maskw, wholeface, 255) wholemask = cv2.bitwise_and(img, img, mask=maskw) wholemask_hsv = cv2.cvtColor(wholemask, cv2.COLOR_BGR2HSV) h,s,v = cv2.split(wholemask_hsv) facearea = np.sum(maskw)/255. mean_v = np.sum(v) * 1.0 / facearea/255. mean_b = np.sum(wholemask[:,:,0]) * 1.0 / facearea mean_g = np.sum(wholemask[:,:,1]) * 1.0 / facearea mean_r = np.sum(wholemask[:,:,2]) * 1.0 / facearea print mean_b,mean_g,mean_r # vs= v.astype("float32") # v_tmp = vs*0.2 # v_tmp2 = v_tmp.astype("uint8") print "wholemask_v= ",mean_v,mean_v*255 # img2 = cv2.merge([h,s,v_tmp2]) # if mean_v<0.88 and mean_v>0.2: # if mean_v>0.4: # continue # cv2.imwrite("./reg/%.2f_%d.jpg"%(mean_v,lightcount),img) lightcount+=1 # draw point cloud # cv2.circle(wholemask, (50,50), 20, (170,220,255), thickness=-1) cv2.imshow("wholemask",wholemask) cv2.imshow("wholemask_hsv",wholemask_hsv) # cv2.imshow("v",v) # cv2.imshow("v_tmp",v_tmp2) # cv2.imshow("img2",img2) # for partshape in whole: # cv2.circle(img, tuple(partshape), 2, color, thickness=2) for i in xrange(5): x1= keypointFive[2*i] y1= keypointFive[2*i+1] # cv2.circle(img, ((int)(x1),(int)(y1)), 2, color, thickness=2) # cv2.imshow("img", img) # # cv2.imshow("mask", leftmask) # # cv2.imshow("mask2", rightmask) # cv2.waitKey(0) left = [] for pt in shape[:9]: left.append(pt) left.append(shape[28]) left.append(shape[27]) for i in range(17,21): left.append(shape[17+21-i]) leftface = np.array([[left]],dtype=np.int32) cv2.fillPoly(mask, leftface, 255) leftmask = cv2.bitwise_and(img, img, mask=mask) right =[] for pt in shape[8:16]: right.append(pt) right.append(shape[26]) right.append(shape[24]) right.append(shape[22]) for pt in shape[27:30]: right.append(pt) # right.append(shape[9]) right = np.array([[right]],dtype=np.int32) cv2.fillPoly(mask2, right, 255) rightmask = cv2.bitwise_and(img, img, mask=mask2) leftmask_hsv = cv2.cvtColor(leftmask, cv2.COLOR_BGR2HSV) rightmask_hsv = cv2.cvtColor(rightmask, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(leftmask_hsv) h2, s2, v2 = cv2.split(rightmask_hsv) leftarea = np.sum(mask) / 255. # print " np.sum(mask) ",np.sum(v),"_leftarea:",leftarea rightarea = np.sum(mask2) / 255. leftvrate = np.sum(v) * 1.0 / leftarea rightRate = np.sum(v2) * 1.0 / rightarea # print "avr =",(np.sum(v) * 1.0 +np.sum(v2) * 1.0)/ (leftarea+ rightarea) # print leftarea, rightarea,"leftRate==",leftvrate,"rightRate==",rightRate # print abs(leftvrate-rightRate) rate = abs(leftvrate / (leftvrate + rightRate) - 0.5) # if rate<0.1: # continue # rate = abs(np.sum(v) * 1.0 / (np.sum(v2)+ np.sum(v))-0.5) # print rate # draw point cloud for shape in shapes: count = 0 for pt in shape: # cv2.circle(img, tuple(pt), 2, color, thickness=2) # cv2.putText(img,str(count), tuple(pt), font, 0.3, (255, 0, 0), 1) count += 1 # get average of light cv2.imshow("img", img) # cv2.imshow("mask", v) # cv2.imshow("mask2", v2) cv2.waitKey(0) yinyangScore[personNum] = rate personNum +=1 drawHist(yinyangScore) cv2.waitKey(0) print "lightcount",lightcount
fpath = os.path.join(root, fname) suffix = os.path.splitext(fname)[1].lower() if os.path.isfile(fpath): yield (i, os.path.relpath(fpath, root), 0) i += 1 if __name__ == '__main__': # fid = open("img.pkl","rb") # container = pkl.load(fid) # print container.__len__() # for i,value in enumerate(container): # print i,value from detect.dlib_face import cv_get_frontal_face_shape_detector predictor_path = "./detect/models/shape_predictor_68_face_landmarks.dat" predictor = cv_get_frontal_face_shape_detector(predictor_path) working_dir = r"Y:\maxiaofang\data\10000\tight" # working_dir = r"./image/" imgList = list_image(working_dir, recursive=True) imgList = list(imgList) container = [] count = 0 for i in xrange(len(imgList)): imgName = str(imgList[i][1]) path = os.path.join(working_dir, imgName) img = cv2.imread(path) h, w = img.shape[:2] maskw = np.zeros([h, w], dtype=np.uint8) det_result = predictor(img) shapes, rects = det_result newContainerPart = {}