Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Help run caffemodel

Hello!

I launch Caffe Models from http://vision.soic.indiana.edu/projec...

I get an error:

OpenCV (3.4.2) C: \ projects \ opencv-python \ opencv \ modules \ dnn \ src \ dnn.cpp: 431: error: (-215: Assertion failed) inputs.size () == requiredOutputs in function 'cv :: dnn :: experimental_dnn_v5 :: DataLayer :: getMemoryShapes'

when I run this code in jupiter notebook

%run C:\Users\DNS\Desktop\hand_detector.py
import numpy as np
from PIL import Image
proto_path = r'C:\Users\DNS\Desktop\hand_classifier.prototxt'
caffe_path = r'C:\Users\DNS\Desktop\hand_classifier.caffemodel'

Detector = HandDetector(proto_path, caffe_path)

img = np.array(Image.open(r'C:\Users\DNS\Desktop\_LABELLED_SAMPLES\CARDS_COURTYARD_B_T\frame_0011.jpg'))

boxes = Detector(img)

On the Internet, it is advised to change the first word in prototxt, but I do not know how, the first layer looks like this:

name: "HandsCaffeNet"
layers {
  name: "data"
  type: WINDOW_DATA
  top: "data"
  top: "label"
  window_data_param {
    source: "text_file_accroding_to_window_data_layer_formatting.txt"
    batch_size: 256
    fg_threshold: 0.5
    bg_threshold: 0.5
    context_pad: 16
    crop_mode: "warp"
  }
  transform_param {
    crop_size: 227
    mean_file: "/path_to_caffe/data/ilsvrc12/imagenet_mean.binaryproto"
    mirror: false
  }
  include: { phase: TEST }
}

hand_detector.py:

import cv2
import numpy as np
from collections import namedtuple

# Type for face bounding box
Box = namedtuple('Box', 'x1 y1 x2 y2')


def adjust_gamma(image, gamma=1.5):
    """
    Adjusting image gamma
    Args:
        image: image in np.uin8 format
        gamma: gamma parameter
    Returns:
        image with adjusted gamma
    """
    invGamma = 1.0 / gamma
    table = np.fromfunction(
        lambda i: ((i / 255.0) ** invGamma) * 255, (256,),
        dtype=np.uint8
    ).astype(np.uint8)
    return cv2.LUT(image, table)


class HandDetector:
    def __init__(
            self,
            prototxt_path: str,
            caffe_path: str,
            conf: float = -1):
        """
        FaceDetector class constructor
        Args:
            prototxt_path: path to prototxt file of Caffe model
            caffe_path: path to caffe file of Caffe model
        """
        self.net = cv2.dnn.readNetFromCaffe(prototxt_path, caffe_path)
        self.net.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL)
        self.confidence = conf

        self.i = 0

        self.transforms = [
            lambda x: adjust_gamma(x),
            lambda x: adjust_gamma(cv2.convertScaleAbs(x, alpha=1.8, beta=0))
        ]

    def get_faces(self, _image: np.ndarray, conf: float = 0.5):
        """
        Get faces bounding boxes list
        Args:
            _image: rgb image in np.uint8 format
            conf: confidence threshold
        Returns:
            List of faces bounding boxes in Box format
        """
        image = _image.copy()
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        (h, w) = image.shape[:2]

        blob = cv2.dnn.blobFromImage(
            image,
            1.0,
            (300, 300), (104.0, 177.0, 123.0)
        )

        self.net.setInput(blob)
        detections = self.net.forward()
        finded = False

        faces = []

        for b in range(detections.shape[1]):
            for i in range(0, detections.shape[2]):

                confidence = detections[0, 0, i, 2]

                if confidence > (
                        conf if self.confidence < 0 else self.confidence):
                    box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                    (startX, startY, endX, endY) = box.astype(np.int32)

                    # _w = endX - startX
                    # _h = endY - startY
                    #
                    # startX -= _w // 15
                    # endX += _w // 15
                    # startY -= _h // 15
                    # endY += _h // 15

                    startX, startY, endX, endY = [
                        0 if x < 0 else x
                        for x in (startX, startY, endX, endY)
                    ]
                    startX, endX = [
                        x if x < image.shape[1] else image.shape[1] - 1
                        for x in (startX, endX)
                    ]
                    startY, endY = [
                        y if y < image.shape[0] else image.shape[0] - 1
                        for y in (startY, endY)
                    ]

                    if (endX - startX) * (endY - startY) <= 0:
                        continue
                    faces.append({'face': None,
                                  'rect': [startX, startY, endX, endY]})
                    finded = True

        if not finded:
            if self.i < len(self.transforms):
                self.i += 1
                return self.get_faces(self.transforms[self.i - 1](image))
            else:
                return []

        faces.sort(
            key=lambda x:
                (x['rect'][0] - x['rect'][2]) ** 2 +
                (x['rect'][1] - x['rect'][3]) ** 2,
            reverse=True
        )

        self.i = 0
        return faces

    def __call__(self, image: np.ndarray, conf: float = 0.5):
        """
        Get faces bounding boxes list in XYXY format
        Args:
            image: gb image in np.uint8 format
        Returns:
            List of hands bounding boxes list in XYXY format
        """
        return [
            face_box['rect']
            for face_box in self.get_faces(image, conf)
        ]

