面部表情识别中的opencv错误

时间:2017-07-22 08:12:50

标签: python-3.x opencv

我在ubuntu 16.04.02 LTS上安装了带有python 3.5的opencv-3.2.0。我想运行以下脚本:

import cv2
import numpy as np
from keras.models import load_model

# laptop camera
rgb = cv2.VideoCapture(1)

# droidcam android
# rgb = cv2.VideoCapture('http://ipaddress:port/mjpegfeed?640x480')


# pre - trinaed xml file for detecting faces
facec = cv2.CascadeClassifier('/home/shubhamrathore/haarcascade_frontalface_default.xml')
font = cv2.FONT_HERSHEY_SIMPLEX


# loading saved cnn model
model = load_model('/home/shubhamrathore/Downloads/face_reco.h5')


# predicting face emotion using saved model
def get_emo(im):
    im = im[np.newaxis, np.newaxis, :, :]
    res = model.predict_classes(im,verbose=0)
    emo = {0: 'Angry', 1: 'Disgust', 2: 'Fear', 3: 'Happy', 4: 'Sad', 5: 'Surprise', 6: 'Neutral'}
    return emo[res[0]]


# reshaping face image
def recognize_face(im):
    im = cv2.resize(im, (48, 48))
    return get_emo(im)


while True:
    _, fr = rgb.read()
    flip_fr = cv2.flip(fr,1)
    gray = cv2.cvtColor(fr, cv2.COLOR_BGR2GRAY)
    faces = facec.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        fc = fr[y:y+h, x:x+w, :]
        gfc = cv2.cvtColor(fc, cv2.COLOR_BGR2GRAY)
        out = recognize_face(gfc)
        cv2.rectangle(fr,(x,y),(x+w,y+h),(255,0,0),2)
        flip_fr = cv2.flip(fr,1)
        cv2.putText(flip_fr, out, (30, 30), font, 1, (255, 255, 0), 2)

    cv2.imshow('rgb', flip_fr)

    # press esc to close the window
    k = cv2.waitKey(1) & 0xEFFFFF
    if k==27:   
        break
    elif k==-1:
        continue
    else:
        # print k
        continue

cv2.destroyAllWindows()

我得到的输出是:

shubhamrathore@Rathore-Inspiron-3543:~$ python class13cam.py
    Using TensorFlow backend.
    VIDEOIO ERROR: V4L: index 1 is not correct!
    /home/shubhamrathore/anaconda3/lib/python3.6/site-
    packages/keras/engine/topology.py:1242: UserWarning: Update your 
    `Conv2D` call to the Keras 2 API: `Conv2D(name="convolution2d_7", 
    activity_regularizer=None, trainable=True, input_dtype="float32", 
    batch_input_shape=[None, 1, ..., activation="relu", kernel_size=(3, 
    3), filters=64, strides=[1, 1], padding="valid", 
    data_format="channels_first", kernel_initializer="glorot_uniform", 
    kernel_regularizer=None, bias_regularizer=None, 
    kernel_constraint=None, bias_constraint=None, use_bias=True)`
     return cls(**config)
    /home/shubhamrathore/anaconda3/lib/python3.6/site-
    packages/keras/engine/topology.py:1242: UserWarning: Update your 
    `Conv2D` call to the Keras 2 API: `Conv2D(name="convolution2d_8", 
    activity_regularizer=None, trainable=True, activation="relu", 
    kernel_size=(3, 3), filters=64, strides=[1, 1], padding="valid", 
    data_format="channels_first", kernel_initializer="glorot_uniform", 
    kernel_regularizer=None, bias_regularizer=None, 
    kernel_constraint=None, bias_constraint=None, use_bias=True)`
      return cls(**config)
    /home/shubhamrathore/anaconda3/lib/python3.6/site-
    packages/keras/engine/topology.py:1242: UserWarning: Update your 
    `Conv2D` call to the Keras 2 API: `Conv2D(name="convolution2d_9", 
    activity_regularizer=None, trainable=True, activation="relu", 
    kernel_size=(3, 3), filters=64, strides=[1, 1], padding="valid", 
    data_format="channels_first", kernel_initializer="glorot_uniform", 
    kernel_regularizer=None, bias_regularizer=None, 
    kernel_constraint=None, bias_constraint=None, use_bias=True)`
      return cls(**config)
    /home/shubhamrathore/anaconda3/lib/python3.6/site-
    packages/keras/engine/topology.py:1242: UserWarning: Update your 
    `MaxPooling2D` call to the Keras 2 API: 
    `MaxPooling2D(name="maxpooling2d_3", trainable=True, pool_size=[2, 2], 
    strides=[2, 2], padding="valid", data_format="channels_first")`
      return cls(**config)
    /home/shubhamrathore/anaconda3/lib/python3.6/site-
        packages/keras/engine/topology.py:1242: UserWarning: Update your 
        `Conv2D` call to the Keras 2 API: `Conv2D(name="convolution2d_10", 
        activity_regularizer=None, trainable=True, activation="relu", 
        kernel_size=(3, 3), filters=32, strides=[1, 1], padding="valid", 
    data_format="channels_first", kernel_initializer="glorot_uniform", 
    kernel_regularizer=None, bias_regularizer=None, 
    kernel_constraint=None, bias_constraint=None, use_bias=True)`
      return cls(**config)
    /home/shubhamrathore/anaconda3/lib/python3.6/site-
    packages/keras/engine/topology.py:1242: UserWarning: Update your 
    `Conv2D` call to the Keras 2 API: `Conv2D(name="convolution2d_11", 
    activity_regularizer=None, trainable=True, activation="relu", 
    kernel_size=(3, 3), filters=32, strides=[1, 1], padding="valid", 
   data_format="channels_first", kernel_initializer="glorot_uniform", kernel_regularizer=None, bias_regularizer=None, kernel_constraint=None, bias_constraint=None, use_bias=True)`
  return cls(**config)
