diff --git a/kivyblocks/custom_camera.py b/kivyblocks/custom_camera.py index fbc8cb1..e395c05 100644 --- a/kivyblocks/custom_camera.py +++ b/kivyblocks/custom_camera.py @@ -1,3 +1,4 @@ +from traceback import print_exc from kivy.app import App from kivy.logger import Logger from kivy.uix.camera import Camera @@ -32,7 +33,7 @@ class CustomCamera(XCamera): image = np.frombuffer(texture.pixels, dtype='uint8') image = image.reshape(texture.height, texture.width, -1) size1 = image.shape - x = 2 + x = 3 if self.isAndroid: x = self.app.get_rotation() y = self.angle_map[x] @@ -41,26 +42,23 @@ class CustomCamera(XCamera): if x > 0: image = np.rot90(image,x) if self.detectFaces: - image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR) - angle = x * 90 - image, faceRect = face_detection(image, (0, 255, 0, 255), angle) - image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA) - size3 = image.shape - size3_2 = size3[:2] - h,w,_ = size3 + 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 numpy_data = image.tostring() self.texture = Texture.create(size=(w,h), \ colorfmt='rgba') self.texture.blit_buffer(numpy_data, size=(w,h), bufferfmt="ubyte", colorfmt='rgba') - size4=self.texture.size self.texture_size = list(self.texture.size) self.canvas.ask_update() - print('size1=',size1, - 'size2=', size2, - 'size3=', size3, - 'size4=', size4) return def change_index(self, *args): diff --git a/kivyblocks/image_processing/image_processing.py b/kivyblocks/image_processing/image_processing.py index f88a137..575e90f 100644 --- a/kivyblocks/image_processing/image_processing.py +++ b/kivyblocks/image_processing/image_processing.py @@ -1,50 +1,57 @@ +import os import cv2 # import imutils import numpy as np def simple_return(image): - return image + return image def crop_image(image): - return image[0:350, 0:350] + return image[0:350, 0:350] +curdir = os.path.dirname(__file__) +pattern_file = os.path.join(curdir,'/cascades/haarcascade_frontalface_default.xml') +detector = cv2.CascadeClassifier(pattern_file) -detector = cv2.CascadeClassifier('image_processing/cascades/haarcascade_frontalface_default.xml') +def face_detection(image, rect_color, rotation=-90): + 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] -def face_detection(image, rect_color, rotation): + 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) - if rotation == 90: - image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE) - if rotation == -90: - image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE) + image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) - orig_image = image.copy() - height, width = orig_image.shape[:2] + if not detector: + print('image_processing.py:detector is None') + return org_image, None - 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) + faceRects = detector.detectMultiScale(image, + scaleFactor=1.2, + minNeighbors=5, + minSize=(20, 20), + flags=cv2.CASCADE_SCALE_IMAGE) - image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + 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) - faceRects = detector.detectMultiScale(image, scaleFactor=1.2, minNeighbors=5, - minSize=(20, 20), flags=cv2.CASCADE_SCALE_IMAGE) + 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) - 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 + return orig_image, faceRects diff --git a/setup.py b/setup.py index 04e08b7..a351af9 100755 --- a/setup.py +++ b/setup.py @@ -24,6 +24,7 @@ package_data = { 'ttf/*.ttf', 'ui/*.uidesc', 'xcamera/xcamera.kv', + 'image_processing/cascades/haarcascade_frontalface_default.xml', 'xcamera/data/*' ], }