Question

I am getting an MJPEG stream from an IP Camera which I am viewing and saving on my computer. The code of how I am doing it can be found here. The answer explains how to extract the images from the stream and save them.

For extracting the images I am using the method listed in the answer and for saving it I am simply putting the images in an avi container using OpenCV. The code is given below.

writer=cv.CreateVideoWriter("video1.avi", cv.CV_FOURCC('X', '2', '6', '4'), fps, (320,240))
cv_image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)bitmap=cv.CreateImageHeader((cv_image.shape[1], cv_image.shape[0]), cv.IPL_DEPTH_8U, 3)
cv.SetData(bitmap, cv_image.tostring(), cv_image.dtype.itemsize * 3 * cv_image.shape[1])
cv.WriteFrame(writer, bitmap)

Here bitmap is the image that I am displaying and putting in the avi container.

Since the image is from an IP Camera it must have some metyadata like a time stamp which the camera inserts.

Question: How do I extract the metadata?

I have thought of 2 ways to do this:

  1. Extract the frames from the video and then access them to access the time stamp.
  2. Extract the time stamp from the video itself.

How do I proceed? Which method do I use? I am using Python and Opencv and am working on Windows 7.

I also read this like related to what I am trying to do. It did not solve my problem.

Was it helpful?

Solution

if there is any metadata attached to the (single image) file, opencv will discard it, unfortunately.

also, the mjpeg protocol does not have any timestamps on its own (it's just a http-multi-part-form interleaved with images [pretty similar to email-attachments], so content-type and content-length is all you get there [and that only if you're using http1.1]).

sorry for the negative answer, but you'll have to look into image-processing tools apart from opencv for this.

OTHER TIPS

For Java, you can use Metadata Extractor. Besides, this you can use ImageMagick and Exiflib, both are excellent(excellent defined as highly stable, robust and actively developed) libraries, however these are command-line tools. For imagemagick, you can find other language bindings.

I had best luck accessing the video metadata with ffmpeg.

Eg.,

ffmpeg -i video_file.mp4

prints out information like this:

  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2019-02-25T00:00:07.000000Z
    location        : +XX.XXXX+XXX.XXXX/
    location-eng    : +XX.XXXX+XXX.XXXX/
  Duration: 00:16:28.86, start: 0.000000, bitrate: 7331 kb/s
    Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1920x1080, 7236 kb/s, SAR 1:1 DAR 16:9, 10.91 fps, 30 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2019-02-25T00:00:07.000000Z
      handler_name    : VideoHandle
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, mono, fltp, 96 kb/s (default)
    Metadata:
      creation_time   : 2019-02-25T00:00:07.000000Z
      handler_name    : SoundHandle

There are python wrappers for ffmpeg, eg., ffmpeg-python. I personally just extracted the information I needed (creation_time) with a shell script, that of course can be called via the subprocess module.

For video metadata, use ffmpeg as Martin Drohmann says.

For image metadata (called "EXIF"):

OpenCV will discard image metadata; for instance, cv2.imread('bear.jpg') will produce a 3-dimensional numpy array of pixel intensities (the third dimension being the R,G,B channels).

So to extract EXIF data, you can use an alternative image processing library for Python, which is very popular, called pillow.

First, on the command line, install pillow: pip install pillow.

Then you can extract EXIF data from bear.jpg:

import PIL.Image
from PIL.ExifTags import TAGS
from pprint import pprint

image = PIL.Image.open('bear.jpg')

# Get the exif data and map to the correct tags
exif_data = {
                PIL.ExifTags.TAGS[k]: v
                for k,v in image._getexif().items()
                if k in PIL.ExifTags.TAGS
            }

pprint(exif_data)

Which will display the EXIF data:

{'ExposureBiasValue': (0, 3),
 'ExposureTime': (1, 40),
 'FNumber': (280, 100),
 'ISOSpeedRatings': 100,
 ...
}

For complete information on EXIF, see https://exiv2.org/tags.html.

I also was in need of getting metadata from a video and OpenCV just couldn't do it.

I have found this and this that seems to get metadata.

I haven't tested it yet though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top