anson0910 / CNN_face_detection

Implementation based on the paper Li et al., “A Convolutional Neural Network Cascade for Face Detection, ” 2015 CVPR
253 stars 148 forks source link

AFLW files #7

Closed melgor closed 8 years ago

melgor commented 8 years ago

Hi do you have any script to extact AFLW from sql database? I mean following files? AFLW_Faces.txt AFLW_Rect.txt AFLW_sex.txt

Second question, do you need "Sex" for Face Detection or it is for other purpose?

Thanks

anson0910 commented 8 years ago

Hi, I remember I used a GUI interface (Qt?) to connect to the SQLITE database.

Well, I just wanted to see if it was able to do gender recognition at the same time. The accuracy was pretty high, but I didn't really need it so I didn't use it. If you need it, you can just modify the labels to be 3 bins instead of binary classfication.

melgor commented 8 years ago

Hi, I found that GUI but I do not know how should I extract this information. So I write a python script for extracting this information, where output file have format

path x y w h

Maybe someone will want to use it.

import sqlite3

list_annotation = list()
# Format for saving: path x y w h
ann_format = "{}/{} {} {} {} {}"

conn = sqlite3.connect('aflw.sqlite')
print "Opened database successfully"

# Get all Face ID
fidQuery = 'SELECT face_id FROM Faces'
faceIDs = conn.execute(fidQuery)

for idx in faceIDs:
  # Get ImgID connected to face
  fidQuery = 'SELECT file_id FROM Faces WHERE face_id = {}'.format(idx[0])
  imgID = conn.execute(fidQuery)
  imgID = [id for id in imgID]

  # Get image name and DB ID
  imgDataQuery = "SELECT db_id,filepath,width,height FROM FaceImages WHERE file_id = '{}'".format(imgID[0][0])
  fileID = conn.execute(imgDataQuery)
  fileID = [id for id in fileID]
  db_id = fileID[0][0]
  filepath = fileID[0][1]

  # Get Face Rect
  faceRectQuery = 'SELECT x,y,w,h FROM FaceRect WHERE face_id = {}'.format(idx[0])
  faceRect = conn.execute(faceRectQuery)
  faceRect = [id for id in faceRect]
  x,y,w,h =  faceRect[0]

  list_annotation.append(ann_format.format(db_id,filepath,x,y,w,h))

with open("AFLW_ann.txt",'w') as f:
  f.writelines("%s\n" % line for line in list_annotation) 

and this is changed aflw.py for a new format.

import numpy as np
import cv2
import os

data_base_dir = "/media/blcv/drive_2TB/Face/AFLW/aflw/data/"     # file containing pictures
save_dir = "/media/blcv/drive_2TB/CODE/Face_Detection/CNN_face_detection/Data/positive"         # file to save cropped male faces
read_file_name_faces = "AFLW_ann.txt"   # file with path to image and rect

faceID_fileName_dict = {}   # dictionary of str to str

if not os.path.isdir(save_dir): os.makedirs(save_dir)
# =========== read face file ===============
with open(read_file_name_faces, "r") as ins:
  for line in ins:
    path,x,y,w,h = line.strip().split(" ")
    id_image = os.path.basename(path).split(".")[0]
    faceID_fileName_dict[id_image] = (path,int(x),int(y),int(w),int(h))

# =========== Start processing ===============
i = 0
for key,value in faceID_fileName_dict.iteritems():
    if i % 10 == 0:
        print "Processing rect number " + str(i)
    path,x,y,w,h = value
    x = max(0, x)
    y = max(0, y)

    read_img_name = os.path.join(data_base_dir, path)
    if not os.path.exists(read_img_name):     # check if file exists
        continue
    img = cv2.imread(read_img_name)     # read image
    cropped_img = img[y : y + h, x : x + w]

    file_name = os.path.join(save_dir ,str(key).zfill(6) + ".jpg")
    cv2.imwrite(file_name, cropped_img)
    i += 1