This commit is contained in:
yumoqing 2022-06-27 15:27:05 +08:00
parent 2b21e67bfd
commit b4ba09d761
7 changed files with 3 additions and 33635 deletions

View File

@ -1,104 +0,0 @@
from traceback import print_exc
from kivy.app import App
from kivy.logger import Logger
from kivy.uix.camera import Camera
from kivy.properties import BooleanProperty, NumericProperty
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import PushMatrix, Rotate, PopMatrix
from kivy.graphics.texture import Texture
import kivy
import numpy as np
import cv2
from kivy.base import Builder
from .image_processing.image_processing import face_detection
from .xcamera.xcamera import XCamera
class CustomCamera(XCamera):
detectFaces = BooleanProperty(False)
angle_map = {
0:90,
1:0,
2:270,
3:180
}
def __init__(self, **kwargs):
super(CustomCamera, self).__init__(**kwargs)
self.isAndroid = kivy.platform == "android"
self.app = App.get_running_app()
def on_tex(self, camera):
texture = camera.texture
image = np.frombuffer(texture.pixels, dtype='uint8')
image = image.reshape(texture.height, texture.width, -1)
size1 = image.shape
x = 3
if self.isAndroid:
x = self.app.get_rotation()
y = self.angle_map[x]
x = y / 90
if x > 0:
image = np.rot90(image,x)
if self.detectFaces:
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')
self.texture_size = list(self.texture.size)
self.canvas.ask_update()
return
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 get_cameras_count(self):
cameras = 1
if self.isAndroid:
cameras = self._camera.get_camera_count()
return cameras
def dismiss(self, *args, **kw):
self.play = False
cv2.destroyAllWindows()
class QrReader(CustomCamera):
def __init__(self, **kw):
super(QrReader, self).__init__(**kw)
self.qr_reader = cv2.QRCodeDetector()
self.register_event_type('on_data')
self.qr_result = None
Logger.info('QrReader:Initialed')
def getValue(self):
return {
"qr_result":self.qr_result
}
def on_data(self, d):
print('data=',d)
def on_tex(self, camera):
super(QrReader, self).on_tex(camera)
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:
self.dispatch('on_data',self.qr_result)

View File

@ -1,57 +0,0 @@
import os
import cv2
# import imutils
import numpy as np
def simple_return(image):
return image
def crop_image(image):
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)
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]
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)
if not detector:
print('image_processing.py:detector is None')
return org_image, None
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

View File

@ -1,88 +0,0 @@
import os
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
facefilepath=os.path.dirname(cv2.__file__)
facepattern = '%s/%s' % (facefilepath, \
'data/haarcascade_frontalface_default.xml')
def set_res(cap, x,y):
cap.set(cv2.CAP_PROP_FRAME_WIDTH, int(x))
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, int(y))
return cap.get(cv2.CAP_PROP_FRAME_WIDTH),cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
class KivyCamera(Image):
def __init__(self, device=0, fps=25.0, face_detect=False, **kwargs):
print('KivyCamera inited')
self.update_task = None
self.capture = None
super(KivyCamera, self).__init__(**kwargs)
self.capture = cv2.VideoCapture(device)
self.face_detect = face_detect
self.device = device
self.faceCascade = None
if face_detect:
self.faceCascade = cv2.CascadeClassifier(facepattern)
if not self.faceCascade:
print('self.faceCascade is None')
self.update_task = Clock.schedule_interval(self.update, 1.0 / fps)
def on_size(self,o,size):
if self.capture:
self.capture.release()
self.capture = cv2.VideoCapture(self.device)
size = set_res(self.capture,self.width,self.height)
print(size)
def add_face_detect(self,frame):
frameGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.faceCascade.detectMultiScale(frameGray,
scaleFactor = 1.2, minNeighbors = 5)
print('add_face_detect(): faces=',faces)
# THIS LINE RAISE ERROR
# 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)
return frame
def update(self, dt):
ret, frame = self.capture.read()
if ret:
if self.width / self.height > frame.shape[1] / frame.shape[0]:
fx = fy = self.height / frame.shape[0]
else:
fx = fy = self.width / frame.shape[1]
frame = cv2.resize(frame, None,
fx=fx, fy=fy,
interpolation=cv2.INTER_LINEAR)
if self.faceCascade:
try:
frame = self.add_face_detect(frame)
except Exception as e:
print('Error, e=',e)
pass
# 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
else:
self.update_task.cancel()
print('failed to read from capture')
def __del__(self):
if self.update_task:
self.update_task.cancel()
self.update_task = None
# self.cupture.close()

View File

@ -1,69 +0,0 @@
import numpy as np
import cv2
from kivy.app import App
from kivy.properties import BooleanProperty
from kivy.core.window import Window
from kivy.graphics.texture import Texture
from kivy.uix.image import Image
from kivy.clock import Clock
class QRCodeReader(Image):
opened = BooleanProperty(False)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.register_event_type('on_data')
self.task = None
if self.opened:
self.open()
def open(self):
self.opened = True
self.cam = cv2.VideoCapture(0)
self.detector = cv2.QRCodeDetector()
self.task = Clock.schedule_interval(self.read,1.0/30.0)
Window.add_widget(self)
def on_data(self,d):
print('QRCodeReader().on_data(),data=',d)
self.dismiss()
def on_touch_down(self,touch):
if not self.colide_point(*touch.pos):
self.dismiss()
super().on_touch_down(touch)
def dismiss(self, *args, **kw):
if not self.opened:
return
self.opened = False
self.task.cancel()
self.task = None
self.cam.release()
cv2.destroyAllWindows()
Window.remove_widget(self)
def showImage(self,img):
image = np.rot90(np.swapaxes(img,0,1))
tex = Texture.create(size=(image.shape[1], image.shape[0]),
colorfmt='rgb')
tex.blit_buffer(image.tostring(),colorfmt='rgb', bufferfmt='ubyte')
self.texture = tex
def read(self,p):
_, img = self.cam.read()
self.showImage(img)
data,bbox,_ = self.detector.detectAndDecode(img)
if data:
d = {
'data':data
}
self.dispatch('on_data',d)
if __name__ == '__main__':
class MyApp(App):
def build(self):
r = QRCodeReader()
return r
myapp = MyApp()
myapp.run()

View File

@ -27,7 +27,7 @@ from .mapview import MapView
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 # from .custom_camera import CustomCamera, QrReader
from .defaultimage import * from .defaultimage import *
from .price import * from .price import *
@ -54,8 +54,8 @@ r('UdpWidget', UdpWidget)
r('ScrollPanel', ScrollPanel) r('ScrollPanel', ScrollPanel)
r('TextInput', TextInput) r('TextInput', TextInput)
r('CameraWithMic', CameraWithMic) r('CameraWithMic', CameraWithMic)
r('CustomCamera', CustomCamera) # r('CustomCamera', CustomCamera)
r('QrReader', QrReader) # r('QrReader', QrReader)
r('Markdown', Markdown) r('Markdown', Markdown)
r('PagePanel', PagePanel) r('PagePanel', PagePanel)
r('Conform', Conform) r('Conform', Conform)