# 파이선 업그레이드
pip install --upgrade pip
# 라이브러리 설치
pip install opencv-python
데이터 가져오기
import cv2
# 로컬에서 이미지 가져오기
image = cv2.imread('이미지 경로')
# 이미지 로드 확인
if image is None:
print("Error: Unable to load image. Please check the file path.")
exit()
# 이미지 확인 (height, width, channels)
print(f"Original Image Shape: {image.shape}")
# 로컬 창에 이미지 출력
cv2.imshow('Original Image', image)
cv2.waitKey(0) # 사용자가 아무 키나 누를때 까지 이미지 창을 열어둡니다.
데이터전처리
# 처리하기 쉽도록 이미지 크기를 조정
image = cv2.resize(image, (800, 600))
print(f"Resized Image Shape: {image.shape}")
# grayscale로 이미지 전환
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Image', image_gray)
cv2.waitKey(0)
이미지가 너무 크면 처리 시간이 지연 될 수 있으므로 이미지 크기를 조절해줍니다.
얼굴 탐지 실행
# 얼굴 탐지를 위한 pre-trained Haar cascade 분류기 로드 하기
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml 경로')
# grayscale 이미지에서 얼굴 탐지 실행하기
detections = face_detector.detectMultiScale(image_gray)
# 탐지된 얼굴 갯수 출력
print(f"Number of faces detected: {len(detections)}")
사각형 그리기
# 탐지된 얼굴에 사각형 그리기
for (x, y, w, h) in detections:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 255), 5)
# 탐지된 얼굴 사각형 출력
cv2.imshow('Faces Detected', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
파라미터 조절
image = cv2.imread('경로people1.jpg')
image = cv2.resize(image, (800, 600))
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
detections = face_detector.detectMultiScale(image_gray, scaleFactor = 1.09)
for (x, y, w, h) in detections:
cv2.rectangle(image, (x, y), (x + w, y + h), (0,255,0), 5)
cv2_imshow(image)
'AI > computer vision' 카테고리의 다른 글
[Amazon Rekognition] Facial Authentication (0) | 2024.10.05 |
---|---|
[AWS SageMaker] ML Model Deployment on SageMaker (feat. Lambda) (2) | 2024.10.03 |