Пример #1
0
        continue

    # Resize?
    if args.width:
        height = bgr_frame.shape[0] * args.width / bgr_frame.shape[1]
        bgr_frame = cv2.resize(bgr_frame, (args.width, height))

    show_image = bgr_frame.copy()
    
    # Convert data
    rgb_frame = cv2.cvtColor(bgr_frame,cv2.COLOR_BGR2RGB)
    pil_frame = Image.fromarray(rgb_frame)

    # Actual detection
    last = time()
    bounding_boxes, landmarks = detect_faces(pil_frame)
    dt = time() - last
    print 'Detection {:.2}s or {:.2} fps'.format(dt, 1./dt)
    print bounding_boxes

    # Draw bounding box
    for (x1, y1, x2, y2, confidence) in bounding_boxes.astype(int):
        cv2.rectangle(show_image, (x1, y1), (x2, y2), (255, 255, 255))
    # Draw landmarks
    for person in landmarks.astype(int):
        for landmark_idx in xrange(5):
            x = person[landmark_idx]
            y = person[landmark_idx + 5]
            print x,y
            cv2.circle(show_image, (x, y), 3, (255, 0, 255))
Пример #2
0
def main(args):

    videos_directory = args.videos_dir
    results_dir = args.results_dir
    vids_name = args.category
    vid_proc_name = args.log_file
    dataset_annotation_file = args.ann_file
    if args.save_videos == 'True':
        save_videos = True
    else:
        save_videos = False

    # Create video window
    cv2.namedWindow('Original')

    # load or create list with processed files
    processed_files = []
    videos_processed_exists = os.path.isfile(
        os.path.join(results_dir, vid_proc_name))
    if not videos_processed_exists:
        with open(os.path.join(results_dir, vid_proc_name), "w") as fp:
            for pfiles in processed_files:
                print(pfiles, file=fp)
    else:
        with open(os.path.join(results_dir, vid_proc_name)) as fp:
            processed_files = fp.read().splitlines()

    # Create annotation file the first time
    annotation_exists = os.path.isfile(
        os.path.join(results_dir, dataset_annotation_file))
    if not annotation_exists:

        try:
            with open(os.path.join(results_dir, dataset_annotation_file),
                      'w') as csvfile:
                writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
                writer.writeheader()
        except IOError:
            print("Error creating annotaton file. I/O error")

    # Get json files list names in videos directory
    files_list = []
    for ann_file in os.listdir(os.path.join(videos_directory, vids_name)):
        if ann_file.endswith(".json"):
            files_list.append(ann_file[0:-5])
    files_list = natsorted(files_list)

    num_files = len(files_list)
    print('found', num_files, 'files')

    # traverse all the files
    for file in files_list:
        # check if current video is not in alredy processed
        if file in processed_files:
            print(file, 'has already been processed. Skipping it.')
            continue

        num_output_video = 0

        # Search for the video files in videos_directory
        video_name = file + '.mp4'
        print('Processing video:', video_name)

        if save_videos:
            # create output directory
            output_dir = os.path.join(results_dir, vids_name, file)

            if not os.path.isdir(output_dir):
                os.mkdir(output_dir)

        # Load watson results
        with open(os.path.join(videos_directory, vids_name,
                               file + '.json')) as f:
            stt_results = json.load(f)

        # Extract all the words with confidence >90
        words_data = extract_words_from_watson_results(stt_results,
                                                       max_words=5)

        # Start the video capture
        cap = cv2.VideoCapture(
            os.path.join(videos_directory, vids_name, video_name))

        # Extract video metadata
        height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        fps = cap.get(cv2.CAP_PROP_FPS)
        print('video resolution:', width, ' x ', height)
        print('video framerate:', fps)

        frame_count = 0
        fps_processing = 30.0  # fps holder
        t = cv2.getTickCount()  # initiate the tickCounter
        count = 0

        for entry in words_data:
            # Extract speech to text data
            print('entry:', type(entry), entry)
            s_sec, s_millisec = divmod(float(entry['start']), 1)
            e_sec, e_millisec = divmod(float(entry['end']), 1)
            s_min = 0
            e_min = 0
            s_millisec = s_millisec * 1000
            e_millisec = e_millisec * 1000

            print('s_sec, s_millisec:', s_sec, s_millisec)

            if s_sec >= 60:
                s_min = math.floor(s_sec / 60.0)
                s_sec = s_sec % 60
            if e_sec >= 60:
                e_min = math.floor(e_sec / 60.0)
                e_sec = e_sec % 60

            # Determine video frames involved in stt entry
            min_frame = s_min * fps * 60 + (s_sec * fps)
            max_frame = e_min * fps * 60 + (e_sec * fps)

            # go to min_frame
            cap.set(cv2.CAP_PROP_POS_FRAMES, min_frame)

            frame_count = min_frame
            # read frames from min_frame to max_frame
            num_people = 0

            valid_video = True
            bbx1 = []
            bby1 = []
            bbx2 = []
            bby2 = []
            consecutive_frames_no_people = 0
            while frame_count < max_frame:
                if count == 0:
                    t = cv2.getTickCount()

                # capture next frame
                ret, frame = cap.read()

                if not ret:
                    continue

                frame_count += 1

                # resize frame for faster processing
                if frame.shape[0] <= 0 or frame.shape[1] <= 0:
                    continue

                frame_small = cv2.resize(frame, (0, 0),
                                         fx=scale,
                                         fy=scale,
                                         interpolation=cv2.INTER_LINEAR)

                # detect faces and landmarjs
                bounding_boxes, landmarks = detect_faces(frame_small)
                num_people = bounding_boxes.shape[0]

                bounding_boxes /= scale
                landmarks /= scale

                # if it detects less than or more than 1 person
                # go to next subtitle
                if num_people != 1:
                    consecutive_frames_no_people += 1

                if consecutive_frames_no_people >= max_bad_frames:
                    print(
                        consecutive_frames_no_people,
                        ' frames without 1 person. Skiping to next subtitle')
                    valid_video = False
                    break

                # if only one person in the scene
                if num_people == 1:
                    consecutive_frames_no_people = 0

                    # extract the bounding box
                    bb = bounding_boxes[0]
                    x1, y1 = int(bb[0]), int(bb[1])
                    x2, y2 = int(bb[2]), int(bb[3])

                    area = (x2 - x1) * (y2 - y1)
                    if area < min_area:
                        valid_video = False
                        break

                    # save bounding box coordinates for final crop
                    bbx1.append(x1)
                    bbx2.append(x2)
                    bby1.append(y1)
                    bby2.append(y2)

                    # draw the bounding box and landmarks on original frame
                    frame = show_bboxes(frame, bounding_boxes, landmarks)

                    # Put fps at which we are processing camera feed on frame
                    cv2.putText(frame, "{0:.2f}-fps".format(fps_processing),
                                (50, height - 50), cv2.FONT_HERSHEY_COMPLEX, 1,
                                (0, 0, 255), 2)

                # Display the image
                cv2.imshow('Original', frame)

                # Read keyboard and exit if ESC was pressed
                k = cv2.waitKey(1) & 0xFF
                if k == 27:
                    exit()
                elif k == ord('q'):
                    break

                # increment frame counter
                count = count + 1
                # calculate fps at an interval of 100 frames
                if (count == 30):
                    t = (cv2.getTickCount() - t) / cv2.getTickFrequency()
                    fps_processing = 30.0 / t
                    count = 0

            # if this was a valid video
            if valid_video and len(bbx1) > 0:
                num_output_video += 1

                # get final coordinates
                bbx1 = np.amin(np.array(bbx1))
                bbx2 = np.amax(np.array(bbx2))
                bby1 = np.amin(np.array(bby1))
                bby2 = np.amax(np.array(bby2))
                bbw = bbx2 - bbx1
                bbh = bby2 - bby1

                entry['bounding_box'] = [bbx1, bby1, bbw, bbh]
                print('entry:', type(entry), entry)

                if save_videos:
                    s_hr = 0
                    e_hr = 0
                    if s_min >= 60:
                        s_hr = math.floor(s_min / 60)
                        s_min = s_min % 60
                    if e_min >= 60:
                        e_hr = math.floor(e_min / 60)
                        e_min = e_min % 60

                    # cut and crop video
                    # ffmpeg -i input.mp4 -ss hh:mm:ss -filter:v crop=w:h:x:y -c:a copy -to hh:mm:ss output.mp4
                    ss = "{0:02d}:{1:02d}:{2:02d}.{3:03d}".format(
                        s_hr, s_min, int(s_sec), math.ceil(s_millisec))
                    es = "{0:02d}:{1:02d}:{2:02d}.{3:03d}".format(
                        e_hr, e_min, int(e_sec), math.ceil(e_millisec))
                    crop = "crop={0:1d}:{1:1d}:{2:1d}:{3:1d}".format(
                        bbw, bbh, bbx1, bby1)

                    out_name = os.path.join(output_dir, str(num_output_video))

                    subprocess.call([
                        'ffmpeg',  #'-hide_banner', '-loglevel', 'panic',
                        '-i',
                        os.path.join(videos_directory, vids_name, video_name),
                        '-ss',
                        ss,
                        '-filter:v',
                        crop,
                        '-c:a',
                        'copy',
                        '-to',
                        es,
                        out_name + '.mp4'
                    ])
                    # save recognized speech
                    text_file = open(out_name + '.txt', "w")
                    text_file.write(entry['text'] + '\n')
                    text_file.write(str(entry['conf']))
                    text_file.close()

        # delete the entries without bounding box
        words_data[:] = [
            dic for dic in words_data if len(dic['bounding_box']) > 0
        ]

        # append results to annotation file
        append_annotation_file(
            os.path.join(results_dir, dataset_annotation_file), words_data)

        # save name of processed file
        processed_files.append(file)
        with open(os.path.join(results_dir, vid_proc_name), "w") as fp:
            for p_file in processed_files:
                print(p_file, file=fp)

    # Release resources
    cap.release()
    cv2.destroyAllWindows()
