def load_db_detect_train_neg():
    #neg image cropping
    nid = 0
    neg_file_list = os.listdir(etc.neg_dir)
    print "neg data number={}".format(len(neg_file_list))
    neg_db = [0 for n in xrange(len(neg_file_list))]
    neg_img = [0 for n in xrange(len(neg_file_list))]
    for file in neg_file_list:
        img = Image.open(etc.neg_dir + file)
        if len(np.shape(np.asarray(img))) != 3:
            continue
        neg_img[nid] = img
        if (nid + 1 % 2000 == 0):
            print nid + 1, "/", len(neg_file_list), "th neg image cropping..."
        neg_db_line = np.zeros((etc.neg_per_img, etc.dim_12), np.float32)
        for neg_iter in xrange(etc.neg_per_img):

            rad_rand = randint(0, min(img.size[0], img.size[1]) - 1)
            while (rad_rand <= etc.face_minimum):
                rad_rand = randint(0, min(img.size[0], img.size[1]) - 1)

            x_rand = randint(0, img.size[0] - rad_rand - 1)
            y_rand = randint(0, img.size[1] - rad_rand - 1)

            neg_cropped_img = img.crop(
                (x_rand, y_rand, x_rand + rad_rand, y_rand + rad_rand))
            neg_cropped_arr = etc.img2array(neg_cropped_img, etc.img_size_12)

            #for debugging
            #neg_cropped_img.save(etc.neg_dir + str(f_num) + "_" + str(r) + ".jpg")

            neg_db_line[neg_iter, :] = neg_cropped_arr

        neg_db[nid] = neg_db_line
        nid += 1

    neg_db = [elem for elem in neg_db if type(elem) != int]
    neg_db = np.vstack(neg_db)
    neg_img = [elem for elem in neg_img if type(elem) != int]

    print "Neg: " + str(np.shape(neg_db)[0])

    return [neg_db, neg_img]
def load_db_detect_train_neg():
    #neg image cropping
    nid = 0
    neg_file_list = os.listdir(etc.neg_dir)
    print "neg data number={}".format(len(neg_file_list))
    neg_db = [0 for n in xrange(len(neg_file_list))]
    neg_img = [0 for n in xrange(len(neg_file_list))]
    for file in neg_file_list:
        img = Image.open(etc.neg_dir + file)
        if len(np.shape(np.asarray(img))) != 3:
            continue
        neg_img[nid] = img
        if(nid+1%2000==0):
            print nid+1, "/" , len(neg_file_list), "th neg image cropping..."
        neg_db_line = np.zeros((etc.neg_per_img,etc.dim_12), np.float32)
        for neg_iter in xrange(etc.neg_per_img):
                     
            rad_rand = randint(0,min(img.size[0],img.size[1])-1)
            while(rad_rand <= etc.face_minimum):
                rad_rand = randint(0,min(img.size[0],img.size[1])-1)
            
            x_rand = randint(0, img.size[0] - rad_rand - 1)
            y_rand = randint(0, img.size[1] - rad_rand - 1)
            
            neg_cropped_img = img.crop((x_rand, y_rand, x_rand + rad_rand, y_rand + rad_rand))
            neg_cropped_arr = etc.img2array(neg_cropped_img,etc.img_size_12)
            
            #for debugging
            #neg_cropped_img.save(etc.neg_dir + str(f_num) + "_" + str(r) + ".jpg")
                   
            neg_db_line[neg_iter,:] = neg_cropped_arr
        
        neg_db[nid] = neg_db_line
        nid += 1
   
    neg_db = [elem for elem in neg_db if type(elem) != int]
    neg_db = np.vstack(neg_db)
    neg_img = [elem for elem in neg_img if type(elem) != int]

    print "Neg: " + str(np.shape(neg_db)[0])
    
    return [neg_db, neg_img]
Exemple #3
0
     
    for e in xrange(etc.epoch_calib_num[cascade_lv]):
        
        fp_conf_mat = open("./result/conf_mat_cali_" + str(e) + "_.txt","w")  
        epoch_start = time.time()
        average_loss = 0
        
        for b_iter in xrange(etc.batch_iter):
            
            db_id = random.sample(xrange(len(train_db)),etc.mini_batch)
    
            if cascade_lv == 0:
                
                lb[:,:] = 0.0
                for id_,did in enumerate(db_id):
                    patch = etc.img2array(train_db[did][0],etc.img_size_12)
                    calib_idx = train_db[did][1]

                    db_12[id_,:] = patch
                    lb[id_,calib_idx] = 1.0

                zipped = zip(db_12, lb)
                np.random.shuffle(zipped)
                X_12 = [elem[0] for elem in zipped]
                Y = [elem[1] for elem in zipped]
                X_12 = np.asarray(X_12)
                Y = np.reshape(np.asarray(Y),(np.shape(X_12)[0],etc.cali_patt_num))
                                
                average_loss += loss_12.eval(feed_dict = {x_12:X_12, y_target:Y})
                train_step_12.run(feed_dict = {x_12:X_12, y_target:Y})
            elif cascade_lv == 1:
