예제 #1
0
    def vid_manip(self, vid, output_dir):
        """
        Builds a "VideoManipulationRequest" and calls 'detect_one'

        Args:
            vid: The video uri to be provided to the analytic.
            output_dir: The output directoy for analytic output files

        Returns:
            The response "VideoManipulation" protobuf.
        """
        vid = self.map(vid)
        output_dir = self.o_map(output_dir)
        req = analytic_pb2.VideoManipulationRequest(options=self.options)
        mime, _ = get_media_type(vid)
        req.video.uri = vid
        req.video.type = mime
        req.request_id = str(uuid.uuid4())
        req.out_dir = output_dir
        logging.info(req)

        resp = self.detect_one(req)
        if resp.HasField('localization'):
            for mir in resp.localization.video_mask:  # type: analytic_pb2.MaskIntRange
                mir.mask.uri = self.o_unmap(mir.mask.uri)
            for mir in resp.localization.video_mask_optout:  # type: analytic_pb2.MaskIntRange
                mir.mask.uri = self.o_unmap(mir.mask.uri)

        for msg in resp.supplement:
            if msg.uri:
                msg.uri = self.o_unmap(msg.uri)
        return resp
예제 #2
0
def get_pipeline_req(media,
                     detection_id="",
                     analytic_ids=[],
                     out_dir="",
                     fuser_id=[],
                     tags=[]):
    req = pipeline_pb2.DetectionRequest()
    mime, mtype = get_media_type(media)
    if mtype == "image":
        img_req = analytic_pb2.ImageManipulationRequest()
        img_req.image.uri = media
        img_req.image.type = mtype
        img_req.out_dir = out_dir
        req.request.img_manip_req.CopyFrom(img_req)
    elif mtype == "video":
        vid_req = analytic_pb2.VideoManipulationRequest()
        vid_req.video.uri = media
        vid_req.video.type = mtype
        vid_req.out_dir = out_dir
        req.request.vid_manip_req.CopyFrom(vid_req)
    else:
        raise ValueError(
            "Unsupported media format.  Could not regocnize the mimetype for {!s}"
            .format(media))

    req.id = detection_id
    req.analytic_id.extend(analytic_ids)
    req.tags.update(tags)
    req.fuser_id.extend(fuser_id)
    return req
예제 #3
0
def get_detection(media, output_dir, request_id=None):
    mime, mtype = get_media_type(media)
    det = analytic_pb2.Detection()
    if not request_id:
        request_id = str(uuid.uuid4())
    if mtype == "image":
        req = analytic_pb2.ImageManipulationRequest()
        req.image.uri = media
        req.image.type = mime
        req.request_id = request_id
        req.out_dir = output_dir
        det.img_manip_req.MergeFrom(req)
    elif mtype == "video":
        req = analytic_pb2.VideoManipulationRequest()
        req.video.uri = media
        req.video.type = mime
        req.request_id = str(uuid.uuid4())
        req.out_dir = output_dir
        det.vid_manip_req.MergeFrom(req)
    else:
        raise ValueError(
            "Invalid media type. {!s} is not currently supported".format(
                mtype))

    return det
예제 #4
0
    def stream_detection(self, probe, donor, output_dir, client_output_path):
        det = analytic_pb2.Detection()
        stream_data = []
        if donor is not None:
            req = analytic_pb2.img_splice_req()
            req.probe.uri = self.map(probe)
            req.donor.uri = self.map(donor)
            req.request_id = str(uuid.uuid4())
            req.out_dir = out
            det.img_splice_req.MergeFrom(req)
            # stream_data.append(det)
        else:
            mime, type = get_media_type(probe)
            if type == "image":
                task = "imgManip"
                req = analytic_pb2.ImageManipulationRequest()
                req.image.uri = self.map(probe)
                req.image.type = mime
                req.request_id = str(uuid.uuid4())
                req.out_dir = output_dir
                det.img_manip_req.MergeFrom(req)
                # stream_data.append(det)

            elif type == "video":
                task = "vidManip"
                req = analytic_pb2.VideoManipulationRequest()
                req.video.uri = self.map(probe)
                req.video.type = mime
                req.request_id = str(uuid.uuid4())
                req.out_dir = output_dir
                det.vid_manip_req.MergeFrom(req)
                # stream_data.append(det)
        return self.stream.detect(det, client_output_path)
