Face detection in real time with MTCNN ? 'NoneType' object has no attribute 'size'

i’m trying to build face recognition in real time in neural network(facenet network) using pytorch and face detection using MTCNN i’ve tried this but doesnt work

import cv2

capture = cv2.VideoCapture(0)

while(True):

    ret, frame = capture.read()
    frames_tracked = []

    print('\rTracking frame: {}'.format(i + 1), end='')
    boxes,_ = mtcnn.detect(frame)
    frame_draw = frame.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()

AttributeError: ‘NoneType’ object has no attribute ‘size’ thanks for any advice

The full error message would be more useful. We would know what part of the code caused the error.

Can you post the full error not just the last line of it so we can tell what line the error is coming from.
For example (it is usually longer):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in a
ValueError

if it detect a face in the first frame it will raise this error

ValueError Traceback (most recent call last)
in
13 frame_draw = frame_pil.copy()
14 draw = ImageDraw.Draw(frame_draw)
—> 15 if boxes:
16 for box in boxes:
17 draw.rectangle(box.tolist(), outline=(255, 0, 0), width=6)

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
if there wasnt a face in the first frame it will raise this error

IndexError Traceback (most recent call last)
in
18 frames_tracked.append(frame_draw.resize((640, 360), Image.BILINEAR))
19 clear_output(wait=True)
—> 20 d = display.display(frames_tracked[0], display_id=True)
21 i = 1
22 try:

IndexError: list index out of range

import cv2

capture = cv2.VideoCapture(0)

while not cv2.waitKey('q') == 27:

    ret, frame = capture.read()
    frames_tracked = []

    print('\rTracking frame: {}'.format(i + 1), end='')
    boxes,_ = mtcnn.detect(frame)
    frame_draw = frame.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))

    if frames_tracked:
        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

 capture.release()
 cv2.destroyAllWindows()

Does this behave differently?

TypeError Traceback (most recent call last)
in
3 capture = cv2.VideoCapture(0)
4
----> 5 while not cv2.waitKey(‘q’) == 27:
6
7 ret, frame = capture.read()

TypeError: an integer is required (got type str)
no , does it work for you

I don’t have mtcnn so I cannot test it but that error can be fixed simply:
while not cv2.waitKey(33) == ord('q'):

I will install it…

I can run it now and I am getting some errors.

1 Like

thank you for your helping

1 Like

I can’t even get an image to save yet. Did you manage to write something that can capture an image and just show it?

install mmcv

video = mmcv.VideoReader('video.mp4')

frames = [Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in video]

display.Video(‘video.mp4’, width=640)

frames_tracked = []
for i, frame in enumerate(frames):
print(’\rTracking frame: {}’.format(i + 1), end=’’)

# Detect faces
boxes, _ = mtcnn.detect(frame)

# Draw faces
frame_draw = frame.copy()
draw = ImageDraw.Draw(frame_draw)
for box in boxes:
    draw.rectangle(box.tolist(), outline=(255, 0, 0), width=6)

# Add to frame list
frames_tracked.append(frame_draw.resize((640, 360), Image.BILINEAR))

print(’\nDone’)

ValueError Traceback (most recent call last)
in
14
15 img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
—> 16 boxes,_ = detector.detect_faces(img)
17 frame_draw = frame.copy()
18 draw = ImageDraw.Draw(frame_draw)

ValueError: not enough values to unpack (expected 2, got 0)

I am getting the same error. I think we just have to do:
boxes = detector.detect_faces(img)

empty list

Finally here we go!!! :grinning:

import cv2
from mtcnn import MTCNN
from PIL import Image, ImageDraw
import os
import numpy as np
import time

os.environ['KMP_DUPLICATE_LIB_OK']='True'

detector = MTCNN()
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()

    if ret:
        boxes = detector.detect_faces(frame)
        frame_draw = frame.copy()
        img = Image.fromarray(frame_draw)
        draw = ImageDraw.Draw(img)

        for box in boxes:
            draw.rectangle(box["box"], outline=(255, 0, 0), width=6)

        opencvImage = cv2.cvtColor(np.array(img)[..., ::-1], cv2.COLOR_RGB2BGR)
        opencvImage = cv2.resize(opencvImage, (720, 480))
        cv2.imshow('output', opencvImage)

        if cv2.waitKey(1) &0xFF == ord('q'):
            break

    else:
        break

cap.release()
cv2.destroyAllWindows()
1 Like

thank you so much
much appreciate

1 Like