Exemple #4
0
                    db_12[etc.pos_batch:,:] = neg_db[neg_id,:etc.dim_12]
                    lb[:etc.pos_batch,:] = np.ones((etc.pos_batch,1),np.float32)
                    lb[etc.pos_batch:,:] = np.zeros((etc.neg_batch,1),np.float32)
                    zipped = zip(db_12, lb)
                    np.random.shuffle(zipped)
                    X_12 = [elem[0] for elem in zipped]
                    Y = [elem[1] for elem in zipped]
                    X_12 = np.asarray(X_12)
                    Y = np.reshape(np.asarray(Y),(np.shape(X_12)[0],1))
                    average_loss += loss_12.eval(feed_dict = {x_12:X_12, y_target:Y})
                    train_step_12.run(feed_dict = {x_12:X_12, y_target:Y})
                elif cascade_lv == 1:
                    db_12[:etc.pos_batch,:] = pos_db[pos_id,:etc.dim_12]
                    db_24[:etc.pos_batch,:] = pos_db[pos_id,etc.dim_12:etc.dim_12+etc.dim_24]
                    for eid,id_ in enumerate(neg_id):
                        neg_12 = etc.img2array(neg_db[id_],etc.img_size_12)
                        neg_24 = etc.img2array(neg_db[id_],etc.img_size_24)
                        db_12[etc.pos_batch+eid,:] = neg_12
                        db_24[etc.pos_batch+eid,:] = neg_24
                    lb[:etc.pos_batch,:] = np.ones((etc.pos_batch,1),np.float32)
                    lb[etc.pos_batch:,:] = np.zeros((etc.neg_batch,1),np.float32)
                    zipped = zip(db_12, db_24,lb)
                    np.random.shuffle(zipped)
                    X_12 = [elem[0] for elem in zipped]
                    X_24 = [elem[1] for elem in zipped]
                    Y = [elem[2] for elem in zipped]
                    X_12 = np.asarray(X_12)
                    X_24 = np.asarray(X_24)
                    Y = np.reshape(np.asarray(Y),(np.shape(X_12)[0],1))

                    FROM_12 = h_fc1_12.eval(feed_dict = {x_12:X_12})
Exemple #5
0
        
      

        if len(detected_list_scale) > 0:
            detected_list[scale] = detected_list_scale

    detected_list = [elem for elem in detected_list if type(elem) != int]
    result_box = [detected_list[i][j] for i in xrange(len(detected_list)) for j in xrange(len(detected_list[i]))]
    

    if len(result_box) > 0:
        
        #12-calib net
        test_db_12 = np.zeros((len(result_box),etc.dim_12),np.float32)
        for id_,box in enumerate(result_box):
            resized_img_12 = etc.img2array(box[5],etc.img_size_12) 
            test_db_12[id_,:] = resized_img_12

        result = h_fc2_12_cali.eval(feed_dict={x_12_cali: test_db_12})
        result_box = etc.calib_run(result_box,result,img)
        

        #NMS_fast for each scale
        scale_cur = result_box[0][6]
        scale_box = []
        final_box = []
        for id_,box in enumerate(result_box):
            if box[6] == scale_cur:
                scale_box.append(box)
            if box[6] != scale_cur or id_ == len(result_box)-1:
                scale_box.sort(key=lambda x :x[4])
     X_12 = [elem[0] for elem in zipped]
     Y = [elem[1] for elem in zipped]
     X_12 = np.asarray(X_12)
     Y = np.reshape(np.asarray(Y), (np.shape(X_12)[0], 1))
     average_loss += loss_12.eval(feed_dict={
         x_12: X_12,
         y_target: Y
     })
     train_step_12.run(feed_dict={x_12: X_12, y_target: Y})
 elif cascade_lv == 1:
     db_12[:etc.pos_batch, :] = pos_db[pos_id, :etc.dim_12]
     db_24[:etc.pos_batch, :] = pos_db[pos_id,
                                       etc.dim_12:etc.dim_12 +
                                       etc.dim_24]
     for eid, id_ in enumerate(neg_id):
         neg_12 = etc.img2array(neg_db[id_], etc.img_size_12)
         neg_24 = etc.img2array(neg_db[id_], etc.img_size_24)
         db_12[etc.pos_batch + eid, :] = neg_12
         db_24[etc.pos_batch + eid, :] = neg_24
     lb[:etc.pos_batch, :] = np.ones((etc.pos_batch, 1),
                                     np.float32)
     lb[etc.pos_batch:, :] = np.zeros((etc.neg_batch, 1),
                                      np.float32)
     zipped = zip(db_12, db_24, lb)
     np.random.shuffle(zipped)
     X_12 = [elem[0] for elem in zipped]
     X_24 = [elem[1] for elem in zipped]
     Y = [elem[2] for elem in zipped]
     X_12 = np.asarray(X_12)
     X_24 = np.asarray(X_24)
     Y = np.reshape(np.asarray(Y), (np.shape(X_12)[0], 1))