In other topics in prototxt the height and width are written, for me I do not see this. I will be glad to help anyone. Thanks in advance!

Help run caffemodel

Hello!

I launch Caffe Models from http://vision.soic.indiana.edu/projec...

EgoHandsDataset I get an error:

OpenCV (3.4.2) C: \ projects \ opencv-python \ opencv \ modules \ dnn \ src \ dnn.cpp: 431: error: (-215: Assertion failed) inputs.size () == requiredOutputs in function 'cv :: dnn :: experimental_dnn_v5 :: DataLayer :: getMemoryShapes'

when I run this code in jupiter notebook

%run C:\Users\DNS\Desktop\hand_detector.py
import numpy as np
from PIL import Image
proto_path = r'C:\Users\DNS\Desktop\hand_classifier.prototxt'
caffe_path = r'C:\Users\DNS\Desktop\hand_classifier.caffemodel'

Detector = HandDetector(proto_path, caffe_path)

img = np.array(Image.open(r'C:\Users\DNS\Desktop\_LABELLED_SAMPLES\CARDS_COURTYARD_B_T\frame_0011.jpg'))

boxes = Detector(img)

On the Internet, it is advised to change the first word in prototxt, but I do not know how, the first layer looks like this:

name: "HandsCaffeNet"
layers {
  name: "data"
  type: WINDOW_DATA
  top: "data"
  top: "label"
  window_data_param {
    source: "text_file_accroding_to_window_data_layer_formatting.txt"
    batch_size: 256
    fg_threshold: 0.5
    bg_threshold: 0.5
    context_pad: 16
    crop_mode: "warp"
  }
  transform_param {
    crop_size: 227
    mean_file: "/path_to_caffe/data/ilsvrc12/imagenet_mean.binaryproto"
    mirror: false
  }
  include: { phase: TEST }
}

hand_detector.py:

import cv2
import numpy as np
from collections import namedtuple

# Type for face bounding box
Box = namedtuple('Box', 'x1 y1 x2 y2')


def adjust_gamma(image, gamma=1.5):
    """
    Adjusting image gamma
    Args:
        image: image in np.uin8 format
        gamma: gamma parameter
    Returns:
        image with adjusted gamma
    """
    invGamma = 1.0 / gamma
    table = np.fromfunction(
        lambda i: ((i / 255.0) ** invGamma) * 255, (256,),
        dtype=np.uint8
    ).astype(np.uint8)
    return cv2.LUT(image, table)


