簡體   English   中英

Kivy 和 python 中的 cv2(讀取條碼)

[英]Kivy with cv2 in python (Read barcode)

我嘗試使用 android 機器制作用於讀取條形碼的應用程序,但我遇到問題無法比較 cv 和 kivy,如果有人可以幫助我,請問。

使用 kivy 從 android 相機讀取條碼的代碼

from kivy.app import App
from kivy.uix.camera import Camera
from pyzbar.pyzbar import decode
import numpy as np
import cv2
from kivy.properties import ListProperty


class MainApp(App):
    
    def build(self):
        
        self.capture = cv2.VideoCapture(0)
        cam = Camera(play=True, resolution=(640, 480))
        #cap=cv2.VideoCapture(0)
        success, frame=self.read()
        for code in decode(frame):
            print(code.type)
            print(code.data.decode('utf-8'))

            cv2.imshow("Results", frame);
            cv2.waitKey(1);
 
       
        return cam

if __name__== "__main__":
    MainApp().run()

“我無法比較 cv 和 kivy”是什么意思? 我在 kivy 使用 cam 運行機器學習應用程序。 那么你究竟需要什么? 您需要在 cv window 中顯示 img,還是要在 kivy 中顯示相機幀? 您可以每隔 x 秒更新 kivy window:

    def __init__(self, **kw):
        super().__init__()
        self.capture = None

    def on_pre_enter(self, *args):
        super().__init__()
        self.capture = cv.VideoCapture(0)
        Clock.schedule_interval(self.update, 1.0 / 30)  # update 30 fps

    def on_leave(self, *args):
        self.capture.release()

然后你可以像這樣將幀顯示為 kivy 圖像紋理:

    def update(self, dt):
        ret, frame = self.capture.read()

        if ret:
            buf1 = cv.flip(frame, 0)
            buf = buf1.tobytes()
            image_texture = Texture.create(
                size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
            image_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
            self.ids['image'].texture = image_texture

您可以在 kivy 中定義圖像:

MDBoxLayout:
    size_hint: .65,1
    orientation: 'vertical'
    Image: #webcam
        id: image

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM