def main(args):
    process_fps = args['process_fps'] if 'process_fps' in args else None

    video = VideoReader(args['filename'], process_fps)
    video.seek(args['start'])
    # video.seek(5*60)
    # video.seek(7625/25)
    # First frame: 4:32 - 16:10

    frames = []

    for i in range(args['frames']):
        print("pos: ", video.current_frame)
        pos, img = video.next_frame()
        timestamp = pos
        args['image'] = img
        # show_image(img)

        try:
            balls = process(args)
        except:
            continue

        frames.append({
            # 'game_id': args['filename'],
            'timestamp': timestamp,
            'balls': list(map(raw_ball, balls))
        })

    print("Successfully processed frames: ", len(frames))

    # write_json(frames, "result.json")
    return json.dumps(frames)
Exemple #2
0
 def __init__(self, camera):
     self.initModel()
     self.initTwitter()
     self.frameTime = 0
     self.frames = 0
     self.lastImageTime = 0
     self.fps = 30
     self.lastTweetTime = 0
     self.vr = VideoReader(camera, self.fps)
Exemple #3
0
def test_iter(tmp_path):
    from videoreader import VideoReader
    import numpy as np

    video_path = make_test_video(tmp_path)
    vr = VideoReader(video_path)

    for frame_number, frame in enumerate(vr[:]):
        brightness = np.mean(frame)
        assert brightness >= frame_number - 2 and brightness <= frame_number + 2
Exemple #4
0
def test_index(tmp_path):
    from videoreader import VideoReader
    import numpy as np

    video_path = make_test_video(tmp_path)
    vr = VideoReader(video_path)

    for frame_number in range(vr.number_of_frames):
        frame = vr[frame_number]
        brightness = np.mean(frame)
        assert brightness >= frame_number - 2 and brightness <= frame_number + 2
Exemple #5
0
def test_slice(tmp_path):
    from videoreader import VideoReader
    import numpy as np

    video_path = make_test_video(tmp_path)
    vr = VideoReader(video_path)

    step = 10
    for index, frame in enumerate(vr[::step]):
        frame_number = index * step
        brightness = np.mean(frame)
        assert brightness >= frame_number - 2 and brightness <= frame_number + 2
Exemple #6
0
def test_frame_attrs(tmp_path):
    import cv2
    from videoreader import VideoReader

    video_path = make_test_video(tmp_path)
    vr = VideoReader(video_path)

    assert vr.frame_width == 480
    assert vr.frame_height == 640
    assert vr.frame_rate == 25.0
    assert vr.fourcc == cv2.VideoWriter_fourcc(*'MJPG')
    assert vr.frame_format == 0
    assert vr.number_of_frames == 255
    assert vr.frame_shape == (480, 640, 3)
    assert vr.current_frame_pos == 0.0
    def __init__(self,
                 filename: str,
                 window_title: str,
                 reverse=False,
                 qsize=16,
                 worker_type='thread'):
        video_reader = VideoReader(filename, reverse, qsize, worker_type)
        print(
            f'{filename} / {video_reader.frame_size} / {video_reader.fps} fps')

        cv.namedWindow(window_title)

        self._window_title = window_title
        self._video_reader = video_reader
        self._rendered_frames = 0
        self._interval = 1 / video_reader.fps

        self._next_execution = 0
        self._loop = asyncio.get_event_loop()

        # for performance measuring
        self._play_start_time = self._report_time = 0
