Пример #1
0
def binary_motion_signal(video_file, theta=20, debug=False, skip_motionless_frames=True):
    blur_ksize = (3,) * 2
    blur_sigma = 0

    frame_gen = helper.video_frame_generator(video_file)
    frame = frame_gen.__next__()

    frame = preprocess_frame(frame, blur_ksize, blur_sigma)

    binary_motion_signal_images = []
    next_frame = frame_gen.__next__()
    while next_frame is not None:
        next_frame = preprocess_frame(next_frame, blur_ksize, blur_sigma)

        image_diff = np.abs(cv2.subtract(frame, next_frame))
        image_diff[image_diff < theta] = 0
        image_diff[image_diff >= theta] = 1
        binary_image = image_diff

        if not skip_motionless_frames or binary_image.sum() != 0:  # skip motionless frames
            binary_motion_signal_images.append(binary_image)

            if debug:
                helper.show(binary_image, 20)

        frame = next_frame
        next_frame = frame_gen.__next__()

    return binary_motion_signal_images
Пример #2
0
 def setMaxSeason(self): 
     if self.isValidShowSeasonEpisode:
         year=show(self.showName)['Year'].split(u'\u2013')
         if  year[1] == '' :
             self.maxSeason=date.today().year-int(year[0])+1
         else:
             year=map(int,show(self.showName)['Year'].split(u'\u2013'))
             self.maxSeason=year[1]-year[0]+1
 def setMaxSeason(self):
     if self.isValidShowSeasonEpisode:
         year = show(self.showName)['Year'].split(u'\u2013')
         if year[1] == '':
             self.maxSeason = date.today().year - int(year[0]) + 1
         else:
             year = map(int, show(self.showName)['Year'].split(u'\u2013'))
             self.maxSeason = year[1] - year[0] + 1
Пример #4
0
def test_blur_gray():
    img1 = os.path.join('sample_data', 'sample_lines_filled.png')

    # img2 = os.path.join('sample_data','blur.png')
    # helper.blur(img1,img2)
    # helper.show(img2)

    img3 = os.path.join('sample_data', 'gray.png')
    helper.gray_scale(img1, img3)
    helper.show(img3)
Пример #5
0
def test_rotate():
    img1 = os.path.join('sample_data', 'resized.png')

    # img2 = os.path.join('sample_data','rotated.png')
    # helper.rotate(img1,60,img2)
    # helper.show(img2)

    # img3 = os.path.join('sample_data','horizental_flip.png')
    # helper.flip_horizontal(img1,img3)
    # helper.show(img3)

    img4 = os.path.join('sample_data', 'vertical_flip.png')
    helper.flip_vertical(img1, img4)
    helper.show(img4)
Пример #6
0
def get_data():
    # Define Parameters
    theta = 20
    tau = 20

    scale_invariant_moments = []
    labels = []
    thetas = [theta] * 6
    taus = [tau] * 6
    classes = ['handwaving', 'boxing', 'handclapping', 'walking', 'jogging', 'running']

    for theta, tau, action in zip(thetas, taus, classes):
        binary_motion_signal_images = []
        for i in range(1, 10):
            for j in range(1, 3):
                person = str(i)
                if i < 10:
                    person = '0' + person

                file = 'input/' + action + '/person' + person + '_' + action + '_d' + str(j) + '_uncomp.avi'
                images = binary_motion_signal(file, theta=theta, debug=False)
                binary_motion_signal_images.extend(images)

        helper.generate_images('binary', action, binary_motion_signal_images)

        mhis = get_motion_history_images(binary_motion_signal_images, tau)

        debug = False
        if debug:
            for j in mhis:
                helper.show(j, 0, True)

        helper.generate_images('mhi', action, mhis)

        pq = [(2, 0), (1, 1), (0, 2), (3, 0), (2, 1), (1, 2), (0, 3), (2, 2)]
        for mhi in mhis:
            hu = hu_moments(mhi, pq)
            scale_invariant_moments.append(hu)
            labels.append(action)

    scale_invariant_moments = np.asarray(scale_invariant_moments)
    labels = np.asarray(labels)

    return scale_invariant_moments, labels
Пример #7
0
def test_create_with_color():
    helper.create_with_color(
        path=os.path.join('sample_data', 'create_with_color.png'))
    helper.show(os.path.join('sample_data', 'create_with_color.png'))
