This commit is contained in:
yumoqing 2021-07-01 16:03:27 +08:00
parent fd88ea096a
commit 695c2051dc
3 changed files with 50 additions and 44 deletions

View File

@ -1,3 +1,4 @@
from traceback import print_exc
from kivy.app import App from kivy.app import App
from kivy.logger import Logger from kivy.logger import Logger
from kivy.uix.camera import Camera from kivy.uix.camera import Camera
@ -32,7 +33,7 @@ class CustomCamera(XCamera):
image = np.frombuffer(texture.pixels, dtype='uint8') image = np.frombuffer(texture.pixels, dtype='uint8')
image = image.reshape(texture.height, texture.width, -1) image = image.reshape(texture.height, texture.width, -1)
size1 = image.shape size1 = image.shape
x = 2 x = 3
if self.isAndroid: if self.isAndroid:
x = self.app.get_rotation() x = self.app.get_rotation()
y = self.angle_map[x] y = self.angle_map[x]
@ -41,26 +42,23 @@ class CustomCamera(XCamera):
if x > 0: if x > 0:
image = np.rot90(image,x) image = np.rot90(image,x)
if self.detectFaces: if self.detectFaces:
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR) try:
angle = x * 90 image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
image, faceRect = face_detection(image, (0, 255, 0, 255), angle) _image, faceRect = face_detection(image, (0, 255, 0, 255))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA) image = cv2.cvtColor(_image, cv2.COLOR_BGR2RGBA)
size3 = image.shape except Exception as e:
size3_2 = size3[:2] print('custom_camera.py:Exception:',e)
h,w,_ = size3 print_exc()
h,w,_ = image.shape
numpy_data = image.tostring() numpy_data = image.tostring()
self.texture = Texture.create(size=(w,h), \ self.texture = Texture.create(size=(w,h), \
colorfmt='rgba') colorfmt='rgba')
self.texture.blit_buffer(numpy_data, self.texture.blit_buffer(numpy_data,
size=(w,h), size=(w,h),
bufferfmt="ubyte", colorfmt='rgba') bufferfmt="ubyte", colorfmt='rgba')
size4=self.texture.size
self.texture_size = list(self.texture.size) self.texture_size = list(self.texture.size)
self.canvas.ask_update() self.canvas.ask_update()
print('size1=',size1,
'size2=', size2,
'size3=', size3,
'size4=', size4)
return return
def change_index(self, *args): def change_index(self, *args):

View File

@ -1,50 +1,57 @@
import os
import cv2 import cv2
# import imutils # import imutils
import numpy as np import numpy as np
def simple_return(image): def simple_return(image):
return image return image
def crop_image(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.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
if rotation == -90:
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
orig_image = image.copy() if not detector:
height, width = orig_image.shape[:2] print('image_processing.py:detector is None')
return org_image, None
new_width = 300 faceRects = detector.detectMultiScale(image,
r = new_width / float(width) scaleFactor=1.2,
dim = (new_width, int(height * r)) minNeighbors=5,
ratio = (width / dim[0], height / dim[1]) minSize=(20, 20),
image = cv2.resize(image, dim) 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, if rotation == 90:
minSize=(20, 20), flags=cv2.CASCADE_SCALE_IMAGE) 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: return orig_image, 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

View File

@ -24,6 +24,7 @@ package_data = {
'ttf/*.ttf', 'ttf/*.ttf',
'ui/*.uidesc', 'ui/*.uidesc',
'xcamera/xcamera.kv', 'xcamera/xcamera.kv',
'image_processing/cascades/haarcascade_frontalface_default.xml',
'xcamera/data/*' 'xcamera/data/*'
], ],
} }