Exemple #8
0
def test_export_boxes():
    vr = VideoReader(path_to_video)
    frames = list(vr[100:110])

    nb_flies = 3
    box_centers = 400 + np.ones((1000, nb_flies, 2))
    box_centers[100, 0, :] = [10, 10]
    box_centers[100, 2, :] = [900, 900]

    box_angles = np.zeros((1000, nb_flies, 1))
    box_angles[101, 0, 0] = 45
    box_angles[101, 2, 0] = -60

    box_size = np.array([100, 100])

    boxes, fly_id, fly_frames = export_boxes(frames, box_centers, box_angles=box_angles, box_size=box_size)
    assert np.all(boxes.shape == (len(frames) * nb_flies, *box_size, vr.frame_channels))
    boxes, fly_id, fly_frames = export_boxes(frames, box_centers, box_angles=box_angles, box_size=box_size)
    assert np.all(boxes.shape == (len(frames) * nb_flies, *box_size, vr.frame_channels))
    plt.subplot(4, 1, 1)
    plt.plot(fly_id)
    plt.subplot(4, 1, 2)
    plt.plot(fly_frames)
    plt.subplot(4, 3, 7)
    plt.imshow(boxes[0, :, :, 0])
    plt.subplot(4, 3, 8)
    plt.imshow(boxes[1, :, :, 0])
    plt.subplot(4, 3, 9)
    plt.imshow(boxes[2, :, :, 0])
    plt.subplot(4, 3, 10)
    plt.imshow(boxes[3, :, :, 0])
    plt.subplot(4, 3, 11)
    plt.imshow(boxes[4, :, :, 0])
    plt.subplot(4, 3, 12)
    plt.imshow(boxes[5, :, :, 0])
    plt.pause(2)
Exemple #9
0
def main(trackfilename: str,
         *,
         frame_start: int = 0,
         frame_stop: int = None,
         frame_step: int = 1,
         batch_size: int = 100,
         save_interval: int = 100,
         start_over: bool = False):

    expID = os.path.basename(trackfilename).partition('_tracks.h5')[0]

    track_path = f"{res_path}/{expID}/{expID}_tracks.h5"

    data_path = root + 'chainingmic/dat'
    video_path = f"{data_path}/{expID}/{expID}.mp4"
    if not os.path.exists(video_path):
        data_path = root + 'chainingmic/dat.processed'
        video_path = f"{data_path}/{expID}/{expID}.mp4"
    if not os.path.exists(video_path):
        raise FileNotFoundError('video file not found.')

    trackfixed_path = f"{res_path}/{expID}/{expID}_tracks_fixed.h5"
    pose_path = f"{res_path}/{expID}/{expID}_poses.h5"

    try:
        os.mkdir(os.path.dirname(trackfixed_path))
    except FileExistsError as e:
        logging.debug(e)

    if not os.path.exists(trackfixed_path):
        logging.info(f"   fixing tracks.")
        fix_tracks(track_path, trackfixed_path)
    else:
        logging.info(f"   fixed tracks exist")

    logging.info(f"   opening video {video_path}.")
    vr = VideoReader(video_path)

    logging.info(f"   loading tracks from {trackfixed_path}.")
    try:
        data = dd.io.load(trackfixed_path)
        centers = data['centers'][:]  # nframe, channel, fly id, coordinates
        tracks = data['lines']
        chbb = data['chambers_bounding_box'][:]
        heads = tracks[:, 0, :, 0, ::-1]  # nframe, fly id, coordinates
        tails = tracks[:, 0, :, 1, ::-1]  # nframe, fly id, coordinates
        heads = heads + chbb[1][0][:]  # nframe, fly id, coordinates
        tails = tails + chbb[1][0][:]  # nframe, fly id, coordinates
        box_angles = angles(heads, tails)
        box_centers = centers[:, 0, :, :]  # nframe, fly id, coordinates
        box_centers = box_centers + chbb[1][0][:]
        nb_flies = box_centers.shape[1]
        logging.info(f"   nflies: {nb_flies}.")
    except OSError as e:
        logging.error(f'   could not load tracks.')
        logging.debug(e)

    if frame_stop is None:
        frame_stop = data['frame_count']
        logging.info(f'   Setting frame_stop: {frame_stop}.')
    frame_numbers = range(frame_start, frame_stop, frame_step)
    batch_idx = list(range(0, len(frame_numbers), batch_size))
    batch_idx.append(
        frame_stop - frame_start - 1
    )  # ADRIAN TESTING THINGS, SHOULD BE REMOVED IF COMMITED ####################################
    logging.info(f"   frame range: {frame_start}:{frame_stop}:{frame_step}.")
    logging.info(
        f"   processing frames in {len(batch_idx)-1} batches of size {batch_size}."
    )

    box_size = [120, 120]
    network = load_network(network_path, image_size=box_size)
    priors_data = dd.io.load(priors_path)
    priors = priors_data['priors']
    nb_frames = len(frame_numbers)
    nb_parts = network.output_shape[-1]
    nb_boxes = nb_frames * nb_flies
    logging.info(f"   loading network from {network_path}.")

    too_small = False
    if not start_over and os.path.exists(pose_path):
        logging.info(f'loading existing results from {pose_path}.')
        d0 = dd.io.load(pose_path)
        if 'last_saved_batch' not in d0:
            logging.info(
                f"   existing results are from an old version - starting over."
            )
            start_over = True
        elif d0['positions'].shape[0] < nb_boxes:
            logging.info(
                f"   results structure too small for nboxes: {d0['positions'].shape[0]} < {nb_boxes} - starting and copying over."
            )
            start_over = True
            too_small = True
        else:
            d = d0
            del (d0)
            logging.info(f"   continuing from batch {d['last_saved_batch']}.")

    if start_over or not os.path.exists(pose_path):
        logging.info(f'initializing new results dictionary.')
        d = {
            'positions': np.zeros((nb_boxes, nb_parts, 2), dtype=np.uint16),
            'confidence': np.zeros((nb_boxes, nb_parts, 1), dtype=np.float16),
            'expID': expID,
            'fixed_angles': np.zeros((nb_boxes, 1), dtype=np.float16),
            'frame_numbers': frame_numbers,
            'fly_id': np.zeros((nb_boxes, ), dtype=np.uintp),
            'fly_frame': np.zeros((nb_boxes, ), dtype=np.uintp),
            'bad_boxes': np.zeros((nb_boxes, 1), dtype=np.bool),
            'last_saved_batch': 0,
            'prior_idxs': np.zeros((nb_boxes, ), dtype=np.bool)
        }

    if too_small:
        logging.info(f'   copying old results to new results dictionary.')
        last_box = batch_idx[d['last_saved_batch']] * nb_flies
        d['positions'][:last_box, ...] = d0['positions'][:last_box, ...]
        d['confidence'][:last_box, ...] = d0['confidence'][:last_box, ...]
        d['fixed_angles'][:last_box, ...] = d0['fixed_angles'][:last_box, ...]
        d['bad_boxes'][:last_box, ...] = d0['bad_boxes'][:last_box, ...]
        d['fly_id'][:last_box] = d0['fly_id'][:last_box]
        d['fly_frame'][:last_box] = d0['fly_frame'][:last_box]
        d['prior_idxs'][:last_box] = d0['prior_idxs'][:last_box]
        del (d0)

    for batch_num in range(d['last_saved_batch'], len(batch_idx) - 1):
        logging.info(f"PROCESSING BATCH {batch_num}.")
        batch_frame_numbers = list(
            range(frame_numbers[batch_idx[batch_num]],
                  frame_numbers[batch_idx[batch_num + 1]]))
        batch_box_numbers = list(
            range(batch_idx[batch_num] * nb_flies,
                  batch_idx[batch_num + 1] * nb_flies))
        logging.info(
            f"   loading frames {batch_frame_numbers[0]}:{batch_frame_numbers[-1]}."
        )
        frames = [frame[:, :, :1] for frame in vr[batch_frame_numbers]
                  ]  # keep only one color channel
        d['positions'][batch_box_numbers, ...], d['confidence'][
            batch_box_numbers, ...], _, d['bad_boxes'][
                batch_box_numbers, ...], d['fly_id'][batch_box_numbers], d[
                    'fly_frame'][batch_box_numbers], _, d['fixed_angles'][
                        batch_box_numbers, ...], d['prior_idxs'][
                            batch_box_numbers] = process_batch(
                                network, frames,
                                box_centers[batch_frame_numbers, ...],
                                box_angles[batch_frame_numbers,
                                           ...], box_size, priors)

        if batch_num % save_interval == 0:
            logging.info(
                f"   saving intermediate results after {batch_num} to {pose_path}."
            )
            dd.io.save(pose_path, d)
            d['last_saved_batch'] = batch_num

    logging.info(f"   saving poses to {pose_path}.")
    dd.io.save(pose_path, d)
