-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
848 lines (729 loc) · 33.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
import logging
import datetime
import json
from enum import Enum
import numpy as np
import cv2
import os.path
from datetime import datetime
# from functools import partial
import threading
from time import sleep
# Kivy imports...
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.uix.button import MDFloatingActionButton
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel
from kivy.graphics.texture import Texture
from kivymd.uix.navigationdrawer import MDNavigationLayout, MDNavigationDrawer
from kivymd.uix.screen import MDScreen
from kivymd.uix.fitimage import FitImage
from kivymd.uix.screenmanager import MDScreenManager
from kivymd.uix.snackbar import Snackbar
from kivymd.uix.toolbar.toolbar import MDTopAppBar
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty, NumericProperty
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.lang import Builder
from kivy import platform
from kivy.metrics import dp
from kivy.graphics import Rectangle, Line, Color, Ellipse, InstructionGroup
# blink imports
import camera_desktop
from tela_inicial import TelaInicial
from historico_analises_list import HistoricoAnalises
from dados_analise_pos_manual import TelaDadosAnalise
from tela_login import TelaLogin
from tela_cadastro import TelaCadastro
from tela_configuracao import TelaConfiguracao
from dados.modelo import DadoFrame, DadosAnalise
from sobre import Sobre
from cadastro_utils import get_storage_path
from tensorflow_utils import TensorflowModel
from utils import get_texture_from_rgba_array
# from blink import blink_utils as blink
logger = logging.getLogger(__file__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
logger.addHandler(handler)
"""
if platform == "macosx" or platform == "android":
try:
# Import TFLite interpreter from tflite_runtime package if it's available.
from tflite_runtime.interpreter import Interpreter
from tflite_runtime.interpreter import load_delegate
except ImportError:
# If not, fallback to use the TFLite interpreter from the full TF package.
import tensorflow as tf
Interpreter = tf.lite.Interpreter
load_delegate = tf.lite.experimental.load_delegate
"""
# Define se usaremos dados reais (dos arquivos json criados nas análises) ou dados "fake" para testes
REAL_DATA = True
if REAL_DATA:
from dados.repositorio_dados_reais import RepositorioDadosAnalise
else:
from dados.repositorio_dados_fake import RepositorioDadosAnalise
if platform == "android":
from android.permissions import request_permission, check_permission, Permission
from camera2.camerawidget import CameraDisplayWidget
cascPath = "assets/haarcascade_frontalface_default.xml"
eyePath = "assets/haarcascade_eye.xml"
# eyePath = "assets/haarcascade_eye_tree_eyeglasses.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
eyeCascade = cv2.CascadeClassifier(eyePath)
KV = """
<ContentNavigationDrawer>:
orientation: "vertical"
padding: "8dp"
spacing: "8dp"
Image:
#id: avatar
size_hint: None, None
size: "56dp", "56dp"
source: "assets/Blink_Logo_Nome_Fundo_Branco.png"
MDLabel:
text: "[email protected]"
font_style: "Caption"
size_hint_y: None
height: self.texture_size[1]
color:33/255, 90/255, 54/255, 1
MDNavigationDrawerDivider:
color:33/255, 90/255, 54/255, 1
OneLineListItem:
text: "Tela Inicial"
#IconLeftWidget:
# icon: "home"
on_press:
root.main_app.go_to_screen("screen_00")
OneLineListItem:
id: novaanalise
text: "Nova análise..."
disabled: True
on_press:
root.main_app.go_to_screen("screen_01")
OneLineListItem:
id: historicoanalises
text: "Histórico de análises"
disabled: True
on_press:
root.main_app.go_to_screen("screen_02")
OneLineListItem:
id: configuracoes
text: "Configurações"
disabled: True
on_press:
root.main_app.go_to_screen("screen_07")
OneLineListItem:
id: "sobre"
text: "Sobre"
on_press:
root.main_app.go_to_screen("screen_03")
"""
class ContentNavigationDrawer(BoxLayout):
main_app = ObjectProperty()
"""
class RootLayout(FloatLayout):
buttons_visible = BooleanProperty(True)
def __init__(self, **kwargs):
super().__init__(**kwargs)
"""
class BlinkApp(MDApp):
camera_display_widget = ObjectProperty()
navigation_drawer = ObjectProperty()
screen_manager = ObjectProperty()
app_bar = ObjectProperty()
screen_01 = ObjectProperty()
screen_02 = ObjectProperty()
root = ObjectProperty()
current_frame = 0
menu = ObjectProperty()
content_navigation_drawer = ObjectProperty()
flip_camera_button = ObjectProperty()
start_analysis_button = ObjectProperty()
# Coordenadas da face e dos olhos
face_rect = ObjectProperty()
left_eye_rect = ObjectProperty()
right_eye_rect = ObjectProperty()
# Controle de status
analise_iniciada = False
running_eyes_detection = False
running_face_detection = False
# TensorflowLite Interpreter
interpreter = ObjectProperty()
model = ObjectProperty()
tensorflow_model = ObjectProperty()
# Repositório de dados de análise
repositorio_dados = ObjectProperty()
dados_analise_selecionado = ObjectProperty()
ultima_analise = ObjectProperty()
Eyes = Enum('Eyes', ['LEFT', 'RIGHT', 'UNDEFINED'])
# Propriedades relacionadas a análise / detecção de face e olhos
DEBUG_EYES = True
min_size_face = NumericProperty()
min_neighbors_face = NumericProperty()
min_size_eyes = NumericProperty()
min_neighbors_eyes = NumericProperty()
left_eye_image_card = ObjectProperty()
left_eye_image_dbg = ObjectProperty()
left_eye_label_dbg = ObjectProperty()
right_eye_image_card = ObjectProperty()
right_eye_image_dbg = ObjectProperty()
right_eye_label_dbg = ObjectProperty()
instruction_group = ObjectProperty()
canvas_widget = ObjectProperty()
# Configuração inicial do arquivo "config.ini"
def build_config(self, config):
logger.info("build_config...")
config.setdefaults(
'cascade-desktop', {
'minSize-face': '450',
'minNeighbors-face': '5',
'minSize-eyes': '80',
'minNeighbors-eyes': '5'
}
)
config.setdefaults(
'cascade-mobile', {
'minSize-face': '200',
'minNeighbors-face': '5',
'minSize-eyes': '70',
'minNeighbors-eyes': '5'
}
)
config.setdefaults(
'debug', {
'debug-eyes': False
}
)
# Método invocado quando uma configuração é modificada via callback...
def configuracao_modificada(self, section, key, value):
logger.info("Configuração modificada - section: {}, key: {}, value: {}".format(section, key, value))
self.DEBUG_EYES = self.config.getboolean('debug', 'debug-eyes')
if self.DEBUG_EYES:
if self.root is not None: # build() já aconteceu...
if self.left_eye_image_card is None: # Os cards dos olhos de debug não foram construídos
self.build_debug_eyes()
if platform == "macosx" or platform == "windows":
self.min_size_face = self.config.getint('cascade-desktop', 'minsize-face')
self.min_neighbors_face = self.config.getint('cascade-desktop', 'minneighbors-face')
self.min_size_eyes = self.config.getint('cascade-desktop', 'minsize-eyes')
self.min_neighbors_eyes = self.config.getint('cascade-desktop', 'minneighbors-eyes')
elif platform == "android":
self.min_size_face = self.config.getint('cascade-mobile', 'minsize-face')
self.min_neighbors_face = self.config.getint('cascade-mobile', 'minneighbors-face')
self.min_size_eyes = self.config.getint('cascade-mobile', 'minsize-eyes')
self.min_neighbors_eyes = self.config.getint('cascade-mobile', 'minneighbors-eyes')
def go_to_screen(self, screen_to_go):
self.navigation_drawer.set_state("close")
self.screen_manager.current = screen_to_go
if screen_to_go == "screen_01":
self.flip_camera_button.opacity = 1
self.start_analysis_button.opacity = 1
self.canvas_widget.opacity = 1
if self.DEBUG_EYES:
self.left_eye_image_card.opacity = 1
self.right_eye_image_card.opacity = 1
else:
self.flip_camera_button.opacity = 0
self.start_analysis_button.opacity = 0
self.canvas_widget.opacity = 0
if self.DEBUG_EYES:
self.left_eye_image_card.opacity = 0
self.right_eye_image_card.opacity = 0
def habilita_menu(self):
self.content_navigation_drawer.ids.novaanalise.disabled = False
self.content_navigation_drawer.ids.historicoanalises.disabled = False
self.content_navigation_drawer.ids.configuracoes.disabled = False
def build_debug_eyes(self):
self.right_eye_image_card = MDCard(
pos_hint={"top": 0.9},
size_hint=(.45, .15),
opacity=0,
)
self.right_eye_image_dbg = FitImage()
with self.right_eye_image_dbg.canvas:
Color(1, 1, 1)
self.right_eye_label_dbg = MDLabel(
text="?",
pos_hint={"top": 1}
)
self.right_eye_image_card.add_widget(self.right_eye_label_dbg)
self.right_eye_image_card.add_widget(self.right_eye_image_dbg)
self.root.add_widget(self.right_eye_image_card)
# Olho esquerdo...
self.left_eye_image_card = MDCard(
pos_hint={"top": 0.9, "right": 1.0},
size_hint=(.45, .15),
opacity=0,
)
self.left_eye_image_dbg = FitImage()
with self.left_eye_image_dbg.canvas:
Color(1, 1, 1)
self.left_eye_label_dbg = MDLabel(
text="?",
pos_hint={"top": 1}
)
self.left_eye_image_card.add_widget(self.left_eye_label_dbg)
self.left_eye_image_card.add_widget(self.left_eye_image_dbg)
self.root.add_widget(self.left_eye_image_card)
def build(self):
self.config.add_callback(self.configuracao_modificada)
self.configuracao_modificada(None, None, None) # Força a atualização das configurações
"""
# ToDo: Pensar nas cargas que podem ser feitas offline, para não travar a thread principal...
if platform == "macosx" or platform == "android":
self.interpreter = Interpreter(model_path="assets/talking_300W_v2_50_epoch.tflite")
self.interpreter.allocate_tensors()
input = self.interpreter.get_input_details()[0]
logger.info("input_tensor.shape: {}".format(input['shape']))
logger.info("input_tensor.quantization_parameters: {}".format(input['quantization_parameters']))
logger.info("input_tensor.quantization_parameters.scales: {}".
format(input['quantization_parameters']['scales']))
# logger.info("input_tensor.quantization_parameters: {}".format(input['quantization_parameters']))
"""
self.repositorio_dados = RepositorioDadosAnalise()
if platform == "android":
if not check_permission(Permission.CAMERA):
request_permission(Permission.CAMERA)
self.root = MDScreen(name="root")
self.app_bar = MDTopAppBar(
title='Blink',
pos_hint={'top': 1},
left_action_items=[['menu', lambda x: self.navigation_drawer.set_state('open')]],
)
Builder.load_string(KV)
self.content_navigation_drawer = ContentNavigationDrawer()
self.content_navigation_drawer.main_app = self
self.navigation_drawer = MDNavigationDrawer(
self.content_navigation_drawer,
id="nav_drawer",
radius=(0, 16, 16, 0),
)
navigation_layout = MDNavigationLayout()
self.screen_manager = MDScreenManager()
# Tela de nova análise
self.screen_01 = MDScreen(name="screen_01")
# screen_01.add_widget(app_bar)
# Widget para sobrepor a imagem e desenhar os retangulos de detecção no cado do Android
self.canvas_widget = Widget(
pos_hint={"top": 1},
size_hint=(1, 1),
# opacity=0,
)
# self.root.add_widget(self.canvas_widget)
self.screen_01.add_widget(self.canvas_widget)
# Tela inicial
screen_00 = TelaInicial(
name="screen_00",
md_bg_color=(33/255., 90/255., 54/255., 1),
)
screen_00.main_app = self
self.screen_manager.add_widget(screen_00)
# Tela de Histórico de Análises
self.screen_02 = HistoricoAnalises(
name="screen_02",
main_app=self,
# md_bg_color=(33 / 255., 90 / 255., 54 / 255., 1)
)
self.screen_manager.add_widget(self.screen_02)
# Tela Sobre
screen_03 = Sobre(
name="screen_03",
md_bg_color=(33 / 255., 90 / 255., 54 / 255., 1),
)
self.screen_manager.add_widget(screen_03)
# Tela com os detalhes sobre uma análise
screen_04 = TelaDadosAnalise(
name="screen_04"
)
screen_04.main_app = self
self.screen_manager.add_widget(screen_04)
# Tela de Login
screen_05 = TelaLogin(
name="screen_05",
md_bg_color=(33 / 255., 90 / 255., 54 / 255., 1),
)
screen_05.main_app = self
self.screen_manager.add_widget(screen_05)
# Tela de Cadastro
screen_06 = TelaCadastro(
name="screen_06",
md_bg_color=(33 / 255., 90 / 255., 54 / 255., 1),
)
screen_06.main_app = self
self.screen_manager.add_widget(screen_06)
# Tela de Configuração
screen_07 = TelaConfiguracao(
name="screen_07",
md_bg_color=(33 / 255., 90 / 255., 54 / 255., 1),
)
screen_07.main_app = self
self.screen_manager.add_widget(screen_07)
self.screen_manager.add_widget(self.screen_01)
navigation_layout.add_widget(self.screen_manager)
navigation_layout.add_widget(self.navigation_drawer)
self.flip_camera_button = MDFloatingActionButton(
icon='camera-flip-outline',
pos_hint={'right': 0.9, 'top': 0.1},
opacity=0, # Escondido na primeira tela
)
self.flip_camera_button.bind(on_press=self.flip_camera_button_press)
self.root.add_widget(navigation_layout)
# O botão de flip da câmera está vinculado a tela raiz, para ficar sobre a imagem da câmera...
self.root.add_widget(self.flip_camera_button)
# Botão de início de análise
self.start_analysis_button = MDFloatingActionButton(
icon='play-circle-outline',
pos_hint={'center_x': 0.5, 'top': 0.1},
md_bg_color=[0, 1, 0, 1], # Verde...
opacity=0, # Escondido na primeira tela
)
self.start_analysis_button.bind(on_press=self.start_analysis_button_press)
self.root.add_widget(self.start_analysis_button)
Clock.schedule_interval(self.update, 0)
Clock.schedule_once(self.init_tensorflow_model, 0)
self.root.add_widget(self.app_bar)
# Imagem do olho para debug...
if self.DEBUG_EYES:
self.build_debug_eyes()
return self.root
def init_tensorflow_model(self, arg):
logger.debug("init_tensorflow_model arg: {}".format(arg))
self.tensorflow_model = TensorflowModel()
# on_start é executado após o build()
# ToDo: Ainda tenho que descobrir como deixar a camera frontal como padrão no Android
def on_start(self):
logger.debug("platform: {}".format(platform))
#
if platform == "android":
self.camera_display_widget = CameraDisplayWidget(
size_hint_y=None,
height=Window.height - self.app_bar.height - dp(40),
width=Window.width,
y=dp(20)
)
elif platform == "macosx":
self.camera_display_widget = camera_desktop.CameraDisplayWidget(
size_hint_y=None,
height=Window.height - self.app_bar.height - dp(40),
width=Window.width,
y=dp(20)
)
self.screen_01.add_widget(
self.camera_display_widget,
index=100, # Z-index, will be drawn under the others widgets
)
logger.info('*** cdw size: {}'.format(self.camera_display_widget.size))
"""if platform == "android":
# self.camera_display_widget.restart_stream()
# Clock.schedule_once(self.flip_camera_button_press, 1)
# Clock.schedule_once(self.flip_camera_button_press, 3)
# Clock.schedule_once(self.flip_camera_button_press, 5)
# self.camera_display_widget.rotate_cameras()
# self.camera_display_widget.rotate_cameras()
"""
def flip_camera_button_press(self, button):
self.camera_display_widget.rotate_cameras()
def start_analysis_button_press(self, button):
if not self.analise_iniciada:
self.analise_iniciada = True
self.start_analysis_button.icon = "stop-circle-outline"
# self.start_analysis_button.md_bg_color = [1, 0, 0, 1], # Vermelho...
self.ultima_analise = DadosAnalise(
data_hora_analise=datetime.now(),
duracao_analise=-1,
quantidade_de_piscadas=-1,
piscadas_por_minuto=-1,
dados_frames=[],
)
else:
self.analise_iniciada = False
self.start_analysis_button.icon = "play-circle-outline"
nome_arquivo = "ANALISE_{}.json".format(self.ultima_analise.data_hora_analise.strftime("%Y%m%d%H%M%S"))
# Checa se o diretório de dados existe e criar caso não exista...
file_folder = os.path.join(get_storage_path(), "json_data")
if not os.path.isdir(file_folder):
os.mkdir(file_folder)
file_path = os.path.join(get_storage_path(), "json_data", nome_arquivo)
with open(file_path, "w", encoding="utf-8") as file:
json.dump(self.ultima_analise.toJSON(), file, ensure_ascii=False, indent=4)
logger.info("JSON: {}".format(self.ultima_analise.toJSON()))
file.close()
# self.start_analysis_button.md_bg_color = [0, 1, 0, 1], # Vermelho...
# Força atualização da lista de análises
self.repositorio_dados = RepositorioDadosAnalise()
self.screen_02 = HistoricoAnalises(
name="screen_02",
main_app=self,
)
"""
def on_start(self):
#self.root.ids.box.add_widget(self.camera_display_widget)
Clock.schedule_interval(self.teste, 1)
"""
def on_texture(self, instance, value):
logger.debug("App texture changed to {}".format(value))
def update(self, dt):
# Se a tela atual não for a tela de análise não faz o update da tela...
if self.screen_manager.current != "screen_01":
return
self.current_frame += 1
begin_update = datetime.now() # Para medir o tempo total do update
self.root.canvas.ask_update()
img = self.camera_display_widget.export_as_image()
logger.debug('*** img: {}'.format(img))
logger.debug("self.min_size_face: {}".format(self.min_size_face))
if img is not None:
# texture.pixels have the RGBA data
# logger.info('*** img.texture.pixels: {}'.format(img.texture.pixels))
pixel_array = np.frombuffer(img.texture.pixels, np.uint8)
logger.debug("pixel_array_shape: {}".format(pixel_array.shape))
# logger.info('*** pixel_array - len: {} height: {}, width: {}'.
# format(len(pixel_array), img.height, img.width))
pixel_array_rgba = pixel_array.reshape(img.height, img.width, 4)
pixel_array = pixel_array_rgba[:, :, :3] # Elimina o canal alpha
logger.debug("pixel_array_shape (after reshape): {}".format(pixel_array.shape))
# *** Get eyes coordinates via YOLO model
begin_yolo = datetime.now()
# self.tensorflow_model.get_eyes_coordinates(pixel_array)
logger.info('*** Tempo yolo: {}'.format(datetime.now() - begin_yolo))
# ***
# logger.info('*** pixel_array: {}'.format(pixel_array))
# Aumenta o contraste...
# ToDo: Explore the pre-processing possibilities.
# pixel_array = self.pre_process_image(pixel_array)
begin_gray_convertion = datetime.now()
# gray = cv2.cvtColor(pixel_array, cv2.COLOR_BGR2GRAY)
gray = pixel_array
logger.info('*** Tempo : cvtColor {}'.format(datetime.now() - begin_gray_convertion))
logger.debug('*** gray: {}'.format(gray))
if not self.running_face_detection:
# Perform face detection in a different Thread, trying to speed up FPS
threading.Thread(
target=self.run_face_detection,
args=(gray, self.min_neighbors_face, self.min_size_face,)).start()
# If a face is already detected...
if self.face_rect is not None:
x = self.face_rect[0]
y = self.face_rect[1]
w = self.face_rect[2]
h = self.face_rect[3]
logger.debug('*** x:{} y:{}, w:{}, h:{}'.format(x, y, w, h))
# Cria uma imagem recortada somente da face para acelerar o processo de detecção dos olhos
face_img_rgba = pixel_array_rgba[
self.face_rect[1]:self.face_rect[1] + self.face_rect[3],
self.face_rect[0]:self.face_rect[0] + self.face_rect[2]
]
face_img_rgb = face_img_rgba[:, :, :3] # Elimina o canal alpha
# face_gray = cv2.cvtColor(face_img_rgb, cv2.COLOR_BGR2GRAY)
face_gray = face_img_rgb
# Tamanho mínimo de detecção. Nas cameras de desktops o tamanho dos olhos é menor
# ToDo: Colocar scaleFactor nas configurações
min_size_eyes = self.min_size_eyes
min_neighbors_eyes = self.min_neighbors_eyes
if not self.running_eyes_detection:
# Perform eye detection in a different Thread, trying to speed up FPS
threading.Thread(
target=self.run_eyes_detection,
args=(face_gray, min_neighbors_eyes, min_size_eyes, self.face_rect)).start()
# Registra frame
if self.analise_iniciada:
if self.ultima_analise is not None:
dados_frame_atual = DadoFrame(
data_hora_frame=datetime.now(),
numero_frame=-1,
olho_direito_aberto=-1,
olho_esquerdo_aberto=-1,
)
# Faces e olhos processados para esse frame, fazer a detecção de olhos abertos / fechados
# via tensorflow
# Para copiar uma área do frame: frame[y:y+h, x:x+w]
# Processa olho direito.
# ToDo: Generalizar essa parte do código, criando uma função
if self.right_eye_rect is not None:
right_eye_img = pixel_array[
self.right_eye_rect[1]:self.right_eye_rect[1] + self.right_eye_rect[3],
self.right_eye_rect[0]:self.right_eye_rect[0] + self.right_eye_rect[2]
]
# *** Cria imagem para debug...
if self.DEBUG_EYES:
right_eye_texture = get_texture_from_rgba_array(
pixel_array_rgba, self.right_eye_rect, (200, 200))
with self.right_eye_image_dbg.canvas:
Rectangle(texture=right_eye_texture, pos=self.right_eye_image_dbg.pos, size=(200, 200))
openess_eye_prediction = self.tensorflow_model.get_openess_eye_prediction(right_eye_img)
if self.analise_iniciada:
dados_frame_atual.olho_direito_aberto = 1 if openess_eye_prediction == "open" else 0
if self.DEBUG_EYES:
self.right_eye_label_dbg.text = openess_eye_prediction
if self.left_eye_rect is not None:
left_eye_img = pixel_array[
self.left_eye_rect[1]:self.left_eye_rect[1] + self.left_eye_rect[3],
self.left_eye_rect[0]:self.left_eye_rect[0] + self.left_eye_rect[2]
]
# Cria imagem para debug...
if self.DEBUG_EYES:
left_eye_texture = get_texture_from_rgba_array(
pixel_array_rgba, self.left_eye_rect, (200, 200))
with self.left_eye_image_dbg.canvas:
Rectangle(texture=left_eye_texture, pos=self.left_eye_image_dbg.pos, size=(200, 200))
openess_eye_prediction = self.tensorflow_model.get_openess_eye_prediction(left_eye_img)
if self.analise_iniciada:
dados_frame_atual.olho_esquerdo_aberto = 1 if openess_eye_prediction == "open" else 0
if self.DEBUG_EYES:
self.left_eye_label_dbg.text = openess_eye_prediction
if self.analise_iniciada:
if self.ultima_analise.dados_frames is None:
# Primeiro frame...
self.ultima_analise.dados_frames = []
self.ultima_analise.dados_frames.append(dados_frame_atual)
else:
self.ultima_analise.dados_frames.append(dados_frame_atual)
# Desenha os retângulos da face e dos olhos no canvas
begin_update_canvas = datetime.now()
if platform == "macosx" or platform == "android":
if self.instruction_group is not None:
# if platform == "macosx":
# self.camera_display_widget.canvas.remove(self.instruction_group)
self.canvas_widget.canvas.remove(self.instruction_group)
# if self.DEBUG_EYES:
self.instruction_group = InstructionGroup()
if self.face_rect is not None:
# Cria sequencia de instrucoes para a face
self.create_group_instructions_rect(
self.camera_display_widget,
self.face_rect,
Color(1, 1, 1), # Face será cinza
)
if self.left_eye_rect is not None:
# Cria sequencia de instrucoes para olho esquerdo
self.create_group_instructions_rect(
self.camera_display_widget,
self.left_eye_rect,
Color(0, 1, 0), # Olho esquerdo será verde
)
if self.right_eye_rect is not None:
# Cria sequencia de instrucoes para olho direito
self.create_group_instructions_rect(
self.camera_display_widget,
self.right_eye_rect,
Color(0, 0, 1), # Olho direito será azul
)
# for instruction in self.instruction_group.get_group('my_group'):
# logger.info("Instruction :{}".format(instruction))
# if platform == "macosx":
# self.camera_display_widget.canvas.add(self.instruction_group)
self.canvas_widget.canvas.add(self.instruction_group)
logger.info('*** Tempo update canvas: {}'.format(datetime.now() - begin_update_canvas))
logger.info('*** Tempo total update: {}'.format(datetime.now() - begin_update))
# logger.info("***Updating Canvas...")
def run_face_detection(self, image, min_neighbors_face, min_size_face):
self.running_face_detection = True
begin = datetime.now()
faces = faceCascade.detectMultiScale(
image,
scaleFactor=1.1,
minNeighbors=min_neighbors_face,
minSize=(min_size_face, min_size_face),
flags=cv2.CASCADE_SCALE_IMAGE,
)
for (x, y, w, h) in faces:
y = y + int(h / 8)
self.face_rect = (x, y, w, int(h/2)) # h/3 -> Elimina a metade da boca...
logger.info('*** Tempo faceCascade: {} #faces:{} '.format(datetime.now() - begin, len(faces)))
if platform == "android":
sleep(0.3) # There is no need to update faces too frequently. Helps to improve FPS
self.running_face_detection = False
def run_eyes_detection(self, image, min_neighbors_eyes, min_size, face_rect):
self.running_eyes_detection = True
logger.debug("run_eyes_detection - image: {}".format(image))
begin = datetime.now()
eyes = eyeCascade.detectMultiScale(
# gray,
# face_gray,
image,
scaleFactor=1.10, # Não precisa escalar muito, a distância da selfie não varia muito... 1.02
minNeighbors=min_neighbors_eyes, # Valores maiores fazem menos detecções mais precisas
# 5 não estava detectando olho direito...
# minSize=(min_size_eyes, min_size_eyes),
minSize=(min_size, min_size),
flags=cv2.CASCADE_SCALE_IMAGE,
)
logger.info('*** Tempo eyeCascade: {} #eyes:{} '.format(datetime.now() - begin, len(eyes)))
x = face_rect[0]
y = face_rect[1]
w = face_rect[2]
h = face_rect[3] * 2
for (ex, ey, ew, eh) in eyes:
logger.info('*** eyes - ex:{} ey:{}, ew:{}, eh:{}'.format(ex, ey, ew, eh))
# Na imagem da face recortada a posição relativa da imagem é zero...
which_eye = self.get_eye(0, 0, w, h, ex, ey, ew, eh)
logger.info("which_eye: {}".format(which_eye))
if which_eye == self.Eyes.LEFT:
self.left_eye_rect = (x + ex, y + ey, ew, eh)
elif which_eye == self.Eyes.RIGHT:
self.right_eye_rect = (x + ex, y + ey, ew, eh)
if platform == "android":
sleep(0.2) # Update eyes less frequently. Helps to improve FPS
self.running_eyes_detection = False
def create_group_instructions_rect(self, cdw, rect, color):
# O y deve ser ajustado, no canvas a origem (0,0) é no canto inferior esquerdo. As coordenadas
# de detecção tem origem no canto superior esquerdo.
ey_ajustado = float(cdw.height - rect[1] + cdw.pos[1] - rect[3])
logger.debug("self.camera_display_widget.pos: {}".format(cdw.pos))
self.instruction_group.add(color)
self.instruction_group.add(
Line(
rectangle=(rect[0], ey_ajustado, rect[2], rect[3]),
width=2,
# group="faces_olhos",
)
)
# self.eye_rectangle.add(Line(rectangle=(0, 0, 50, 50), width=3))
# self.eye_rectangle.add(Line(rectangle=(0, self.camera_display_widget.height, 50, 50), width=3))
# =========== Define uma Snackbar
def snackbar_show(self, texto):
snackbar = Snackbar(text=texto)
snackbar.open()
# Tentando aumentar o contraste da imagem para ver o resultado na detecção de olhos...
# https://stackoverflow.com/questions/39308030/how-do-i-increase-the-contrast-of-an-image-in-python-opencv
def pre_process_image(self, img):
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l_channel, a, b = cv2.split(lab)
# Applying CLAHE to L-channel
# feel free to try different values for the limit and grid size:
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
cl = clahe.apply(l_channel)
# merge the CLAHE enhanced L-channel with the a and b channel
limg = cv2.merge((cl, a, b))
# Converting image from LAB Color model to BGR color spcae
enhanced_img = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
return enhanced_img
def get_eye(self, x, y, w, h, ex, ey, ew, eh):
# Calcula o retangulo possível para os olhos
dist = 0.1
_xr = x + w * dist # right eye x
_xl = x + (w / 2) + w * dist # left eye x
_y = y + h * dist
_w = (w * (1 - 4 * dist)) / 2
_h = (h * (1 - 4 * dist)) / 2
# Calcula o x,y central dos olhos
_ex = ex + ew / 2
_ey = ey + eh / 2
logger.debug("_xr:{} _xl:{} _y:{} _w:{} _h:{} / _ex:{} _ey:{}".format(_xr, _xl, _y, _w, _h, _ex, _ey))
if(_ex < (_xr + _w)) and (_ex > _xr) and (_ey < (_y + _h)) and (_ey > _y):
return self.Eyes.RIGHT
# elif (_ex < (_xl + _w)) and (_ex > _xl) and (_ey < (_y + _h)) and (_ey > _y):
elif (
(_ex < (_xl + _w)) and
(_ex > _xl) and (_ey < (_y + _h)) and (_ey > _y)
):
return self.Eyes.LEFT
else:
return self.Eyes.UNDEFINED
if __name__ == "__main__":
BlinkApp().run()