[컴비콜라] 남자? 여자?(gender recognition using cloud IDE+python+tensorflow)

https://www.youtube.com/watch?v=4u6SZ3suXIA&t=22s

 

1. 구름 IDE 주소

https://ide.goorm.io/

 

2. 라이브러리(패키지, 모듈) 설치

opencv-python 설치

pip3 install opencv-python

 

cvlib 설치

pip3 install cvlib

 

3. 성별 인식 파이썬 코드

# import necessary packages
import cv2
import cvlib as cv
import numpy as np

# read input image
img = cv2.imread('테스트에 사용할 이미지 파일명')

# apply face detection
face, conf = cv.detect_face(img)

padding = 20

# loop through detected faces
for f in face:
    (startX,startY) = max(0, f[0]-padding), max(0, f[1]-padding)
    (endX,endY) = min(img.shape[1]-1, f[2]+padding), min(img.shape[0]-1, f[3]+padding)
    
    # draw rectangle over face
    cv2.rectangle(img, (startX,startY), (endX,endY), (0,255,0), 2)

    face_crop = np.copy(img[startY:endY, startX:endX])

    # apply gender detection
    (label, confidence) = cv.detect_gender(face_crop)

    idx = np.argmax(confidence)
    label = label[idx]
    label = "{}: {:.2f}%".format(label, confidence[idx] * 100)
    Y = startY - 10 if startY - 10 > 10 else startY + 10  
    cv2.putText(img, label, (startX, Y),  cv2.FONT_HERSHEY_SIMPLEX,
                0.7, (0, 255, 0), 2)

# save output
cv2.imwrite("gender_detection.jpg", img)

 

댓글()