Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Problem with using Convex hull in Face Detection.

import cv2
import numpy as np

# LOADING HAND CASCADE
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)
  final = cv2.GaussianBlur(img2, (7, 7), 0)
  contours, hierarchy = cv2.findContours(final, 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]
   finger1 = tuple(cnt[p][0])
   finger2 = 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 (IndexError: list index out of range).so could any one help me to fix this Problem.

Problem with using Convex hull in Face Detection.

import cv2 import numpy as np # LOADING HAND CASCADE 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)
  final = cv2.GaussianBlur(img2, (7, 7), 0)
  contours, hierarchy = cv2.findContours(final, 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]
   finger1 start = tuple(cnt[p][0])
   finger2 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 (IndexError: list index out of range).so (AttributeError: 'NoneType' object has no attribute 'shape').so could any one help me to fix this Problem.