Ask Your Question
0

Problem with using Convex hull in Face Detection.

asked 2019-03-02 17:09:58 -0600

Dedo gravatar image

updated 2019-03-03 06:50:34 -0600

import cv2 import numpy as np

face_cascade = cv2.CascadeClassifier('C:/Users/Ahmed/Desktop/Python Files/haarcascade_frontalface_alt.xml')

# VIDEO CAPTURE
cap = cv2.VideoCapture(0)

while 1:
 ret, img = cap.read()
 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 faces = face_cascade.detectMultiScale(img_gray, 1.3, 5)
 ret,thresh = cv2.threshold(img_gray, 127, 255, 0)
 mask = np.zeros(thresh.shape, dtype="uint8")  # CREATING MASK

 for (x, y, w, h) in faces:
  img2 = cv2.bitwise_and(thresh, mask)
  contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  cv2.drawContours(img, contours, 0, (255, 255, 0), 3)
  cv2.drawContours(final, contours, 0, (255, 255, 0), 3)

  cnt = contours[0]
  hull = cv2.convexHull(cnt, returnPoints=False)
  defects = cv2.convexityDefects(cnt, hull)


  for i in range(defects.shape[0]):
   p, q, r, s = defects[i, 0]
   start = tuple(cnt[p][0])
   end = tuple(cnt[q][0])
   dip = tuple(cnt[r][0])
  cv2.line(img, start, end, [0, 255, 0], 5)

 cv2.imshow('img',img)

 k = cv2.waitKey(30)
 if k == 27:
     break

 cv2.waitKey(0)
 cv2.destroyAllWindows()

i am trying to detect the Face using Convex Hull Algorithm , but my Code doesn't work with me .and gives me this Error (AttributeError: 'NoneType' object has no attribute 'shape').so could any one help me to fix this Problem.

edit retag flag offensive close merge delete

Comments

use a debugger

LBerger gravatar imageLBerger ( 2019-03-03 02:11:25 -0600 )edit

imho, you're using the wrong idea. to get the face outliine. please have a look at facial landmarks

berak gravatar imageberak ( 2019-03-03 02:26:01 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-03-03 04:53:46 -0600

supra56 gravatar image

Error (IndexError: list index out of range) can be found this.

cv2.line(img, start, end, [0, 255, 0], 5)

to:

cv2.line(img, finger1, finger2, [0, 255, 0], 5)
edit flag offensive delete link more

Comments

i have already fixed this Error but the Code doesn't work too and gives me another Error (AttributeError: 'NoneType' object has no attribute 'shape') ?!

Dedo gravatar imageDedo ( 2019-03-03 06:53:10 -0600 )edit

Change:

mask = np.zeros(thresh.shape, dtype="uint8")  # CREATING MASK

to

mask = np.zeros(img.shape, dtype="uint8")  # CREATING MASK
supra56 gravatar imagesupra56 ( 2019-03-03 08:54:57 -0600 )edit

Your code is messed up. img2 = cv2.bitwise_and(thresh, mask). Rem it out cv2.drawContours(final, contours, 0, (255, 255, 0), 3). And this too cv2.waitKey(0). I will be later.

supra56 gravatar imagesupra56 ( 2019-03-03 09:35:11 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-03-02 17:09:58 -0600

Seen: 584 times

Last updated: Mar 03 '19