Exemple #7
0
        arr = np.asarray(img)
        img = np.zeros((img.size[1], img.size[0], etc.input_channel), np.uint8)
        img[:, :, 0] = arr
        img[:, :, 1] = arr
        img[:, :, 2] = arr
        img = Image.fromarray(img)

    #12-net
    result_box = etc.slidingW_Test(img, thr_12, x_12, h_fc2_12)

    if len(result_box) > 0:

        #12-calib net
        test_db_12 = np.zeros((len(result_box), etc.dim_12), np.float32)
        for id_, box in enumerate(result_box):
            resized_img_12 = etc.img2array(box[5], etc.img_size_12)
            test_db_12[id_, :] = resized_img_12

        result = h_fc2_12_cali.eval(feed_dict={x_12_cali: test_db_12})
        result_box = etc.calib_run(result_box, result, img)

        #NMS_fast for each scale
        scale_cur = result_box[0][6]
        scale_box = []
        final_box = []
        for id_, box in enumerate(result_box):
            if box[6] == scale_cur:
                scale_box.append(box)
            if box[6] != scale_cur or id_ == len(result_box) - 1:
                scale_box.sort(key=lambda x: x[4])
                scale_box.reverse()
def load_db_detect_train_12_net_pos():
   
    print "Loading training db..."

    #annot_dir = etc.db_dir + "aflw/data/"
    annot_dir ="/home/gavinpan/workspace/dataset/aflw/data/"
    #annot_fp = open(annot_dir + "annot", "r")
    annot_fp = open("face_rect.txt", "r")
    raw_data = annot_fp.readlines()
    annot_fp.close()
    #pos image cropping
    print "pos data number={}".format(len(raw_data)-1)
    pos_db = [0 for _ in xrange(len(raw_data)-1)]
    for i,line in enumerate(raw_data):
      	if(not line.startswith('#')):
            if(i%5000==0):
                print i, "/", len(raw_data), "th pos image cropping..."
            parsed_line = line.split()
            imagePath = parsed_line[1].strip()
            imagePath = annot_dir + imagePath
            img = Image.open(imagePath)
            #check if not RGB
            if len(np.shape(np.asarray(img))) != 3 or np.shape(np.asarray(img))[2] != etc.input_channel:
                continue
            
            #pos_db_line = np.zeros((2,etc.dim_12 + etc.dim_24 + etc.dim_48), np.float32)
            pos_db_line = np.zeros((2,etc.dim_12), np.float32)

            #left = int(parsed_line[1])
            #upper = int(parsed_line[2])
            #right = int(parsed_line[3]) + left
            #lower = int(parsed_line[4]) + upper

            left = int(parsed_line[2])
            upper = int(parsed_line[3])
            right = int(parsed_line[4]) + left
            lower = int(parsed_line[5]) + upper
       
            if right >= img.size[0]:
                right = img.size[0]-1
            if lower >= img.size[1]:
                lower = img.size[1]-1
            #crop
            cropped_img = img.crop((left, upper, right, lower))
            #flip
            flipped_img = cropped_img.transpose(Image.FLIP_LEFT_RIGHT)

            cropped_arr_12 = etc.img2array(cropped_img,etc.img_size_12)
            flipped_arr_12 = etc.img2array(flipped_img,etc.img_size_12)
            '''
            cropped_arr_24 = etc.img2array(cropped_img,etc.img_size_24)
            flipped_arr_24 = etc.img2array(flipped_img,etc.img_size_24)
            cropped_arr_48 = etc.img2array(cropped_img,etc.img_size_48)
            flipped_arr_48 = etc.img2array(flipped_img,etc.img_size_48)
            '''
            #for debugging
            #cropped_img.save(etc.pos_dir + str(i)  + ".jpg")
        
            pos_db_line[0,:etc.dim_12] = cropped_arr_12
            '''
            pos_db_line[0,etc.dim_12:etc.dim_12+etc.dim_24] = cropped_arr_24
            pos_db_line[0,etc.dim_12+etc.dim_24:] = cropped_arr_48
            '''
            pos_db_line[1,:etc.dim_12] = flipped_arr_12
            '''
            pos_db_line[1,etc.dim_12:etc.dim_12+etc.dim_24] = flipped_arr_24
            pos_db_line[1,etc.dim_12+etc.dim_24:] = flipped_arr_48
            '''
            pos_db[i-1] = pos_db_line
    #end for load raw data
    pos_db = [elem for elem in pos_db if type(elem) != int]    
    pos_db = np.vstack(pos_db) 
    print "Pos: " + str(np.shape(pos_db)[0])
    return pos_db