class HandDetector:
    def __init__(
            self,
            prototxt_path: str,
            caffe_path: str,
            conf: float = -1):
        """
        FaceDetector class constructor
        Args:
            prototxt_path: path to prototxt file of Caffe model
            caffe_path: path to caffe file of Caffe model
        """
        self.net = cv2.dnn.readNetFromCaffe(prototxt_path, caffe_path)
        self.net.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL)
        self.confidence = conf

        self.i = 0

        self.transforms = [
            lambda x: adjust_gamma(x),
            lambda x: adjust_gamma(cv2.convertScaleAbs(x, alpha=1.8, beta=0))
        ]

    def get_faces(self, _image: np.ndarray, conf: float = 0.5):
        """
        Get faces bounding boxes list
        Args:
            _image: rgb image in np.uint8 format
            conf: confidence threshold
        Returns:
            List of faces bounding boxes in Box format
        """
        image = _image.copy()
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        (h, w) = image.shape[:2]

        blob = cv2.dnn.blobFromImage(
            image,
            1.0,
            (300, 300), (104.0, 177.0, 123.0)
        )

        self.net.setInput(blob)
        detections = self.net.forward()
        finded = False

        faces = []

        for b in range(detections.shape[1]):
            for i in range(0, detections.shape[2]):

                confidence = detections[0, 0, i, 2]

                if confidence > (
                        conf if self.confidence < 0 else self.confidence):
                    box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                    (startX, startY, endX, endY) = box.astype(np.int32)

                    # _w = endX - startX
                    # _h = endY - startY
                    #
                    # startX -= _w // 15
                    # endX += _w // 15
                    # startY -= _h // 15
                    # endY += _h // 15

                    startX, startY, endX, endY = [
                        0 if x < 0 else x
                        for x in (startX, startY, endX, endY)
                    ]
                    startX, endX = [
                        x if x < image.shape[1] else image.shape[1] - 1
                        for x in (startX, endX)
                    ]
                    startY, endY = [
                        y if y < image.shape[0] else image.shape[0] - 1
                        for y in (startY, endY)
                    ]

                    if (endX - startX) * (endY - startY) <= 0:
                        continue
                    faces.append({'face': None,
                                  'rect': [startX, startY, endX, endY]})
                    finded = True

        if not finded:
            if self.i < len(self.transforms):
                self.i += 1
                return self.get_faces(self.transforms[self.i - 1](image))
            else:
                return []

        faces.sort(
            key=lambda x:
                (x['rect'][0] - x['rect'][2]) ** 2 +
                (x['rect'][1] - x['rect'][3]) ** 2,
            reverse=True
        )

        self.i = 0
        return faces

    def __call__(self, image: np.ndarray, conf: float = 0.5):
        """
        Get faces bounding boxes list in XYXY format
        Args:
            image: gb image in np.uint8 format
        Returns:
            List of hands bounding boxes list in XYXY format
        """
        return [
            face_box['rect']
            for face_box in self.get_faces(image, conf)
        ]

In other topics in prototxt the height and width are written, for me I do not see this. I will be glad to help anyone. Thanks in advance!

Help run caffemodel

Hello!

I launch Caffe Models from EgoHandsDataset I get an error:

OpenCV (3.4.2) C: \ projects \ opencv-python \ opencv \ modules \ dnn \ src \ dnn.cpp: 431: error: (-215: Assertion failed) inputs.size () == requiredOutputs in function 'cv :: dnn :: experimental_dnn_v5 :: DataLayer :: getMemoryShapes'

when I run this code in jupiter notebook

%run C:\Users\DNS\Desktop\hand_detector.py
import numpy as np
from PIL import Image
proto_path = r'C:\Users\DNS\Desktop\hand_classifier.prototxt'
caffe_path = r'C:\Users\DNS\Desktop\hand_classifier.caffemodel'

Detector = HandDetector(proto_path, caffe_path)

img = np.array(Image.open(r'C:\Users\DNS\Desktop\_LABELLED_SAMPLES\CARDS_COURTYARD_B_T\frame_0011.jpg'))

boxes = Detector(img)

On the Internet, it is advised to change the first word in prototxt, but I do not know how, the first layer looks like this:

name: "HandsCaffeNet"
layers {
  name: "data"
  type: WINDOW_DATA
  top: "data"
  top: "label"
  window_data_param {
    source: "text_file_accroding_to_window_data_layer_formatting.txt"
    batch_size: 256
    fg_threshold: 0.5
    bg_threshold: 0.5
    context_pad: 16
    crop_mode: "warp"
  }
  transform_param {
    crop_size: 227
    mean_file: "/path_to_caffe/data/ilsvrc12/imagenet_mean.binaryproto"
    mirror: false
  }
  include: { phase: TEST }
}

hand_detector.py:

import cv2
import numpy as np
from collections import namedtuple

# Type for face bounding box
Box = namedtuple('Box', 'x1 y1 x2 y2')


def adjust_gamma(image, gamma=1.5):
    """
    Adjusting image gamma
    Args:
        image: image in np.uin8 format
        gamma: gamma parameter
    Returns:
        image with adjusted gamma
    """
    invGamma = 1.0 / gamma
    table = np.fromfunction(
        lambda i: ((i / 255.0) ** invGamma) * 255, (256,),
        dtype=np.uint8
    ).astype(np.uint8)
    return cv2.LUT(image, table)