예제 #5
0
    def detect_batch(self, dir, out, make_dirs=False):
        """
        Traverses an input directory building and sending the appropriate request
        proto based on the media mtype of the files.

        Args:
            dir: The input directoy containing media files.  Should contain only
                image or video files and any subdirectories will not be used.
            output_dir: The parent directory for analytic output directories.
                Each request will have it's own output directory underneath this
                parent directory.

        Returns:
            A dictionary that maps the request_id (automatically generated UUID)
            to the response proto, or an empty dict if no files are found.
        """
        # Simple directory parsing, assume one level and only image/video files
        for _, _, files in os.walk(dir):
            break
        if not files:
            print("No files found in directory {!r}".format(dir),
                  file=sys.stderr)
            return {}

        output_dir = self.o_map(out)
        results = {}
        for f in files:
            f = os.path.join(dir, f)
            mime, mtype = get_media_type(f)
            f = self.map(f)
            logging.info("Processing {!s} of type {!s}".format(f, mtype))
            if mtype == "image":
                task = "imgManip"
                req = analytic_pb2.ImageManipulationRequest()
                req.image.uri = f
                req.image.type = mime

            elif mtype == "video":
                task = "vidManip"
                req = analytic_pb2.VideoManipulationRequest()
                req.video.uri = f
                req.video.type = mime

            req.request_id = str(uuid.uuid4())
            req.out_dir = os.path.join(output_dir, req.request_id)
            if make_dirs and not os.path.exists(
                    os.path.join(out, req.request_id)):
                print("Making directory -- {!s}".format(
                    os.path.join(output_dir, req.request_id)))
                os.mkdir(os.path.join(out, req.request_id))
            results[req.request_id] = self.detect_one(req)
        return results
예제 #6
0
    def vid_manip(self, vid, output_dir):
        """
        Builds a "VideoManipulationRequest" and calls 'detect_one'

        Args:
            vid: The video uri to be provided to the analytic.
            output_dir: The output directoy for analytic output files

        Returns:
            The response "VideoManipulation" protobuf.
        """
        vid = self.map(vid)
        output_dir = self.o_map(output_dir)
        req = analytic_pb2.VideoManipulationRequest()
        mime, _ = get_media_type(vid)
        req.video.uri = vid
        req.video.type = mime
        req.request_id = str(uuid.uuid4())
        req.out_dir = output_dir

        return self.detect_one(req)
예제 #7
0
mimetypes.add_type("video/MP2T", ".ts")
mimetypes.add_type("video/mp4", ".mp4")
mimetypes.add_type("video/quicktime", ".mov")
mimetypes.add_type("video/x-flv", ".flv")
mimetypes.add_type("video/x-ms-wmv", ".wmv")
mimetypes.add_type("video/x-msvideo", ".avi")

additional_image_types = frozenset(
    ["application/octet-stream", "application/pdf"])

additional_video_types = frozenset(
    ["application/x-mpegURL", "application/mxf"])

analytic_req_map = {
    "image": analytic_pb2.ImageManipulationRequest(),
    "video": analytic_pb2.VideoManipulationRequest()
}


def get_media_type(uri):
    """
    'get_media_type' takes a filepath and returns the typestring and media type.
    If the mimetype is not discernable, the typestring returned will be
    "application/octet-stream", and the media type "application".
    """
    filename, ext = os.path.splitext(uri)
    typestring = mimetypes.types_map.get(ext.lower(),
                                         'application/octet-stream')

    if typestring in additional_video_types:
        return typestring, 'video'