2013-08-05 3 views
7

मैं जानना चाहता हूं कि चेहरे को धुंधला करने का एक तरीका है जिसे स्वचालित रूप से हार्कास्केड चेहरे वर्गीकरण द्वारा पहचाना गया है।चेहरों को धुंधला करने के लिए opencv (पायथन) का उपयोग कैसे करें?

नीचे दिए गए कोड का उपयोग करके, मैं चेहरे का पता लगाने, इस चेहरे के चारों ओर छवि को फसल करने या उस पर एक आयत खींचने में सक्षम हूं।

image = cv2.imread(imagepath) 

# Specify the trained cascade classifier 
face_cascade_name = "./haarcascade_frontalface_alt.xml" 

# Create a cascade classifier 
face_cascade = cv2.CascadeClassifier() 

# Load the specified classifier 
face_cascade.load(face_cascade_name) 

#Preprocess the image 
grayimg = cv2.cvtColor(image, cv2.cv.CV_BGR2GRAY) 
grayimg = cv2.equalizeHist(grayimg) 

#Run the classifiers 
faces = face_cascade.detectMultiScale(grayimg, 1.1, 2, 0|cv2.cv.CV_HAAR_SCALE_IMAGE, (30, 30)) 

print "Faces detected" 

if len(faces) != 0:   # If there are faces in the images 
    for f in faces:   # For each face in the image 

     # Get the origin co-ordinates and the length and width till where the face extends 
     x, y, w, h = [ v for v in f ] 

     # Draw rectangles around all the faces 
     cv2.rectangle(image, (x,y), (x+w,y+h), (255,255,255)) 
     sub_face = image[y:y+h, x:x+w] 
     for i in xrange(1,31,2): 
      cv2.blur(sub_face, (i,i)) 
     face_file_name = "./face_" + str(y) + ".jpg" 
     cv2.imwrite(face_file_name, sub_face) 

लेकिन मैं लोगों के चेहरे को धुंधला करना चाहता हूं ताकि उन्हें पहचाना जा सके।

क्या आपके पास ऐसा करने का विचार है?

आपकी मदद के लिए धन्यवाद

अरनॉड

+1

उपयोग [इन] (http://docs.opencv.org/doc/tutorials/imgproc/gausian_median_blur_bilateral_filter/gausian_median_blur_bilateral_filter.html) कार्यों में से एक है और में पारित चेहरे युक्त – Hammer

+0

हाय हैमर, मैंने सोचा कि मुझे यह नहीं पता कि छवि के उस हिस्से को केवल धुंधला करने के लिए जहां चेहरे का पता चला है। धन्यवाद। –

+0

मैं आखिर में जो चाहता हूं वह करने में सफल रहा। 'sub_face = cv2.GaussianBlur (sub_face, (23, 23), 30)' तो मैं एक नया एक को यह धुंधला छवि ओवरलैप: 'result_image [y कि एक gaussianblur के रूप में आप का सुझाव दिया है लागू करने के लिए: y + sub_face.shape [0], x: x + sub_face.shape [1]] = sub_face' –

उत्तर

11

मैं अंत में मैं क्या चाहते करने के लिए सफल रहा। ऐसा करने के लिए हैमर ने सुझाव दिया है कि एक gaussianblur लागू करें। कोड है:

image = cv2.imread(imagepath) 
result_image = image.copy() 

# Specify the trained cascade classifier 
face_cascade_name = "./haarcascade_frontalface_alt.xml" 

# Create a cascade classifier 
face_cascade = cv2.CascadeClassifier() 

# Load the specified classifier 
face_cascade.load(face_cascade_name) 

#Preprocess the image 
grayimg = cv2.cvtColor(image, cv2.cv.CV_BGR2GRAY) 
grayimg = cv2.equalizeHist(grayimg) 

#Run the classifiers 
faces = face_cascade.detectMultiScale(grayimg, 1.1, 2, 0|cv2.cv.CV_HAAR_SCALE_IMAGE, (30, 30)) 

print "Faces detected" 

if len(faces) != 0:   # If there are faces in the images 
    for f in faces:   # For each face in the image 

     # Get the origin co-ordinates and the length and width till where the face extends 
     x, y, w, h = [ v for v in f ] 

     # get the rectangle img around all the faces 
     cv2.rectangle(image, (x,y), (x+w,y+h), (255,255,0), 5) 
     sub_face = image[y:y+h, x:x+w] 
     # apply a gaussian blur on this new recangle image 
     sub_face = cv2.GaussianBlur(sub_face,(23, 23), 30) 
     # merge this blurry rectangle to our final image 
     result_image[y:y+sub_face.shape[0], x:x+sub_face.shape[1]] = sub_face 
     face_file_name = "./face_" + str(y) + ".jpg" 
     cv2.imwrite(face_file_name, sub_face) 

# cv2.imshow("Detected face", result_image) 
cv2.imwrite("./result.png", result_image) 

अरनॉड

+0

आपको लेंस (चेहरे)! = 0' की आवश्यकता नहीं है, लूप के लिए एक रिक्त सूची पर पुनरावृत्ति नहीं है। उर्फ, लूप के लिए प्रत्येक के शीर्ष पर अगर कथन में प्रभावी रूप से बनाया गया है। – Kurt

+0

'x, y, w, h = f' अधिक पायथनिक है – Kurt

संबंधित मुद्दे