小编ppp*_*ery的帖子

OpenCv 错误无法通过视频捕获打开相机

我正在通过 opencv 使用我的摄像头,然后在重新启动后突然运行我的代码,它显示以下错误:

[ WARN:0] global /io/opencv/modules/videoio/src/cap_v4l.cpp (802) open VIDEOIO ERROR: V4L: can't open camera by index 0
Traceback (most recent call last):
  File "test.py", line 20, in <module>
    retval, buffer_img = cv2.imencode('.jpg', frame)
cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/loadsave.cpp:877: error: (-215:Assertion failed) !image.empty() in function 'imencode'
Run Code Online (Sandbox Code Playgroud)
cap = cv2.VideoCapture(0)  # here it throws an error


import json
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    retval, buffer_img = cv2.imencode('.jpg', frame)

    resdata = base64.b64encode(buffer_img)

    resdata = "data:image/png;base64,"+ str(resdata.decode("utf-8"))
    PARAMS = {'image': resdata}

    # …
Run Code Online (Sandbox Code Playgroud)

python ubuntu opencv video-capture python-3.x

16
推荐指数
5
解决办法
4万
查看次数

16
推荐指数
2
解决办法
2770
查看次数

在Python中,当它是一个一次性的命名元组时,如何调用超类?

因此,我有一个用于串行API的大量消息Payload类,每个类都有许多不可变字段,一个解析方法和一些共享的方法.我构造它的方式是每个都将从字段行为的namedtuple继承,并从父类接收公共方法.但是,我在构造函数方面遇到了一些困难:

class Payload:
    def test(self):
        print("bar")

class DifferentialSpeed(Payload, namedtuple('DifferentialSpeed_', 
    'left_speed right_speed left_accel right_accel')):
    __slots__ = ()
    def __init__(self, **kwargs):
        super(DifferentialSpeed, self).__init__(**kwargs)
        # TODO: Field verification
        print("foo")

    @classmethod
    def parse(self, raw):
        # Dummy for now
        return self(left_speed = 0.0, right_speed = 0.1,
                    left_accel = 0.2, right_accel = 0.3)

    def __str__(self):
        return "Left Speed: %fm/s\nRight Speed: %fm/s\n"\
            "Left Acceleration: %fm/s^2\nRight Acceleration: %fm/s^2" % (
            self.left_speed, self.right_speed, self.left_accel, self.right_accel)


payload = DifferentialSpeed.parse('dummy')
print(payload)
Run Code Online (Sandbox Code Playgroud)

这有效,但我得到以下警告:

DeprecationWarning: object.__init__() takes no parameters
  super(DifferentialSpeed, self).__init__(**kwargs)
Run Code Online (Sandbox Code Playgroud)

如果我**kwargs …

inheritance multiple-inheritance super namedtuple python-3.x

13
推荐指数
2
解决办法
5860
查看次数

包子与nestjs兼容吗?

我目前有一个使用 Nestjs 的项目,我想知道它与 Bun 1.0 的兼容性如何。我在互联网上没有找到明确的答案,我不想将我的项目转移到bun上,只是因为兼容性问题而陷入死胡同。

这是一个目前使用react和mongodb的小项目,但我计划使用bun中sqlite的内置支持。

我已经尝试在项目中使用bun,并且我可以安装我当前正在使用的模块,但我担心它能走多远。我没有时间测试所有内容,所以如果我能得到一些信息那就太好了。

nestjs bun

13
推荐指数
1
解决办法
1万
查看次数

Python - 从for循环创建json对象数组

我试图从html中提取值,然后将它们转换为json数组,到目前为止,我已经能够得到我想要的,但只能作为单独的字符串:

我做了两个for循环:

for line in games_html.findAll('div', class_="product_score"):
  score= ("{'Score': %s}" % line.getText(strip=True))
  print score

for line in games_html.findAll('a'):
  title= ("{'Title': '%s'}" % line.getText(strip=True))
  print title
Run Code Online (Sandbox Code Playgroud)

产生这两个输出:

{'Title': 'Uncanny Valley'}
{'Title': 'Subject 13'}
{'Title': '2Dark'}
{'Title': 'Lethal VR'}
{'Title': 'Earthlock: Festival of Magic'}
{'Title': 'Knee Deep'}
{'Title': 'VR Ping Pong'}
Run Code Online (Sandbox Code Playgroud)

{'Score': 73}
{'Score': 73}
{'Score': 72}
{'Score': 72}
{'Score': 72}
{'Score': 71}
{'Score': 71}
Run Code Online (Sandbox Code Playgroud)

