Exemplo n.º 1
0
while True:
    dt = (time.time() - last_time)
    last_time = time.time()

    x = int((0.5 - random.random()) * noise_x + dt * slope_x -
            dt * piscope.x_correction * degree_per_step_x + bump(bump_x) * 50)
    y = int((0.5 - random.random()) * noise_y + dt * slope_y -
            dt * piscope.y_correction * degree_per_step_y + bump(bump_y) * 50)
    M = np.float32([[1, 0, x], [0, 1, y]])
    frame = cv2.warpAffine(frame, M, (cols, rows))

    tracker.queue.put(frame)

    vis_frame = frame.copy()
    draw_str(vis_frame, (20, 20), 'track count: %d' % len(tracker.tracks))
    if draw_trails:
        cv2.polylines(vis_frame, [
            np.int32(tr)
            for tr in [[t[:2] for t in tr] for tr in tracker.tracks]
        ], False, (0, 255, 0))

    for tr in tracker.tracks:
        cv2.circle(vis_frame, (tr[-1][0], tr[-1][1]), 2, (0, 255, 0), -1)

    cv2.imshow('lk_track', vis_frame)
    ch = 0xFF & cv2.waitKey(1)
    if ch == 27:
        break
    time.sleep(0.1)
        return frame, t0

    threadn = cv2.getNumberOfCPUs()
    pool = ThreadPool(processes = threadn)
    pending = deque()

    threaded_mode = True

    latency = StatValue()
    frame_interval = StatValue()
    last_frame_time = clock()
    while True:
        while len(pending) > 0 and pending[0].ready():
            res, t0 = pending.popleft().get()
            latency.update(clock() - t0)
            draw_str(res, (20, 20), "threaded      :  " + str(threaded_mode))
            draw_str(res, (20, 40), "latency        :  %.1f ms" % (latency.value*1000))
            draw_str(res, (20, 60), "frame interval :  %.1f ms" % (frame_interval.value*1000))
            cv2.imshow('threaded video', res)
        if len(pending) < threadn:
            ret, frame = cap.read()
            t = clock()
            frame_interval.update(t - last_frame_time)
            last_frame_time = t
            if threaded_mode:
                task = pool.apply_async(process_frame, (frame.copy(), t))
            else:
                task = DummyTask(process_frame(frame, t))
            pending.append(task)
        ch = 0xFF & cv2.waitKey(1)
        if ch == ord(' '):
      return 0


while True:
      dt = (time.time() - last_time)
      last_time = time.time()

      x = int((0.5-random.random())*noise_x + dt*slope_x-dt*piscope.x_correction*degree_per_step_x+ bump(bump_x)*50)
      y = int((0.5-random.random())*noise_y + dt*slope_y-dt*piscope.y_correction*degree_per_step_y+bump(bump_y)*50)
      M = np.float32([[1,0,x],[0,1,y]])
      frame = cv2.warpAffine(frame,M,(cols,rows))

      tracker.queue.put(frame)
      
      vis_frame = frame.copy()
      draw_str(vis_frame, (20, 20), 'track count: %d' % len(tracker.tracks))
      if draw_trails:
         cv2.polylines(vis_frame, [np.int32(tr) for tr in [[t[:2] for t in tr]  for tr in tracker.tracks]], False, (0, 255, 0))

      for tr in tracker.tracks:
         cv2.circle(vis_frame, (tr[-1][0], tr[-1][1]), 2, (0, 255, 0), -1)

      cv2.imshow('lk_track', vis_frame)
      ch = 0xFF & cv2.waitKey(1)
      if ch == 27:
          break
      time.sleep(0.1)