2017-12-07 10 views
1

gif 이미지에서 얼굴을 감지하려고하지만 OpenCV가 gif 형식을 지원하지 않으므로 PIL 모듈을 사용하여 gif 이미지를 사용하여 OpenCV를 numpy 배열로 다시 변환합니다. 그러나 이렇게하면 어설 션 오류가 발생합니다. 여기 OpenCV 오류 발생 : 어설 션 실패 (scn == 3 || scn == 4)

import cv2 
import numpy as np 
from PIL import Image 

# get the features and pass it to the Cascade Classifier 
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") 
# read the image 
img = Image.open("mypic.sleepy") 
# check if image exists 
if img is None: 
    raise Exception("could not load image !") 
# represent the image in matrix format for the OpenCV to work on it 
img = np.array(img) 
# convert it to gray scale 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

# detect the objects resembling faces 
faces = face_cascade.detectMultiScale(gray,1.5,3) #(image,scale_factor, minm_no_of_neighbours) 
for face in faces: 
    # the detected face is represented in the form of a rectangle 
    x, y, w, h = face 
    # draw a rectangle on the face in the image 
    cv2.rectangle(img, (x,y), (x + w, y + h), (0, 255, 0), 2) 
# show the image 
cv2.imshow("Detected Faces", img) 
# hold the window 
cv2.waitKey(0) 
# destroy all windows 
cv2.destroyAllWindows() 

이 아래에있는 내 코드는 내가 인터넷에서 발견되는 일반적인 제안 이미지가로드되지 않는 것입니다

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/souvik/opencv-3.3.0/modules/imgproc/src/color.cpp, line 10638 

가 발생하고있는 오류입니다 그것이 발생 이유입니다 이러한 오류는 분명히 내 경우에는 이미지가 실제로로드되지 않으면 내 코드는 예외를 던질 것이다. 또한이 코드를 실행하려고하면

print(img.shape) 

나는 (243, 320) 값을 얻습니다. 그래서 내가 어디로 잘못 가고 있습니까?

+3

로드하는 이미지는 이미 그레이 스케일 (단일 채널)입니다. ** 회색조로 변환 ** – Miki

+0

@Miki 당신은 맞았고 컬러 GIF 이미지로 바꾸었지만 문제는 여전히 지속됩니다! –

+0

gif는 아마도 colormapped, 즉 단일 채널입니다. 색깔이있는 PNG, JPG 또는 기본적으로는 – Miki

답변

1

다른 색상의 gif 이미지로 코드를 시도했으며 img에서 face_cascade를 사용하면 코드가 작동하는 것 같습니다. 회색 음영 변환 및 주석 처리를 시도하십시오.

faces = face_cascade.detectMultiScale(img,1.5,3) 
관련 문제