Opencv пустой белый экран на Android APK Python, приложение kivyAndroid

Форум для тех, кто программирует под Android
Ответить Пред. темаСлед. тема
Anonymous
 Opencv пустой белый экран на Android APK Python, приложение kivy

Сообщение Anonymous »

Я разрабатываю приложение для Android, которое использует алгоритмы обнаружения OpenCV для анализа изображений. В настоящее время я тестирую приложение с помощью эмулятора Android с эмулируемой камерой, поскольку у меня пока нет физического устройства для тестирования. Все остальные части приложения работают правильно, но я столкнулся с проблемой, из-за которой я не могу получить доступ к камере с помощью OpenCV.
Я пробовал использовать модуль камеры OpenCV, но постоянно получаю такие ошибки, как:
Error: Could not open OpenCV camera at index -1. Trying next index.
Error: Could not open any OpenCV camera.

Требуется ли какая-либо конкретная конфигурация или настройка для работы камеры OpenCV с эмулятором Android?
Я также рассматривал возможность использования Plyer для доступа к камеру, но я не уверен, совместима ли она с возможностями обработки изображений OpenCV. Будет ли использование Plyer подходящим решением в этом случае?
Буду очень благодарен за любую помощь!
Главное, что я пробовал, это пробовать разные варианты Идентификаторы cv2.VideoCapture, но ни один из них, похоже, не работает, а также различные конфигурации взаимодействия Python с файлом kivy.
**
файлrequirements.txt:
* *
appdirs==1.4.4
build==1.2.2.post1
buildozer==1.5.0
certifi==2024.8.30
charset-normalizer==3.4.0
colorama==0.4.6
Cython==3.0.11
distlib==0.3.9
docutils==0.21.2
filelock==3.16.1
idna==3.10
Jinja2==3.1.4
Kivy==2.3.0
Kivy-Garden==0.1.5
kivymd==1.1.1
MarkupSafe==3.0.2
numpy==2.1.3
opencv-python==4.10.0.84
packaging==24.2
pexpect==4.9.0
pillow==10.4.0
platformdirs==4.3.6
plyer==2.1.0
ptyprocess==0.7.0
Pygments==2.18.0
pyjnius==1.5.0
pyproject_hooks==1.2.0
python-for-android==2024.1.21
requests==2.32.3
sh==1.14.3
six==1.16.0
toml==0.10.2
urllib3==2.2.3
virtualenv==20.27.1

**
Функция камеры только для файлов Python:
**
import cv2

from kivy.app import App

from kivy.lang import Builder #building files correctly

from kivy.uix.button import Button #for the buttons

from kivy.uix.boxlayout import BoxLayout # for the layout (there are different types of layouts in kivy this is just the most basic one)

from kivy.uix.floatlayout import FloatLayout # for non dynamic layout so the buttons dont move for camera page

from kivy.uix.screenmanager import ScreenManager, Screen #for the screen manager to track which screen is being shown

from kivy.uix.label import Label # for the label like headers

from kivy.uix.widget import Widget # adds widget for each of the classes

from kivy.uix.dropdown import DropDown # adds dropdown widgets

from kivy.graphics import Color, Ellipse, Rectangle # adds color to the circle

from kivy.graphics.texture import Texture #used for OpenCV image data

from kivy.core.window import Window # Sets the background color for app

from kivy.uix.camera import Camera #Kivy's built-in Camera widget

from kivy.uix.popup import Popup #For dialog window for popups when needed

from kivy.uix.image import Image #Widgets for displaying selected images

from kivy.properties import NumericProperty, StringProperty # control text values

from kivy.utils import get_color_from_hex # color translating from hex to rgba

from plyer import filechooser #From plyer library for image selection

from PIL import Image as PILImage #Python Image Library

from PIL import ImageDraw, ImageFont #Python Image Library

from kivy.clock import Clock #Used to capture frames form OpenCV Camera

import os #Python module for operating system interactions

import numpy as np #Used for handling image data from OpenCV's numpy arrays

from kivy.utils import platform

if platform == "android":

from android.permissions import request_permissions, Permission

request_permissions([Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE, Permission.CAMERA, Permission.INTERNET])

class CameraPage(Screen):

def __init__(self, **kwargs):

super(CameraPage, self).__init__(**kwargs)

self.cap = None # OpenCV VideoCapture

self.image_cache = None

self.frame_event = None

self.current_frame = None # Captures the current frame

# Start OpenCV camera when user is in the camera page

def on_enter(self, *args):

self.start_opencv_camera()

# Stop OpenCV camera when user exits the camera page

def on_leave(self, *args):

if self.cap:

self.cap.release() # Release the camera

print("Camera has been released.")

if self.frame_event:

self.frame_event.cancel() # Cancel frame update

# Start OpenCV camera

def start_opencv_camera(self):

camera_indices = [0, 1, 2] # List of indices to try for camera

self.cap = None # Initialize VideoCapture as None

# Try opening cameras in the order specified in the list

for index in camera_indices:

self.cap = cv2.VideoCapture(index)

if self.cap.isOpened():

print(f"OpenCV camera started on index {index}.")

break # If successful, exit the loop

else:

print(f"Error: Could not open OpenCV camera at index {index}. Trying next index.")

# If camera still isn't opened, log the error

if not self.cap or not self.cap.isOpened():

print("Error: Could not open any OpenCV camera.")

else:

# Schedule the update of camera frames by 30 FPS

self.frame_event = Clock.schedule_interval(self.update_camera_feed, 1.0 / 30.0)

# Update the Image widget with the camera feed

def update_camera_feed(self, dt):

if self.cap:

ret, frame = self.cap.read()

if ret:

self.current_frame = frame # Store the current frame

# Convert BGR frame to RGB

buf = cv2.flip(frame, 0).tobytes()

texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')

texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')

self.ids.camera_feed.texture = texture

# Capture image using the current frame from OpenCV

def capture_opencv_image(self):

if self.current_frame is not None:

# Convert OpenCV frame (BGR) to PIL Image (RGB)

pil_image = PILImage.fromarray(cv2.cvtColor(self.current_frame, cv2.COLOR_BGR2RGB))

# Save to cache directory

cache_dir = './image_cache'

if not os.path.exists(cache_dir):

os.mkdir(cache_dir)

cached_image_path = os.path.join(cache_dir, 'captured_image.png')

pil_image.save(cached_image_path)

self.image_cache = cached_image_path

print(f"Image captured and cached at: {cached_image_path}")

else:

print("Error: No frame available to capture")

# Function that opens gallery works for windows and should work for android

def open_gallery(self, *args):

# Opens the file chooser to select images

filechooser.open_file(on_selection=self.display_image)

# Display the selected image in a popup

def display_image(self, selection):

if selection:

# Cache for the selected image

self.image_cache = selection[0]

# Create/open popup

image_popup = ImagePopup()

image_popup.ids.img.source = self.image_cache

image_popup.open()

# Cache the selected image

def cache_image(self, instance):

# Checks if cache location exists

if self.image_cache:

cache_dir = './image_cache' # Cache directory

if not os.path.exists(cache_dir):

os.mkdir(cache_dir) # Create cache location if does not existing

# Select a location/path for the cached image

cached_image_path = os.path.join(cache_dir, 'cached_image.png')

try:

with open(self.image_cache, 'rb') as source_file:

with open(cached_image_path, 'wb') as dest_file:

dest_file.write(source_file.read())

print(f"Image cached at: {cached_image_path}")

except Exception as e:

print(f"Error caching the image: {e}")

else:

print("Error: No image selected for caching")


**
файл kivy:
**
#:kivy 1.0

ScreenManager:

FrontPage:

SettingsPage:

CameraPage:

ManualPage:

:

name: 'front_page'

FloatLayout:

#size: root.width - 200, root.height - 200

#pos: 100, 100

orientation: 'vertical'

Label:

text: 'Block Lens'

font_size: 50

pos_hint: {'center_y': 0.7}

# Will add font style to the title

RoundedButton:

text: 'Go to settings page'

on_press: app.root.current = 'settings_page'

#shrinks the button and adjust the button based on x axis and y axis

pos_hint: {'center_x': 0.2, 'center_y': 0.5}

size: 200, 200