Пример #3
0
id_file = open('id_list.txt', 'w')

for file in file_array :
    img_path = input_dir+file
    PIL_img = Image.open(img_path)
    cv2_img = cv2.imread(img_path)

    filename = file.split('.')[0]
    id_file.write(filename+'\n')

    # array that contain fake probability of faces in the img
    total_prob = []

    # detect faces using mtcnn => 얼굴 인식
    bboxs, landmarks = detect_faces(PIL_img)
    org_height, org_width, channel = cv2_img.shape
    # print("org_height= " + str(org_height) + ", org_width= " + str(org_width))

    # crop face area in the img => 이미지 별로 얼굴 부분 자르기
    for i in range(len(bboxs)):

        output_path = output_dir + filename + '_' + str(i) + '.jpg'
        if os.path.isfile(output_path):
            print('file exist1 - ' + output_path)
            break

        x1, y1, x2, y2, score = bboxs[i]
        height = int(y2) - int(y1)
        width = int(x2) - int(x1)
        # print("height= "+str(height)+", width= "+str(width))
Пример #4
0
def get_aligned_images(origin_path, output_path, crop_size, replace=False):
    if os.path.isdir(origin_path):
        files = os.listdir(origin_path)
        for idx, file in enumerate(files):

            # get images in each file
            imgs_root = os.path.join(origin_path, file)
            # print(imgs_root)
            if os.path.isdir(imgs_root):
                for img_path in os.listdir(imgs_root):

                    img = Image.open(os.path.join(imgs_root, img_path))

                    # preprocessing image

                    bounding_boxes, landmarks = detect_faces(img)
                    img_cv2 = np.array(img)[..., ::-1]
                    if len(bounding_boxes) and len(landmarks):
                        for i in range(len(bounding_boxes)):
                            box = bounding_boxes[i][:4].astype(
                                np.int32).tolist()
                            for idx, coord in enumerate(box[:2]):
                                if coord > 1:
                                    box[idx] -= 1
                            if box[2] + 1 < img_cv2.shape[1]:
                                box[2] += 1
                            if box[3] + 1 < img_cv2.shape[0]:
                                box[3] += 1
                            face = img_cv2[box[1]:box[3], box[0]:box[2]]
                            if face.shape[0] <= 0 or face.shape[1] <= 0:
                                continue
                            landmark = landmarks[i]
                            facial5points = [[
                                landmark[j] - box[0], landmark[j + 5] - box[1]
                            ] for j in range(5)]
                            dst_img = warp_and_crop_face(face,
                                                         facial5points,
                                                         crop_size=crop_size)
                            dst_img = Image.fromarray(dst_img[..., ::-1])

                            save_root = os.path.join(output_path,
                                                     'normal_aligned',
                                                     str(file))
                            save_path = os.path.join(save_root, img_path)
                            if not os.path.exists(save_root):
                                os.makedirs(save_root)
                            print('normal_save_path = ', save_path)
                            dst_img.save(save_path)
                    else:

                        print(
                            'there no bounding box and landmarks detected for image {}_{}'
                            .format(imgs_root, img_path))
                        save_root = os.path.join(output_path,
                                                 'unnormal_aligned', str(file))
                        save_path = os.path.join(save_root, img_path)
                        # pdb.set_trace()
                        print(
                            'except_path = ', save_path
                        )  # 'images/test_output_file/none_bounding_boxes_imgs/Darryl_Stingley_0001.jpg'
                        # pdb.set_trace()
                        if not os.path.exists(save_root):
                            os.makedirs(save_root)
                        print('unnormal_save_path = ', save_path)
                        img.save(save_path)

                        continue
