Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 摄像头坏了?_Python_Camera_Pygame - Fatal编程技术网

Python 摄像头坏了?

Python 摄像头坏了?,python,camera,pygame,Python,Camera,Pygame,此脚本正在使用face.com API分析面部反应。由于某种原因,pygame找不到我的相机(我想这就是问题所在)。我的代码有什么问题吗 我已经安装了pygame,看起来工作得很好。但是下面的代码给了我错误 #################################################### # IMPORTS #################################################### # imports for capturing a fr

此脚本正在使用face.com API分析面部反应。由于某种原因,pygame找不到我的相机(我想这就是问题所在)。我的代码有什么问题吗

我已经安装了pygame,看起来工作得很好。但是下面的代码给了我错误

####################################################
#  IMPORTS
####################################################

# imports for capturing a frame from the webcam
import pygame
import pygame.camera
import pygame.image

# import for detecting faces in the photo
import face_client

# import for storing data
from pysqlite2 import dbapi2 as sqlite

# miscellaneous imports
from time import strftime, localtime, sleep
import os
import sys

####################################################
# CONSTANTS
####################################################

DB_FILE_PATH="log.db"
FACE_COM_APIKEY="API CODE"
FACE_COM_APISECRET="API SECRET"
DALELANE_FACETAG="name@name.name"
POLL_FREQUENCY_SECONDS=3

