-2

i've tried to detect faces from my webcam , but it was only detect the first face which appeared in the first frame , then it will crush , i have used mtcnn to detection operation , after detecting the first face (if it exist ) then it will stop capturing and crushed and will remain on the screen, i've used jupyter notebook as editor and the notebook will remain loading .. but it will work fine without using mtcnn detector ! but when there is no faces in the first frame it will raise this error whenever i use if boxes: before for box in boxes:

IndexError: list index out of range

but if there was a face it will raise this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

but i wont use if boxes: if there was not a face to detect it will raise this error :

TypeError: 'NoneType' object is not iterable

and if detect a face in the first frame then it will remain loading after detecting the face!

capture = cv2.VideoCapture(0)  
while(True):      
    ret, frame = capture.read()
    frames_tracked = []
    print('\rTracking frame: {}'.format(i + 1), end='')
    frame_pil = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
    frame_pil = Image.fromarray(frame_pil)
    boxes,_ = mtcnn.detect(frame_pil)
    frame_draw = frame_pil.copy()
    draw = ImageDraw.Draw(frame_draw)
    for box in boxes:
        draw.rectangle(box.tolist(), outline=(255, 0, 0), width=6)          
        frames_tracked.append(frame_draw.resize((640, 360), Image.BILINEAR))      

    d = display.display(frames_tracked[0], display_id=True)
    i = 1     
    try:
        while True:
            d.update(frames_tracked[i % len(frames_tracked)]) 
            i += 1     
    except KeyboardInterrupt:
        pass

    if cv2.waitKey('q') == 27:
        break
capture.release() 
cv2.destroyAllWindows()

please i have to complete my project for my final year project

artiest
  • 23
  • 1
  • 1
  • 9
  • 5
    "this is for the second time i post this issue"... I would say this is not a good starting point. – Berriel Jan 29 '20 at 15:16
  • thank you , i updated , but please if you know a way to solve let me know @Berriel – artiest Jan 29 '20 at 15:20
  • If someone wants to know how to do this it was answered on: https://forums.fast.ai/t/face-detection-in-real-time-with-mtcnn-nonetype-object-has-no-attribute-size/62363/19?u=hadus – Hadus Feb 01 '20 at 21:04

1 Answers1

1

I can't reproduce it in my PC, but maybe an exception will make it:

import cv2  
i = 0
capture = cv2.VideoCapture(0)  
while(True):      
    ret, frame = capture.read()
    frames_tracked = []
    print('\rTracking frame: {}'.format(i + 1), end='')
    frame_pil = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
    frame_pil = Image.fromarray(frame_pil)
    boxes = []
    boxes,_ = mtcnn.detect(frame_pil)
    frame_draw = frame_pil.copy()
    draw = ImageDraw.Draw(frame_draw)

    try:
        for box in boxes:
            draw.rectangle(box.tolist(), outline=(255, 0, 0), width=6)          
            frames_tracked.append(frame_draw.resize((640, 360), Image.BILINEAR)) 
    except ValueError:
        print("Oops! ...")     

    d = display.display(frames_tracked[0], display_id=True)
    i = 1     
    try:
        while True:
            d.update(frames_tracked[i % len(frames_tracked)]) 
            i += 1     
    except KeyboardInterrupt:
        pass

    if cv2.waitKey('q') == 27:
        break
capture.release() 
cv2.destroyAllWindows()

If this doesn't work. Maybe just check if boxes is None?:

if boxes is not None:
    for box in boxes:    
        print("seems to work")
Ivan
  • 1,352
  • 2
  • 13
  • 31
  • no still after detecting the first face in the frame it will remain loading , and whenever there is no face ( 'NoneType' object is not iterable) – artiest Jan 29 '20 at 16:51
  • 1
    You could try `if box is None` meanwhile. I'll try getting mtcnn so that i can fully test it in my PC – Ivan Jan 29 '20 at 16:54
  • i tried it as well,as before after detecting the first face it will remain loading , you can get mtcnn from (pip install facenet_pytorch) > from facenet_pytorch import MTCNN ) i will appreciate you – artiest Jan 29 '20 at 16:59
  • @Ivan in case you were also interested in a solution: https://forums.fast.ai/t/face-detection-in-real-time-with-mtcnn-nonetype-object-has-no-attribute-size/62363/19?u=hadus – Hadus Feb 01 '20 at 21:05