Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

I am using the OpenCV package with the face_recognition package to detect faces on my laptop webcam.

Whenever I run it, the code runs fine but I run into the same GStreamer error.

from imutils.video import VideoStream
import face_recognition
import pickle
import argparse
import time
import cv2
import imutils

ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", type=str, help="path to output video")
ap.add_argument("-y", "--display", type=int, default=1,
                help="0 to prevent display of frames to screen")
ap.add_argument("-d", "--detection", default="hog",
                type=str, help="Detection method (hog/cnn")
args = vars(ap.parse_args())


print("[INFO] loading encodings...")

data = pickle.load(open("encodings.p", "rb"))

print("[INFO] starting video stream...")
vs = VideoStream().start()
writer = None
time.sleep(3)

while True:

    frame = vs.read()

    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    rgb = imutils.resize(frame, width=750)
    r = frame.shape[1] / float(rgb.shape[1])

    boxes = face_recognition.face_locations(rgb, model=args["detection"])
    encodings = face_recognition.face_encodings(rgb, boxes)

    for encoding, locations in zip(encodings, boxes):
        matches = face_recognition.compare_faces(data["encodings"], encoding)
        name = "Unknown"
        names = {}

        if True in matches:
            ids = [i for (i, b) in enumerate(matches) if b]
            for i in ids:
                name = data["names"][i]
                names[name] = names.get(name, 0) + 1

            name = max(names, key=names.get)

        for (top, right, bottom, left) in boxes:

            top = int(top * r)
            right = int(right * r)
            bottom = int(bottom * r)
            left = int(left * r)

            cv2.rectangle(frame, (left, top), (right, bottom), (255, 0, 0), 3)
            y = top - 15 if top - 15 > 15 else top + 15
            cv2.putText(frame, name, (left, y),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 0), 2)

    if writer is None and args["output"] is not None:
        fourcc = cv2.VideoWriter_fourcc(*"MJPG")
        writer = cv2.VideoWriter(
            args["output"], fourcc, 20, (frame.shape[1], frame.shape[2]), True)

    if writer is not None:
        writer.write(frame)

    if args["display"] == 1:
        cv2.imshow("frame", frame)
        key = cv2.waitKey(1)

        if key == ord("q"):
            break

cv2.destroyAllWindows()
vs.stop()

if writer is not None:
    writer.release()

I can't find any problems, yet I always get this error:

[ WARN:0] global /home/azazel/opencv/modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

The camera still displays and the facial recog works but what does the error signify and how can I fix it?

like image 832
Kai Strachan Avatar asked Jul 25 '20 17:07

Kai Strachan


People also ask

Why can't I see the duration of a GStreamer stream?

The issue originates from opencv, and is still unresolved Sorry, something went wrong. The stream position is determined by Gstreamer, not by the camera video source. Since we created a live source with the cameras the duration is always undetermined. Maybe the programming team finds a workaround.

Why is GStreamer not working with OpenCV?

Essentially, it is a problem that arised due to the way Gstreamer reads in frames and the way OpenCV tracks video frames. I think it is worth a try to add the line of code and rebuild OpenCV to see if it solves the problem.

Is it possible to use GStreamer pipeline instead of videocapture?

However, the VideoCapture has alot of latency when used in my program so i decided to use the gstreamer pipeline instead. rtsp cameras generaly streams as h264/h265 encoded data. If you are trying to decode that data via on CPU but not GPU, it will not give you much increasing about speed.


1 Answers

This is a bug from trying to use Gstreamer with OpenCV. It is mentioned in https://github.com/opencv/opencv/issues/10324 and https://github.com/opencv/opencv/pull/14834 (fix in the second link)

Essentially, it is a problem that arised due to the way Gstreamer reads in frames and the way OpenCV tracks video frames. I think it is worth a try to add the line of code and rebuild OpenCV to see if it solves the problem.

like image 156
Iberico Avatar answered Oct 21 '22 15:10

Iberico