def lena_test(net,net2):
#    img = scale_image(cv.LoadImage("sample/lena.bmp"))
    img = scale_image(cv.LoadImage("sample/Group-Oct06.jpg"))
    found = []
    
    buf_nf_sum= buf_f_sum=true_neg_sum=0
    k=0
    for i, (sample, box) in enumerate(samples_generator(img, 32,32,slide_step=4, resize_step=1.5,bw_from_v_plane=False)):
        nf, f = net.activate(get_flatten_image(sample))
        nf2, f2 = net2.activate(get_flatten_image(laplace(sample)))
        buf_nf, buf_f = tuple(net['out'].inputbuffer[0])
        buf_nf2, buf_f2 = tuple(net2['out'].inputbuffer[0])
        if f > nf and f2 > nf2 and buf_f > 250000 and buf_f2 > 50000:
            print "%d - %d %d" % (i, buf_f, buf_f2)
#            buf_nf, buf_f = tuple(net['out'].inputbuffer[0])
            buf_f_sum+=buf_f2
            buf_nf_sum+=buf_nf2
            found.append(box)
        else:
#            buf_nf, _ = tuple(net['out'].inputbuffer[0])
            true_neg_sum+=buf_nf2
            k+=1

    draw_boxes(found, img, color=cv.RGB(255,255,255), thickness=1, with_text=False)
    print "Avr nf %.3f, f %.3f, true negative %.3f" % (buf_nf_sum/len(found), buf_f_sum/len(found), true_neg_sum/k)
    show_image(img)
Beispiel #2
0
def get_face_regions(ann, ann2, img, classifier):
    img = normalize_rgb(img, aggressive=0.005)
    mask, seqs = get_mask_with_contour(img,
                                       ret_cont=True,
                                       ret_img=True,
                                       with_init_mask=False)
    if not seqs:
        return img


#    show_image(mask)
    skin_regions, min_rects = get_skin_rectangles(seqs)
    skin_regions = merge_boxes(skin_regions)
    draw_boxes(skin_regions, img, with_text=False, thickness=1)
    #
    #    small_img = prepare_bw(scale_image(img))
    #    cv.EqualizeHist(small_img, small_img)
    #    objects = cv.HaarDetectObjects(small_img, classifier, cv.CreateMemStorage(0), 1.1, 3,
    #                         cv.CV_HAAR_DO_CANNY_PRUNING | cv.CV_HAAR_FIND_BIGGEST_OBJECT | cv.CV_HAAR_DO_ROUGH_SEARCH,
    #                         min_size=(50,50))
    #    found = [[k*2 for k in obj] for obj, num in objects]
    #    draw_boxes(found, img, with_text=False, color=cv.RGB(255,255,255))

    for region in skin_regions:
        #        cv.SetImageROI(img, region)

        #        cv.ResetImageROI(img)
        #        if objects:

        cv.SetImageROI(img, region)
        region_img = cv.CreateImage(region[2:], img.depth, img.channels)
        cv.Copy(img, region_img)
        found = []
        try:
            for i, (sample, box) in enumerate(
                    samples_generator(region_img,
                                      32,
                                      32,
                                      slide_step=4,
                                      resize_step=1.5,
                                      bw_from_v_plane=False)):
                #                cv.SaveImage(root_folder+"webcam/%d.png" % (p+i), sample)
                nf, f = ann.activate(get_flatten_image(sample))
                nf2, f2 = ann2.activate(get_flatten_image(laplace(sample)))
                buf_nf, buf_f = tuple(ann['out'].inputbuffer[0])
                _, buf_f2 = tuple(ann2['out'].inputbuffer[0])
                if f > nf and f2 > nf2 and buf_f > 250000 and buf_f2 > 50000:
                    found.append(box)
        except Exception:
            pass
        if found:
            draw_boxes(found,
                       img,
                       with_text=False,
                       color=cv.RGB(255, 255, 255))
        cv.ResetImageROI(img)
    return img
Beispiel #3
0
def get_face_regions(ann, ann2, img, classifier):
    img = normalize_rgb(img, aggressive=0.005)
    mask, seqs = get_mask_with_contour(img, ret_cont=True, ret_img=True, with_init_mask=False)
    if not seqs:
        return img
#    show_image(mask)
    skin_regions, min_rects = get_skin_rectangles(seqs)
    skin_regions = merge_boxes(skin_regions)
    draw_boxes(skin_regions,img,with_text=False,thickness=1)
