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-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 = {
|
|
|
|
0:270,
|
|
|
|
1:0,
|
|
|
|
2:90,
|
|
|
|
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):
|
|
|
|
super(CustomCamera, self).on_tex(camera)
|
|
|
|
if self.isAndroid:
|
|
|
|
x = self.app.get_rotation()
|
|
|
|
if not x:
|
|
|
|
x = 1
|
|
|
|
angle = self.angle_map[x]
|
|
|
|
with canvas.before:
|
|
|
|
PushMatrix()
|
|
|
|
Rotate(**{
|
|
|
|
angle: angle,
|
|
|
|
axis: (0, 0, 1),
|
|
|
|
origin: self.center})
|
|
|
|
with canvas.after:
|
|
|
|
PopMatrix()
|
|
|
|
|
2021-02-01 23:07:58 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def on_tex(self, *l):
|
|
|
|
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)
|
|
|
|
|
|
|
|
if self.detectFaces:
|
|
|
|
image, faceRect = face_detection(image, (0, 255, 0, 255), self.angle)
|
|
|
|
|
|
|
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
|
|
|
|
numpy_data = image.tostring()
|
|
|
|
self.texture.blit_buffer(numpy_data, bufferfmt="ubyte", colorfmt='rgba')
|
|
|
|
super(CustomCamera, self).on_tex(self.texture)
|
|
|
|
|
|
|
|
def get_cameras_count(self):
|
|
|
|
cameras = 1
|
|
|
|
if self.isAndroid:
|
|
|
|
cameras = self._camera.get_camera_count()
|
|
|
|
return cameras
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def on_tex(self, *l):
|
|
|
|
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:
|
|
|
|
print('qr read done')
|
|
|
|
self.dispatch('on_data',self.qr_result)
|
|
|
|
super(QrReader, self).on_tex(self.texture)
|
|
|
|
|
2021-05-24 12:25:29 +08:00
|
|
|
def dismiss(self, *args, **kw):
|
2021-06-30 19:37:33 +08:00
|
|
|
self.play = False
|
2021-05-24 12:25:29 +08:00
|
|
|
cv2.destroyAllWindows()
|
|
|
|
|