gocv icon indicating copy to clipboard operation
gocv copied to clipboard

Error opening video capture device

Open Wenshiqi222 opened this issue 2 years ago • 13 comments

Description

Recently,i wanna do some ai with the livestream, and i installed gocv and Opencv as https://github.com/hybridgroup/gocv shows step by step on my centOS VM, but wen i tried to open the stream in my code which is shown bleow

func (c *Client) StartOpencvStream() {
	var err error
	c.OpenCv, err = gocv.VideoCaptureFile(c.Url)

i got the error

OpenCV(4.5.3) /tmp/opencv/opencv-4.5.3/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): rtmp://192.168.137.160:1935/myapp/rus in function 'icvExtractPattern'


Error opening video capture device: rtmp://192.168.137.160:1935/myapp/rus

but the vidoe stream can be opened by the VLC, i cannot find the problem.

Steps to Reproduce

  1. prepare a video live stream by the FFmpeg,push the stream to the server ffmpeg -re -i "/home/rus.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://192.168.137.160:1935/myapp/rus
  2. Try to open the stream with gocv code:
func (c *Client) StartOpencvStream() {
	var err error
	c.OpenCv, err = gocv.VideoCaptureFile(c.Url) //  failed at this line

	if err != nil {
		log.Errorf("open stream url error:%v", err)
		return
	}
	//defer webcam.Close()
	defer func() {
		c.OpenCv.Close()
		close(c.BaseImg)
		close(c.WarnData)
	}()
  1. got the error shows below:
[ERROR:0] global /tmp/opencv/opencv-4.5.3/modules/videoio/src/cap.cpp (162) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.5.3) /tmp/opencv/opencv-4.5.3/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): rtmp://192.168.137.160:1935/myapp/rus in function 'icvExtractPattern'


Error opening video capture device: rtmp://192.168.137.160:1935/myapp/rus
  1. i try to change the livestream type to the HLS, but still got the error(but this time it didn‘t show the “can‘t ’find starting numer...error” as rtmp stream shows above): Error opening video capture device: http://192.168.137.160:8080/live/russ.m3u8

Your Environment

  • Operating System and version: Linux version 3.10.0-957.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) )
  • OpenCV version used:opencv lib version: 4.5.3
  • How did you install OpenCV? Follow the goCV project steps https://github.com/hybridgroup/gocv
  • GoCV version used:gocv version: 0.28.0
  • Go version: go version go1.16.3 linux/amd64
  • Did you run the env.sh or env.cmd script before trying to go run or go build? No

Wenshiqi222 avatar Sep 09 '21 07:09 Wenshiqi222

@deadprogram @drnic @mattn @lstep can somebody help me witht this, really in hurry

Wenshiqi222 avatar Sep 14 '21 08:09 Wenshiqi222

I'd check the syntax you are using, it is known to work with various streams.

deadprogram avatar Sep 14 '21 08:09 deadprogram

I'd check the syntax you are using, it is known to work with various streams.

You mean the stream's URI? but even with the local video file, still got the same error :

[root@master1 gocv]# pwd
/root/gocv/gocv
[root@master1 gocv]# go run ./cmd/capwindow/main.go /home/rus.mp4 
Error opening video capture device: /home/rus.mp4

Wenshiqi222 avatar Sep 14 '21 08:09 Wenshiqi222

The correct syntax for that command is go run ./cmd/capwindow/main.go 0 where 0 is the ID of the connected camera device e.g. webcam that you want to use. Not a file, which is why error says Error opening video capture device

deadprogram avatar Sep 14 '21 09:09 deadprogram

The correct syntax for that command is go run ./cmd/capwindow/main.go 0 where 0 is the ID of the connected camera device e.g. webcam that you want to use. Not a file, which is why error says Error opening video capture device

thanks, my bad! But the code "gocv.VideoCaptureFile("rtmp://192.168.137.160:1935/myapp/rus.mp4")" got the same error, is this also needed to pass the Device Number ?

Wenshiqi222 avatar Sep 14 '21 09:09 Wenshiqi222

I think what you want to use is https://pkg.go.dev/gocv.io/x/gocv#OpenVideoCapture but your URL may or may not be correct. That part you will have to figure out yourself, since is also based on server you are trying to use.

deadprogram avatar Sep 14 '21 09:09 deadprogram

I think what you want to use is https://pkg.go.dev/gocv.io/x/gocv#OpenVideoCapture but your URL may or may not be correct. That part you will have to figure out yourself, since is also based on server you are trying to use.

Yes, since i wanna open the Live Stream through RTMP Protocol,so the Function OpenVideoCapture will call the Function VideoCaptureFile any way. Just now I tried to Call the OpenVideoCapture Function First in my code ,but it didn't work . Now the URL may not be correct, but what else can I do with the strem URI o(╥﹏╥)o ,can gocv read the RTMP live stream?

Wenshiqi222 avatar Sep 14 '21 09:09 Wenshiqi222

If OpenCV can do it, GoCV can do it, but that all depends on the installed libs on your system, the server, syntax, etc.

deadprogram avatar Sep 14 '21 09:09 deadprogram

If OpenCV can do it, GoCV can do it, but that all depends on the installed libs on your system, the server, syntax, etc.

since my opencv was installed by the makefile under gocv repository, and as the tutorial shows, i can run the command:

[root@master1 gocv]# go run ./cmd/version/main.go
gocv version: 0.28.0
opencv lib version: 4.5.3

does this mean my opencv and libs is correctly installed?

Wenshiqi222 avatar Sep 14 '21 09:09 Wenshiqi222

That does not mean that you have any particular libs used internally to OpenCV that may require separate installation, such as gstreamer or FFMPEG. Sorry I cannot be more specific. GoCV is a wrapper so OpenCV docs are where to look for those kinds of details.

deadprogram avatar Sep 14 '21 09:09 deadprogram

That does not mean that you have any particular libs used internally to OpenCV that may require separate installation, such as gstreamer or FFMPEG. Sorry I cannot be more specific. GoCV is a wrapper so OpenCV docs are where to look for those kinds of details.

Really thanks for your help, i think it should be the server OpenCV env miss the FFMPEG libs , i will try to install this lib then try again! Really thanks for your patience!

Wenshiqi222 avatar Sep 14 '21 09:09 Wenshiqi222

No problem! It you get it figured out for your situation and have time to submit a PR for docs update someplace with what you have learned, I am sure others will find it very helpful.

deadprogram avatar Sep 14 '21 09:09 deadprogram

edit win_build_opencv.cmd try: line 41(building opencv): add -DWITH_FFMPEG=ON

yuezhilanyi avatar Sep 07 '22 03:09 yuezhilanyi