class AudienceMonitor():

    #
    # prepare the database where we store the results
    #
    def initialiseDB(self):
        self.connection = sqlite.connect(DB_FILE_PATH, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
        cursor = self.connection.cursor()

        cursor.execute('SELECT name FROM sqlite_master WHERE type="table" AND NAME="facelog" ORDER BY name')
        if not cursor.fetchone():
            cursor.execute('CREATE TABLE facelog(ts timestamp unique default current_timestamp, isSmiling boolean, smilingConfidence int, mood text, moodConfidence int)')

        cursor.execute('SELECT name FROM sqlite_master WHERE type="table" AND NAME="guestlog" ORDER BY name')
        if not cursor.fetchone():
            cursor.execute('CREATE TABLE guestlog(ts timestamp unique default current_timestamp, isSmiling boolean, smilingConfidence int, mood text, moodConfidence int, agemin int, ageminConfidence int, agemax int, agemaxConfidence int, ageest int, ageestConfidence int, gender text, genderConfidence int)')

        self.connection.commit()

    #
    # initialise the camera
    #
    def prepareCamera(self):
        # prepare the webcam
        pygame.camera.init()
        self.camera = pygame.camera.Camera(pygame.camera.list_cameras()[0], (900, 675))
        self.camera.start()

    #
    # take a single frame and store in the path provided
    #
    def captureFrame(self, filepath):
        # save the picture
        image = self.camera.get_image()
        pygame.image.save(image, filepath)

    #
    # gets a string representing the current time to the nearest second
    #
    def getTimestampString(self):
        return strftime("%Y%m%d%H%M%S", localtime())

    #
    # get attribute from face detection response
    #
    def getFaceDetectionAttributeValue(self, face, attribute):
        value = None
        if attribute in face['attributes']:
            value = face['attributes'][attribute]['value']
        return value

    #
    # get confidence from face detection response
    #
    def getFaceDetectionAttributeConfidence(self, face, attribute):
        confidence = None
        if attribute in face['attributes']:
            confidence = face['attributes'][attribute]['confidence']
        return confidence

    #
    # detects faces in the photo at the specified path, and returns info
    #
    def faceDetection(self, photopath):
        client = face_client.FaceClient(FACE_COM_APIKEY, FACE_COM_APISECRET)
        response = client.faces_recognize(DALELANE_FACETAG, file_name=photopath)
        faces = response['photos'][0]['tags']
        for face in faces:
            userid = ""
            faceuseridinfo = face['uids']
            if len(faceuseridinfo) > 0:
                userid = faceuseridinfo[0]['uid']
            if userid == DALELANE_FACETAG:
                smiling = self.getFaceDetectionAttributeValue(face, "smiling")
                smilingConfidence = self.getFaceDetectionAttributeConfidence(face, "smiling")
                mood = self.getFaceDetectionAttributeValue(face, "mood")
                moodConfidence = self.getFaceDetectionAttributeConfidence(face, "mood")
                self.storeResults(smiling, smilingConfidence, mood, moodConfidence)
            else:
                smiling = self.getFaceDetectionAttributeValue(face, "smiling")
                smilingConfidence = self.getFaceDetectionAttributeConfidence(face, "smiling")
                mood = self.getFaceDetectionAttributeValue(face, "mood")
                moodConfidence = self.getFaceDetectionAttributeConfidence(face, "mood")
                agemin = self.getFaceDetectionAttributeValue(face, "age_min")
                ageminConfidence = self.getFaceDetectionAttributeConfidence(face, "age_min")
                agemax = self.getFaceDetectionAttributeValue(face, "age_max")
                agemaxConfidence = self.getFaceDetectionAttributeConfidence(face, "age_max")
                ageest = self.getFaceDetectionAttributeValue(face, "age_est")
                ageestConfidence = self.getFaceDetectionAttributeConfidence(face, "age_est")
                gender = self.getFaceDetectionAttributeValue(face, "gender")
                genderConfidence = self.getFaceDetectionAttributeConfidence(face, "gender")
                # if the face wasnt recognisable, it might've been me after all, so ignore
                if "tid" in face and face['recognizable'] == True:
                    self.storeGuestResults(smiling, smilingConfidence, mood, moodConfidence, agemin, ageminConfidence, agemax, agemaxConfidence, ageest, ageestConfidence, gender, genderConfidence)
                    print face['tid']

    #
    # stores face results in the DB
    #
    def storeGuestResults(self, smiling, smilingConfidence, mood, moodConfidence, agemin, ageminConfidence, agemax, agemaxConfidence, ageest, ageestConfidence, gender, genderConfidence):
        cursor = self.connection.cursor()
        cursor.execute('INSERT INTO guestlog(isSmiling, smilingConfidence, mood, moodConfidence, agemin, ageminConfidence, agemax, agemaxConfidence, ageest, ageestConfidence, gender, genderConfidence) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
                        (smiling, smilingConfidence, mood, moodConfidence, agemin, ageminConfidence, agemax, agemaxConfidence, ageest, ageestConfidence, gender, genderConfidence))
        self.connection.commit()

    #
    # stores face results in the DB
    #
    def storeResults(self, smiling, smilingConfidence, mood, moodConfidence):
        cursor = self.connection.cursor()
        cursor.execute('INSERT INTO facelog(isSmiling, smilingConfidence, mood, moodConfidence) values(?, ?, ?, ?)',
                        (smiling, smilingConfidence, mood, moodConfidence))
        self.connection.commit()

monitor = AudienceMonitor()
monitor.initialiseDB()
monitor.prepareCamera()
while True:
    photopath = "data/photo" + monitor.getTimestampString() + ".bmp"
    monitor.captureFrame(photopath)
    try:
        faceresults = monitor.faceDetection(photopath)
    except:
        print "Unexpected error:", sys.exc_info()[0]
    os.remove(photopath)
    sleep(POLL_FREQUENCY_SECONDS)
我得到的错误是

 Traceback (most recent call last):
  File "gamemood.py", line 147, in <module>
    monitor.prepareCamera()
  File "gamemood.py", line 56, in prepareCamera
    self.camera = pygame.camera.Camera(pygame.camera.list_cameras()[0], (900, 675))
TypeError: 'NoneType' object is not subscriptable
回溯(最近一次呼叫最后一次):
文件“gamemood.py”,第147行,在
monitor.prepareCamera()
文件“gamemood.py”,第56行,在prepareCamera中
self.camera=pygame.camera.camera(pygame.camera.list_camera()[0],(900675))
TypeError:“非类型”对象不可下标

我不明白这个错误!怎么了?Pygame找不到我的相机?

list\u相机
返回的是
None
,而不是列表,您看到的错误是在尝试检索
None[0]
时发生的错误

从源代码来看,当Pygame在非Linux平台上编译时,
camera
模块中的许多功能似乎都被悄悄地禁用了。即使这些函数可以在Mac OS上运行,也有一些预处理器指令直接将其排除在外

来自Pygame 1.9.1:

/* list_cameras() - lists cameras available on the computer */
PyObject* list_cameras (PyObject* self, PyObject* arg)
{
#if defined(__unix__)
    PyObject* ret_list;
    PyObject* string;
    char** devices;
    int num_devices, i;

    num_devices = 0;
    ret_list = NULL;
    ret_list = PyList_New (0);
    if (!ret_list)
        return NULL;

    devices = v4l2_list_cameras(&num_devices);

    for(i = 0; i < num_devices; i++) {
        string = PyString_FromString(devices[i]);
        PyList_Append(ret_list, string);
        Py_DECREF(string);
        free(devices[i]);
    }
    free(devices);

    return ret_list;
#else
    Py_RETURN_NONE;
#endif
}
/*列出摄像头()-列出计算机上可用的摄像头*/
PyObject*列表\摄像机(PyObject*自身,PyObject*参数)
{
#如果已定义(\uuuuunix\uuuuuu)
PyObject*ret_列表;
PyObject*字符串;
字符**设备;
int num_设备,i;
num_设备=0;
ret_list=NULL;
ret_list=PyList_New(0);
如果(!重新列表)
返回NULL;
设备=v4l2\u列表\u摄像机(&num\u设备);
对于(i=0;i

我认为这是对发生的事情最可能的解释。您的代码可能没有问题,但您正在调用的函数只能返回
None
。我对Mac OS或
camera
模块了解不够,无法告诉您,如果您将所有这些语句都更改为
\if defined(uuu unix_uuu)| defined(u APPLE_uu)
并自己编译,那么哪些语句会起作用,哪些不起作用。

这是在什么平台上运行的,您是如何安装pygame的?我是在Mac上安装的。我很肯定我有所有的要求。