라즈베리파이에서 파이썬으로 얼굴인식을 하기 위해서는 다음의 간단한 소스코드를 사용할 수 있습니다.
웹캠 사용시
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier('/home/pi/opencv-3.3.0/data/haarcascades/haarcascade_frontalface_alt.xml')
cap = cv2.VideoCapture(0)
scaling_factor=0.5
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in face_rects:
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 3)
cv2.imshow('Face Detector', frame)
c = cv2.waitKey(1)
if c == 27:
beak
cap.release()
cv2.destroyAllWindows()
파이카메라 사용시
import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
face_cascade = cv2.CascadeClassifier('/home/pi/opencv-3.3.0/data/haarcascades/haarcascade_frontalface_alt.xml')
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(640, 480))
time.sleep(0.1)
for frame in camera.capture_continuous(rawCapture, format = "bgr", use_video_port=True):
img = frame.array
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in face_rects:
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 3)
cv2.imshow('Face Detector', img)
key = cv2.waitKey(1) & 0xFF
rawCapture.truncate(0)
if key == ord("q"):
beak
cap.release()
cv2.destroyAllWindows()
최신댓글