RoundedButton:

text: 'Go to camera page'

on_press: app.root.current = 'camera_page'

#shrinks the button

pos_hint: {'center_x': 0.8, 'center_y': 0.5}

size: 200, 200

RoundedButton:

text: 'Go to manual page'

on_press: app.root.current = 'manual_page'

#shrinks the button

pos_hint: {'center_x': 0.5, 'center_y': 0.3}

size: 200, 200

:

name: 'settings_page'

# TOP LAYOUT SET UP - BACK BUTTON AND SETTINGS BANNER

BoxLayout:

orientation: 'vertical'

# SETTING LAYOUT

FloatLayout:

size_hint_y: None

height: '50dp'

# BACK BUTTON LAYOUT

Button:

id: back_button

text: 'Back'

background_color: app.get_color("Text Color") # White background for button

color: app.get_color("Border Color") #black text for button

border: app.get_color("Border Color")

size_hint: None, None

size: '50dp', '30dp'

pos_hint: {"x": 0, "top": 1}

on_release: app.root.current = 'front_page'

# LABEL

Label:

id: settings_label

text: 'Settings'

font_size: app.text_size_labels

color: app.get_color("Text Color")

size_hint_y: None

height: '25dp'

pos_hint: {"center_x": 0.5, "top": 0.8}

# SCROLL SETTINGS SETUP

ScrollView:

id: scroll_view

do_scroll_x: False

canvas.before:

Color:

rgba: app.get_color("Background Color")

Rectangle:

pos: self.pos

size: self.size

# SETTING UP SCROLLABLE AREA

BoxLayout:

id: settings_container

orientation: 'vertical'

size_hint_y: None

height: self.minimum_height

# SETTINGS WIDGETS HERE

# Text Settings

BoxLayout:

orientation: 'horizontal'

size_hint_y: None

height: '40dp'

Label:

text: "Text Size"

font_size: app.text_size_default_font

color: app.get_color("Text Color")

size_hint_y: None

size_hint_x: 0.4

height: '40dp'

Button:

id: text_size_small

text: "Small"

font_size: app.text_size_default_font

background_color: app.get_color("Non-Selected Color") if self.text != app.text_size_default_font else app.get_color("Selected Box Color")

color: app.get_color("Text Color")

size_hint_x: 0.2

on_release: app.set_font_size('Small')

Button:

id: text_size_default

text: "Default"

font_size: app.text_size_default_font

background_color: app.get_color("Non-Selected Color") if self.text != app.text_size_default_font else app.get_color("Selected Box Color")

color: app.get_color("Text Color")

size_hint_x: 0.2

on_release: app.set_font_size('Default')

Button:

id: text_size_large

text: "Large"

font_size: app.text_size_default_font

font_size: app.text_size_default_font

background_color: app.get_color("Non-Selected Color") if self.text != app.text_size_default_font else app.get_color("Selected Box Color")

color: app.get_color("Text Color")

size_hint_x: 0.2

on_release: app.set_font_size('Large')

#COLORBLIND WIDGET HERE

BoxLayout:

orientation: 'horizontal'

size_hint_y: None

height: '40dp'

Label:

text: "Color Blind Mode"

font_size: app.text_size_default_font

color: app.get_color("Text Color")

size_hint_y: None

height: '40dp'

Button:

id: color_blind_button

text: 'Default'

font_size: app.text_size_default_font

background_color: app.get_color("Non-Selected Color")

color: app.get_color("Text Color")

size_hint_y: None

height: '40dp'

Label:

text: "Summary Text-To-Speech"

font_size: app.text_size_default_font

color: app.get_color("Text Color")

size_hint_y: None

height: '40dp'

Label:

text: 'About Us'

font_size: app.text_size_labels

color: app.get_color("Text Color")

size_hint_y: None

height: '25dp'

pos_hint: {"center_x": 0.5, "top": 1}

Label:

text: root.about_us_text

font_size: app.text_size_default_font

color: app.get_color("Text Color")

size_hint_y: None

text_size: self.width, None

halign: 'left'

valign: 'top'

height: self.texture_size[1]

:

name: 'camera_page'

FloatLayout:

Image:

id: camera_feed

