2

ERROR:

  File "identify.py", line 57, in <module>
    faceIds.append(face['faceId'])
TypeError: 'DetectedFace' object is not subscriptable

CODE (Below is the code block)

from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.vision.face.models import TrainingStatusType, Person, SnapshotObjectType, OperationStatusType
import global_variables as global_var
import os, urllib
import sqlite3
from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_letter, column_index_from_string
from openpyxl.cell import Cell
import time
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)



#get current date
currentDate = time.strftime("%d_%m_%y")
wb = load_workbook(filename = "reports.xlsx")
sheet = wb['Cse15']

def getDateColumn():
    for i in range(1, len(list(sheet.rows)[0]) + 1):
        col = get_column_letter(i)
        if sheet['%s%s'% (col,'1')].value == currentDate:
            return col

Key = global_var.key


ENDPOINT = 'https://centralindia.api.cognitive.microsoft.com'
face_client = FaceClient(ENDPOINT,CognitiveServicesCredentials(Key))


connect = sqlite3.connect("Face-DataBase")


attend = [0 for i in range(60)] 

currentDir = os.path.dirname(os.path.abspath(__file__))
directory = os.path.join(currentDir, 'Cropped_faces')
for filename in os.listdir(directory):
    if filename.endswith(".jpg"):
        print(filename)
        img_data = open(os.path.join(directory,filename), "rb")
        res = face_client.face.detect_with_stream(img_data)
        print("Res = {}".format(res))

        if len(res) < 1:
            print("No face detected.")
            continue

        faceIds = []
        for face in res:
            faceIds.append(face['faceId'])      #error occuring line
        res = face_client.face.identify(faceIds, global_var.personGroupId)
        print(filename)
        print("res = {}".format(res))

        for face  in res:
            if not face['candidates']:
                print("Unknown")
            else:
                personId = face['candidates'][0]['personId']
                print("personid = {}".format(personId))
                #cmd =  + personId
                cur = connect.execute("SELECT * FROM Students WHERE personID = (?)", (personId,))
                #print("cur = {}".format(cur))
                for row in cur:
                    print("aya")
                    print("row = {}".format(row))
                    attend[int(row[0])] += 1
                print("---------- " + row[1] + " recognized ----------")
        time.sleep(6)

for row in range(2, len(list(sheet.columns)[0]) + 1):
    rn = sheet.cell(row = row, column  =1).value
    if rn is not None:
        print("rn = {}".format(rn))
        rn = rn[-2:]
        if attend[int(rn)] != 0:
            col = getDateColumn()
            print("col = {}".format(col))
            sheet['%s%s' % (col, str(row))] = 0

wb.save(filename = "reports.xlsx")      

This is an automatic attendance system that captures multiple faces and updates it in excel sheet. Azure Cognitive face API is used for face detection. I hope objects have changed for new API. Thid code contains commends for older version of azure but it has changed now.If anyone know how to solve the error do help.

Arun Soorya
  • 447
  • 2
  • 9

0 Answers0