Пример #8
0
def test_create_transparent():
    helper.create_transparent(
        path=os.path.join('sample_data', 'create_trans.png'))
    helper.show(os.path.join('sample_data', 'create_trans.png'))
Пример #9
0
import image_chopper as ic
import XMLreader as r
import time
import helper as h
import os

xml_dir = './labels'
reader = r.XMLreader()
df = reader.read_configs(xml_dir, "*")
print('XML info')
print(df.head())

img_dir = './images'
img_chopper = ic.image_chopper(df, width=150, height=150)
images = img_chopper.get(img_dir)
print('images')
print(images.iloc[0:2])
h.show(images[0:4])

output_dir = './output'
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

start_time = time.time()
img_chopper.save(img_dir, output_dir, separate=True)
print('chopping time', time.time() - start_time)
def main(args):
    # PRINT PARAMS
    args_text = json.dumps(args.__dict__)
    print(args_text)
    # data checks
    if not (args.img_path is None and args.img_index is not None
            or args.img_path is not None and args.img_index is None):
        raise ValueError('can only set one of img-path, img-index')
    if args.img_path and not (args.orig_class or args.target_class):
        raise ValueError('orig and target class required with image path')
    if (args.target_class is None and args.img_index is None):
        raise ValueError('must give target class if not using index')

    assert args.samples_per_draw % args.batch_size == 0

    gpus = helper.get_available_gpus()
    print("available_gpus:", gpus)
    if args.gpus:
        if args.gpus > len(gpus):
            raise RuntimeError('not enough GPUs! (requested %d, found %d)' %
                               (args.gpus, len(gpus)))
        gpus = gpus[:args.gpus]
    if not gpus:
        raise NotImplementedError('no support for using CPU-only because lazy')
    if args.batch_size % (2 * len(gpus)) != 0:
        raise ValueError(
            'batch size must be divisible by 2 * number of GPUs (batch_size=%d, gpus=%d)'
            % (BATCH_SIZE, len(gpus)))
    print("gpus:", gpus)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    attack = RegionAttack(sess, args, model, gpus)

    # an input gray image
    num_targets = 1000
    SIZE = 299
    initial_img = np.zeros((SIZE, SIZE, 3)) + 0.5

    dir_prefix = args.out_dir
    os.system("mkdir -p {}/{}".format(dir_prefix, args.save))

    filename = "{}/{}.txt".format(dir_prefix, args.save)
    myfile = open(filename, 'a+')
    myfile.write(args_text + '\n')
    myfile.close()

    # main loop
    # generate data
    # generate gray imgs for attacking
    all_targets = np.eye(num_targets)
    all_inputs = [initial_img for i in range(num_targets)]
    all_inputs = np.asarray(all_inputs)

    original_predict, _ = attack.predict(initial_img)
    original_predict = np.squeeze(original_predict)
    original_class = np.argsort(original_predict)

    all_labels = np.zeros((num_targets, num_targets))
    all_labels[:, original_class[num_targets -
                                 1]] = 1  # true labels for gray img

    print('Done of generating data')
    img_no = 0
    total_success = 0
    l2_total = 0.0
    eval_cost_total = 0

    start_ID = 0
    #test_classes = num_targets
    test_classes = 5  # for debug purpose

    for i in range(start_ID, test_classes):
        inputs = np.squeeze(all_inputs[i:i + 1])
        targets = all_targets[i:i + 1]
        labels_real = all_labels[i:i + 1]
        print("true label:", np.argmax(labels_real))
        print("target:", np.argmax(targets))
        if np.argmax(targets) == orig_class:
            print("**Original class", orig_class)
            continue
        # test if the image is correctly classified
        oritinal_predict, _ = attack.predict(initial_img)
        original_predict = np.squeeze(original_predict)
        original_prob = np.sort(original_predict)
        original_class = np.argsort(original_predict)

        print("original probabilities:", original_prob[-1:-6:-1])
        print("original classification:", original_class[-1:-6:-1])
        print("original probabilities (most unlikely):", original_prob[:6])
        print("original classification (most unlikely):", original_class[:6])

        if args.size_selection > 0:
            # if True, we perform size selection
            # size_candidates = np.random.choice(range(32, 150), 10, replace=False)
            size_candidates = np.random.choice(
                range(32, 150), 2, replace=False)
            fh, fw, adv_init = attack.active_select_size(
                inputs, size_candidates, targets)
        else:
            # set a default size which works well
            fw = 64
            adv_init = initial_img

        regs = np.array([fw])
        for k_size in regs:
            fh, fw = k_size, k_size
            opt = {}
            opt['height'] = fh
            opt['width'] = fw
            opt['target'] = np.argmax(targets)

            img_no += 1

            timestart = time.time()
            if np.argmax(targets) == np.argmax(original_predict):
                adv, eval_costs = inputs, 1
            else:
                adv, eval_costs = attack.attack_fast(adv_init, opt)

            timeend = time.time()
            l2_distortion = np.sum((adv - inputs)**2)**.5
            l0_distortion = np.sum(abs(adv - inputs) > 0)
            l1_distortion = np.sum(abs(adv - inputs))
            li_distortion = np.max(abs(adv - inputs))

            adversarial_predict, _ = attack.predict(adv)

            adversarial_predict = np.squeeze(adversarial_predict)
            adversarial_prob = np.sort(adversarial_predict)
            adversarial_class = np.argsort(adversarial_predict)
            print("adversarial probabilities:", adversarial_prob[-1:-6:-1])
            print("adversarial classification:", adversarial_class[-1:-6:-1])
            success = False

            if adversarial_class[-1] == np.argmax(targets):
                success = True
            if success:
                total_success += 1
                l2_total += l2_distortion
                eval_cost_total += eval_costs
            suffix = "id{}_seq{}_prev{}_adv{}_{}_dist{}".format(
                img_no, i, original_class[-1], adversarial_class[-1], success,
                l2_distortion)
            print("Saving to", suffix)
            helper.show(
                adv, "{}/{}/{}_adversarial_{}.png".format(
                    dir_prefix, args.save, img_no, suffix))
            helper.show(
                adv - inputs, "{}/{}/{}_diff_{}.png".format(
                    dir_prefix, args.save, img_no, suffix))

            print(
                "[STATS][L1] total={}, seq={}, time={:.3f}, prev_class={}, new_class={}, distortion={:.5f}, l2_avg={:.5f}, success_rate={:.6f}, query={}, avg_query={:.1f},  !!!success!!!={}\n".
                format(
                    img_no, i, timeend - timestart, original_class[-1],
                    adversarial_class[-1], l2_distortion, 0
                    if total_success == 0 else l2_total / total_success,
                    total_success / float(img_no), eval_costs, 0
                    if total_success == 0 else eval_cost_total / total_success,
                    success))

            sys.stdout.flush()
            str1 = "img_no:{} prev:{} adv_label:{} Success:{} time:{:.3f} eval_costs:{} l2_dist:{:.3f} l0_dist:{} l1_dist:{} li_dist:{:.6f} success_rate:{:.5f} avg_query:{:.3f} size:{}\n".format(
                img_no, original_class[-1], adversarial_class[-1], success,
                timeend - timestart, eval_costs, l2_distortion, l0_distortion,
                l1_distortion, li_distortion, total_success / float(img_no), 0
                if total_success == 0 else eval_cost_total / total_success, fh)
            myfile = open(filename, 'a+')
            myfile.write(str1)
            myfile.close()
    success_rate = total_success / img_no
    l2_distortion_average = 0 if total_success == 0 else l2_total / total_success
    eval_cost_average = eval_cost_total / total_success
    myfile = open(filename, 'a+')
    myfile.write("success_rate:%f average_cost:%d average_l2_distortion:%.3f\n"
                 % (success_rate, eval_cost_average, l2_distortion_average))
    myfile.close()
    # close the file
    print("success_rate:%.3f average_cost:%d average_l2_distortion:%.3f" %
          (success_rate, eval_cost_average, l2_distortion_average))