/home/shubhamrathore/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py:1242: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(name="convolution2d_12", activity_regularizer=None, trainable=True, activation="relu", kernel_size=(3, 3), filters=32, strides=[1, 1], padding="valid", data_format="channels_first", kernel_initializer="glorot_uniform", kernel_regularizer=None, bias_regularizer=None, kernel_constraint=None, bias_constraint=None, use_bias=True)`
  return cls(**config)
/home/shubhamrathore/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py:1242: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(name="maxpooling2d_4", trainable=True, pool_size=[2, 2], strides=[2, 2], padding="valid", data_format="channels_first")`
  return cls(**config)
/home/shubhamrathore/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py:1242: UserWarning: Update your `Dropout` call to the Keras 2 API: `Dropout(trainable=True, name="dropout_2", rate=0.5)`
  return cls(**config)
/home/shubhamrathore/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py:1242: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(name="dense_1", activity_regularizer=None, trainable=True, input_dim=None, activation="relu", units=128, kernel_initializer="glorot_uniform", kernel_regularizer=None, bias_regularizer=None, kernel_constraint=None, bias_constraint=None, use_bias=True)`
  return cls(**config)
/home/shubhamrathore/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py:1242: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(name="dense_2", activity_regularizer=None, trainable=True, input_dim=None, activation="relu", units=64, kernel_initializer="glorot_uniform", kernel_regularizer=None, bias_regularizer=None, kernel_constraint=None, bias_constraint=None, use_bias=True)`
  return cls(**config)
/home/shubhamrathore/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py:1242: UserWarning: Update your `Dropout` call to the Keras 2 API: `Dropout(trainable=True, name="dropout_3", rate=0.5)`
  return cls(**config)
/home/shubhamrathore/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py:1242: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(name="dense_3", activity_regularizer=None, trainable=True, input_dim=None, activation="linear", units=7, kernel_initializer="glorot_uniform", kernel_regularizer=None, bias_regularizer=None, kernel_constraint=None, bias_constraint=None, use_bias=True)`
  return cls(**config)
2017-07-22 13:14:39.568141: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 13:14:39.568170: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 13:14:39.568181: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 13:14:39.568201: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 13:14:39.568211: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in ipp_cvtColor, file /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/imgproc/src/color.cpp, line 7456
Traceback (most recent call last):
  File "class13cam.py", line 38, in <module>
    gray = cv2.cvtColor(fr, cv2.COLOR_BGR2GRAY)
cv2.error: /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/imgproc/src/color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor

此外,当我在终端上运行'python'并'import cv2'时,不会引发错误:

shubhamrathore@Rathore-Inspiron-3543:~$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0-dev'

我从源代码构建并安装它。这是什么错误?

1 个答案:

答案 0 :(得分:0)

我认为python的版本不同。错误消息&#39; /home/shubhamrathore/anaconda3/lib/python3.6/site-     packages / keras / engine / topology.py:1242:..&#39; show version是3.6,因为python路径不是常规的,我认为python3.6是由你自己构建的并且由脚本使用。但是你在shell上发出的命令是&#39; python3&#39;它显示的版本是Python 3.5.2。

相关问题