import cv2

from cam import USBCam

c = USBCam()  #USBCam(show=True)


def capture(frame):
    # frame 처리
    cv2.imshow('frame', frame)
    key = cv2.waitkey(25)
    if key == 27: return False
    return True


c.run(capture)

def detect_face(frame):
    global face_image
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # 이미지에서 얼굴을 검출합니다.
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    # 얼굴이 검출되었다면 얼굴 위치에 대한 좌표 정보를 리턴받습니다.
    for (x, y, w, h) in faces:
        # 원본 이미지에 얼굴의 위치를 표시
        # cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
        minLength = min(w, h)
        if minLength < 150: break
        width = max(w, h)
        # 얼굴 부분 검출
        # face_image = frame[y:y+h, x:x+w].copy()
        x = x + w // 2 - width // 2
        y = y + h // 2 - width // 2
        face_image = frame[y:y + width, x:x + width].copy()
        cv2.rectangle(frame, (x, y), (x + width, y + width), (255, 0, 0), 2)
        face_image = cv2.resize(face_image,
                                dsize=(FACE_WIDTH, FACE_WIDTH),
                                interpolation=cv2.INTER_AREA)
        save_image(face_image)  # 얼굴영역 저장
    frame[0:FACE_WIDTH, 0:FACE_WIDTH] = face_image[:]  # 좌측 상단에 출력
    return True


c = USBCam(show=True)
print('cam start')
c.run(detect_face)
Beispiel #3
0
def intrusion_detection(output_dict):
    persons = []
    for ix, obj_ix in enumerate(output_dict['detection_classes']):
        if obj_ix == 1 and output_dict['detection_scores'][ix] >= 0.5:
            persons.append(ix)

    return len(persons)


def detect(frame):
    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    output_dict = api.inference_image(frame_rgb)

    # 침입인지 아닌지 판단... person ID : 1
    if intrusion_detection(output_dict):
        print("침입 발생")
    # 레코딩 시작.
    # 카톡으로 알림 전송 등 후속 처리 ...

    labeled_image = api.visualize(frame_rgb, output_dict)
    labeled_image = cv2.cvtColor(labeled_image, cv2.COLOR_RGB2BGR)
    cv2.imshow('frame', labeled_image)
    key = cv2.waitKey(1)
    if key == 27:
        return False
    else:
        return True


cam = USBCam()
cam.run(detect)