Пример #11
0
world = [['G', 'G', 'G'], ['G', 'R', 'R'], ['G', 'G', 'G']]
measurements = ['R', 'R']
motions = [[0, 0], [0, 1]]
sensor_right = 1.0
p_move = 0.5
p = helper.localize(world, measurements, motions, sensor_right, p_move)
correct_answer = ([[0.0, 0.0, 0.0], [0.0, 0.33333333, 0.66666666],
                   [0.0, 0.0, 0.0]])

if p == correct_answer:
    print('Test 7 Passed')

#############################################################
# For the following test case, your output should be
# [[0.01105, 0.02464, 0.06799, 0.04472, 0.02465],
#  [0.00715, 0.01017, 0.08696, 0.07988, 0.00935],
#  [0.00739, 0.00894, 0.11272, 0.35350, 0.04065],
#  [0.00910, 0.00715, 0.01434, 0.04313, 0.03642]]
# (within a tolerance of +/- 0.001 for each entry)

colors = [['R', 'G', 'G', 'R', 'R'], ['R', 'R', 'G', 'R', 'R'],
          ['R', 'R', 'G', 'G', 'R'], ['R', 'R', 'R', 'R', 'R']]
measurements = ['G', 'G', 'G', 'G', 'G']
motions = [[0, 0], [0, 1], [1, 0], [1, 0], [0, 1]]
p = helper.localize(colors,
                    measurements,
                    motions,
                    sensor_right=0.7,
                    p_move=0.8)