Пример #5
0
from src import detect_faces
from PIL import Image

image = Image.open('images/office1.jpg')
bounding_boxes, landmarks = detect_faces(image)

print("bounding_boxes:\n", bounding_boxes)
print("landmarks:\n", landmarks)
Пример #6
0
from src import detect_faces
from PIL import Image
import cv2
img = cv2.imread('image.jpg')
image = Image.open('image.jpg')
bboxes, landmarks = detect_faces(image)
#print(landmarks)

for i, bounding_box in enumerate(bboxes):

    print(bounding_box)
    cv2.rectangle(img, (int(bounding_box[0]), int(bounding_box[1])),
                  (int(bounding_box[2]), int(bounding_box[3])), (0, 155, 255),
                  2)

#cv2.circle(image,(keypoints['left_eye']), 2, (0,155,255), 2)
#cv2.circle(image,(keypoints['right_eye']), 2, (0,155,255), 2)
#cv2.circle(image,(keypoints['nose']), 2, (0,155,255), 2)
#cv2.circle(image,(keypoints['mouth_left']), 2, (0,155,255), 2)
#cv2.circle(image,(keypoints['mouth_right']), 2, (0,155,255), 2)
cv2.imwrite("frame1.jpg", img)
Пример #7
0
def find_bb_ld_img(
    imgname="/home/diqong/Documents/Zhejiang University Email system_files/2.jpg",
    crop_img_name="/home/diqong/Documents/Zhejiang University Email system_files/3.jpg",
):
    """
    imgname=imgname.replace("image00002.jpg","image00032.jpg")
    #print imgname
    image=Image.open(imgname)

    drawObject = ImageDraw.Draw(image)
    matObj = sio.loadmat(imgname.replace('jpg', 'mat'))
    print(imgname)
    lms = matObj['pt3d_68'][:2]
    print(lms)
    for i in range(lms.shape[1]):
        print(i)
        drawObject.ellipse((lms[0][i]-1,lms[1][i]-1,lms[0][i]+1,lms[1][i]+1),fill = "red")

    image.save('/data2/lmd_jdq/AFLW2000-3D/test.jpg')
    print('OK')
    f**k
    exit(1)
    """

    if os.path.exists(imgname.replace(".jpg", ".npy")):
        pass
    # try:
    if True:
        imgname = imgname.replace("\n", "")
        # imgname=imgname.replace("image00002.jpg","image00032.jpg")
        # print imgname
        image = Image.open(imgname)
        bounding_boxes, landmarks = detect_faces(image)

        # print bounding_boxes, landmarks, imgname
        # exit()

        nolandmard = 0
        inx = 0
        if len(landmarks) >= 1:
            minIoU = -1
            for i, bb in enumerate(bounding_boxes):
                x1a, y1a, x2a, y2a, _ = bounding_boxes[i]
                IoU = (x2a - x1a) * (y2a - y1a)
                if IoU > minIoU:
                    minIoU = IoU
                    inx = i
            print(imgname)
            np.save(imgname.replace(".jpg", ".npy"), bounding_boxes[inx])
        elif len(landmarks) == 0:
            nolandmard = 1
            print("-" * 10, imgname)
            return 0
    # except:
    #    nolandmard=1
    """
    re-align AFLW2000-3D
    start
    """
    return 0
    matObj = sio.loadmat(imgname.replace("jpg", "mat"))
    lms = matObj["pt3d_68"][:2]

    lm = []
    lm.append((lms[:, 36] + lms[:, 39]) / 2)
    lm.append((lms[:, 42] + lms[:, 45]) / 2)
    lm.append(lms[:, 30])
    lm.append(lms[:, 48])
    lm.append(lms[:, 54])

    nolandmard = False
    """
    end
    """

    if not nolandmard:
        # f.write(dir + "/" + img+","+str(bounding_boxes[inx][0])+","+str(bounding_boxes[inx][1])+","+str(bounding_boxes[inx][2])+","+str(bounding_boxes[inx][3])+",")
        # for i in range(5):
        #     f.write(str(landmarks[inx][i])+","+str(landmarks[inx][5+i]))
        # f.write("\n")
        # print imgname
        # lm=[]
        # for i in range(5):
        #    lm.append([ landmarks[inx][i], landmarks[inx][5+i] ])
        image = np.asarray(image)

        matObj = sio.loadmat(imgname.replace("jpg", "mat"))

        image, matObj_align = alignment(image, lm, matObj)

        crop_img = image[:, :, ::-1]
        # if not os.path.exists(out_dir + dir):
        #     os.makedirs(out_dir + dir)
        # print(dir)
        cv2.imwrite(crop_img_name, crop_img)
        sio.savemat(crop_img_name.replace("jpg", "mat"), matObj_align)