def load_db_detect_train():
   
    print "Loading training db..."

    #annot_dir = etc.db_dir + "aflw/data/"
    annot_dir ="/home/gavinpan/workspace/dataset/aflw/data/"
    #annot_fp = open(annot_dir + "annot", "r")
    annot_fp = open("face_rect.txt", "r")
    raw_data = annot_fp.readlines()
    annot_fp.close()
    #pos image cropping
    print "pos data number={}".format(len(raw_data)-1)
    pos_db = [0 for _ in xrange(len(raw_data)-1)]
    for i,line in enumerate(raw_data):
      	if(not line.startswith('#')):
            if(i%5000==0):
                print i, "/", len(raw_data), "th pos image cropping..."
            parsed_line = line.split()
            imagePath = parsed_line[1].strip()
            imagePath = annot_dir + imagePath
            img = Image.open(imagePath)
            #check if not RGB
            if len(np.shape(np.asarray(img))) != 3 or np.shape(np.asarray(img))[2] != etc.input_channel:
                continue
            pos_db_line = np.zeros((2,etc.dim_12 + etc.dim_24 + etc.dim_48), np.float32)
            #left = int(parsed_line[1])
            #upper = int(parsed_line[2])
            #right = int(parsed_line[3]) + left
            #lower = int(parsed_line[4]) + upper

            left = int(parsed_line[2])
            upper = int(parsed_line[3])
            right = int(parsed_line[4]) + left
            lower = int(parsed_line[5]) + upper
       
            if right >= img.size[0]:
                right = img.size[0]-1
            if lower >= img.size[1]:
                lower = img.size[1]-1
            #crop
            cropped_img = img.crop((left, upper, right, lower))
            #flip
            flipped_img = cropped_img.transpose(Image.FLIP_LEFT_RIGHT)

            cropped_arr_12 = etc.img2array(cropped_img,etc.img_size_12)
            flipped_arr_12 = etc.img2array(flipped_img,etc.img_size_12)
            cropped_arr_24 = etc.img2array(cropped_img,etc.img_size_24)
            flipped_arr_24 = etc.img2array(flipped_img,etc.img_size_24)
            cropped_arr_48 = etc.img2array(cropped_img,etc.img_size_48)
            flipped_arr_48 = etc.img2array(flipped_img,etc.img_size_48)
        
            #for debugging
            #cropped_img.save(etc.pos_dir + str(i)  + ".jpg")
        
            pos_db_line[0,:etc.dim_12] = cropped_arr_12
            pos_db_line[0,etc.dim_12:etc.dim_12+etc.dim_24] = cropped_arr_24
            pos_db_line[0,etc.dim_12+etc.dim_24:] = cropped_arr_48
        
            pos_db_line[1,:etc.dim_12] = flipped_arr_12
            pos_db_line[1,etc.dim_12:etc.dim_12+etc.dim_24] = flipped_arr_24
            pos_db_line[1,etc.dim_12+etc.dim_24:] = flipped_arr_48
        
            pos_db[i-1] = pos_db_line
    #end for load raw data
    pos_db = [elem for elem in pos_db if type(elem) != int]    
    pos_db = np.vstack(pos_db) 

    #neg image cropping
    nid = 0
    neg_file_list = os.listdir(etc.neg_dir)
    neg_db = [0 for n in xrange(len(neg_file_list))]
    neg_img = [0 for n in xrange(len(neg_file_list))]
    for file in neg_file_list:
        img = Image.open(etc.neg_dir + file)
        if len(np.shape(np.asarray(img))) != 3:
            continue
        neg_img[nid] = img
        print nid+1, "/" , len(neg_file_list), "th neg image cropping..."
        neg_db_line = np.zeros((etc.neg_per_img,etc.dim_12), np.float32)
        for neg_iter in xrange(etc.neg_per_img):
                     
            rad_rand = randint(0,min(img.size[0],img.size[1])-1)
            while(rad_rand <= etc.face_minimum):
                rad_rand = randint(0,min(img.size[0],img.size[1])-1)
            
            x_rand = randint(0, img.size[0] - rad_rand - 1)
            y_rand = randint(0, img.size[1] - rad_rand - 1)
            
            neg_cropped_img = img.crop((x_rand, y_rand, x_rand + rad_rand, y_rand + rad_rand))
            neg_cropped_arr = etc.img2array(neg_cropped_img,etc.img_size_12)
            
            #for debugging
            #neg_cropped_img.save(etc.neg_dir + str(f_num) + "_" + str(r) + ".jpg")
                   
            neg_db_line[neg_iter,:] = neg_cropped_arr
        
        neg_db[nid] = neg_db_line
        nid += 1
   
    neg_db = [elem for elem in neg_db if type(elem) != int]
    neg_db = np.vstack(neg_db)
    neg_img = [elem for elem in neg_img if type(elem) != int]
     
    print "Pos: " + str(np.shape(pos_db)[0])
    print "Neg: " + str(np.shape(neg_db)[0])
    
    return [pos_db, neg_db, neg_img]