(它们更长,但你可以对此有所了解...)

我如何使用python从这些中创建一个json数组,如下所示:

[{'Title': 'Uncanny Valley', 'Score': 73}, {....}]
Run Code Online (Sandbox Code Playgroud)

之后我会使用生成的数组做其他事情....

我是否需要将循环中的项目存储到列表中然后合并它们?你能否根据我的情况说明一个例子?

非常感谢帮助,这对我来说是一次非常酷的学习体验,因为我直到现在才使用bash.Python看起来更性感.

python maps json loops list

12
推荐指数
1
解决办法
3万
查看次数

Python:访问使用ElementTree解析的xml文件中的嵌套子代

我是xml解析的新手.此xml文件具有以下树:

FHRSEstablishment
 |--> Header
 |    |--> ...
 |--> EstablishmentCollection
 |    |--> EstablishmentDetail
 |    |    |-->...
 |    |--> Scores
 |    |    |-->...
 |--> EstablishmentCollection
 |    |--> EstablishmentDetail
 |    |    |-->...
 |    |--> Scores
 |    |    |-->...
Run Code Online (Sandbox Code Playgroud)

但是当我使用ElementTree访问它并查找child标签和属性时,

import xml.etree.ElementTree as ET
import urllib2
tree = ET.parse(
   file=urllib2.urlopen('http://ratings.food.gov.uk/OpenDataFiles/FHRS408en-GB.xml' % i))
root = tree.getroot()
for child in root:
   print child.tag, child.attrib
Run Code Online (Sandbox Code Playgroud)

我只得到:

Header {}
EstablishmentCollection {}
Run Code Online (Sandbox Code Playgroud)

我认为这意味着他们的属性是空的.为什么会这样,我怎么能访问内部嵌套孩子EstablishmentDetailScores

编辑

感谢下面的答案,我可以进入树内,但如果我想要检索诸如此类的值,则会Scores失败:

for node in root.find('.//EstablishmentDetail/Scores'): …
Run Code Online (Sandbox Code Playgroud)

python xml tree elementtree xml-parsing

12
推荐指数
1
解决办法
2万
查看次数

在 DataFrame 内的数据类型之间切换

我试图搜索是否有一种方法可以轻松更改带有数字的字符串的数据类型。比如我面临的问题如下:

df = pl.Dataframe({"foo": 
    ["100CT pen", "pencils 250CT", "what 125CT soever", "this is a thing"]}
)
Run Code Online (Sandbox Code Playgroud)

我可以提取并创建一个名为 的新列{"bar": ["100", "250", "125", ""]}。但后来我找不到一个方便的函数来将此列转换为 Int64 或 float dtypes,以便结果为[100, 250, 125, null].

另外,反之亦然。[100, 250, 125, 0]有时,有一个方便的函数将的 列转换为 会很有用["100", "250", "125", "0"]。它是已经存在的东西吗?

python python-polars

12
推荐指数
2
解决办法
2万
查看次数

系统在OS X上处于空闲状态时的通知

我知道有一些方法可以在OS X上使用IOKit框架获得系统空闲时间,但我想知道是否有可用的通知.

我可以创建一个计时器来检查空闲时间是否大于x,这很好.如果我在几秒钟后检测到空闲模式并不重要.

问题是检测Mac何时不再空闲.我希望我的应用尽快显示通知,而不是几秒钟之后.

有办法收到通知吗?(iChat似乎有一个)

macos cocoa notifications

11
推荐指数
1
解决办法
3771
查看次数

jQuery - 检测鼠标是否仍然存在?

我想知道是否有办法在jQuery中检测鼠标是否空闲3秒钟.有没有我不知道的插件?因为我不相信有一个原生的jQuery方法.任何帮助将非常感激!

javascript mouse jquery plugins

11
推荐指数
2
解决办法
1万
查看次数

Why does using `or` within an except clause not cause a SyntaxError? Is there a valid use for it?

At work, I stumbled upon an except clause with an or operator:

try:
    # Do something.
except IndexError or KeyError:
    # ErrorHandling
Run Code Online (Sandbox Code Playgroud)

I know the exception classes should be passed as a tuple, but it bugged me that it wouldn't even cause a SyntaxError.

So first I wanted to investigate whether it actually works. And it doesn't.

>>> def with_or_raise(exc):
...     try:
...         raise exc()
...     except IndexError or KeyError:
...         print('Got ya!')
...

>>> with_or_raise(IndexError)
Got ya!

>>> …
Run Code Online (Sandbox Code Playgroud)

python error-handling

11
推荐指数
1
解决办法
141
查看次数