Пример #8
0
args = parser.parse_args()

names = glob.glob(osp.join(args.lfw, '*') )
names = [x for x in names if osp.isdir(x) ]
names = sorted(names )
imgs = []
for x in names:
    imgs = imgs + sorted(glob.glob(osp.join(x, '*.jpg') ) )

with open(args.output, 'w') as fOut:
    cnt = 0
    for imgName in imgs:
        cnt += 1
        print('%d/%d: %s' % (cnt, len(imgs), imgName ) )
        img = Image.open(imgName )
        _, landmarks = detect_faces(img )
        faceNum = landmarks.shape[0]
        if faceNum > 1:
            print('Warning: %s faces have been detected!' % faceNum )

        largestMark = np.array(landmarks[0, :] ).reshape([2, 5] )
        largestArea = computeArea(largestMark )
        for n in range(1, faceNum):
            mark = np.array(landmarks[n, :] ).reshape([2, 5] )
            area =  computeArea(mark )
            if area > largestArea:
                largestMark = mark
                largestArea = area

        landmarks = np.transpose(largestMark, [1, 0] ).reshape(10 )
Пример #9
0
args = parser.parse_args()

os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "3"

if __name__ == "__main__":

    if args.dataset == '300WLP':

        images = open(args.src_dir + '/images.txt').readlines()
        for image in tqdm(images[args.start:args.end]):
            cv_orig_im = cv2.imread(os.path.join(args.src_dir, image[:-1]))
            pil_im = Image.fromarray(
                cv2.cvtColor(cv_orig_im, cv2.COLOR_BGR2RGB))
            # bboxes, landmarks = detect_faces(pil_im, min_face_size = 30, in_weights_dir = args.weights_dir)
            bboxes, landmarks = detect_faces(pil_im, min_face_size=30)
            if len(bboxes) < 1:
                print('no face found')
                continue
            elif len(bboxes) > 1:
                max_prob = 0
                max_idx = 0
                for j in range(len(bboxes)):
                    if bboxes[j, 4] > max_prob:
                        max_prob = bboxes[j, 4]
                        max_idx = j
                final_bbox = bboxes[max_idx, :]
                final_landmarks = landmarks[max_idx, :]
            else:
                final_bbox = bboxes
                final_landmarks = landmarks