def load_db_detect_train():

    print "Loading training db..."

    annot_dir = etc.db_dir + "AFLW/aflw/data/"
    annot_fp = open(annot_dir + "annot", "r")
    raw_data = annot_fp.readlines()

    #pos image cropping
    pos_db = [0 for _ in xrange(len(raw_data))]
    for i, line in enumerate(raw_data):
        print i, "/", len(raw_data), "th pos image cropping..."
        parsed_line = line.split(',')
        img = Image.open(etc.pos_dir + parsed_line[0])
        #check if not RGB
        if len(np.shape(np.asarray(img))) != 3 or np.shape(
                np.asarray(img))[2] != etc.input_channel:
            continue
        pos_db_line = np.zeros((2, etc.dim_12 + etc.dim_24 + etc.dim_48),
                               np.float32)
        left = int(parsed_line[1])
        upper = int(parsed_line[2])
        right = int(parsed_line[3]) + left
        lower = int(parsed_line[4]) + upper

        if right >= img.size[0]:
            right = img.size[0] - 1
        if lower >= img.size[1]:
            lower = img.size[1] - 1

        cropped_img = img.crop((left, upper, right, lower))
        flipped_img = cropped_img.transpose(Image.FLIP_LEFT_RIGHT)

        cropped_arr_12 = etc.img2array(cropped_img, etc.img_size_12)
        flipped_arr_12 = etc.img2array(flipped_img, etc.img_size_12)
        cropped_arr_24 = etc.img2array(cropped_img, etc.img_size_24)
        flipped_arr_24 = etc.img2array(flipped_img, etc.img_size_24)
        cropped_arr_48 = etc.img2array(cropped_img, etc.img_size_48)
        flipped_arr_48 = etc.img2array(flipped_img, etc.img_size_48)

        #for debugging
        #cropped_img.save(etc.pos_dir + str(i)  + ".jpg")

        pos_db_line[0, :etc.dim_12] = cropped_arr_12
        pos_db_line[0, etc.dim_12:etc.dim_12 + etc.dim_24] = cropped_arr_24
        pos_db_line[0, etc.dim_12 + etc.dim_24:] = cropped_arr_48

        pos_db_line[1, :etc.dim_12] = flipped_arr_12
        pos_db_line[1, etc.dim_12:etc.dim_12 + etc.dim_24] = flipped_arr_24
        pos_db_line[1, etc.dim_12 + etc.dim_24:] = flipped_arr_48

        pos_db[i] = pos_db_line

    pos_db = [elem for elem in pos_db if type(elem) != int]
    pos_db = np.vstack(pos_db)

    #neg image cropping
    nid = 0
    neg_file_list = os.listdir(etc.neg_dir)
    neg_db = [0 for n in xrange(len(neg_file_list))]
    neg_img = [0 for n in xrange(len(neg_file_list))]
    for file in neg_file_list:
        img = Image.open(etc.neg_dir + file)
        if len(np.shape(np.asarray(img))) != 3:
            continue
        neg_img[nid] = img
        print nid + 1, "/", len(neg_file_list), "th neg image cropping..."
        neg_db_line = np.zeros((etc.neg_per_img, etc.dim_12), np.float32)
        for neg_iter in xrange(etc.neg_per_img):

            rad_rand = randint(0, min(img.size[0], img.size[1]) - 1)
            while (rad_rand <= etc.face_minimum):
                rad_rand = randint(0, min(img.size[0], img.size[1]) - 1)

            x_rand = randint(0, img.size[0] - rad_rand - 1)
            y_rand = randint(0, img.size[1] - rad_rand - 1)

            neg_cropped_img = img.crop(
                (x_rand, y_rand, x_rand + rad_rand, y_rand + rad_rand))
            neg_cropped_arr = etc.img2array(neg_cropped_img, etc.img_size_12)

            #for debugging
            #neg_cropped_img.save(etc.neg_dir + str(f_num) + "_" + str(r) + ".jpg")

            neg_db_line[neg_iter, :] = neg_cropped_arr

        neg_db[nid] = neg_db_line
        nid += 1

    neg_db = [elem for elem in neg_db if type(elem) != int]
    neg_db = np.vstack(neg_db)
    neg_img = [elem for elem in neg_img if type(elem) != int]

    print "Pos: " + str(np.shape(pos_db)[0])
    print "Neg: " + str(np.shape(neg_db)[0])

    return [pos_db, neg_db, neg_img]
