首页 \ 问答 \ Python屏幕捕获错误(Python screen capture error)

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.py

from 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.py

from 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
更新时间:2024-04-25 09:04

最满意答案

尝试使用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.

相关问答

更多
  • 该程序正在电路板上运行。 从节目评论... 这个例子将'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 ' ...
  • 您可以使用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") ...
  • 编组具有内联指针的结构时,需要将该值定义为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 ...
  • 像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 ...
  • 尝试使用UnmanagedMemoryStream : 此类支持使用现有的基于流的模型访问非托管内存,并且不要求将非托管内存中的内容复制到堆中。 这意味着您将寻找/读取/重置流,但这避免了编组。 它仍然不是你希望在.NET属性中包含这些访问的意义上的。 另一种选择:也许你可以在获得非托管指针后使用System.Buffer 。 你可能需要做一些聪明的演员。 Try using the UnmanagedMemoryStream: This class supports access to unmanaged ...
  • 看起来神奇的事情发生在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 ...

相关文章

更多

最新问答

更多
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • Java中的不可变类(Immutable class in Java)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • EXCEL VBA 基础教程下载
  • RoR - 邮件中的动态主体(部分)(RoR - Dynamic body (part) in mailer)
  • 无法在Google Script中返回2D数组?(Can not return 2D Array in Google Script?)
  • JAVA环境变量的设置和对path , classpth ,java_home设置作用和目的?
  • mysql 关于分组查询、时间条件查询
  • 如何使用PowerShell匹配运算符(How to use the PowerShell match operator)
  • Effective C ++,第三版:重载const函数(Effective C++, Third edition: Overloading const function)
  • 如何用DELPHI动态建立MYSQL的数据库和表? 请示出源代码。谢谢!
  • 带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
  • 使用前端框架带来哪些好处,相对于使用jquery
  • Ruby将字符串($ 100.99)转换为float或BigDecimal(Ruby convert string ($100.99) to float or BigDecimal)
  • 高考完可以去做些什么?注意什么?
  • 如何声明放在main之后的类模板?(How do I declare a class template that is placed after the main?)
  • 如何使用XSLT基于兄弟姐妹对元素进行分组(How to group elements based on their siblings using XSLT)
  • 在wordpress中的所有页面的标志(Logo in all pages in wordpress)
  • R:使用rollapply对列组进行求和的问题(R: Problems using rollapply to sum groups of columns)
  • Allauth不会保存其他字段(Allauth will not save additional fields)
  • python中使用sys模块中sys.exit()好像不能退出?
  • 将Int拆分为3个字节并返回C语言(Splitting an Int to 3 bytes and back in C)
  • 在SD / MMC中启用DDR会导致问题吗?(Enabling DDR in SD/MMC causes problems? CMD 11 gives a response but the voltage switch wont complete)
  • sed没有按预期工作,从字符串中间删除特殊字符(sed not working as expected, removing special character from middle of string)
  • 如何将字符串转换为Elixir中的函数(how to convert a string to a function in Elixir)