kivyblocks/kivyblocks/custom_camera.py

105 lines
2.8 KiB
Python
Raw Normal View History

2021-07-01 16:03:27 +08:00
from traceback import print_exc
2021-07-01 10:24:05 +08:00
from kivy.app import App
2021-04-23 14:53:33 +08:00
from kivy.logger import Logger
2021-02-01 23:07:58 +08:00
from kivy.uix.camera import Camera
from kivy.properties import BooleanProperty, NumericProperty
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
2021-07-01 10:24:05 +08:00
from kivy.graphics import PushMatrix, Rotate, PopMatrix
2021-07-01 14:54:16 +08:00
from kivy.graphics.texture import Texture
2021-07-01 10:24:05 +08:00
2021-02-01 23:07:58 +08:00
import kivy
import numpy as np
import cv2
from kivy.base import Builder
from .image_processing.image_processing import face_detection
2021-04-23 15:13:41 +08:00
from .xcamera.xcamera import XCamera
2021-02-01 23:07:58 +08:00
2021-04-23 15:13:41 +08:00
class CustomCamera(XCamera):
2021-02-01 23:07:58 +08:00
detectFaces = BooleanProperty(False)
2021-07-01 10:24:05 +08:00
angle_map = {
2021-07-01 17:48:31 +08:00
0:90,
2021-07-01 10:24:05 +08:00
1:0,
2021-07-01 17:48:31 +08:00
2:270,
2021-07-01 10:24:05 +08:00
3:180
}
2021-02-01 23:07:58 +08:00
def __init__(self, **kwargs):
super(CustomCamera, self).__init__(**kwargs)
self.isAndroid = kivy.platform == "android"
2021-07-01 10:24:05 +08:00
self.app = App.get_running_app()
2021-06-30 19:37:33 +08:00
2021-07-01 10:24:05 +08:00
def on_tex(self, camera):
2021-07-01 14:54:16 +08:00
texture = camera.texture
image = np.frombuffer(texture.pixels, dtype='uint8')
image = image.reshape(texture.height, texture.width, -1)
size1 = image.shape
2021-07-01 16:03:27 +08:00
x = 3
2021-07-01 10:24:05 +08:00
if self.isAndroid:
x = self.app.get_rotation()
2021-07-01 14:54:16 +08:00
y = self.angle_map[x]
x = y / 90
2021-02-01 23:07:58 +08:00
2021-07-01 11:33:03 +08:00
if x > 0:
image = np.rot90(image,x)
2021-02-01 23:07:58 +08:00
if self.detectFaces:
2021-07-01 16:03:27 +08:00
try:
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
_image, faceRect = face_detection(image, (0, 255, 0, 255))
image = cv2.cvtColor(_image, cv2.COLOR_BGR2RGBA)
except Exception as e:
print('custom_camera.py:Exception:',e)
print_exc()
h,w,_ = image.shape
2021-02-01 23:07:58 +08:00
numpy_data = image.tostring()
2021-07-01 14:54:16 +08:00
self.texture = Texture.create(size=(w,h), \
colorfmt='rgba')
self.texture.blit_buffer(numpy_data,
size=(w,h),
bufferfmt="ubyte", colorfmt='rgba')
2021-07-01 11:33:03 +08:00
self.texture_size = list(self.texture.size)
self.canvas.ask_update()
return
def change_index(self, *args):
new_index = 1 if self.index == 0 else 0
self._camera._set_index(new_index)
self.index = new_index
self.angle = -90 if self.index == 0 else 90
2021-02-01 23:07:58 +08:00
def get_cameras_count(self):
cameras = 1
if self.isAndroid:
cameras = self._camera.get_camera_count()
return cameras
2021-07-01 11:33:03 +08:00
def dismiss(self, *args, **kw):
self.play = False
cv2.destroyAllWindows()
2021-07-01 10:24:05 +08:00
class QrReader(CustomCamera):
2021-02-01 23:07:58 +08:00
def __init__(self, **kw):
super(QrReader, self).__init__(**kw)
self.qr_reader = cv2.QRCodeDetector()
self.register_event_type('on_data')
self.qr_result = None
2021-04-23 14:53:33 +08:00
Logger.info('QrReader:Initialed')
2021-02-01 23:07:58 +08:00
def getValue(self):
return {
"qr_result":self.qr_result
}
def on_data(self, d):
print('data=',d)
2021-07-01 11:33:03 +08:00
def on_tex(self, camera):
super(QrReader, self).on_tex(camera)
2021-02-01 23:07:58 +08:00
image = np.frombuffer(self.texture.pixels, dtype='uint8')
image = image.reshape(self.texture.height, self.texture.width, -1)
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
self.qr_result, bbox,_ = self.qr_reader.detectAndDecode(image)
if self.qr_result:
self.dispatch('on_data',self.qr_result)
2021-05-24 12:25:29 +08:00