oldgarden21 commited on
Commit
be15e1b
·
verified ·
1 Parent(s): 022699a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -1,20 +1,31 @@
1
- import computervision
 
 
 
2
 
3
- # Load the cascade classifier for face detection
4
- face_cascade = computervision.CascadeClassifier('haarcascade_frontalface_alt.xml')
 
 
 
5
 
6
- # Load the input image
7
- img = computervision.Image('input_image.jpg')
8
 
9
- # Convert the image to grayscale
10
- gray = img.convert_to_grayscale()
11
 
12
- # Detect faces in the grayscale image using the Haar cascade
13
- faces = face_cascade.detect_multi_scale(gray, scale_factor=1.1, min_neighbors=5, min_size=(30, 30))
14
 
15
- # Draw rectangles around the detected faces
16
- for (x, y, w, h) in faces:
17
- img.draw_rectangle((x, y), (x+w, y+h), fill=(0, 255, 0))
18
 
19
- # Display the output image with the detected faces
20
- img.show()
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from PIL import Image
4
+ import gradio as gr
5
 
6
+ def detect_faces(image):
7
+ # OpenCV를 사용하여 이미지를 배열로 변환
8
+ open_cv_image = np.array(image)
9
+ # OpenCV에서 사용하는 색상 순서는 RGB가 아닌 BGR이므로 변환
10
+ open_cv_image = open_cv_image[:, :, ::-1].copy()
11
 
12
+ # 사전 훈련된 얼굴 인식 분류기 로드
13
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
14
 
15
+ # 이미지를 회색조로 변환
16
+ gray = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2GRAY)
17
 
18
+ # 얼굴 감지
19
+ faces = face_cascade.detectMultiScale(gray, 1.1, 4)
20
 
21
+ # 감지된 얼굴 주위에 사각형 그리기
22
+ for (x, y, w, h) in faces:
23
+ cv2.rectangle(open_cv_image, (x, y), (x+w, y+h), (255, 0, 0), 2)
24
 
25
+ # OpenCV에서 사용한 BGR 이미지를 PIL 이미지로 변환
26
+ return Image.fromarray(open_cv_image[:, :, ::-1])
27
+
28
+ iface = gr.Interface(fn=detect_faces, inputs=gr.inputs.Image(), outputs="image", title="얼굴 감지", description="이미지를 업로드하면 얼굴에 사각형을 그립니다.")
29
+
30
+ if __name__ == "__main__":
31
+ iface.launch()