#
#    small_img = prepare_bw(scale_image(img))
#    cv.EqualizeHist(small_img, small_img)
#    objects = cv.HaarDetectObjects(small_img, classifier, cv.CreateMemStorage(0), 1.1, 3,
#                         cv.CV_HAAR_DO_CANNY_PRUNING | cv.CV_HAAR_FIND_BIGGEST_OBJECT | cv.CV_HAAR_DO_ROUGH_SEARCH,
#                         min_size=(50,50))
#    found = [[k*2 for k in obj] for obj, num in objects]
#    draw_boxes(found, img, with_text=False, color=cv.RGB(255,255,255))

    for region in skin_regions:
#        cv.SetImageROI(img, region)

#        cv.ResetImageROI(img)
#        if objects:



        cv.SetImageROI(img, region)
        region_img = cv.CreateImage(region[2:],img.depth,img.channels)
        cv.Copy(img, region_img)
        found = []
        try:
            for i,(sample, box) in enumerate(samples_generator(region_img, 32, 32, slide_step=4, resize_step=1.5, bw_from_v_plane=False)):
#                cv.SaveImage(root_folder+"webcam/%d.png" % (p+i), sample)
                nf, f = ann.activate(get_flatten_image(sample))
                nf2, f2 = ann2.activate(get_flatten_image(laplace(sample)))
                buf_nf, buf_f = tuple(ann['out'].inputbuffer[0])
                _, buf_f2 = tuple(ann2['out'].inputbuffer[0])
                if f > nf and f2 > nf2 and buf_f > 250000 and buf_f2 > 50000:
                    found.append(box)
        except Exception:
            pass
        if found:
            draw_boxes(found, img, with_text=False, color=cv.RGB(255,255,255))
        cv.ResetImageROI(img)
    return img
Beispiel #4
0
def lena_test(net, net2):
    #    img = scale_image(cv.LoadImage("sample/lena.bmp"))
    img = scale_image(cv.LoadImage("sample/Group-Oct06.jpg"))
    found = []

    buf_nf_sum = buf_f_sum = true_neg_sum = 0
    k = 0
    for i, (sample, box) in enumerate(
            samples_generator(img,
                              32,
                              32,
                              slide_step=4,
                              resize_step=1.5,
                              bw_from_v_plane=False)):
        nf, f = net.activate(get_flatten_image(sample))
        nf2, f2 = net2.activate(get_flatten_image(laplace(sample)))
        buf_nf, buf_f = tuple(net['out'].inputbuffer[0])
        buf_nf2, buf_f2 = tuple(net2['out'].inputbuffer[0])
        if f > nf and f2 > nf2 and buf_f > 250000 and buf_f2 > 50000:
            print "%d - %d %d" % (i, buf_f, buf_f2)
            #            buf_nf, buf_f = tuple(net['out'].inputbuffer[0])
            buf_f_sum += buf_f2
            buf_nf_sum += buf_nf2
            found.append(box)
        else:
            #            buf_nf, _ = tuple(net['out'].inputbuffer[0])
            true_neg_sum += buf_nf2
            k += 1

    draw_boxes(found,
               img,
               color=cv.RGB(255, 255, 255),
               thickness=1,
               with_text=False)
    print "Avr nf %.3f, f %.3f, true negative %.3f" % (
        buf_nf_sum / len(found), buf_f_sum / len(found), true_neg_sum / k)
    show_image(img)
def extract_falses(ann1, ann2, check, testpath=None, falses_dir=None, do_copy=True,threshold=None):
    print "Testing ANN with all negatives or positives"
#    testpath = root_folder + "lenas"

    if do_copy:
        if os.path.exists(falses_dir):
            shutil.rmtree(falses_dir)
        else:
            os.makedirs(falses_dir)

    wrong = 0
    avg_level = 0
    avg_f=avg_p = 0
    tot=0
    prev_dir=None
    found_for_dir=0
    fk = 0
    found_list=[]
    avg_f_list=[]
    avg_p_list=[]
    def extract(found_list):
        for f, file in found_list:
            dest_name = os.path.join(falses_dir,"-".join(["%.2f" % (f*100)] + file.split(os.sep)[-2:]))
            if not os.path.exists(falses_dir):
                os.makedirs(falses_dir)
            shutil.copy(file, dest_name)
        found_list=[]
    
    for fullpath, name in yield_files_in_path(testpath):
        dir = fullpath.split(os.path.sep)[-2]
        try:
            if prev_dir and dir != prev_dir:
                print "In %s was wrong %d out of %d (%.2f%%)" % (prev_dir, found_for_dir,fk, found_for_dir/fk*100)
                found_for_dir=0
                fk = 0
                if do_copy:
                    extract(found_list)


            img = cv.LoadImage(fullpath, iscolor=False)
