Spaces:
Sleeping
Sleeping
Update app.py
Browse fileschange gradio image to np image for openCV
app.py
CHANGED
|
@@ -34,23 +34,23 @@ def check_passport_photo(image: Image.Image) -> bool:
|
|
| 34 |
"""
|
| 35 |
if image is None:
|
| 36 |
raise ValueError("No image uploaded.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
|
| 39 |
if image is None:
|
| 40 |
raise ValueError("Invalid or corrupted image file.")
|
| 41 |
|
|
|
|
| 42 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
face_detector = cv2.CascadeClassifier(HAAR_CASCADE_PATH)
|
| 48 |
-
detected_faces = face_detector.detectMultiScale(
|
| 49 |
-
gray_image,
|
| 50 |
-
scaleFactor=FACE_DETECTION_SCALE_FACTOR,
|
| 51 |
-
minNeighbors=FACE_DETECTION_MIN_NEIGHBORS,
|
| 52 |
-
minSize=MIN_FACE_SIZE
|
| 53 |
-
)
|
| 54 |
return len(detected_faces) == 1
|
| 55 |
|
| 56 |
@tool
|
|
|
|
| 34 |
"""
|
| 35 |
if image is None:
|
| 36 |
raise ValueError("No image uploaded.")
|
| 37 |
+
|
| 38 |
+
# Convert PIL image to NumPy array
|
| 39 |
+
image = np.array(image) # PIL -> NumPy
|
| 40 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert RGB to BGR (OpenCV uses BGR)
|
| 41 |
|
|
|
|
| 42 |
if image is None:
|
| 43 |
raise ValueError("Invalid or corrupted image file.")
|
| 44 |
|
| 45 |
+
# Convert to grayscale
|
| 46 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 47 |
+
|
| 48 |
+
# Load face detector
|
| 49 |
+
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 50 |
|
| 51 |
+
# Detect faces
|
| 52 |
+
detected_faces = face_detector.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(80, 80))
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return len(detected_faces) == 1
|
| 55 |
|
| 56 |
@tool
|