【发布时间】:2020-06-02 08:45:56
【问题描述】:
我正在尝试将通过 aliexpress EasyCap 捕获的外部摄像头中的视频投射到我的 kivy 应用程序中。我遇到的一个问题是在尝试进行分段错误时崩溃
texture = Texture.create(size=(frame.shape[0], frame.shape[1]))
我发现问题出在kivy's side。它有时无法创建 NPOT 纹理。因此,我将其更改为 POT 形状并将可能的内容复制到另一个 numpy 数组中。
flipped = cv2.flip(frame, 0)
buf = np.zeros((512, 512, 3), dtype=np.uint8)
for i in range(min(frame.shape[0], 512)):
for j in range(min(frame.shape[1], 512)):
buf[i, j] = flipped[i, j]
buf = buf.tostring()
texture = Texture.create(size=(512, 512))
texture.blit_buffer(buf, colorfmt="bgr", bufferfmt="ubyte")
self.texture = texture
但它仍然会因以下行中的旧分段错误而崩溃:
texture.blit_buffer(buf, colorfmt="bgr", bufferfmt="ubyte")
如果相关,则在 buf.tostring() 之前的 cv2.imshow("image", buf) 正确显示图像。
这是原始代码:
from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2
import threading
from time import sleep
import numpy as np
class KivyCamera(Image):
def __init__(self, **kwargs):
super(KivyCamera, self).__init__(**kwargs)
self.fps = 30
self.capture = cv2.VideoCature(0)
threading.Thread(target=self.update).start()
def update(self):
while True:
ret, frame = self.capture.read()
if ret:
buf = cv2.flip(frame, 0).tostring()
texture = Texture.create(size=(frame.shape[0], frame.shape[1])
texture.blit_buffer(buf, colorfmt="bgr", bufferfmt="ubyte")
self.texture = texture
sleep(1.0 / self.fps)
class CamApp(App):
def build(self):
return KivyCamera()
if __name__ == "__main__":
CamApp().run()
【问题讨论】:
标签: python kivy opencv-python