Пример #10
0
    imshow(origin_img)


if __name__ == '__main__':

    # img_name = 'F-D_03_1/frame000.jpg'
    img_name = 'img/frame036.jpg'

    # PLimg = Image.open(img_name)
    image = cv2.imread(img_name)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    PLimg = Image.fromarray(image)

    # Image.fromarray(image)
    # image.show()
    bounding_boxes, landmarks = detect_faces(PLimg)
    img_copy = show_bboxes(PLimg, bounding_boxes, landmarks)
    # img_copy.show()

    rotated_img, eye_center, angle = align_face(PLimg, landmarks)

    cv2.imwrite('rotated.jpg', rotated_img)
    rotated_img = cv2.cvtColor(rotated_img, cv2.COLOR_BGR2RGB)
    rotated_PLimg = Image.fromarray(rotated_img)
    # rotated_PLimg.show()

    # # rotated_img.imshow()
    #
    #
    rotated_landmarks = rotate_landmarks(landmarks, eye_center, angle,
                                         rotated_PLimg.size[0])
from src import detect_faces, show_bboxes
from PIL import Image
import os
import torch
from torch import nn
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import numpy as np
import cv2
import numpy.random as npr
import sys
import utils as utils
from torch.autograd import Variable
import torchvision.models as models
import torch.optim as optim

img_path = '/home/lc/106_resnet/show/2.jpg'
img = Image.open(img_path)
bounding_boxes, _, x = detect_faces(img)
print bounding_boxes
bb = bounding_boxes[0]
print bb
Пример #12
0
def job(l):
    print('%s start' % mp.current_process())
    # 把传进来的list内容都提脸,写jpg
    for dname in l:
        print(dname[27:-4])
        os.mkdir(os.path.join(config.str1, dname[27:-4]), 777)
        os.chmod(os.path.join(config.str1, dname[27:-4]),
                 stat.S_IRWXU + stat.S_IRWXG + stat.S_IRWXO)
        for fname in os.listdir(dname):
            # print(fname)
            # pass
            img = Image.open(os.path.join(dname, fname))
            bounding_boxes, landmarks = detect_faces(img)
            #show_bboxes(img, bounding_boxes, landmarks)
            if len(bounding_boxes):
                if bounding_boxes.size != 0:
                    bb = bounding_boxes[0]
                    # print(bb)
                    w1 = bb[0]
                    h1 = bb[1]
                    w2 = bb[2]
                    h2 = bb[3]
                    aa = []
                    w3 = w1 - 0.1 * (w2 - w1)
                    h3 = h1 - 0.2 * (h2 - h1)
                    w4 = w2 + 0.3 * (w2 - w1)
                    h4 = h2 + 0.2 * (h2 - h1)
                    if (h4 - h3) > (w4 - w3):
                        w3 -= 0.5 * ((h4 - h3) - (w4 - w3))
                        w4 += 0.5 * ((h4 - h3) - (w4 - w3))
                    if (h4 - h3) > (w4 - w3):
                        w3 -= 0.5 * ((h4 - h3) - (w4 - w3))
                        w4 += 0.5 * ((h4 - h3) - (w4 - w3))
                    if (h4 - h3) > (w4 - w3):
                        w3 -= 0.5 * ((h4 - h3) - (w4 - w3))
                        w4 += 0.5 * ((h4 - h3) - (w4 - w3))
                    if w3 < 0:
                        w3 = 0
                    if h3 < 0:
                        h3 = 0
                    if w4 > img.size[0]:
                        w4 = img.size[0]
                    if h4 > img.size[1]:
                        h4 = img.size[1]
                    aa.append(w3)
                    aa.append(h3)
                    aa.append(w4)
                    aa.append(h4)
                    # print(w3)
                    # print(h3)
                    # print(w4-w3)
                    # print(h4-h3)

                    face = img.crop(aa[:4])
                    # face.show()
                    # print(face.size)
                    face.save(os.path.join(config.str1, dname[27:-4],
                                           fname))  # TODO
                    print("saved")
                else:
                    print("no face found!")
            else:
                print("bounding_boxes is empty!")

    print('%s finish' % mp.current_process())
Пример #13
0
from PIL import Image

# img = Image.open('images/office1.jpg')
# bounding_boxes, landmarks = detect_faces(img)
# im = show_bboxes(img, bounding_boxes, landmarks)
# img.show()
# im.show()

# img = Image.open('images/office2.jpg')
# bounding_boxes, landmarks = detect_faces(img)
# im = show_bboxes(img, bounding_boxes, landmarks)
# img.show()
# im.show()

# img = Image.open('images/office3.jpg')
# bounding_boxes, landmarks = detect_faces(img)
# im = show_bboxes(img, bounding_boxes, landmarks)
# img.show()
# im.show()

