Python屏幕捕获错误(Python screen capture error)
我正在尝试修改这里给出的用于屏幕流的代码。 在上面的教程中,它用于从磁盘读取图像,而我试图截取屏幕截图。 我收到此错误。
断言isinstance(数据,字节)'应用程序必须写入字节'AssertionError:应用程序必须写入字节
我应该做些什么改变才能发挥作用?
这就是我迄今为止所做的 -
<br>index.html<br> <html> <head> <title>Video Streaming Demonstration</title> </head> <body> <h1>Video Streaming Demonstration</h1> <img src="{{ url_for('video_feed') }}"> </body> </html>
app.py#!/usr/bin/env python from flask import Flask, render_template, Response import time # emulated camera from camera import Camera # Raspberry Pi camera module (requires picamera package) # from camera_pi import Camera app = Flask(__name__) @app.route('/') def index(): """Video streaming home page.""" return render_template('index.html') def gen(camera): """Video streaming generator function.""" while True: time.sleep(0.1) frame = camera.get_frame() yield (frame) @app.route('/video_feed') def video_feed(): """Video streaming route. Put this in the src attribute of an img tag.""" return Response(gen(Camera()), mimetype='multipart/x-mixed-replace; boundary=frame') if __name__ == '__main__': app.run(host='0.0.0.0', debug=True, threaded=True)
camera.pyfrom time import time from PIL import Image from PIL import ImageGrab import sys if sys.platform == "win32": grabber = Image.core.grabscreen class Camera(object): def __init__(self): #self.frames = [open('shot0' + str(f) + '.png', 'rb').read() for f in range(1,61)] self.frames = [ImageGrab.grab() for f in range(1,61)] def get_frame(self): return self.frames[int(time()) % 3]
完整的错误: 链接
I am trying to modify the code given here for screen streaming. In the above tutorial it was for reading images from disk whereas I am trying to take screenshots. I receive this error.
assert isinstance(data, bytes), 'applications must write bytes' AssertionError: applications must write bytes
What changes should I make for it to work?
This is what I've done so far -
<br>index.html<br> <html> <head> <title>Video Streaming Demonstration</title> </head> <body> <h1>Video Streaming Demonstration</h1> <img src="{{ url_for('video_feed') }}"> </body> </html>
app.py#!/usr/bin/env python from flask import Flask, render_template, Response import time # emulated camera from camera import Camera # Raspberry Pi camera module (requires picamera package) # from camera_pi import Camera app = Flask(__name__) @app.route('/') def index(): """Video streaming home page.""" return render_template('index.html') def gen(camera): """Video streaming generator function.""" while True: time.sleep(0.1) frame = camera.get_frame() yield (frame) @app.route('/video_feed') def video_feed(): """Video streaming route. Put this in the src attribute of an img tag.""" return Response(gen(Camera()), mimetype='multipart/x-mixed-replace; boundary=frame') if __name__ == '__main__': app.run(host='0.0.0.0', debug=True, threaded=True)
camera.pyfrom time import time from PIL import Image from PIL import ImageGrab import sys if sys.platform == "win32": grabber = Image.core.grabscreen class Camera(object): def __init__(self): #self.frames = [open('shot0' + str(f) + '.png', 'rb').read() for f in range(1,61)] self.frames = [ImageGrab.grab() for f in range(1,61)] def get_frame(self): return self.frames[int(time()) % 3]
Full error : Link
原文:https://stackoverflow.com/questions/40060653
最满意答案
尝试使用UnmanagedMemoryStream :
此类支持使用现有的基于流的模型访问非托管内存,并且不要求将非托管内存中的内容复制到堆中。
这意味着您将寻找/读取/重置流,但这避免了编组。 它仍然不是你希望在.NET属性中包含这些访问的意义上的。
另一种选择:也许你可以在获得非托管指针后使用System.Buffer 。 你可能需要做一些聪明的演员。
Try using the UnmanagedMemoryStream:
This class supports access to unmanaged memory using the existing stream-based model and does not require that the contents in the unmanaged memory be copied to the heap.
This means you will be seeking/reading/resetting the stream, but this avoids the marshalling. It's still not live in the sense you'd want to probably want to wrap these accesses in .NET properties.
Another alternative: maybe you could use System.Buffer, after getting the unmanaged pointer. You may need to do some clever casting.
相关问答
更多-
Nios 2“Hello World”?(Nios 2 “Hello World”?)[2022-04-07]
该程序正在电路板上运行。 从节目评论... 这个例子将'Hello from Nios II'打印到STDOUT流。 这种情况下的STDOUT流是软件终端。 因此,Nios II板正在运行hello world程序并将输出发送到计算机。 要使用电路板上的屏幕,您必须使用SOPC构建器将LCD显示器包含在配置中,然后直接写入LCD屏幕。 The program IS running on the board. From the program comments... This example prints ' ... -
在C#中释放非托管代码(Freeing up of unmanaged code in C#)[2022-04-25]
您可以使用Marshal.Copy方法(IntPtr,Double [],Int32,Int32)将double值数组从非托管ptr复制到托管ffData数组。 IntPtr ptr = ComputeFFTW(packetSig, packetSig.Length, (int)samplFrequency,(int)fftPoints); Marshal.Copy(ptr, fftData, 0, fftData.Length); 如果ComputeFFTW返回指向动态分配内存的指针,则需要在使用后 ... -
尝试这个: [DllImport("data.dll")] internal static unsafe extern int MyExternalFunction(DATA[] pData); 并完全省略C#指针。 编辑:我没有测试,但它是有道理的指针的方式将无法正常工作,因为托管数组的内存布局是不相同的非托管的。 编组人员必须有机会获取数组并将其转换为本地格式,然后再转回。 我不确定在这种情况下是否需要ref修饰符,但它可能是一个选项。 try this: [DllImport("data.dll") ...
-
编组结构,嵌入指针从C#到非托管驱动程序(Marshalling struct with embedded pointer from C# to unmanaged driver)[2020-09-24]
编组具有内联指针的结构时,需要将该值定义为IntPtr而不是数组 [StructLayout(LayoutKind.Sequential)] public struct IoctlWriteRegsIn { public uint Address; public IntPtr Buffer; public uint Size; } When marshaling a struct which has an inline pointer, you need to define the ... -
C#结构中的事件?(Are Events in C# structs?)[2022-05-05]
像EventHandler这样的委托类型是不可变的类型。 当您使用赋值( = )或复合赋值( += )时,会创建一个新实例。 该字典保留旧的实例。 委托类型是引用类型,但重要的是它们的不变性。 当你有一个event ,使用+=语法甚至不是一个赋值。 它是对add访问器或事件的调用。 它将以线程安全的方式重新分配后台字段(新实例)。 请记住,你可以自己编写你的事件访问器。 例如: public static event EventHandler TestEvent { add { lock ( ... -
绝对使用Linq2Twitter - http://linqtotwitter.codeplex.com/ 它的UpdateStatus方法有11个重载 - 整个实现非常好。 所以你的例子是: var tweet = twitterCtx.UpdateStatus("Hello world"); Definitely use Linq2Twitter - http://linqtotwitter.codeplex.com/ It's UpdateStatus method has 11 overloads ...
-
我在我的工作中处理类似的问题,我的主要任务是编写托管接口到某些高性能,低延迟dll,这涉及到简单的情况,我必须使用简单的c ++ / cli包含原生类,其中包含一个指向本地类或更复杂的问题,其中本机代码是服务器端发布者,托管代码必须使用委托来订阅它,即它们必须转换为本地回调。 据我所知,底层.NET是一个复杂的COM服务器。 可以使用将ComVisible属性设置为true编写.net程序集,然后将其作为传统的COM组件,然后可以将其从本地C ++代码用作COM组件。 可以使用DllImport属性实现使用 ...
-
C#中的不安全代码不适用于开发单独的应用程序。 您可以将不安全的代码用于某种时间关键型操作,但通常它不是最有效和最方便的方法。 恕我直言,它主要是为了给C#提供与非托管动态链接库和非托管代码集成的机会,因此从我的观点来看,主要原因是INTEGRATION。 我可以看到托管和非托管代码集成的3种常用方法: C#和P / Invoke中的 不安全代码 。 在已编译的非托管DLL上构建C#包装器。 托管C ++ 。 通过现有的C / C ++代码构建托管程序集。 COM互操作。 从.NET客户端调用Runtime ...
-
来自非管理世界的C#结构能否“实时” - 更新?(Can C# structs coming from the unmanaged world be “live”-updating?)[2022-01-07]
尝试使用UnmanagedMemoryStream : 此类支持使用现有的基于流的模型访问非托管内存,并且不要求将非托管内存中的内容复制到堆中。 这意味着您将寻找/读取/重置流,但这避免了编组。 它仍然不是你希望在.NET属性中包含这些访问的意义上的。 另一种选择:也许你可以在获得非托管指针后使用System.Buffer 。 你可能需要做一些聪明的演员。 Try using the UnmanagedMemoryStream: This class supports access to unmanaged ... -
Spring MVC Samples中的“Hello World”来自哪里?(Where is the “Hello World” coming from in Spring MVC Samples)[2023-03-27]
看起来神奇的事情发生在JSP代码底部的jQuery中。 对于Hello World教程来说,这有点复杂,但除此之外。 单击链接,使用AJAX从服务器请求数据: $("a.textLink").click(function(){ var link = $(this); $.ajax({ url: link.attr("href"), dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, lin ...