bigfix
This commit is contained in:
parent
743c6290f0
commit
b7febb6567
@ -366,10 +366,19 @@ class Blocks(EventDispatcher):
|
|||||||
w.bind(**{event:f})
|
w.bind(**{event:f})
|
||||||
# Logger.info('Block: %s bind built', str(desc))
|
# Logger.info('Block: %s bind built', str(desc))
|
||||||
|
|
||||||
|
def multipleAction(self, widget, desc, *args):
|
||||||
|
mydesc = {
|
||||||
|
'wid':desc['wid'],
|
||||||
|
'event':desc['event']
|
||||||
|
}
|
||||||
|
for a in desc['actions']:
|
||||||
|
new_desc = mydesc.copy()
|
||||||
|
new_desc.update(a)
|
||||||
|
self.unication(widget,new_desc, **args)
|
||||||
|
|
||||||
def uniaction(self,widget,desc, *args):
|
def uniaction(self,widget,desc, *args):
|
||||||
Logger.info('Block: uniaction() called, desc=%s', str(desc))
|
Logger.info('Block: uniaction() called, desc=%s', str(desc))
|
||||||
|
|
||||||
|
|
||||||
acttype = desc.get('actiontype')
|
acttype = desc.get('actiontype')
|
||||||
if acttype=='blocks':
|
if acttype=='blocks':
|
||||||
return self.blocksAction(widget,desc, *args)
|
return self.blocksAction(widget,desc, *args)
|
||||||
@ -381,8 +390,30 @@ class Blocks(EventDispatcher):
|
|||||||
return self.scriptAction(widget, desc, *args)
|
return self.scriptAction(widget, desc, *args)
|
||||||
if acttype == 'method':
|
if acttype == 'method':
|
||||||
return self.methodAction(widget, desc, *args)
|
return self.methodAction(widget, desc, *args)
|
||||||
|
if acttype == 'event':
|
||||||
|
return self.eventAction(widget, desc, *args)
|
||||||
|
if acttype == 'multiple':
|
||||||
|
return self.MultipleAction(widget, desc, *args)
|
||||||
|
|
||||||
alert("actiontype(%s) invalid" % acttype,title='error')
|
alert("actiontype(%s) invalid" % acttype,title='error')
|
||||||
|
|
||||||
|
def eventAction(self, widget, desc, *args):
|
||||||
|
target = Blocks.getWidgetById(desc.get('target','self'),widget)
|
||||||
|
event = desc.get('dispatch_event')
|
||||||
|
if not event:
|
||||||
|
Logger.info('Block: eventAction():desc(%s) miss dispatch_event',
|
||||||
|
str(desc))
|
||||||
|
return
|
||||||
|
params = desc.get('params',{})
|
||||||
|
d = self.getActionData(widget,desc)
|
||||||
|
if d:
|
||||||
|
params.update(d)
|
||||||
|
try:
|
||||||
|
target.dispatch(event, **params)
|
||||||
|
except Exception as e:
|
||||||
|
Logger.info(f'Block: eventAction():dispatch {event} error')
|
||||||
|
return
|
||||||
|
|
||||||
def blocksAction(self,widget,desc, *args):
|
def blocksAction(self,widget,desc, *args):
|
||||||
target = Blocks.getWidgetById(desc.get('target','self'),widget)
|
target = Blocks.getWidgetById(desc.get('target','self'),widget)
|
||||||
if target is None:
|
if target is None:
|
||||||
|
87
kivyblocks/custom_camera.py
Normal file
87
kivyblocks/custom_camera.py
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
from kivy.uix.camera import Camera
|
||||||
|
from kivy.properties import BooleanProperty, NumericProperty
|
||||||
|
from kivy.uix.button import Button
|
||||||
|
from kivy.uix.boxlayout import BoxLayout
|
||||||
|
import kivy
|
||||||
|
import numpy as np
|
||||||
|
import cv2
|
||||||
|
from kivy.base import Builder
|
||||||
|
from .image_processing.image_processing import face_detection
|
||||||
|
|
||||||
|
btxt = """<CustomCamera>:
|
||||||
|
resolution: (1920,1050)
|
||||||
|
play: True
|
||||||
|
keep_ratio: True
|
||||||
|
allow_stretch: True
|
||||||
|
canvas.before:
|
||||||
|
PushMatrix
|
||||||
|
Rotate:
|
||||||
|
angle: root.angle
|
||||||
|
axis: 0, 0, 1
|
||||||
|
origin: root.center
|
||||||
|
canvas.after:
|
||||||
|
PopMatrix
|
||||||
|
"""
|
||||||
|
|
||||||
|
class CustomCamera(Camera):
|
||||||
|
detectFaces = BooleanProperty(False)
|
||||||
|
angle = NumericProperty(0)
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
super(CustomCamera, self).__init__(**kwargs)
|
||||||
|
|
||||||
|
self.isAndroid = kivy.platform == "android"
|
||||||
|
if self.isAndroid:
|
||||||
|
self.angle = -90
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
class QrReader(Camera):
|
||||||
|
def __init__(self, **kw):
|
||||||
|
super(QrReader, self).__init__(**kw)
|
||||||
|
self.qr_reader = cv2.QRCodeDetector()
|
||||||
|
self.register_event_type('on_data')
|
||||||
|
self.qr_result = None
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
Builder.load_string(btxt)
|
0
kivyblocks/image_processing/__init__.py
Normal file
0
kivyblocks/image_processing/__init__.py
Normal file
BIN
kivyblocks/image_processing/__pycache__/__init__.cpython-37.pyc
Normal file
BIN
kivyblocks/image_processing/__pycache__/__init__.cpython-37.pyc
Normal file
Binary file not shown.
Binary file not shown.
33314
kivyblocks/image_processing/cascades/haarcascade_frontalface_default.xml
Normal file
33314
kivyblocks/image_processing/cascades/haarcascade_frontalface_default.xml
Normal file
File diff suppressed because it is too large
Load Diff
50
kivyblocks/image_processing/image_processing.py
Normal file
50
kivyblocks/image_processing/image_processing.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import cv2
|
||||||
|
# import imutils
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def simple_return(image):
|
||||||
|
return image
|
||||||
|
|
||||||
|
|
||||||
|
def crop_image(image):
|
||||||
|
return image[0:350, 0:350]
|
||||||
|
|
||||||
|
|
||||||
|
detector = cv2.CascadeClassifier('image_processing/cascades/haarcascade_frontalface_default.xml')
|
||||||
|
|
||||||
|
|
||||||
|
def face_detection(image, rect_color, rotation):
|
||||||
|
|
||||||
|
if rotation == 90:
|
||||||
|
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
||||||
|
if rotation == -90:
|
||||||
|
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
|
||||||
|
|
||||||
|
orig_image = image.copy()
|
||||||
|
height, width = orig_image.shape[:2]
|
||||||
|
|
||||||
|
new_width = 300
|
||||||
|
r = new_width / float(width)
|
||||||
|
dim = (new_width, int(height * r))
|
||||||
|
ratio = (width / dim[0], height / dim[1])
|
||||||
|
image = cv2.resize(image, dim)
|
||||||
|
|
||||||
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||||
|
|
||||||
|
faceRects = detector.detectMultiScale(image, scaleFactor=1.2, minNeighbors=5,
|
||||||
|
minSize=(20, 20), flags=cv2.CASCADE_SCALE_IMAGE)
|
||||||
|
|
||||||
|
for (x, y, w, h) in faceRects:
|
||||||
|
x = int(x * ratio[0])
|
||||||
|
y = int(y * ratio[1])
|
||||||
|
w = x + int(w * ratio[0])
|
||||||
|
h = y + int(h * ratio[1])
|
||||||
|
cv2.rectangle(orig_image, (x, y), (w, h), rect_color, 2)
|
||||||
|
|
||||||
|
if rotation == 90:
|
||||||
|
orig_image = cv2.rotate(orig_image, cv2.ROTATE_90_CLOCKWISE)
|
||||||
|
if rotation == -90:
|
||||||
|
orig_image = cv2.rotate(orig_image, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
||||||
|
|
||||||
|
return orig_image, faceRects
|
@ -23,8 +23,11 @@ from .chart2d import Chart2d
|
|||||||
from .message import Conform
|
from .message import Conform
|
||||||
from .pagepanel import PagePanel
|
from .pagepanel import PagePanel
|
||||||
from .markdown import Markdown
|
from .markdown import Markdown
|
||||||
|
from .custom_camera import CustomCamera, QrReader
|
||||||
|
|
||||||
r = Factory.register
|
r = Factory.register
|
||||||
|
r('CustomCamera', CustomCamera)
|
||||||
|
r('QrReader', QrReader)
|
||||||
r('Markdown', Markdown)
|
r('Markdown', Markdown)
|
||||||
r('PagePanel', PagePanel)
|
r('PagePanel', PagePanel)
|
||||||
r('Conform', Conform)
|
r('Conform', Conform)
|
||||||
|
Loading…
Reference in New Issue
Block a user