Exemple #10
0
class Detector:
    def __init__(self, camera):
        self.initModel()
        self.initTwitter()
        self.frameTime = 0
        self.frames = 0
        self.lastImageTime = 0
        self.fps = 30
        self.lastTweetTime = 0
        self.vr = VideoReader(camera, self.fps)

    def initModel(self):
        self.interpreter = tf.lite.Interpreter(model_path=MODEL_FILE)
        self.interpreter.allocate_tensors()
        self.input_details = self.interpreter.get_input_details()
        self.output_details = self.interpreter.get_output_details()

        self.model_height = self.input_details[0]['shape'][1]
        self.model_width = self.input_details[0]['shape'][2]

        self.floating_model = (self.input_details[0]['dtype'] == np.float32)

    def initTwitter(self):
        with open(TWITTER_CONFIG) as f:
            data = json.load(f)
        auth = tweepy.OAuthHandler(data['apiKey'], data['apiSecret'])
        auth.set_access_token(data['token'], data['tokenSecret'])
        self.twitter = tweepy.API(auth)

    def detect(self, image):
        image = image.copy()

        # Test model on random input data.
        input_shape = self.input_details[0]['shape']
        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image_resized = cv2.resize(image_rgb,
                                   (self.model_width, self.model_height))
        input_data = np.expand_dims(image_resized, axis=0)
        if self.floating_model:
            input_data = (np.float32(input_data) - INPUT_MEAN) / INPUT_STD
        self.interpreter.set_tensor(self.input_details[0]['index'], input_data)

        # Run the network
        self.interpreter.invoke()

        boxes = self.interpreter.get_tensor(self.output_details[0]['index'])[0]
        classes = self.interpreter.get_tensor(
            self.output_details[1]['index'])[0]
        scores = self.interpreter.get_tensor(
            self.output_details[2]['index'])[0]

        return boxes, scores, classes

    def triggerDetection(self, frame, orig_frame):
        self.log('Norppa detected!')

        self.lastDetectTime = time.time()

        # Only react every 30 seconds
        if (time.time() - self.lastImageTime) < 30:
            return

        # Save the original image and frame with detection boxes
        filename = "images/frame%s.jpg" % datetime.now().strftime(
            "%Y%m%d-%H%M%S")
        o_filename = "images/orig_frame%s.jpg" % datetime.now().strftime(
            "%Y%m%d-%H%M%S")
        cv2.imwrite(filename, frame)
        cv2.imwrite(o_filename, orig_frame)
        self.log("Wrote %s" % filename)

        # Tweet
        self.tweetNorppaIsLive(frame)

        self.lastImageTime = time.time()

    def tweetNorppaIsLive(self, frame):
        # only tweet every TWEET_TIME_MINUTES minutes
        if time.time() - self.lastTweetTime < TWEET_TIME_MINUTES * 60:
            return

        # Get random message from messages.json
        with open(MESSAGES_FILE, 'r') as f:
            messages = json.load(f)

        randomMessage = random.choices(messages)[0].replace('#', 'hashtag-')

        filename = 'uploaded_image.jpg'
        cv2.imwrite(filename, frame)
        self.twitter.update_with_media(filename, status=randomMessage)
        self.log('Tweeted: %s' % randomMessage)
        os.remove(filename)
        self.lastTweetTime = time.time()

    def checkCoordinates(self, x, y, x2, y2):
        # You can add coordinate & width/height checks here
        width = x2 - x
        height = y2 - y
        area = width * height
        return area < 350000 and area > 10000

    def log(self, msg):
        date = datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
        print(date, msg)

    def run(self):
        print("Ready")
        self.vr.start()
        self.startTime = time.time()
        frameCounter = 0
        lastFrameTime = 0
        detected = False
        detections = 0

        # Wait for the stream to start
        while not self.vr.more() or time.time() - self.startTime > 10:
            time.sleep(0.5)

        frame = None
        while True:
            if not self.vr.more():
                print('asd')
                break

            # Read the latest frame
            frame = np.copy(self.vr.read())
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frameCounter += 1

            lastFrameTime = time.time()

            orig_frame = frame.copy()
            detected = False

            # Get detection boxes and scores from tensorflow
            boxes, scores, classes = self.detect(frame)

            # Debug logging
            if scores[0] > 0.4:
                self.log(scores[0])

            # Go through the boxes
            for i in range(len(scores)):
                score = scores[i]
                box = boxes[i]

                y = int(box[0] * HEIGHT)
                x = int(box[1] * WIDTH)
                y2 = int(box[2] * HEIGHT)
                x2 = int(box[3] * WIDTH)

                if score < THRESHOLD:
                    continue

                # Some sanity checks
                if not self.checkCoordinates(x, y, x2, y2):
                    continue

                # Norppa detected here!
                detected = True
                self.log(score)
                print((x, y), (x2, y2))

                # Draw the detection box to the frame
                cv2.rectangle(frame, (x, y), (x2, y2), (0, 255, 0), 2)
                label = '%s: %d%%' % ('norppa', int(score * 100))
                labelSize, baseLine = cv2.getTextSize(label,
                                                      cv2.FONT_HERSHEY_SIMPLEX,
                                                      0.7, 2)
                label_ymin = max(y, labelSize[1] + 10)
                # Draw white box to put label text in
                cv2.rectangle(frame, (x, label_ymin - labelSize[1] - 10),
                              (x + labelSize[0], label_ymin + baseLine - 10),
                              (255, 255, 255), cv2.FILLED)
                cv2.putText(frame, label, (x, label_ymin - 7),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
            if detected:
                detections += 1
            else:
                detections = 0

            # Only detect after the second frame in a row that breaks the threshold
            if detections >= 2:
                self.triggerDetection(frame, orig_frame)

            if frameCounter % 1000 == 0:
                self.log('1000 frames')
                frameCounter = 0
            frameTime = time.time() - lastFrameTime
            if frameTime < 1 / self.fps:
                time.sleep(1 / self.fps - frameTime)
        self.vr.stop()
        time.sleep(1)
Exemple #11
0
from io_parser import IOParser

# video io
from videoreader import VideoReader
from videowriter import VideoWriter

# OpenCV
import cv2

if __name__ == '__main__':
    parser = IOParser()
    reader = VideoReader(parser.input_file)
    writer = VideoWriter(parser.output_file, reader.fps, (reader.width, reader.height), color=False)

    '''
        OpenCV allows for you to easily switch between colorspaces using the cvtColor method
        Note - no video players will be able to play a single channel grayscale video so each channel, R,G,B are 
        given the same intensity values to represent grayscale.
    '''

    for frame in reader:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        '''
        if we didn't specify that our VideoWriter is grayscale, we would have to convert back to BGR to output a 
        playable version.
        '''
        # to_bgr = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
        writer.write_frame(gray)

    writer.close()
    reader.close()
    if filepath_timestamps is None:
        filepath_timestamps = Path(root, dat_path, datename,
                                   f'{datename}_timeStamps.h5')

    # Create samplestamps object
    ss = None
    if os.path.exists(filepath_daq) and os.path.exists(filepath_timestamps):
        ss, last_sample_number, sampling_rate = ld.load_times(
            filepath_timestamps, filepath_daq)
        if sampling_rate is None:
            sampling_rate = audio_sampling_rate
        path_tried = (filepath_daq, filepath_timestamps)
    elif os.path.exists(filepath_video):  # Video (+tracks) only
        # if there is only the video, generate fake timestamps for video and samples from fps
        from videoreader import VideoReader
        vr = VideoReader(filepath_video)
        frame_times = np.arange(0, vr.number_of_frames, 1) / vr.frame_rate
        if target_sampling_rate == 0 or target_sampling_rate is None:
            resample_video_data = False
            target_sampling_rate = vr.frame_rate
        frame_times[-1] = frame_times[
            -2]  # for auto-monotonize to not mess everything up
        sampling_rate = 2 * target_sampling_rate  # vr.frame_rate
        sample_times = np.arange(0, vr.number_of_frames,
                                 1 / sampling_rate) / vr.frame_rate
        last_sample_number = len(sample_times)
        ss = SampStamp(sample_times, frame_times)
        path_tried = (filepath_video)
    elif os.path.exists(filepath_daq) and not os.path.exists(
            filepath_timestamps):  # Audio (+ annotations) only
        # if there is no video and no timestamps - generate fake from samplerate and number of samples
Exemple #13
0
def main(expID: str = 'localhost-20180720_182837',
         frame_start: int = 1000,
         frame_stop: int = 2000,
         frame_step: int = 100):

    ## Fix tracks
    # Paths
    trackPath = f"{resPath}\{expID}\{expID}_tracks.h5"
    videoPath = f"{dataPath}\{expID}\{expID}.mp4"
    trackfixedPath = f"{resPath}\{expID}\\{expID}_tracks_fixed.h5"
    savingPath = f"{resPath}\{expID}"
    posePath = f"{savingPath}\{expID}_pose.h5"
    fixedBoxesPath = f"{savingPath}\{expID}_fixedBoxes.h5"

    # Do not fix if they are already fixed
    if not os.path.exists(trackfixedPath):
        logging.info(f"   doing adrian_fix_tracks")
        adrian_fix_tracks(trackPath, trackfixedPath)
    else:
        logging.info(f"   fixed tracks already exist")

    ## Load video
    logging.info(f"   loading video from {videoPath}.")
    vr = VideoReader(videoPath)
    ## Load track
    logging.info(f"   loading tracks from {trackfixedPath}.")
    has_tracks = False
    try:
        data = dd.io.load(trackfixedPath)
        centers = data['centers'][:]  # nframe, channel, fly id, coordinates
        tracks = data['lines']
        chbb = data['chambers_bounding_box'][:]
        heads = tracks[:, 0, :, 0, ::-1]  # nframe, fly id, coordinates
        tails = tracks[:, 0, :, 1, ::-1]  # nframe, fly id, coordinates
        heads = heads + chbb[1][0][:]  # nframe, fly id, coordinates
        tails = tails + chbb[1][0][:]  # nframe, fly id, coordinates
        has_tracks = True
        box_centers = centers[:, 0, :, :]  # nframe, fly id, coordinates
        box_centers = box_centers + chbb[1][0][:]
        nb_flies = box_centers.shape[1]
        logging.info(f"   nflies: {nb_flies}.")
    except OSError as e:
        logging.error(f'   could not load tracks.')

    ## Specifications for boxes
    if frame_stop == 0:
        frame_stop = data['frame_count']
        logging.info(f'   Setting frame_stop: {0}.'.format(frame_stop))
    frame_range = range(frame_start, frame_stop, frame_step)
    logging.info(f"   frame range: {frame_start}:{frame_stop}:{frame_step}.")

    ## Create list of frames
    logging.info(f"   getting frames from video.")
    frames = list(vr[frame_start:frame_stop:frame_step])
    nb_frames = len(frames)

    ## Calculate angle for export boxes
    logging.info(f"   calculating box angles.")
    box_angles = get_angles(heads[frame_range, ...], tails[frame_range, ...])

    ## Export boxes function
    logging.info(f"   exporting boxes of {expID}.")
    boxes, fly_id, fly_frame = export_boxes(frames,
                                            box_centers[frame_range, ...],
                                            box_size=np.array([120, 120]),
                                            box_angles=box_angles)
    boxes = normalize_boxes(boxes)
    # boxes = np.rot90(boxes,2,(1,2))   # Flips boxes, in case network expected fly images in the other direction

    ## Predictions
    logging.info(f"   doing predictions.")
    confmaps = predict_confmaps(networkPath, boxes[:, :, :, :1])
    logging.info(f"   processing confidence maps.")
    positions, confidence = process_confmaps_simple(confmaps)

    ## Recalculation of angles for further orientation fix
    logging.info(f"   recalculating box angles.")
    newbox_angles, bad_boxes = detect_bad_boxes_byAngle(positions)
    logging.info(
        f"   found {np.sum(bad_boxes)} cases of boxes with angles above threshold."
    )
    box_angles = box_angles + newbox_angles
    logging.info(f"   re-exporting boxes of {expID}.")
    boxes, fly_id, fly_frame = export_boxes(frames,
                                            box_centers[frame_range, ...],
                                            box_size=np.array([120, 120]),
                                            box_angles=box_angles)
    boxes = normalize_boxes(boxes)

    ## Final predictions
    logging.info(f"   re-doing predictions.")
    confmaps = predict_confmaps(networkPath, boxes[:, :, :, :1])
    logging.info(f"   re-processing confidence maps.")
    positions, confidence = process_confmaps_simple(confmaps)

    ## Saving data
    logging.info(f"   saving pose predictions to: {posePath}.")
    posedata = {
        'positions': positions,
        'confidence': confidence,
        'confmaps': confmaps,
        'expID': expID,
        'frame_range': frame_range,
        'fly_id': fly_id,
        'fly_frame': fly_frame,
        'bad_boxes': bad_boxes
    }
    dd.io.save(posePath, posedata)
    logging.info(f"   saving fixed boxes to: {fixedBoxesPath}.")
    fixedBoxesdata = {'boxes': boxes, 'fly_id': fly_id, 'fly_frame': fly_frame}
    dd.io.save(fixedBoxesPath, fixedBoxesdata)

    ## Play movie of the exported boxes
    if play_boxes:
        logging.info(f"   playing video of boxes.")
        vplay(boxes[fly_id == 0, ...])
Exemple #14
0
def main(video, json_logger=None, visualise_callback=False, mode=None, inferencer=None, model_name=None, image_size=None, model_alpha=None, shot_name=None, preset_name=None, prob_threshold=None, iou_threshold=None, union_threshold=None, model_precision=None, inference_device=None, num_shots_hint=None, video_descriptor=None, gender_estimation=False, emotions_recognision=False, run_faceid=False, create_feauture_list=None, faceid_search=False):
    if faceid_search:
        run_faceid = True
    if not(create_feauture_list is None):
        feauture_list = []
        run_faceid = True
        if preset_name is None:
            preset_name = 'accuracy'

    if not(mode is None):
        try:
            mode = str(mode)
        except (TypeError, ValueError):
            raise ErrorSignal(invalid_argument_value)
    else:
        mode = default_mode

    if not(json_logger is None):
        try:
            json_logger = str(json_logger)
        except (TypeError, ValueError):
            raise ErrorSignal(invalid_argument_value)

    exit_code = 1

    reader = VideoReader(video, width_hint, height_hint)
    detector = FaceDetection(reader.get_width(), reader.get_height(), inferencer=inferencer, model_name=model_name, image_size=image_size, model_alpha=model_alpha, shot_name=shot_name, preset_name=preset_name, prob_threshold=prob_threshold, iou_threshold=iou_threshold, union_threshold=union_threshold, model_precision=model_precision, inference_device=inference_device, num_shots_hint=num_shots_hint)
    if gender_estimation:
        gender_estimatior = GenderEstimation(reader.get_width(), reader.get_height(), inferencer=inferencer)
    if run_faceid:
        faceid = FaceID(reader.get_width(), reader.get_height(), inferencer=inferencer)
    if emotions_recognision:
        emotions_recogniser = EmotionsRecognision(reader.get_width(), reader.get_height(), inferencer=inferencer)
    if faceid_search:
        try:
            faceid_dict_f = open(faceid_dict, "r")
        except IOError:
            raise ErrorSignal(faceid_dict_missing)
        faceid_json = json.load(faceid_dict_f)
        faceid_dict_f.close()
        faceids = []
        for name in faceid_json:
            faceids.append((name, faceid_json[name]['threshold'], np.array(faceid_json[name]['feautures'])))

    callbacks = []

    if visualise_callback:
        callbacks.append(Visualiser(reader.get_width(), reader.get_height()))

    if not(json_logger is None):
        callbacks.append(JSONLogger(reader.get_width(), reader.get_height(), json_logger))

    if len(callbacks) == 0:
        reader.release()
        raise ErrorSignal(nothing_to_do)

    if mode == 'byframe':
        frame_number = 0
        try:
            while True:
                frame = reader.read()
                boxes = detector.detect(frame)
                genders = None
                faceid_feautures = None
                emotions = None
                names = None
                if gender_estimation:
                    genders = gender_estimatior.estimate(frame, boxes)
                if emotions_recognision:
                    emotions = emotions_recogniser.recognise(frame, boxes)
                if run_faceid:
                    faceid_feautures = faceid.feautures(frame, boxes)
                if faceid_search:
                    names = []
                    for i in range(faceid_feautures.shape[0]):
                        face_names = []
                        for fid in faceids:
                            d = np.min(cosine_dist_norm(faceid_feautures[i].reshape(1, 128), fid[2]))
                            if d < fid[1]:
                                face_names.append(fid[0])
                        names.append(face_names)
                if not(create_feauture_list is None):
                    if faceid_feautures.shape[0] > 1:
                        raise ErrorSignal(only_one_face_required_in_create_feauture_list_mode)
                    elif faceid_feautures.shape[0] == 1:
                        feauture_list.append(faceid_feautures[0])
                frame_number += 1
                for callback in callbacks:
                    try:
                        callback.call(video=video, frame=frame, boxes=boxes, frame_number=frame_number, genders=genders, emotions=emotions, faceid_feautures=faceid_feautures, names=names)
                    except QuitSignal as ret:
                        exit_code = int(ret)
                        break
                else:
                    continue
                break
        except KeyboardInterrupt:
            exit_code = 0
            pass
        reader.release()
        for callback in callbacks:
            callback.destroy()
    elif mode == 'realtime':
        raise NotImplementedError()
    else:
        raise ErrorSignal(unknown_mode)

    if not(create_feauture_list is None):
        try:
            create_feauture_list = str(create_feauture_list)
        except (KeyError, ValueError):
            raise ErrorSignal(invalid_argument_value)
        feautures, threshold = feauture_select(feauture_list)
        try:
            faceid_dict_f = open(faceid_dict, "r")
        except IOError:
            raise ErrorSignal(faceid_dict_missing)
        faceid_json = json.load(faceid_dict_f)
        faceid_dict_f.close()
        faceid_json[create_feauture_list] = {
            'threshold': float(threshold),
            'feautures': feautures.tolist()
        }
        try:
            faceid_dict_f = open(faceid_dict, "w")
        except IOError:
            raise ErrorSignal(faceid_dict_missing)
        json.dump(faceid_json, faceid_dict_f)
        faceid_dict_f.close()
        print(faceid_success_prefix + create_feauture_list + faceid_success_suffix)

    return exit_code
Exemple #15
0
from io_parser import IOParser

# video io
from videoreader import VideoReader
from videowriter import VideoWriter

# OpenCV
import cv2

# numpy
import numpy as np

if __name__ == '__main__':
    parser = IOParser()
    reader = VideoReader(parser.input_file)
    writer = VideoWriter(parser.output_file, reader.fps,
                         (reader.width, reader.height))
    '''
        OpenCV 3 offers a high-level tracking API that features several tracking algorithms you can experiment with.
        For now we'll use the Kernelized Correlation Filters tracker (KCF), which uses a Bag of Features (like Bag of Words)
        approach to define positive and negative 'patches' to differentiate between the foreground and background. This 
        is used to set up the Appearance Model of our tracker (how our tracked region looks) as well as the Motion Model 
        a predictive estimation of where the object will be based on its velocity and location in previous frames.
        
    '''
    # initialize the tracker
    tracker = cv2.Tracker_create('KCF')

    # bounding boxes are represented as x, y, w, h
    bounding_box = (400, 230, 30, 40)