size_hint: (1, 0.8)

pos_hint: {'x': 0, 'top': 1}

Button:

text: 'Capture'

size_hint: (0.3, 0.1)

pos_hint: {'center_x': 0.5, 'y': 0.2}

on_press: root.capture_opencv_image()

BoxLayout:

orientation: 'horizontal'

size_hint_y: None

height: '50dp'

pos_hint: {'bottom': 1}

Button:

text: 'Go to front page'

on_press: app.root.current = 'front_page'

Button:

text: 'Gallery'

on_press: root.open_gallery()

:

name: 'manual_page'

BoxLayout:

orientation: 'vertical'

Label:

text: 'Welcome to the manual page'

font_size: 20

Button:

text: 'Go to front page'

on_press: app.root.current = 'front_page'


**
Код ошибки из logcat:
**
V onWindowFocusChanged(): false
2024-12-02 01:36:12.928 10575-10575 SDL org.test.myapp V nativeFocusChanged()
2024-12-02 01:36:13.256 586-610 WindowManager system_server V Sent Transition (#180) createdAt=12-02 01:36:12.833 via request=TransitionRequestInfo { type = OPEN, triggerTask = TaskInfo{userId=0 taskId=45 displayId=0 isRunning=true baseIntent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.test.myapp/org.kivy.android.PythonActivity } baseActivity=ComponentInfo{org.test.myapp/org.kivy.android.PythonActivity} topActivity=ComponentInfo{com.google.android.permissioncontroller/com.android.permissioncontroller.permission.ui.GrantPermissionsActivity} origActivity=null realActivity=ComponentInfo{org.test.myapp/org.kivy.android.PythonActivity} numActivities=2 lastActiveTime=17100318 supportsMultiWindow=true resizeMode=1 isResizeable=true minWidth=-1 minHeight=-1 defaultMinSize=220 token=WCT{RemoteToken{24afd65 Task{4cf9d18 #45 type=standard A=10242:org.test.myapp}}} topActivityType=1 pictureInPictureParams=null shouldDockBigOverlays=false launchIntoPipHostTaskId=-1 lastParentTaskIdBeforePip=-1 displayCutoutSafeInsets=null topActivityInfo=ActivityInfo{cd97023 com.android.permissioncontroller.permission.ui.GrantPermissionsActivity} launchCookies=[android.os.BinderProxy@4f6c6eb] positionInParent=Point(0, 0) parentTaskId=-1 isFocused=true isVisible=true isVisibleRequested=true isSleeping=false locusId=null displayAreaFeatureId=1 isTopActivityTransparent=true appCompatTaskInfo=AppCompatTaskInfo { topActivityInSizeCompat=false topActivityEligibleForLetterboxEducation= falseisLetterboxEducationEnabled= true isLetterboxDoubleTapEnabled= false topActivityEligibleForUserAspectRatioButton= false topActivityBoundsLetterboxed= false isFromLetterboxDoubleTap= false topActivityLetterboxVerticalPosition= -1 topActivityLetterboxHorizontalPosition= -1 topActivityLetterboxWidth=1080 topActivityLetterboxHeight=2400 isUserFullscreenOverrideEnabled=false isSystemFullscreenOverrideEnabled=false cameraCompatTaskInfo=CameraCompatTaskInfo { cameraCompatControlState=hidden freeformCameraCompatMode=inactive}}}, pipTask = null, remoteTransition = null, displayChange = null, flags = 0, debugId = 180 }
2024-12-02 01:36:13.397 10575-10575 PythonActivity org.test.myapp V onRequestPermissionsResult()
2024-12-02 01:36:13.398 10575-10575 PythonActivity org.test.myapp V onRequestPermissionsResult passed to callback
2024-12-02 01:36:13.433 10575-10575 PythonActivity org.test.myapp V onResume()
2024-12-02 01:36:13.433 10575-10575 SDL org.test.myapp V onResume()
2024-12-02 01:36:13.449 10575-10599 python org.test.myapp I [WARNING] [Base ] Unknown provider
2024-12-02 01:36:13.452 10575-10599 python org.test.myapp I [INFO ] [Base ] Start application main loop
2024-12-02 01:36:13.502 10575-10575 SDL org.test.myapp V onWindowFocusChanged(): true
2024-12-02 01:36:13.504 10575-10575 SDL org.test.myapp V nativeFocusChanged()
2024-12-02 01:36:13.618 586-716 ImeTracker system_server I org.test.myapp:e80cb21b: onRequestHide at ORIGIN_SERVER reason HIDE_SAME_WINDOW_FOCUSED_WITHOUT_EDITOR fromUser false
2024-12-02 01:36:13.618 586-716 ImeTracker system_server I org.test.myapp:e80cb21b: onCancelled at PHASE_SERVER_SHOULD_HIDE
2024-12-02 01:36:13.626 10575-10599 python org.test.myapp I [INFO ] [GL ] NPOT texture support is available
2024-12-02 01:36:13.626 1273-1273 GoogleInpu...hodService com...gle.android.inputmethod.latin I GoogleInputMethodService.onStartInput():1899 onStartInput(EditorInfo{EditorInfo{packageName=org.test.myapp, inputType=0, inputTypeString=NULL, enableLearning=false, autoCorrection=false, autoComplete=false, imeOptions=0, privateImeOptions=null, actionName=UNSPECIFIED, actionLabel=null, initialSelStart=-1, initialSelEnd=-1, initialCapsMode=0, label=null, fieldId=0, fieldName=null, extras=null, hintText=null, hintLocales=[]}}, false)
2024-12-02 01:36:13.631 586-1918 PackageConfigPersister system_server W App-specific configuration not found for packageName: org.test.myapp and userId: 0
2024-12-02 01:36:20.387 10575-10599 python org.test.myapp I Error: Could not open OpenCV camera at index -1. Trying next index.
2024-12-02 01:36:20.387 10575-10599 python org.test.myapp I Error: Could not open OpenCV camera at index 0. Trying next index.
2024-12-02 01:36:20.387 10575-10599 python org.test.myapp I Error: Could not open OpenCV camera at index 1. Trying next index.
2024-12-02 01:36:20.387 10575-10599 python org.test.myapp I Error: Could not open OpenCV camera at index 2. Trying next index.
2024-12-02 01:36:20.388 10575-10599 python org.test.myapp I Error: Could not open any OpenCV camera.
2024-12-02 01:36:21.097 10575-10599 python org.test.myapp I Error: No frame available to capture
2024-12-02 01:36:21.123 10575-10599 EGL_emulation org.test.myapp D app_time_stats: avg=468.48ms min=4.96ms max=5895.82ms count=15
2024-12-02 01:36:22.445 10575-10599 EGL_emulation org.test.myapp D app_time_stats: avg=660.67ms min=67.26ms max=1254.09ms count=2
2024-12-02 01:36:22.863 10575-10599 python org.test.myapp I Camera has been released.
2024-12-02 01:36:23.909 10575-10599 EGL_emulation org.test.myapp D app_time_stats: avg=125.97ms min=5.73ms max=997.70ms count=11
2024-12-02 01:36:24.960 10575-10599 EGL_emulation org.test.myapp D app_time_stats: avg=28.44ms min=5.08ms max=110.71ms count=29


Подробнее здесь: https://stackoverflow.com/questions/792 ... n-kivy-app
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Opencv пустой белый экран на Android APK Python, приложение kivy
    Anonymous » » в форуме Python
    0 Ответы
    16 Просмотры
    Последнее сообщение Anonymous
  • Приложение показывает пустой белый экран, а сайт - белый список
    Anonymous » » в форуме JAVA
    0 Ответы
    20 Просмотры
    Последнее сообщение Anonymous
  • Приложение показывает пустой белый экран, а сайт - белый список
    Anonymous » » в форуме JAVA
    0 Ответы
    15 Просмотры
    Последнее сообщение Anonymous
  • Белый экран Opencv на Android не может получить доступ к камере Python kivy
    Anonymous » » в форуме Python
    0 Ответы
    15 Просмотры
    Последнее сообщение Anonymous
  • Белый экран Opencv на Android не может получить доступ к камере Python kivy
    Anonymous » » в форуме Android
    0 Ответы
    12 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Android»