class HandDetector:
    def __init__(
            self,
            prototxt_path: str,
            caffe_path: str,
            conf: float = -1):
        """
        FaceDetector class constructor
        Args:
            prototxt_path: path to prototxt file of Caffe model
            caffe_path: path to caffe file of Caffe model
        """
        self.net = cv2.dnn.readNetFromCaffe(prototxt_path, caffe_path)
        self.net.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL)
        self.confidence = conf

        self.i = 0

        self.transforms = [
            lambda x: adjust_gamma(x),
            lambda x: adjust_gamma(cv2.convertScaleAbs(x, alpha=1.8, beta=0))
        ]

    def get_faces(self, _image: np.ndarray, conf: float = 0.5):
        """
        Get faces bounding boxes list
        Args:
            _image: rgb image in np.uint8 format
            conf: confidence threshold
        Returns:
            List of faces bounding boxes in Box format
        """
        image = _image.copy()
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        (h, w) = image.shape[:2]

        blob = cv2.dnn.blobFromImage(
            image,
            1.0,
            (300, 300), (104.0, 177.0, 123.0)
        )

        self.net.setInput(blob)
        detections = self.net.forward()
        finded = False

        faces = []

        for b in range(detections.shape[1]):
            for i in range(0, detections.shape[2]):

                confidence = detections[0, 0, i, 2]

                if confidence > (
                        conf if self.confidence < 0 else self.confidence):
                    box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                    (startX, startY, endX, endY) = box.astype(np.int32)

                    # _w = endX - startX
                    # _h = endY - startY
                    #
                    # startX -= _w // 15
                    # endX += _w // 15
                    # startY -= _h // 15
                    # endY += _h // 15

                    startX, startY, endX, endY = [
                        0 if x < 0 else x
                        for x in (startX, startY, endX, endY)
                    ]
                    startX, endX = [
                        x if x < image.shape[1] else image.shape[1] - 1
                        for x in (startX, endX)
                    ]
                    startY, endY = [
                        y if y < image.shape[0] else image.shape[0] - 1
                        for y in (startY, endY)
                    ]

                    if (endX - startX) * (endY - startY) <= 0:
                        continue
                    faces.append({'face': None,
                                  'rect': [startX, startY, endX, endY]})
                    finded = True

        if not finded:
            if self.i < len(self.transforms):
                self.i += 1
                return self.get_faces(self.transforms[self.i - 1](image))
            else:
                return []

        faces.sort(
            key=lambda x:
                (x['rect'][0] - x['rect'][2]) ** 2 +
                (x['rect'][1] - x['rect'][3]) ** 2,
            reverse=True
        )

        self.i = 0
        return faces

    def __call__(self, image: np.ndarray, conf: float = 0.5):
        """
        Get faces bounding boxes list in XYXY format
        Args:
            image: gb image in np.uint8 format
        Returns:
            List of hands bounding boxes list in XYXY format
        """
        return [
            face_box['rect']
            for face_box in self.get_faces(image, conf)
        ]

In other topics in prototxt the height and width are written, for me I do not see this. I will be glad to help anyone. Thanks in advance!

Help run caffemodel

Hello!

I launch Caffe Models from EgoHandsDataset . I get an error:

OpenCV (3.4.2) C: \ projects \ opencv-python \ opencv \ modules \ dnn \ src \ dnn.cpp: 431: error: (-215: Assertion failed) inputs.size () == requiredOutputs in function 'cv :: dnn :: experimental_dnn_v5 :: DataLayer :: getMemoryShapes'

when I run this code in jupiter notebook

%run C:\Users\DNS\Desktop\hand_detector.py
import numpy as np
from PIL import Image
proto_path = r'C:\Users\DNS\Desktop\hand_classifier.prototxt'
caffe_path = r'C:\Users\DNS\Desktop\hand_classifier.caffemodel'

Detector = HandDetector(proto_path, caffe_path)

img = np.array(Image.open(r'C:\Users\DNS\Desktop\_LABELLED_SAMPLES\CARDS_COURTYARD_B_T\frame_0011.jpg'))

boxes = Detector(img)

On the Internet, it is advised to change the first word in prototxt, but I do not know how, the first layer looks like this:

name: "HandsCaffeNet"
layers {
  name: "data"
  type: WINDOW_DATA
  top: "data"
  top: "label"
  window_data_param {
    source: "text_file_accroding_to_window_data_layer_formatting.txt"
    batch_size: 256
    fg_threshold: 0.5
    bg_threshold: 0.5
    context_pad: 16
    crop_mode: "warp"
  }
  transform_param {
    crop_size: 227
    mean_file: "/path_to_caffe/data/ilsvrc12/imagenet_mean.binaryproto"
    mirror: false
  }
  include: { phase: TEST }
}

hand_detector.py:

import cv2
import numpy as np
from collections import namedtuple

# Type for face bounding box
Box = namedtuple('Box', 'x1 y1 x2 y2')


def adjust_gamma(image, gamma=1.5):
    """
    Adjusting image gamma
    Args:
        image: image in np.uin8 format
        gamma: gamma parameter
    Returns:
        image with adjusted gamma
    """
    invGamma = 1.0 / gamma
    table = np.fromfunction(
        lambda i: ((i / 255.0) ** invGamma) * 255, (256,),
        dtype=np.uint8
    ).astype(np.uint8)
    return cv2.LUT(image, table)


class HandDetector:
    def __init__(
            self,
            prototxt_path: str,
            caffe_path: str,
            conf: float = -1):
        """
        FaceDetector class constructor
        Args:
            prototxt_path: path to prototxt file of Caffe model
            caffe_path: path to caffe file of Caffe model
        """
        self.net = cv2.dnn.readNetFromCaffe(prototxt_path, caffe_path)
        self.net.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL)
        self.confidence = conf

        self.i = 0

        self.transforms = [
            lambda x: adjust_gamma(x),
            lambda x: adjust_gamma(cv2.convertScaleAbs(x, alpha=1.8, beta=0))
        ]

    def get_faces(self, _image: np.ndarray, conf: float = 0.5):
        """
        Get faces bounding boxes list
        Args:
            _image: rgb image in np.uint8 format
            conf: confidence threshold
        Returns:
            List of faces bounding boxes in Box format
        """
        image = _image.copy()
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        (h, w) = image.shape[:2]

        blob = cv2.dnn.blobFromImage(
            image,
            1.0,
            (300, 300), (104.0, 177.0, 123.0)
        )

        self.net.setInput(blob)
        detections = self.net.forward()
        finded = False

        faces = []

        for b in range(detections.shape[1]):
            for i in range(0, detections.shape[2]):

                confidence = detections[0, 0, i, 2]

                if confidence > (
                        conf if self.confidence < 0 else self.confidence):
                    box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                    (startX, startY, endX, endY) = box.astype(np.int32)

                    # _w = endX - startX
                    # _h = endY - startY
                    #
                    # startX -= _w // 15
                    # endX += _w // 15
                    # startY -= _h // 15
                    # endY += _h // 15

                    startX, startY, endX, endY = [
                        0 if x < 0 else x
                        for x in (startX, startY, endX, endY)
                    ]
                    startX, endX = [
                        x if x < image.shape[1] else image.shape[1] - 1
                        for x in (startX, endX)
                    ]
                    startY, endY = [
                        y if y < image.shape[0] else image.shape[0] - 1
                        for y in (startY, endY)
                    ]

                    if (endX - startX) * (endY - startY) <= 0:
                        continue
                    faces.append({'face': None,
                                  'rect': [startX, startY, endX, endY]})
                    finded = True

        if not finded:
            if self.i < len(self.transforms):
                self.i += 1
                return self.get_faces(self.transforms[self.i - 1](image))
            else:
                return []

        faces.sort(
            key=lambda x:
                (x['rect'][0] - x['rect'][2]) ** 2 +
                (x['rect'][1] - x['rect'][3]) ** 2,
            reverse=True
        )

        self.i = 0
        return faces

    def __call__(self, image: np.ndarray, conf: float = 0.5):
        """
        Get faces bounding boxes list in XYXY format
        Args:
            image: gb image in np.uint8 format
        Returns:
            List of hands bounding boxes list in XYXY format
        """
        return [
            face_box['rect']
            for face_box in self.get_faces(image, conf)
        ]

In other topics in prototxt the height and width are written, for me I do not see this. I will be glad to help anyone. Thanks in advance!