feat: fixed bug in camera code

This commit is contained in:
2021-01-17 20:14:17 +01:00
parent 049cc72ecd
commit e834335e27

View File

@@ -1,6 +1,7 @@
import configparser import configparser
import os.path import os.path
from io import BytesIO from io import BytesIO
from pprint import pprint
from typing import NamedTuple, Optional, Tuple from typing import NamedTuple, Optional, Tuple
import cv2 import cv2
@@ -46,7 +47,7 @@ class Camera:
self.rotate = get_rotate_value(self.config['rotate']) self.rotate = get_rotate_value(self.config['rotate'])
self.detect_face = False self.detect_face = True
self.draw_face = self.config.getboolean('draw_face', fallback=False) self.draw_face = self.config.getboolean('draw_face', fallback=False)
def get_image(self) -> Tuple[Optional[bytes], Optional[FacePos]]: def get_image(self) -> Tuple[Optional[bytes], Optional[FacePos]]:
@@ -60,10 +61,11 @@ class Camera:
if self.detect_face: if self.detect_face:
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(image_gray, 1.3, 5) faces = face_cascade.detectMultiScale(image_gray, 1.3, 5)
face = FacePos(*faces[0]) if faces is not None and len(faces) > 0:
if self.draw_face: face = FacePos(*faces[0])
for (x, y, w, h) in faces: if self.draw_face:
image = cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2) for (x, y, w, h) in faces:
image = cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
jpg = Image.fromarray(image_rgb) jpg = Image.fromarray(image_rgb)