Exemple #11
0
    for e in xrange(etc.epoch_calib_num[cascade_lv]):

        fp_conf_mat = open("./result/conf_mat_cali_" + str(e) + "_.txt", "w")
        epoch_start = time.time()
        average_loss = 0

        for b_iter in xrange(etc.batch_iter):

            db_id = random.sample(xrange(len(train_db)), etc.mini_batch)

            if cascade_lv == 0:

                lb[:, :] = 0.0
                for id_, did in enumerate(db_id):
                    patch = etc.img2array(train_db[did][0], etc.img_size_12)
                    calib_idx = train_db[did][1]

                    db_12[id_, :] = patch
                    lb[id_, calib_idx] = 1.0

                zipped = zip(db_12, lb)
                np.random.shuffle(zipped)
                X_12 = [elem[0] for elem in zipped]
                Y = [elem[1] for elem in zipped]
                X_12 = np.asarray(X_12)
                Y = np.reshape(np.asarray(Y),
                               (np.shape(X_12)[0], etc.cali_patt_num))

                average_loss += loss_12.eval(feed_dict={
                    x_12: X_12,
def load_db_detect_train_12_net_pos():

    print "Loading training db..."

    #annot_dir = etc.db_dir + "aflw/data/"
    annot_dir = "/home/gavinpan/workspace/dataset/aflw/data/"
    #annot_fp = open(annot_dir + "annot", "r")
    annot_fp = open("face_rect.txt", "r")
    raw_data = annot_fp.readlines()
    annot_fp.close()
    #pos image cropping
    print "pos data number={}".format(len(raw_data) - 1)
    pos_db = [0 for _ in xrange(len(raw_data) - 1)]
    for i, line in enumerate(raw_data):
        if (not line.startswith('#')):
            if (i % 5000 == 0):
                print i, "/", len(raw_data), "th pos image cropping..."
            parsed_line = line.split()
            imagePath = parsed_line[1].strip()
            imagePath = annot_dir + imagePath
            img = Image.open(imagePath)
            #check if not RGB
            if len(np.shape(np.asarray(img))) != 3 or np.shape(
                    np.asarray(img))[2] != etc.input_channel:
                continue

            #pos_db_line = np.zeros((2,etc.dim_12 + etc.dim_24 + etc.dim_48), np.float32)
            pos_db_line = np.zeros((2, etc.dim_12), np.float32)

            #left = int(parsed_line[1])
            #upper = int(parsed_line[2])
            #right = int(parsed_line[3]) + left
            #lower = int(parsed_line[4]) + upper

            left = int(parsed_line[2])
            upper = int(parsed_line[3])
            right = int(parsed_line[4]) + left
            lower = int(parsed_line[5]) + upper

            if right >= img.size[0]:
                right = img.size[0] - 1
            if lower >= img.size[1]:
                lower = img.size[1] - 1
            #crop
            cropped_img = img.crop((left, upper, right, lower))
            #flip
            flipped_img = cropped_img.transpose(Image.FLIP_LEFT_RIGHT)

            cropped_arr_12 = etc.img2array(cropped_img, etc.img_size_12)
            flipped_arr_12 = etc.img2array(flipped_img, etc.img_size_12)
            '''
            cropped_arr_24 = etc.img2array(cropped_img,etc.img_size_24)
            flipped_arr_24 = etc.img2array(flipped_img,etc.img_size_24)
            cropped_arr_48 = etc.img2array(cropped_img,etc.img_size_48)
            flipped_arr_48 = etc.img2array(flipped_img,etc.img_size_48)
            '''
            #for debugging
            #cropped_img.save(etc.pos_dir + str(i)  + ".jpg")

            pos_db_line[0, :etc.dim_12] = cropped_arr_12
            '''
            pos_db_line[0,etc.dim_12:etc.dim_12+etc.dim_24] = cropped_arr_24
            pos_db_line[0,etc.dim_12+etc.dim_24:] = cropped_arr_48
            '''
            pos_db_line[1, :etc.dim_12] = flipped_arr_12
            '''
            pos_db_line[1,etc.dim_12:etc.dim_12+etc.dim_24] = flipped_arr_24
            pos_db_line[1,etc.dim_12+etc.dim_24:] = flipped_arr_48
            '''
            pos_db[i - 1] = pos_db_line
    #end for load raw data
    pos_db = [elem for elem in pos_db if type(elem) != int]
    pos_db = np.vstack(pos_db)
    print "Pos: " + str(np.shape(pos_db)[0])
    return pos_db
Exemple #13
0
def procimg(img):
    if len(np.shape(np.asarray(img))) < 3:
        arr = np.asarray(img)
        img = np.zeros((img.size[1], img.size[0], etc.input_channel), np.uint8)
        img[:, :, 0] = arr
        img[:, :, 1] = arr
        img[:, :, 2] = arr
        img = Image.fromarray(img)

    #12-net
    result_box = etc.slidingW_Test(img, thr_12, x_12, h_fc2_12)

    if len(result_box) > 0:

        #12-calib net
        test_db_12 = np.zeros((len(result_box), etc.dim_12), np.float32)
        for id_, box in enumerate(result_box):
            resized_img_12 = etc.img2array(box[5], etc.img_size_12)
            test_db_12[id_, :] = resized_img_12

        result = h_fc2_12_cali.eval(feed_dict={x_12_cali: test_db_12})
        result_box = etc.calib_run(result_box, result, img)

        #NMS_fast for each scale
        scale_cur = result_box[0][6]
        scale_box = []
        final_box = []
        for id_, box in enumerate(result_box):
            if box[6] == scale_cur:
                scale_box.append(box)
            if box[6] != scale_cur or id_ == len(result_box) - 1:
                scale_box.sort(key=lambda x: x[4])
                scale_box.reverse()
                supp_box_id = etc.NMS_fast(scale_box)
                final_box += [scale_box[i] for i in supp_box_id]
                scale_cur += 1
                scale_box = [box]
        result_box = final_box
        final_box = []

    if len(result_box) > 0:
        #24-net
        test_db_12 = np.zeros((len(result_box), etc.dim_12), np.float32)
        test_db_24 = np.zeros((len(result_box), etc.dim_24), np.float32)
        for id_, box in enumerate(result_box):
            resized_img_12 = etc.img2array(box[5], etc.img_size_12)
            resized_img_24 = etc.img2array(box[5], etc.img_size_24)

            test_db_12[id_, :] = resized_img_12
            test_db_24[id_, :] = resized_img_24

        final_box = []
        test_db = np.empty((0, etc.dim_24), np.float32)
        for tit in xrange(len(test_db_12) / etc.test_bench_num + 1):
            if tit == len(test_db_12) / etc.test_bench_num:
                test_12 = test_db_12[tit * etc.test_bench_num:, :]
                test_24 = test_db_24[tit * etc.test_bench_num:, :]
                box_tmp = result_box[tit * etc.test_bench_num:]
            else:
                test_12 = test_db_12[tit * etc.test_bench_num:(tit + 1) *
                                     etc.test_bench_num, :]
                test_24 = test_db_24[tit * etc.test_bench_num:(tit + 1) *
                                     etc.test_bench_num, :]
                box_tmp = result_box[tit * etc.test_bench_num:(tit + 1) *
                                     etc.test_bench_num]

            result = h_fc2_24.eval(feed_dict={x_12: test_12, x_24: test_24})
            result_id = np.where(result > thr_24)[0]
            test_db = np.append(test_db, test_24[result_id, :], axis=0)
            final_box += [box_tmp[i] for i in result_id]
        result_box = final_box
        final_box = []

    if len(result_box) > 0:
        #24-net_calib
        test_db_24 = test_db
        result = h_fc2_24_cali.eval(feed_dict={x_24_cali: test_db_24})
        result_box = etc.calib_run(result_box, result, img)

        #NMS_fast for each scale
        scale_cur = result_box[0][6]
        scale_box = []
        final_box = []
        for id_, box in enumerate(result_box):
            if box[6] == scale_cur:
                scale_box.append(box)
            if box[6] != scale_cur or id_ == len(result_box) - 1:
                scale_box.sort(key=lambda x: x[4])
                scale_box.reverse()
                supp_box_id = etc.NMS_fast(scale_box)
                final_box += [scale_box[i] for i in supp_box_id]
                scale_cur += 1
                scale_box = [box]
        result_box = final_box
        final_box = []

    test_db_12 = np.zeros((len(result_box), etc.dim_12), np.float32)
    test_db_24 = np.zeros((len(result_box), etc.dim_24), np.float32)
    test_db_48 = np.zeros((len(result_box), etc.dim_48), np.float32)
    for id_, box in enumerate(result_box):
        resized_img_12 = etc.img2array(box[5], etc.img_size_12)
        resized_img_24 = etc.img2array(box[5], etc.img_size_24)
        resized_img_48 = etc.img2array(box[5], etc.img_size_48)

        test_db_12[id_, :] = resized_img_12
        test_db_24[id_, :] = resized_img_24
        test_db_48[id_, :] = resized_img_48

    for thr_idx in xrange(etc.thr_num):

        #thr_ = 1e-3 + thr_idx * 1e-3
        thr_ = thr_idx * 1e-2
        final_box = []
        if len(result_box) > 0:
            #48-net
            result_box_copy = []
            result_box_copy += [
                copy.deepcopy([box[0], box[1], box[2], box[3], box[4]]) +
                [box[5].copy()] + copy.deepcopy([box[6], box[7]])
                for box in result_box
            ]

            final_box = []
            for tit in xrange(len(test_db_12) / etc.test_bench_num + 1):
                if tit == len(test_db_12) / etc.test_bench_num:
                    test_12 = test_db_12[tit * etc.test_bench_num:, :]
                    test_24 = test_db_24[tit * etc.test_bench_num:, :]
                    test_48 = test_db_48[tit * etc.test_bench_num:, :]
                    box_tmp = result_box_copy[tit * etc.test_bench_num:]
                else:
                    test_12 = test_db_12[tit * etc.test_bench_num:(tit + 1) *
                                         etc.test_bench_num, :]
                    test_24 = test_db_24[tit * etc.test_bench_num:(tit + 1) *
                                         etc.test_bench_num, :]
                    test_48 = test_db_48[tit * etc.test_bench_num:(tit + 1) *
                                         etc.test_bench_num, :]
                    box_tmp = result_box_copy[tit *
                                              etc.test_bench_num:(tit + 1) *
                                              etc.test_bench_num]

                result = h_fc2_48.eval(feed_dict={
                    x_12: test_12,
                    x_24: test_24,
                    x_48: test_48
                })
                result_id = np.where(result > thr_)[0]
                final_box += [box_tmp[i] for i in result_id]

        #global_NMS_fast
        final_box.sort(key=lambda x: x[4])
        final_box.reverse()
        supp_box_id = etc.NMS_fast(final_box)
        final_box = [final_box[i] for i in supp_box_id]

        if len(final_box) > 0:

            #48-net_calib
            test_db_48_calib = np.zeros((len(final_box), etc.dim_48),
                                        np.float32)
            for id_, box in enumerate(final_box):
                resized_img_48 = etc.img2array(box[5], etc.img_size_48)
                test_db_48_calib[id_, :] = resized_img_48
            result = h_fc2_48_cali.eval(
                feed_dict={x_48_cali: test_db_48_calib})
            final_box = etc.calib_run(final_box, result, img)

            for box in final_box:
                h = box[3] - box[1]
                box[1] -= 0.2 * h
                box[1] = max(0, box[1])
                ImageDraw.Draw(img).rectangle((box[0], box[1], box[2], box[3]),
                                              outline=(0, 0, 255))
            final_box = []
    return img