我已经工作了几天,试图在python-for-android中使用cv2.VideoCapture()。我正在使用kivy和buildozer来构建apk for android。这是我的代码
from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2
class KivyCamera(Image):
def __init__(self, capture, fps, **kwargs):
super(KivyCamera, self).__init__(**kwargs)
self.capture = capture
Clock.schedule_interval(self.update, 1.0 / fps)
def update(self, dt):
print 'hello'
ret, frame = self.capture.read()
print frame
if ret:
# convert it to texture
buf1 = cv2.flip(frame, 0)
buf = buf1.tostring()
image_texture = Texture.create(
size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
image_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
# display image from the texture
self.texture = image_texture
class CamApp(App):
def build(self):
self.capture = cv2.VideoCapture(1)
self.my_camera = KivyCamera(capture=self.capture, fps=30)
return self.my_camera
def on_stop(self):
#without this, app will not exit even if the window is closed
self.capture.release()
if __name__ == '__main__':
CamApp().run()
此代码完全在PC上运行并成功显示视频源,但如果我使用buildozer将其构建到apk文件,则应用程序启动后会立即崩溃。 有人可以帮我解决这个问题吗? 无论如何,如果有人这样做,请评论..
这是来自android设备的logcat:
08-14 10:05:16.807 16826-16826/? A/DEBUG: backtrace:
08-14 10:05:16.807 16826-16826/? A/DEBUG: #00 pc 00018b00 /system/lib/libc.so (strlen+71)
08-14 10:05:16.807 16826-16826/? A/DEBUG: #01 pc 00505fc9 /data/data/org.test.myapp/files/app/lib/python2.7/site-packages/cv2.so
08-14 10:05:16.807 16826-16826/? A/DEBUG: #02 pc 005069fb /data/data/org.test.myapp/files/app/lib/python2.7/site-packages/cv2.so
08-14 10:05:16.807 16826-16826/? A/DEBUG: #03 pc 00506e0d /data/data/org.test.myapp/files/app/lib/python2.7/site-packages/cv2.so
08-14 10:05:16.807 16826-16826/? A/DEBUG: #04 pc 0037a4fb /data/data/org.test.myapp/files/app/lib/python2.7/site-packages/cv2.so
08-14 10:05:16.807 16826-16826/? A/DEBUG: #05 pc 0037aa71 /data/data/org.test.myapp/files/app/lib/python2.7/site-packages/cv2.so
08-14 10:05:16.807 16826-16826/? A/DEBUG: #06 pc 0037261d /data/data/org.test.myapp/files/app/lib/python2.7/site-packages/cv2.so
08-14 10:05:16.807 16826-16826/? A/DEBUG: #07 pc 0037291b /data/data/org.test.myapp/files/app/lib/python2.7/site-packages/cv2.so
08-14 10:05:16.807 16826-16826/? A/DEBUG: #08 pc 003729ff /data/data/org.test.myapp/files/app/lib/python2.7/site-packages/cv2.so
08-14 10:05:16.807 16826-16826/? A/DEBUG: #09 pc 0015cf2b /data/data/org.test.myapp/files/app/lib/python2.7/site-packages/cv2.so
08-14 10:05:16.807 16826-16826/? A/DEBUG: #10 pc 000603c5 /data/app/org.test.myapp-1/lib/arm/libpython2.7.so (PyCFunction_Call+144)