#            nf1, f1 = activate_with_threshold(ann,get_flatten_image(img),threshold=threshold)
            nf1, f1 = ann1.activate(get_flatten_image(img))
            nf2, f2 = ann2.activate(get_flatten_image(laplace(img)))
            buf_nf, buf_f = tuple(ann1['out'].inputbuffer[0])
#            diff = int(f2*100) - int(nf*100)
            value = buf_nf if nf1 > f1 else buf_f
            if check(nf1,f1) and check(nf2,f2):
                found_list.append((f1, fullpath))
                found_for_dir+=1
                wrong+=1
                avg_f_list.append(value)
                avg_f+= value
            else:
                a=0
                avg_p+= value
                avg_p_list.append(value)
            avg_level+=buf_nf if nf1 > f1 else buf_f
            fk+=1

            tot+=1
        except IOError:
            pass
        prev_dir = dir

    if found_list:
        extract(found_list)

    print "Error is %.2f%%" % (wrong/tot)
#    print "Found %d faces out of %d negatives (%.2f%%)" % (found, tot, found/tot*100)
    print "Average level (+) for all: %.2f" % (avg_level/tot)
    print "Average level (f) for trues: %.2f" % (avg_p/tot)
    print "Average level (nf) for falses: %.2f" % (avg_f/wrong)
    return wrong
Beispiel #6
0
def extract_falses(ann1,
                   ann2,
                   check,
                   testpath=None,
                   falses_dir=None,
                   do_copy=True,
                   threshold=None):
    print "Testing ANN with all negatives or positives"
    #    testpath = root_folder + "lenas"

    if do_copy:
        if os.path.exists(falses_dir):
            shutil.rmtree(falses_dir)
        else:
            os.makedirs(falses_dir)

    wrong = 0
    avg_level = 0
    avg_f = avg_p = 0
    tot = 0
    prev_dir = None
    found_for_dir = 0
    fk = 0
    found_list = []
    avg_f_list = []
    avg_p_list = []

    def extract(found_list):
        for f, file in found_list:
            dest_name = os.path.join(
                falses_dir,
                "-".join(["%.2f" % (f * 100)] + file.split(os.sep)[-2:]))
            if not os.path.exists(falses_dir):
                os.makedirs(falses_dir)
            shutil.copy(file, dest_name)
        found_list = []

    for fullpath, name in yield_files_in_path(testpath):
        dir = fullpath.split(os.path.sep)[-2]
        try:
            if prev_dir and dir != prev_dir:
                print "In %s was wrong %d out of %d (%.2f%%)" % (
                    prev_dir, found_for_dir, fk, found_for_dir / fk * 100)
                found_for_dir = 0
                fk = 0
                if do_copy:
                    extract(found_list)

            img = cv.LoadImage(fullpath, iscolor=False)
            #            nf1, f1 = activate_with_threshold(ann,get_flatten_image(img),threshold=threshold)
            nf1, f1 = ann1.activate(get_flatten_image(img))
            nf2, f2 = ann2.activate(get_flatten_image(laplace(img)))
            buf_nf, buf_f = tuple(ann1['out'].inputbuffer[0])
            #            diff = int(f2*100) - int(nf*100)
            value = buf_nf if nf1 > f1 else buf_f
            if check(nf1, f1) and check(nf2, f2):
                found_list.append((f1, fullpath))
                found_for_dir += 1
                wrong += 1
                avg_f_list.append(value)
                avg_f += value
            else:
                a = 0
                avg_p += value
                avg_p_list.append(value)
            avg_level += buf_nf if nf1 > f1 else buf_f
            fk += 1

            tot += 1
        except IOError:
            pass
        prev_dir = dir

    if found_list:
        extract(found_list)

    print "Error is %.2f%%" % (wrong / tot)
    #    print "Found %d faces out of %d negatives (%.2f%%)" % (found, tot, found/tot*100)
    print "Average level (+) for all: %.2f" % (avg_level / tot)
    print "Average level (f) for trues: %.2f" % (avg_p / tot)
    print "Average level (nf) for falses: %.2f" % (avg_f / wrong)
    return wrong