helper.show(p)  # displays your answer
Пример #12
0
def test_sample_lines():
    helper.draw_sample_lines(os.path.join('sample_data', 'sample_lines.png'))
    helper.show(os.path.join('sample_data', 'sample_lines.png'))
Пример #13
0
def test_sample_rect():
    helper.draw_sample_rect(os.path.join('sample_data', 'sample_rect.png'))
    helper.show(os.path.join('sample_data', 'sample_rect.png'))
Пример #14
0
def test_sample_circle():
    helper.draw_sample_circle(os.path.join('sample_data', 'sample_circle.png'))
    helper.show(os.path.join('sample_data', 'sample_circle.png'))
Пример #15
0
def test_fill_on_img():
    img1 = os.path.join('sample_data', 'sample_lines.png')
    img2 = os.path.join('sample_data', 'sample_lines_filled.png')
    helper.fill_color(img1, img2, left_top=(50, 100))
    helper.show(img2)
Пример #16
0
def test_resize():
    img1 = os.path.join('sample_data', 'merged.png')
    img2 = os.path.join('sample_data', 'resized.png')
    #helper.resize(img1,0.5,img2)
    helper.resize(img1, 2, img2)
    helper.show(img2)
Пример #17
0
def test_merge():
    img1 = os.path.join('sample_data', 'sample_lines.png')
    img2 = os.path.join('sample_data', 'cropped.png')
    img3 = os.path.join('sample_data', 'merged.png')
    helper.merge(img2, img1, img3, (150, 150))
    helper.show(img3)
Пример #18
0
  x = t2.pen()
  t2.stamp()
  t2.pendown()
  t2.circle(100)
  t2.pen(x)

# global key event for turtle window
turtle.onkey(dot, "o")
turtle.onkey(stamp, "space")
w.onkey(testfun, "t")
w.onkey(w.bye, "x")

# extra key events for oturtles on window
t2._screen.onkey(t2.testfun,"h")

h.show(turtle._CFG)
print "use turtlekeys to move, v b m o space h t for Actions and x for close the Window"

# bind a callback on time
oldColor = None
def gruen(e):
  global oldColor
  print e
  ot.showturtle()
  oldColor = ot.color()
  ot.color("black","green")
  root.after(3000, cback)

def cback():
  global oldColor
  if isinstance(oldColor, tuple):
Пример #19
0
def test_watermark():
    img1 = os.path.join('sample_data', 'rotated.png')
    img2 = os.path.join('sample_data', 'watermark.png')
    helper.watermark(img1, img2, 'by iorilan', (700, 100))
    helper.show(img2)
Пример #20
0
def test_download():
    url = 'https://www.google.com/logos/doodles/2020/wear-a-mask-save-lives-copy-6753651837108810-s.png'
    img1 = os.path.join('sample_data', 'download.png')
    helper.download(url, img1)
    helper.show(img1)
Пример #21
0
def test_image_crop():
    helper.crop(os.path.join('sample_data',
                             'sample_circle.png'), (100, 20, 200, 100),
                os.path.join('sample_data', 'cropped.png'))
    helper.show(os.path.join('sample_data', 'cropped.png'))