# img = Image.open('images/office4.jpg')
# bounding_boxes, landmarks = detect_faces(img, thresholds=[0.6, 0.7, 0.85])
# im = show_bboxes(img, bounding_boxes, landmarks)
# img.show()
# im.show()

img = Image.open('images/office5.jpg')
bounding_boxes, landmarks = detect_faces(img, min_face_size=10)
im = show_bboxes(img, bounding_boxes, landmarks)
im.show()
Пример #14
0
def main():
    # Load the subtitles
    subs = pysrt.open('/home/juan/Videos/AMOR.es.srt', encoding='iso-8859-1')

    # Start the video capture
    cap = cv2.VideoCapture('/home/juan/Videos/AMOR.mp4')

    cv2.namedWindow('Original')
    cv2.namedWindow('Cropped')

    # Extract number of subtitles
    num_subs = len(subs)
    print('Num subtitles:', num_subs)

    # Extract video metadata
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    fps = cap.get(cv2.CAP_PROP_FPS)
    print('video resolution:', width, ' x ', height)
    print('video framerate:', fps)

    cv2.waitKey(0)

    for s_idx, sub in enumerate(subs):
        s = "{0:1d}, {1:02d}:{2:02d} to {3:02d}:{4:02d} {5:s}"
        text = cleanhtml(sub.text)
        print(
            s.format(s_idx, sub.start.minutes, sub.start.seconds,
                     sub.end.minutes, sub.end.seconds, text))

    while True:
        # capture next frame
        ret, frame = cap.read()

        if PROCESS_VIDEO:
            # resiz frame for faster processing
            frame = cv2.resize(frame, (0, 0),
                               fx=0.5,
                               fy=0.5,
                               interpolation=cv2.INTER_LINEAR)

            # detect faces and landmarjs
            bounding_boxes, landmarks = detect_faces(frame)

            # if only one face detected
            if bounding_boxes.shape[0] == 1:
                # extract the bounding box
                bb = bounding_boxes[0]
                x1, y1, x2, y2 = int(bb[0]), int(bb[1]), int(bb[2]), int(bb[3])

                # crop the face
                cropped = frame[y1:y2, x1:x2]
                cv2.imshow('Cropped', cropped)

            # draw the bounding box and landmarks on original frame
            frame = show_bboxes(frame, bounding_boxes, landmarks)

        # Display the image
        cv2.imshow('Original', frame)

        # Read keyboard and exit if ESC was pressed
        k = cv2.waitKey(10) & 0xFF
        if k == 27:
            break

    # Release resources
    cap.release()
    cv2.destroyAllWindows()
import os

os.environ["CUDA_VISIBLE_DEVICES"] = '1'
img_path = '/net/deepfake-defense/datasets/CelebA/img/img_celeba/'
pert_path = '/net/deepfake-defense/datasets/CelebA/img/MTCNN_ifgsm/'
result_f = open(
    '/home/zhujunxiao/protect_face_identity/face_image_protection/MTCNN/mtcnn-pytorch/results/MTCNN_detection_result_celeba.csv',
    'w')
result_f.write(
    'image, detected bounding box, detected boundingbox(after perturbation)\n')
for img_index in range(1, 2001):
    img_filename = format(img_index, '06d') + '.jpg'
    print(img_filename)
    img = Image.open(img_path + img_filename)
    img = img.resize((224, 224))
    bounding_boxes, landmarks = detect_faces(img)
    bounding_box_str = []
    for box in bounding_boxes:
        bounding_box_str.append(' '.join([str(x) for x in box]))
    # for i in range(len(bounding_boxes)):
    #     result_f.write(img_filename + ',' + ' '.join([str(x) for x in bounding_boxes[i]]))
    #     result_f.write(',' + ' '.join([str(x) for x in landmarks[i]]) + '\n')
    pert_img = pert_path + img_filename
    img = Image.open(pert_img)
    img = img.resize((224, 224))
    pert_bounding_boxes, _ = detect_faces(img)
    pert_bounding_box_str = []
    for box in pert_bounding_boxes:
        pert_bounding_box_str.append(' '.join([str(x) for x in box]))
    result_f.write(img_filename + ',')
    result_f.write(';'.join([x for x in bounding_box_str]) + ',')