From 9d777e773b2c3a026edc2b45d000160b0ad89612 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 23 Sep 2020 09:42:46 +0800 Subject: [PATCH] bugfix --- kivyblocks/blocks.py | 7 ------- kivyblocks/kivycamera.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 kivyblocks/kivycamera.py diff --git a/kivyblocks/blocks.py b/kivyblocks/blocks.py index 613b9c5..c6d1d79 100755 --- a/kivyblocks/blocks.py +++ b/kivyblocks/blocks.py @@ -5,13 +5,6 @@ import json from traceback import print_exc import kivy -from kivy.config import Config -from kivy.resources import resource_add_path -resource_add_path(os.path.join(os.path.dirname(__file__),'./ttf')) -Config.set('kivy', 'default_font', [ - 'msgothic', - 'DroidSansFallback.ttf']) - from functools import partial from appPublic.dictExt import dictExtend diff --git a/kivyblocks/kivycamera.py b/kivyblocks/kivycamera.py new file mode 100644 index 0000000..88691be --- /dev/null +++ b/kivyblocks/kivycamera.py @@ -0,0 +1,39 @@ +from kivy.app import App +from kivy.uix.image import Image +from kivy.clock import Clock +from kivy.graphics.texture import Texture +from kivy.factory import Factory +import cv2 + +class KivyCamera(Image): + def __init__(self, fps=25.0, face_detect=Falsem **kwargs): + super(KivyCamera, self).__init__(**kwargs) + self.capture = cv2.VideoCapture(0) + self.face_detect = face_detect + self.faceCascade = None + if face_detect: + self.faceCascade = cv2.CascadeClassifier("Resources/haarcascades/haarcascade_frontalface_default.xml") # added + Clock.schedule_interval(self.update, 1.0 / fps) + + def add_face_detech(self,frame): + frameGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + faces = self.faceCascade.detectMultiScale(frameGray, 1.1, 4) + for (x, y, w, h) in faces: # added + cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) + + + def update(self, dt): + ret, frame = self.capture.read() + if ret: + if self.faceCascade: + frame = self.add_face_detech(frame) + # 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 + +Factory.register('Camera',KivyCamera)