Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opencv流式url python_Python_Opencv_Stream - Fatal编程技术网

Opencv流式url python

Opencv流式url python,python,opencv,stream,Python,Opencv,Stream,我想用opencv处理一个流式url,所以请阅读它。 首先,我尝试在窗口中阅读和显示视频,但我做不到。。。 我尝试了几种方法,但都不管用: 1。文件缓冲 此方法包括使用另一个库播放视频,将其保存到文件中,并使用opencv实时读取 问题:Opencv无法打开文件并引发错误 [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fa6f3825a00] moov atom not found OpenCV: Couldn't read video stream from file "tmp.

我想用opencv处理一个流式url,所以请阅读它。 首先,我尝试在窗口中阅读和显示视频,但我做不到。。。 我尝试了几种方法,但都不管用:

1。文件缓冲

此方法包括使用另一个库播放视频,将其保存到文件中,并使用opencv实时读取

问题:Opencv无法打开文件并引发错误

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fa6f3825a00] moov atom not found
OpenCV: Couldn't read video stream from file "tmp.mp4"
2。使用opencv打开流url

此方法包括使用Opencv直接打开url流。它可以工作,但视频不是以正常速度播放,而是以数据输入的速度读取,因此,如果视频质量低(数据较少),则加载速度更快,读取速度更快

此外,我在打开时遇到一个错误:

[AVBSFContext @ 0x7fe9f71e6920] Invalid NAL unit 0, skipping.
3。使用Opencv解码字节

此方法包括使用外部库读取数据,然后使用opencv对其进行解码。但是Opencv永远无法解码图像

这是我的代码:

import numpy as np
import cv2
import time
import requests
import streamlink


class Cam:

    def __init__(self, url):
        self.stream = None
        self.stream_url = url

    def choose_stream(self):
        print("Loading stream...")
        streams = streamlink.streams(self.stream_url)
        stream = streams['360p']
        print("Loaded !")
        return stream

    def show_img(self, ret, img):
        try:
            if ret:
                cv2.imshow('result', img)
        except:
            print("DERP")

    def run_with_file_buffer(self):
        self.stream = self.choose_stream()
        buffer_file_name = "tmp.mp4"
        video_file = open(buffer_file_name, "wb")
        fd = self.stream.open()
        for i in range(0, 512):
            if (i % 256) == 0:
                print("Buffering {i}...".format(i=i), end="\r")
            new_bytes = fd.read(1024)
            video_file.write(new_bytes)
        print("Done buffering.")

        cam = cv2.VideoCapture(buffer_file_name)
        while True:
            new_bytes = fd.read(1024 * 16)
            video_file.write(new_bytes)
            ret, img = cam.read()
            self.show_img(ret, img)
            if cv2.waitKey(10) == ord('q'):
                break
        video_file.close()
        fd.close()

    def run_with_stream_url(self):
        self.stream = self.choose_stream()
        print("Reading : " + self.stream.url)
        cam = cv2.VideoCapture(self.stream.url)
        if not cam.isOpened():
            print("Error opening video stream or file")
        while cam.isOpened():
            ret, img = cam.read()
            self.show_img(ret, img)
            if cv2.waitKey(10) == ord('q'):
                break

    def run_with_bytes(self):
        self.stream = self.choose_stream()
        fd = self.stream.open()
        data = b''
        while True:
            data += fd.read(1024)
            a = data.find(b'\xff\xd8')
            b = data.find(b'\xff\xd9')
            if a != -1 and b != -1:
                img_data = data[a:b + 2]
                data = data[b + 2:]
                np_data = np.frombuffer(img_data, dtype=np.uint8)
                # np_data = np.fromstring(img_data, dtype=np.uint8)
                if len(np_data) != 0: # Always false
                    img = cv2.imdecode(np_data, cv2.IMREAD_COLOR)
                    if img:
                        cv2.imshow('cam', img)
            if cv2.waitKey(10) == ord('q'):
                break


if __name__ == "__main__":
    url = 'https://www.twitch.tv/{id}'
    cam = Cam(url)
    cam.run_with_bytes()
    cv2.destroyAllWindows()